Skip to content

Commit

Permalink
텍스처 매핑
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnjiwoo committed Oct 4, 2024
1 parent f793299 commit d504d54
Show file tree
Hide file tree
Showing 44 changed files with 398 additions and 71 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 added Binaries/Debug/TexPixelShader.cso
Binary file not shown.
Binary file added Binaries/Debug/TexVertexShader.cso
Binary file not shown.
2 changes: 1 addition & 1 deletion MyD3DFramework/EngineConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define POINT_LIGHT_MAXIMUM_COUNT 8
#define SPOT_LIGHT_MAXIMUM_COUNT 4
#define USABLE_LIGHT_MAXIMUM_COUNT DIRECTIONAL_LIGHT_MAXIMUM_COUNT + POINT_LIGHT_MAXIMUM_COUNT + SPOT_LIGHT_MAXIMUM_COUNT
#define MSAA_OPTION false
#define MSAA_OPTION true

namespace EngineConfig
{
Expand Down
8 changes: 4 additions & 4 deletions MyD3DFramework/LightObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ void LightObject::Initialize()
{
m_trans = CreateComponent<CTransform>();
m_light = CreateComponent<CLight>(eLightType::DirectionalLight, m_trans);
m_light->SetDiffuse(Color{ Colors::Blue });
m_light->SetAmbient(Color{ Colors::White });
m_light->SetDiffuse(Color{ Colors::LightYellow });
m_light->SetAmbient(Color{ Colors::LightYellow });
m_light->SetSpecular(Color{ Colors::White * 0.3f });
m_trans->SetRotateByDirection(Vector3{ 1,-1,1 });

m_trans->SetRotateByDirection(Vector3{ 1,-2,2});
}

void LightObject::Update()
Expand Down
8 changes: 4 additions & 4 deletions MyD3DFramework/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class Mesh
virtual ~Mesh() = default;

template<typename VertexType>
inline void SetVertexBuffer(ID3D11Device* inDevice, const std::vector<VertexType>& inVertices);
inline void SetIndexBuffer(ID3D11Device* inDevice, const std::vector<UINT>& inIndices);
inline void CreateVertexBuffer(ID3D11Device* inDevice, const std::vector<VertexType>& inVertices);
inline void CreateIndexBuffer(ID3D11Device* inDevice, const std::vector<UINT>& inIndices);

inline void BindBuffers(ID3D11DeviceContext* inDeviceContext);
inline uint32 GetIndexBufferSize() const { return m_indexBufferSize; }
Expand All @@ -23,7 +23,7 @@ class Mesh
};

template<typename VertexType>
inline void Mesh::SetVertexBuffer(ID3D11Device* inDivice, const std::vector<VertexType>& inVertices)
inline void Mesh::CreateVertexBuffer(ID3D11Device* inDivice, const std::vector<VertexType>& inVertices)
{
m_vertexBuffer.Reset();

Expand All @@ -41,7 +41,7 @@ inline void Mesh::SetVertexBuffer(ID3D11Device* inDivice, const std::vector<Vert
CHECK_FAILED(inDivice->CreateBuffer(&desc, &subData, m_vertexBuffer.GetAddressOf()));
}

inline void Mesh::SetIndexBuffer(ID3D11Device* inDivice, const std::vector<UINT>& inIndices)
inline void Mesh::CreateIndexBuffer(ID3D11Device* inDivice, const std::vector<UINT>& inIndices)
{
m_indexBuffer.Reset();

Expand Down
16 changes: 16 additions & 0 deletions MyD3DFramework/MyD3DFramework.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@
<Image Include="small.ico" />
</ItemGroup>
<ItemGroup>
<FxCompile Include="TexPixelShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<EntryPointName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">PS</EntryPointName>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">5.0</ShaderModel>
</FxCompile>
<FxCompile Include="TexVertexShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
<EntryPointName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">VS</EntryPointName>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">5.0</ShaderModel>
</FxCompile>
<None Include="..\.gitignore" />
<None Include="cpp.hint" />
<FxCompile Include="LightPixelShader.hlsl">
Expand Down
9 changes: 9 additions & 0 deletions MyD3DFramework/MyD3DFramework.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@
<Filter Include="06.Component\Behavior">
<UniqueIdentifier>{aef79aa2-bad6-4122-9d08-b5e65d462a52}</UniqueIdentifier>
</Filter>
<Filter Include="07.RenderModules\Shader\Texture+Light+Trans">
<UniqueIdentifier>{2905fbef-b18c-4358-a7b6-a8459bcc0fde}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cpp">
Expand Down Expand Up @@ -416,5 +419,11 @@
<FxCompile Include="LightPixelShader.hlsl">
<Filter>07.RenderModules\Shader\Light+Trans</Filter>
</FxCompile>
<FxCompile Include="TexVertexShader.hlsl">
<Filter>07.RenderModules\Shader\Texture+Light+Trans</Filter>
</FxCompile>
<FxCompile Include="TexPixelShader.hlsl">
<Filter>07.RenderModules\Shader\Texture+Light+Trans</Filter>
</FxCompile>
</ItemGroup>
</Project>
68 changes: 41 additions & 27 deletions MyD3DFramework/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,16 @@ void Renderer::InitializeRenderResoucre()

effect->SetProperties(
m_device.Get(),
L"LightVertexShader.hlsl",
L"TexVertexShader.hlsl",
"VS",
"vs_5_0",
L"LightPixelShader.hlsl",
L"TexPixelShader.hlsl",
"PS",
"ps_5_0",
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }
}
);

