Skip to content

Commit

Permalink
퐁 쉐이더 나이브 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnjiwoo committed Sep 22, 2024
1 parent ee802c7 commit 90dc54b
Show file tree
Hide file tree
Showing 50 changed files with 163 additions and 78 deletions.
1 change: 1 addition & 0 deletions MyD3DFramework/ContantBuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct cbPerObject
Matrix World;
Matrix WorldInvTranspose;
Matrix ViewProj;
Material Material;
};

struct cbPerFrame
Expand Down
5 changes: 3 additions & 2 deletions MyD3DFramework/DirectionalLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ struct DirectionalLight
//¹Ý»ç±¤
Color Specular;

//(X,Y,Z) = Direction, (W) = 패킹
Vector4 DirectionRange;
//(X,Y,Z) = Direction
Vector3 Direction;
float Pad;
};


1 change: 1 addition & 0 deletions MyD3DFramework/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void InputManager::Update()
}

// ¸¶¿ì½º À§Ä¡ ¾÷µ¥ÀÌÆ®
m_prevMousePos = m_mousePos;
::GetCursorPos(&m_mousePos);
::ScreenToClient(m_hwnd, &m_mousePos);
}
Expand Down
3 changes: 3 additions & 0 deletions MyD3DFramework/InputManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ class InputManager
inline bool GetKeyAway(eKeyCode key) { return m_stateList[static_cast<uint8>(key)] == eKeyState::Away; }

POINT GetMousePos()const { return m_mousePos; }
POINT GetDeltaMousePos() const { return POINT{ m_mousePos.x - m_prevMousePos.x, m_mousePos.y - m_prevMousePos.y }; }

private:
std::array<eKeyState, UINT8_MAX> m_stateList = {};
POINT m_mousePos = {};
POINT m_prevMousePos = {};

HWND m_hwnd = NULL;
};

