Skip to content

Commit

Permalink
알파 블렌딩, 안개 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnjiwoo committed Oct 5, 2024
1 parent d504d54 commit 72801bc
Show file tree
Hide file tree
Showing 25 changed files with 115 additions and 28 deletions.
Binary file modified Binaries/Debug/LightPixelShader.cso
Binary file not shown.
Binary file modified Binaries/Debug/LightVertexShader.cso
Binary file not shown.
Binary file modified Binaries/Debug/MyD3DFramework.exe
Binary file not shown.
Binary file modified Binaries/Debug/SimplePixelShader.cso
Binary file not shown.
Binary file modified Binaries/Debug/SimpleVertexShader.cso
Binary file not shown.
Binary file modified Binaries/Debug/TexPixelShader.cso
Binary file not shown.
Binary file modified Binaries/Debug/TexVertexShader.cso
Binary file not shown.
2 changes: 1 addition & 1 deletion MyD3DFramework/CameraObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
CameraObject::CameraObject()
{
m_trans = CreateComponent<CTransform>();
m_camera = CreateComponent<CCamera>(::XMConvertToRadians(45.f), 0.5f, 100.f, m_trans);
m_camera = CreateComponent<CCamera>(::XMConvertToRadians(45.f), 0.5f, 50.f, m_trans);
m_input = CreateComponent<CKeyInput>();

/* ÀÎDz ¼³Á¤ */
Expand Down
8 changes: 8 additions & 0 deletions MyD3DFramework/ContantBuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ struct cbPerFrame
unsigned int PointLightCount;
unsigned int SpotLightCount;
float Pad;
};

struct cbPerRarely
{
Vector4 FogColor;
float FogStart;
float FogRange;
Vector2 Pad;
};
14 changes: 11 additions & 3 deletions MyD3DFramework/DevScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ void DevScene::EnterScene()

CreateGameObject<LightObject>();

Pawn* pawn = CreateGameObject<Pawn>();
pawn->GetComponentOrNull<CTransform>()->SetPosition(Vector3{ 0.f,0.f,3.f });
cmrObj->GetComponentOrNull<CTransform>()->SetRotateByLookAtPoint(Vector3{ 0.f,0.f,3.f });
{
Pawn* pawn = CreateGameObject<Pawn>();
pawn->GetComponentOrNull<CTransform>()->SetPosition(Vector3{ 0.f,0.f,3.f });
cmrObj->GetComponentOrNull<CTransform>()->SetRotateByLookAtPoint(Vector3{ 0.f,0.f,3.f });
}

{
Pawn* pawn = CreateGameObject<Pawn>();
pawn->GetComponentOrNull<CTransform>()->SetPosition(Vector3{ 0.f,-2.f,5.f });
cmrObj->GetComponentOrNull<CTransform>()->SetRotateByLookAtPoint(Vector3{ 0.f,0.f,3.f });
}
}

void DevScene::UpdateScene()
Expand Down
2 changes: 1 addition & 1 deletion MyD3DFramework/Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void Effect::SetProperties(ID3D11Device* inDevice, std::wstring_view inVertexSha
CHECK_FAILED(hr);
}

void Effect::RegisterConstantBuffer(ID3D11Device* inDevice, uint32 inContantBuffeStructSize, const std::string inConstantBufferName, uint32 inSlotToUse, uint32 inApplyFlags)
void Effect::CreateConstantBuffer(ID3D11Device* inDevice, uint32 inContantBuffeStructSize, const std::string inConstantBufferName, uint32 inSlotToUse, uint32 inApplyFlags)
{
assert(!m_cbRepo.contains(inConstantBufferName));

Expand Down
2 changes: 1 addition & 1 deletion MyD3DFramework/Effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Effect
const std::vector<D3D11_INPUT_ELEMENT_DESC>& inInputElementDesc
);