Expand Down Expand Up @@ -220,8 +221,8 @@ void Renderer::InitializeRenderResoucre()
//머테리얼 생성
{
Material* m = CreateMaterial(sBasicMaterialKey);
m->Ambient = Color(0.2f, 0.2f, 0.2f);
m->Diffuse = Color(0.7f, 0.7f, 0.7f);
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;
}
Expand All @@ -235,6 +236,26 @@ void Renderer::InitializeRenderResoucre()
m->Specular.w = 32;
}

/* 샘플러 스테이트 */
{

D3D11_SAMPLER_DESC desc = { };
desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
desc.MaxLOD = D3D11_FLOAT32_MAX;

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

/* 텍스처 생성 */
{
HRESULT hr = LoadAndCreateTexture2D(L"C:\\Users\\alsxm\\Desktop\\dev\\MyDX3DFramework\\woodTexture.png", nullptr, m_shaderResourceView.GetAddressOf(), true);
CHECK_FAILED(hr);
}

SetCurrentEffect(sBasicEffectKey);
SetCurrentRenderState(sBasicRenderStateKey);

Expand All @@ -246,15 +267,15 @@ void Renderer::CreateCubeMesh()
Mesh* mesh = CreateMesh(sCubeMeshKey);

//버텍스 버퍼 생성
std::vector<VertexNormal> vertices = {
{Vector3{-1.f,-1.f,-1.f}, Vector3{-1.f,-1.f,-1.f}},
{Vector3{-1.f,+1.f,-1.f}, Vector3{-1.f,+1.f,-1.f}},
{Vector3{+1.f,+1.f,-1.f}, Vector3{+1.f,+1.f,-1.f}},
{Vector3{+1.f,-1.f,-1.f}, Vector3{+1.f,-1.f,-1.f}},
{Vector3{-1.f,-1.f,+1.f}, Vector3{-1.f,-1.f,+1.f}},
{Vector3{-1.f,+1.f,+1.f}, Vector3{-1.f,+1.f,+1.f}},
{Vector3{+1.f,+1.f,+1.f}, Vector3{+1.f,+1.f,+1.f}},
{Vector3{+1.f,-1.f,+1.f}, Vector3{+1.f,-1.f,+1.f}}
std::vector<VertexNormalTexture> vertices = {
{Vector3{-1.f,-1.f,-1.f}, Vector3{-1.f,-1.f,-1.f}, Vector2{0.f,1.f}},
{Vector3{-1.f,+1.f,-1.f}, Vector3{-1.f,+1.f,-1.f}, Vector2{0.f,0.f}},
{Vector3{+1.f,+1.f,-1.f}, Vector3{+1.f,+1.f,-1.f}, Vector2{1.f,0.f}},
{Vector3{+1.f,-1.f,-1.f}, Vector3{+1.f,-1.f,-1.f}, Vector2{1.f,1.f}},
{Vector3{-1.f,-1.f,+1.f}, Vector3{-1.f,-1.f,+1.f}, Vector2{1.f,1.f}},
{Vector3{-1.f,+1.f,+1.f}, Vector3{-1.f,+1.f,+1.f}, Vector2{1.f,0.f}},
{Vector3{+1.f,+1.f,+1.f}, Vector3{+1.f,+1.f,+1.f}, Vector2{0.f,0.f}},
{Vector3{+1.f,-1.f,+1.f}, Vector3{+1.f,-1.f,+1.f}, Vector2{0.f,1.f}}
};

//노멀 벡터 정규화
Expand All @@ -263,7 +284,7 @@ void Renderer::CreateCubeMesh()
v.Normal.Normalize();
}

mesh->SetVertexBuffer(m_device.Get(), vertices);
mesh->CreateVertexBuffer(m_device.Get(), vertices);

//인덱스 버퍼 생성
std::vector<UINT> indices = {
Expand All @@ -275,7 +296,7 @@ void Renderer::CreateCubeMesh()
3, 2, 6, 3, 6, 7
};

mesh->SetIndexBuffer(m_device.Get(), indices);
mesh->CreateIndexBuffer(m_device.Get(), indices);
}