21 changes: 10 additions & 11 deletions MyD3DFramework/LightPixelShader.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void ComputeDirectionalLight(
outSpecular = float4(0.f, 0.f, 0.f, 0.f);

//Light 벡터
float3 lightVecW = -gDirLight.DirectionRange.xyz;
float3 lightVecW = -gDirLight.Direction;

//ambient
outAmbient = gMaterial.Ambient * gDirLight.Ambient;
Expand Down Expand Up @@ -49,9 +49,9 @@ void ComputePointLight(
outSpecular = float4(0.f, 0.f, 0.f, 0.f);

//범위를 넘어갈 경우 리턴
float3 lightVecW = gPointLight.PositionRange.xyz - inPosition;
float3 lightVecW = gPointLight.Position - inPosition;
float dist = length(lightVecW);
if (dist > gPointLight.PositionRange.w)
if (dist > gPointLight.Range)
{
return;
}
Expand All @@ -77,7 +77,7 @@ void ComputePointLight(
}

//감쇠 계수
float att = dot(gSpotLight.Attenuation.xyz, float3(1.f, dist, dist * dist));
float att = dot(gPointLight.Attenuation, float3(1.f, dist, dist * dist));
att = max(att, SMALL_NUMBER);

float attInv = 1.f / att;
Expand All @@ -101,9 +101,9 @@ void ComputeSpotLight(
outSpecular = float4(0.f, 0.f, 0.f, 0.f);

//범위를 넘어갈 경우 리턴
float3 lightVecW = gSpotLight.PositionRange.xyz - inPosition;
float3 lightVecW = gSpotLight.Position - inPosition;
float dist = length(lightVecW);
if (dist > gSpotLight.PositionRange.w)
if (dist > gSpotLight.Range)
{
return;
}
Expand All @@ -129,18 +129,17 @@ void ComputeSpotLight(
}

//스포트라이트 계수
float spotFactor = pow(max(0, dot(-lightVecW, gSpotLight.DirectionSpot.xyz)), gSpotLight.DirectionSpot.w);

float spotFactor = pow(max(0, dot(-lightVecW, gSpotLight.SpotDirection)), gSpotLight.Exponent);

//스포트라이트 계수 * 감쇠 계수
float att = dot(gSpotLight.Attenuation.xyz, float3(1.f, dist, dist * dist));
float att = dot(gSpotLight.Attenuation, float3(1.f, dist, dist * dist));
att = max(att, SMALL_NUMBER);

float attInv = spotFactor / att;

outAmbient *= spotFactor;
outAmbient *= spotFactor;
outSpecular *= attInv;
outDiffuse *= attInv;
outDiffuse *= attInv;
}

float4 PS(LIGHT_VS_OUTPUT input) : SV_TARGET
Expand Down
2 changes: 1 addition & 1 deletion MyD3DFramework/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ struct Material
{
Color Ambient;
Color Diffuse;
Color Specular;
Color Specular; //W´Â Áöźö°čźö
};

13 changes: 6 additions & 7 deletions MyD3DFramework/PointLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ struct PointLight
//반사광
Color Specular;

//(X,Y,Z) = Position, (W) = Range
Vector4 PositionRange;
//Position
Vector3 Position;
float Range;

//차례대로 a0, a1, a2를 의미, W = 패킹
Vector4 Attenuation;
//차례대로 a0, a1, a2를 의미
Vector3 Attenuation;
float Pad;
};



144 changes: 109 additions & 35 deletions MyD3DFramework/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void Renderer::InputAssembler()
//Vertex Shader 컴파일
ComPtr<ID3DBlob> vsBlob = nullptr;
LoadAndCopileShaderFromFile(
L"SimpleVertexShader.hlsl",
L"LightVertexShader.hlsl",
"VS",
"vs_5_0",
vsBlob.GetAddressOf()
Expand All @@ -187,7 +187,7 @@ void Renderer::InputAssembler()
D3D11_INPUT_ELEMENT_DESC inputLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};

hr = m_device->CreateInputLayout(
Expand All @@ -202,7 +202,7 @@ void Renderer::InputAssembler()
//픽셀쉐이더 컴파일
ComPtr<ID3DBlob> psBlob = nullptr;
LoadAndCopileShaderFromFile(
L"SimplePixelShader.hlsl",
L"LightPixelShader.hlsl",
"PS",
"ps_5_0",
psBlob.GetAddressOf()
Expand All @@ -216,20 +216,26 @@ void Renderer::InputAssembler()
);

//버텍스 버퍼 생성
VertexColor vertices[] = {
{Vector3{-1.f,-1.f,-1.f}, Color(Colors::Red)},
{Vector3{-1.f,+1.f,-1.f}, Color(Colors::Blue)},
{Vector3{+1.f,+1.f,-1.f}, Color(Colors::Green)},
{Vector3{+1.f,-1.f,-1.f}, Color(Colors::Yellow)},
{Vector3{-1.f,-1.f,+1.f}, Color(Colors::Cyan)},
{Vector3{-1.f,+1.f,+1.f}, Color(Colors::Magenta)},
{Vector3{+1.f,+1.f,+1.f}, Color(Colors::Silver)},
{Vector3{+1.f,-1.f,+1.f}, Color(Colors::Gold)}
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}}
};

//노멀 벡터 정규화
for (auto& v : vertices)
{
v.Normal.Normalize();
}

D3D11_BUFFER_DESC vbd = {};
vbd.Usage = D3D11_USAGE_DEFAULT;
vbd.ByteWidth = sizeof(VertexColor) * 8;
vbd.ByteWidth = sizeof(VertexNormal) * 8;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;

D3D11_SUBRESOURCE_DATA vinitData = {};
Expand All @@ -239,7 +245,7 @@ void Renderer::InputAssembler()
hr = m_device->CreateBuffer(&vbd, &vinitData, m_vertexBuffer.GetAddressOf());
CHECK_FAILED(hr);

UINT strid = sizeof(VertexColor);
UINT strid = sizeof(VertexNormal);
UINT offset = 0;
m_deviceContext->IASetVertexBuffers(0, 1, m_vertexBuffer.GetAddressOf(), &strid, &offset);

Expand Down Expand Up @@ -282,6 +288,7 @@ void Renderer::InputAssembler()
desc.ByteWidth = sizeof(cbPerObject);
desc.Usage = D3D11_USAGE_DEFAULT;
hr = m_device->CreateBuffer(&desc, nullptr, m_cBufferPerObject.GetAddressOf());
CHECK_FAILED(hr);
}