void RegisterConstantBuffer(
void CreateConstantBuffer(
ID3D11Device* inDevice,
uint32 inContantBuffeStructSize,
const std::string inConstantBufferName,
Expand Down
2 changes: 1 addition & 1 deletion MyD3DFramework/Macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
if (FAILED(hr)) \
{ \
auto& buffer = Buffer512W::GetInst(); \
buffer.Input(L"DirectX Fail!\nError Code: %d\nMessage: %s", static_cast<int>(hr), msg); \
buffer.Input(L"DirectX Fail!\nError Code: %ld\nMessage: %s", static_cast<HRESULT>(hr), msg); \
WinUtile::Alert(buffer.c_str(), L"Error"); \
PostQuitMessage(0); \
}
Expand Down
2 changes: 1 addition & 1 deletion MyD3DFramework/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
struct Material
{
Color Ambient;
Color Diffuse;
Color Diffuse; //W´Â žËĆÄäłÎ
Color Specular; //W´Â Áöźö°čźö
};

59 changes: 56 additions & 3 deletions MyD3DFramework/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void Renderer::InitializeRenderResoucre()
//컨스턴트 버퍼 설정

//cbPerObject
effect->RegisterConstantBuffer(
effect->CreateConstantBuffer(
m_device.Get(),
sizeof(cbPerObject),
sCbPerObjectKey,
Expand All @@ -210,21 +210,31 @@ void Renderer::InitializeRenderResoucre()
);

//cbPerFrame
effect->RegisterConstantBuffer(
effect->CreateConstantBuffer(
m_device.Get(),
sizeof(cbPerFrame),
sCbPerFrameKey,
1,
CONSTANT_BUFFER_APPLY_VERTEX_SHADER | CONSTANT_BUFFER_APPLY_PIXEL_SHADER
);

//cbPerRarely
effect->CreateConstantBuffer(
m_device.Get(),
sizeof(cbPerRarely),
sCbPerRarelyKey,
2,
CONSTANT_BUFFER_APPLY_PIXEL_SHADER
);

//머테리얼 생성
{
Material* m = CreateMaterial(sBasicMaterialKey);
m->Ambient = Color(0.4f, 0.4f, 0.4f);
m->Diffuse = Color(1.f, 1.f, 1.f);
m->Specular = Color(1.0f, 1.0f, 1.0f);
m->Specular.w = 32;
m->Diffuse.w = 1.f;
}

//머테리얼 생성
Expand All @@ -236,9 +246,27 @@ void Renderer::InitializeRenderResoucre()
m->Specular.w = 32;
}

/* 샘플러 스테이트 */
/* 블렌더 스테이트 */
{
D3D11_BLEND_DESC desc = {};
desc.AlphaToCoverageEnable = false;
desc.IndependentBlendEnable = false;

desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

HRESULT hr = m_device->CreateBlendState(&desc, m_blendState.GetAddressOf());
CHECK_FAILED(hr);
}

/* 샘플러 스테이트 */
{
D3D11_SAMPLER_DESC desc = { };
desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
Expand All @@ -255,10 +283,29 @@ void Renderer::InitializeRenderResoucre()
HRESULT hr = LoadAndCreateTexture2D(L"C:\\Users\\alsxm\\Desktop\\dev\\MyDX3DFramework\\woodTexture.png", nullptr, m_shaderResourceView.GetAddressOf(), true);
CHECK_FAILED(hr);
}

/* 안개 설정 */
{
cbPerRarely cb = {};

cb.FogStart = 25.f;
cb.FogRange = 25.f;
cb.FogColor = Color(Colors::LightSkyBlue);

effect->UpdateConstantBuffer(
m_deviceContext.Get(),
sCbPerRarelyKey,
cb
);

//지금 업데이트 해도 됨
effect->BindConstantBuffer(m_deviceContext.Get(), sCbPerRarelyKey);
}

SetCurrentEffect(sBasicEffectKey);
SetCurrentRenderState(sBasicRenderStateKey);


}

void Renderer::CreateCubeMesh()
Expand Down Expand Up @@ -357,6 +404,12 @@ void Renderer::Render()
m_curEffect->BindConstantBuffer(m_deviceContext.Get(), sCbPerFrameKey);
m_curEffect->BindConstantBuffer(m_deviceContext.Get(), sCbPerObjectKey);
m_curRenderState->Apply(m_deviceContext.Get());

/* 블랜드 스테이트 */
FLOAT blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
m_deviceContext->OMSetBlendState(m_blendState.Get(), blendFactor, 0xffffffff);


m_curMesh = nullptr;

/* 카메라 설정 */
Expand Down
2 changes: 2 additions & 0 deletions MyD3DFramework/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Renderer
const inline static std::string sGroundMaterialKey = "GroundMaterial";
const inline static std::string sCbPerFrameKey = "cbPerFrame";
const inline static std::string sCbPerObjectKey = "cbPerObject";
const inline static std::string sCbPerRarelyKey = "cbPerRarely";

private:
Renderer();;
Expand All @@ -150,6 +151,7 @@ class Renderer
ComPtr<ID3D11RenderTargetView> m_renderTargetView = nullptr;
ComPtr<ID3D11ShaderResourceView> m_shaderResourceView = nullptr;
ComPtr<ID3D11SamplerState> m_samplerState = nullptr;
ComPtr<ID3D11BlendState> m_blendState = nullptr;

CD3D11_VIEWPORT m_viewport = {};
D3D_FEATURE_LEVEL m_featureLevel = {};
Expand Down
16 changes: 12 additions & 4 deletions MyD3DFramework/ShaderHeader.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
// 구조체
// ---------------------------------------

Texture2D txDiffuse : register(t0);
SamplerState samLinear : register(s0);

struct Material
{
float4 Ambient;
Expand Down Expand Up @@ -52,7 +49,10 @@ struct SpotLight
// ---------------------------------------
// 컨스턴트 버퍼
// ---------------------------------------


Texture2D txDiffuse : register(t0);
SamplerState samLinear : register(s0);

cbuffer cPerObject : register(b0)
{
matrix gWorld;
Expand All @@ -74,6 +74,14 @@ cbuffer cbPerFrame : register(b1)
unsigned int gSpotLightCount;
float Pad;
}

cbuffer cbPerRarely : register(b2)
{
float4 gFogColor;
float gFogStart;
float gFogRange;
}

// ---------------------------------------
// 선형 변환만 정의하는 버텍스 쉐이더
// ---------------------------------------
Expand Down
26 changes: 17 additions & 9 deletions MyD3DFramework/TexPixelShader.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,14 @@ void ComputeSpotLight(

float4 PS(TEX_VS_OUTPUT input) : SV_TARGET
{
//텍스처 매핑 미리 수행
float4 texColor = txDiffuse.Sample(samLinear, input.UV);
clip(texColor.a - 0.1f); //텍스처의 알파 값이 0.1 미만이면 클리핑

//시선 벡터
float3 eyeVecW = normalize(gEyePosW.xyz - input.PosW);
float3 eyeVecW = gEyePosW.xyz - input.PosW;
float distToEye = length(eyeVecW);
eyeVecW /= distToEye;

//초기화
float4 ambient = float4(0.f, 0.f, 0.f, 0.f);
Expand All @@ -162,7 +168,7 @@ float4 PS(TEX_VS_OUTPUT input) : SV_TARGET
float4 s = float4(0.f, 0.f, 0.f, 0.f);

int i;

//방향성 광원
for (i = 0; i < gDirLightCount; ++i)
{
Expand All @@ -172,7 +178,6 @@ float4 PS(TEX_VS_OUTPUT input) : SV_TARGET
diffuse += d;
}


//점 광원
for (i = 0; i < gPointLightCount; ++i)
{
Expand All @@ -192,13 +197,16 @@ float4 PS(TEX_VS_OUTPUT input) : SV_TARGET
diffuse += d;
}

//Light
float4 fianlColor = (ambient + diffuse) * texColor + specular;

//최종 조명 계산

float4 texColor = txDiffuse.Sample(samLinear, input.UV);
float4 finalColor = (ambient + diffuse) * texColor + specular;
finalColor.w = gMaterial.Diffuse.w * texColor.a; //알파 값은 머테리얼에서 가져옴
return finalColor;
//Fog
float fogLerp = saturate((distToEye - gFogStart) / gFogRange);
fianlColor = lerp(fianlColor, gFogColor, fogLerp);

//Texture + Material Alpha
fianlColor.w = gMaterial.Diffuse.w * texColor.a; //알파 값은 머테리얼에서 가져옴
return fianlColor;
}


Expand Down
Binary file modified MyD3DFramework/x64/Debug/MyD3DFramework.ilk
Binary file not shown.
8 changes: 4 additions & 4 deletions MyD3DFramework/x64/Debug/MyD3DFramework.log
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\CMeshRenderer.h(5,7):
C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Effect.cpp(31,26): warning C4267: '인수': 'size_t'에서 'UINT'(으)로 변환하면서 데이터가 손실될 수 있습니다.
(소스 파일 '/x64/Debug/unity_LRIO0T8STGO3EFCP.cpp'을(를) 컴파일하는 중)

C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(409,41): warning C4267: '=': 'size_t'에서 'unsigned int'(으)로 변환하면서 데이터가 손실될 수 있습니다.
C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(462,41): warning C4267: '=': 'size_t'에서 'unsigned int'(으)로 변환하면서 데이터가 손실될 수 있습니다.
(소스 파일 '/x64/Debug/unity_LRIO0T8STGO3EFCP.cpp'을(를) 컴파일하는 중)

C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(410,45): warning C4267: '=': 'size_t'에서 'unsigned int'(으)로 변환하면서 데이터가 손실될 수 있습니다.
C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(463,45): warning C4267: '=': 'size_t'에서 'unsigned int'(으)로 변환하면서 데이터가 손실될 수 있습니다.
(소스 파일 '/x64/Debug/unity_LRIO0T8STGO3EFCP.cpp'을(를) 컴파일하는 중)

C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(411,43): warning C4267: '=': 'size_t'에서 'unsigned int'(으)로 변환하면서 데이터가 손실될 수 있습니다.
C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(464,43): warning C4267: '=': 'size_t'에서 'unsigned int'(으)로 변환하면서 데이터가 손실될 수 있습니다.
(소스 파일 '/x64/Debug/unity_LRIO0T8STGO3EFCP.cpp'을(를) 컴파일하는 중)

C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\WindowsApp.cpp(34,33): warning C4302: '형식 캐스트': 'LPWSTR'에서 'WORD'(으)로 잘립니다.
Expand All @@ -30,7 +30,7 @@ C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Mesh.h(33,37): warning
(소스 파일 '/x64/Debug/unity_LRIO0T8STGO3EFCP.cpp'을(를) 컴파일하는 중)
C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Mesh.h(33,37):
템플릿 인스턴스화 컨텍스트(가장 오래된 인스턴스화 컨텍스트)가
C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(287,8):
C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.cpp(334,8):
컴파일되는 함수 템플릿 인스턴스화 'void Mesh::CreateVertexBuffer<VertexNormalTexture>(ID3D11Device *,const std::vector<VertexNormalTexture,std::allocator<VertexNormalTexture>> &)'에 대한 참조를 확인하세요.

C:\Users\alsxm\Desktop\dev\MyDX3DFramework\MyD3DFramework\Renderer.h(70,1): warning C4715: 'Renderer::GetFeatureLevelToString': 모든 제어 경로에서 값을 반환하지는 않습니다.
Expand Down
Binary file modified MyD3DFramework/x64/Debug/MyD3DFramework.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified MyD3DFramework/x64/Debug/pch.obj
Binary file not shown.
Binary file modified MyD3DFramework/x64/Debug/unity_LRIO0T8STGO3EFCP.obj
Binary file not shown.
Binary file modified MyD3DFramework/x64/Debug/vc143.idb
Binary file not shown.
Binary file modified MyD3DFramework/x64/Debug/vc143.pdb
Binary file not shown.

0 comments on commit 72801bc

Please sign in to comment.