void Renderer::CreateSphereMesh(int latitudeSegments, int longitudeSegments, float radius)
Expand Down Expand Up @@ -324,13 +345,14 @@ void Renderer::CreateSphereMesh(int latitudeSegments, int longitudeSegments, flo
}
}

mesh->SetVertexBuffer(m_device.Get(), vertices);

mesh->SetIndexBuffer(m_device.Get(), indices);
mesh->CreateVertexBuffer(m_device.Get(), vertices);
mesh->CreateIndexBuffer(m_device.Get(), indices);
}
void Renderer::Render()
{
/* 렌더링 옵션 복구 */
m_deviceContext->PSSetShaderResources(0, 1, m_shaderResourceView.GetAddressOf());
m_deviceContext->PSSetSamplers(0, 1, m_samplerState.GetAddressOf());
m_curEffect->Apply(m_deviceContext.Get());
m_curEffect->BindConstantBuffer(m_deviceContext.Get(), sCbPerFrameKey);
m_curEffect->BindConstantBuffer(m_deviceContext.Get(), sCbPerObjectKey);
Expand Down Expand Up @@ -420,14 +442,6 @@ void Renderer::Render()
}
}

//========================================
//
// 1. Light 관리
// 2. 상수버퍼 관리
// 3. 렌더링 테스트 하면 끝!
//
//========================================

void Renderer::ResizeWindow(const WindowSize& winSize)
{
//스왑체인, 렌더타겟, 깊이-스텐실 버퍼, 뷰포트 수정
Expand Down
19 changes: 19 additions & 0 deletions MyD3DFramework/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ class Renderer
}
}

HRESULT LoadAndCreateTexture2D(
std::wstring_view inFileName,
ID3D11Resource** inOutTexture,
ID3D11ShaderResourceView** inOutShdaerResourceView,
bool doGenerateMipMap)
{

HRESULT hr = CreateWICTextureFromFile(m_device.Get(), inFileName.data(), inOutTexture, inOutShdaerResourceView);
CHECK_FAILED(hr);
if (doGenerateMipMap)
{
m_deviceContext->GenerateMips(m_shaderResourceView.Get());
}

return hr;
}

NODISCARD Effect* GetEffect(const std::string& inKey);
NODISCARD Effect* GetCurrentEffect() const { assert(m_curEffect); return m_curEffect; }

Expand Down Expand Up @@ -131,6 +148,8 @@ class Renderer
ComPtr<ID3D11Texture2D> m_depthStencilBuffer = nullptr;
ComPtr<ID3D11DepthStencilView> m_depthStencilView = nullptr;
ComPtr<ID3D11RenderTargetView> m_renderTargetView = nullptr;
ComPtr<ID3D11ShaderResourceView> m_shaderResourceView = nullptr;
ComPtr<ID3D11SamplerState> m_samplerState = nullptr;

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

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

struct Material
{
float4 Ambient;
Expand Down Expand Up @@ -104,3 +107,34 @@ struct LIGHT_VS_OUTPUT
//월드 좌표계 법선벡터(광원 계산을 위함)
float3 NormalW : NORMAL;
};

// ---------------------------------------
// 선형 변환 + 광원 + 텍스처
// ---------------------------------------

struct TEX_VS_INPUT
{
//로컬 좌표
float3 PosL : POSITION;

//로컬 법선벡터
float3 NormalL : NORMAL;

//텍스처 좌표
float2 UV : TEXCOORD0;
};

struct TEX_VS_OUTPUT
{
//동차 좌표계 좌표
float4 PosH : SV_POSITION;

//월드 좌표계 좌표(광원 계산을 위함)
float3 PosW : POSITION;

//월드 좌표계 법선벡터(광원 계산을 위함)
float3 NormalW : NORMAL;

//텍스처 좌표
float2 UV : TEXCOORD0;
};
Loading

0 comments on commit d504d54

Please sign in to comment.