//Contant Buffer Per Frame
Expand All @@ -292,53 +299,119 @@ void Renderer::InputAssembler()
desc.ByteWidth = sizeof(cbPerFrame);
desc.Usage = D3D11_USAGE_DEFAULT;
hr = m_device->CreateBuffer(&desc, nullptr, m_cBufferPerFrame.GetAddressOf());
CHECK_FAILED(hr);
}

//뷰 투영 행렬 초기화
Matrix view = ::XMMatrixLookAtLH(m_cmrPosition, m_cmrLookAt, m_worldUpAxis);
Matrix proj = ::XMMatrixPerspectiveFovLH(
const static Vector3 s_cmrLookAt{ 0.f, 0.f, 5.f };
m_viewMat = ::XMMatrixLookAtLH(m_cmrPosition, s_cmrLookAt, m_worldUpAxis);
m_cmrLook = s_cmrLookAt - m_cmrPosition;
m_cmrLook.Normalize();

m_projMat = ::XMMatrixPerspectiveFovLH(
m_fov,
WindowsApp::GetInst().GetAspectRatio(),
m_nearPlane,
m_farPlane);

m_viewProjMat = view * proj;

//광원 초기화
m_dirLight.Ambient = Colors::White;
m_dirLight.Diffuse = Colors::White;
m_dirLight.Specular = Colors::White;

m_material.Ambient = Colors::Red * 0.5f;
m_material.Diffuse = Colors::Red * 0.5f;
m_material.Specular = Colors::Red * 0.5f;
m_spotLight.Ambient = Color(0.2f, 0.2f, 0.2f, 1.f);
m_spotLight.Diffuse = Color(1.0f, 0.95f, 0.8f, 1.f);
m_spotLight.Specular = Color(1.0f, 1.0f, 1.0f, 1.f);
m_spotLight.SpotDirection = Vector3(0.f, 0.f, 1.f);
m_spotLight.Attenuation = Vector3(1.f,0.1f,0.f);
m_spotLight.Range = 50.f;

//머테리얼 설정
m_material.Ambient = Color(0.4f, 0.4f, 0.4f, 1.f);
m_material.Diffuse = Color(0.5f, 0.5f, 0.5f, 1.f);
m_material.Specular = Color(0.8f, 0.8f, 0.8f, 16.f);
}

void Renderer::Update(float inDeltaTime)
{
//Move Camera
{
static float s_cmrMoveSpeed = 3.f;
static float s_cmrRotateSpeed = ::XMConvertToRadians(30.f);

if (KEY_HOLD(eKeyCode::A))
{
Vector3 dir = -m_worldUpAxis.Cross(m_cmrLook);
m_cmrPosition += dir * s_cmrMoveSpeed * inDeltaTime;
}

if (KEY_HOLD(eKeyCode::D))
{
Vector3 dir = m_worldUpAxis.Cross(m_cmrLook);
m_cmrPosition += dir * s_cmrMoveSpeed * inDeltaTime;
}

if (KEY_HOLD(eKeyCode::S))
{
m_cmrPosition += -m_cmrLook * s_cmrMoveSpeed * inDeltaTime;
}

if (KEY_HOLD(eKeyCode::W))
{
m_cmrPosition += m_cmrLook * s_cmrMoveSpeed * inDeltaTime;
}

if (KEY_HOLD(eKeyCode::Space))
{
m_cmrPosition.y += s_cmrMoveSpeed * inDeltaTime;
}

if (KEY_HOLD(eKeyCode::L_Shift))
{
m_cmrPosition.y += -s_cmrMoveSpeed * inDeltaTime;
}

if (KEY_HOLD(eKeyCode::Left))
{
m_cmrLook = ::XMVector3Rotate(m_cmrLook, Quaternion::CreateFromYawPitchRoll(-s_cmrRotateSpeed * inDeltaTime,0.f,0.f));
}

if (KEY_HOLD(eKeyCode::Right))
{
m_cmrLook = ::XMVector3Rotate(m_cmrLook, Quaternion::CreateFromYawPitchRoll(s_cmrRotateSpeed * inDeltaTime, 0.f, 0.f));
}

if (KEY_HOLD(eKeyCode::Up))
{
m_cmrLook = ::XMVector3Rotate(m_cmrLook, Quaternion::CreateFromYawPitchRoll(0.f, -s_cmrRotateSpeed * inDeltaTime, 0.f));
}

if (KEY_HOLD(eKeyCode::Down))
{
m_cmrLook = ::XMVector3Rotate(m_cmrLook, Quaternion::CreateFromYawPitchRoll(0.f, s_cmrRotateSpeed * inDeltaTime, 0.f));
}
}

static const float s_rotateSpeed = ::XMConvertToRadians(30.f);
float s_rotateDelta = inDeltaTime * s_rotateSpeed;

//Update cbPerObject
{
cbPerObject cb = {};

static const float s_rotateSpeed = ::XMConvertToRadians(30.f);
float s_rotateDelta = inDeltaTime * s_rotateSpeed;

m_rotation *= Quaternion::CreateFromYawPitchRoll(s_rotateDelta, s_rotateDelta, s_rotateDelta);
// m_rotation *= Quaternion::CreateFromYawPitchRoll(s_rotateDelta, 0, 0);

Matrix world = ::XMMatrixScaling(m_scale.x, m_scale.y, m_scale.z) *
::XMMatrixRotationQuaternion(m_rotation) *
::XMMatrixTranslation(m_position.x, m_position.y, m_position.z);
Matrix world = ::XMMatrixAffineTransformation(m_scale, Vector3::Zero, m_rotation, m_position);
m_viewMat = ::XMMatrixLookToLH(m_cmrPosition, m_cmrLook, m_worldUpAxis);

cb.World = ::XMMatrixTranspose(world);
cb.WorldInvTranspose = ::XMMatrixInverse(nullptr, world);
cb.ViewProj = ::XMMatrixTranspose(m_viewProjMat);
cb.ViewProj = ::XMMatrixTranspose(m_viewMat * m_projMat);
cb.Material = m_material;

m_deviceContext->UpdateSubresource(m_cBufferPerObject.Get(), 0, nullptr, &cb, 0, 0);
}

//Update cbPerFrame
{
cbPerFrame cb = {};
m_spotLight.Position = m_cmrPosition;

cb.gEyePosW = ToVector4(m_cmrPosition, true);
cb.gDirLight = m_dirLight;
Expand All @@ -352,7 +425,7 @@ void Renderer::Update(float inDeltaTime)
void Renderer::Render()
{
//렌더링 상태 재설정
UINT strid = sizeof(VertexColor);
UINT strid = sizeof(VertexNormal);
UINT offset = 0;

m_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
Expand All @@ -365,7 +438,8 @@ void Renderer::Render()
m_deviceContext->VSSetConstantBuffers(0, 1, m_cBufferPerObject.GetAddressOf());

m_deviceContext->PSSetShader(m_pixelShader.Get(), nullptr, 0);
m_deviceContext->PSSetConstantBuffers(0, 1, m_cBufferPerFrame.GetAddressOf());
m_deviceContext->PSSetConstantBuffers(0, 1, m_cBufferPerObject.GetAddressOf());
m_deviceContext->PSSetConstantBuffers(1, 1, m_cBufferPerFrame.GetAddressOf());

// 큐브를 그리기 위한 인덱스 개수 지정
m_deviceContext->DrawIndexed(36, 0, 0);
Expand Down
7 changes: 4 additions & 3 deletions MyD3DFramework/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ class Renderer
Vector3 m_scale = Vector3::One;

//카메라(임시)
Vector3 m_cmrLookAt = Vector3{ 0.f,0.f, 1.f };
Vector3 m_cmrPosition = Vector3{ 0.f,0.f,0.f };
Vector3 m_cmrLook = Vector3{ 0.f,0.f,1.f };
Vector3 m_cmrPosition = Vector3{ 0.f,3.f,0.f };
Vector3 m_worldUpAxis = Vector3{ 0.f,1.f,0.f };
float m_nearPlane = 0.5f;
float m_farPlane = 100.f;

Matrix m_viewProjMat = Matrix::Identity;
Matrix m_viewMat = Matrix::Identity;
Matrix m_projMat = Matrix::Identity;

float m_fov = ::XMConvertToRadians(45.f);

Expand Down
Loading

0 comments on commit 90dc54b

Please sign in to comment.