Skip to content

Commit

Permalink
Tutorial 6: Lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
fo40225 committed Mar 15, 2016
1 parent 774f9a9 commit f9f6ff6
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 53 deletions.
159 changes: 109 additions & 50 deletions DirectX11Lab/DirectX11Lab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow)
g_hInst = hInstance;
RECT rc = { 0, 0, 800, 600 };
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
g_hWnd = CreateWindow(L"TutorialWindowClass", L"Direct3D 11 Tutorial 5",
g_hWnd = CreateWindow(L"TutorialWindowClass", L"Direct3D 11 Tutorial 6",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
nullptr);
Expand Down Expand Up @@ -331,7 +331,7 @@ HRESULT InitDevice()

// Compile the vertex shader
ID3DBlob* pVSBlob = nullptr;
hr = CompileShaderFromFile(L"Tutorial05.fx", "VS", "vs_4_0", &pVSBlob);
hr = CompileShaderFromFile(L"Tutorial06.fx", "VS", "vs_4_0", &pVSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
Expand All @@ -351,7 +351,7 @@ HRESULT InitDevice()
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "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 },
};
UINT numElements = ARRAYSIZE(layout);

Expand All @@ -367,7 +367,7 @@ HRESULT InitDevice()

// Compile the pixel shader
ID3DBlob* pPSBlob = nullptr;
hr = CompileShaderFromFile(L"Tutorial05.fx", "PS", "ps_4_0", &pPSBlob);
hr = CompileShaderFromFile(L"Tutorial06.fx", "PS", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
Expand All @@ -381,22 +381,60 @@ HRESULT InitDevice()
if (FAILED(hr))
return hr;

// Compile the pixel shader
pPSBlob = nullptr;
hr = CompileShaderFromFile(L"Tutorial06.fx", "PSSolid", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}

// Create the pixel shader
hr = g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), nullptr, &g_pPixelShaderSolid);
pPSBlob->Release();
if (FAILED(hr))
return hr;

// Create vertex buffer
SimpleVertex vertices[] =
{
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT4(0.0f, 1.0f, 1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT4(1.0f, 0.0f, 1.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLO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0.0f, 0.0f, -1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f) },

{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, 1.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, 1.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, 1.0f) },
};

D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(SimpleVertex) * 8;
bd.ByteWidth = sizeof(SimpleVertex) * 24;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
Expand All @@ -412,25 +450,26 @@ HRESULT InitDevice()
g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);

// Create index buffer
// Create vertex buffer
WORD indices[] =
{
3,1,0,
2,1,3,

0,5,4,
1,5,0,
6,4,5,
7,4,6,

3,4,7,
0,4,3,
11,9,8,
10,9,11,

1,6,5,
2,6,1,
14,12,13,
15,12,14,

2,7,6,
3,7,2,
19,17,16,
18,17,19,

6,4,5,
7,4,6,
22,20,21,
23,20,22
};
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(WORD) * 36; // 36 vertices needed for 12 triangles in a triangle list
Expand All @@ -456,22 +495,20 @@ HRESULT InitDevice()
if (FAILED(hr))
return hr;

// Initialize the world matrix
g_World1 = XMMatrixIdentity();
g_World2 = XMMatrixIdentity();
// Initialize the world matrices
g_World = XMMatrixIdentity();

// Initialize the view matrix
XMVECTOR Eye = XMVectorSet(0.0f, 1.0f, -5.0f, 0.0f);
XMVECTOR Eye = XMVectorSet(0.0f, 4.0f, -10.0f, 0.0f);
XMVECTOR At = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
g_View = XMMatrixLookAtLH(Eye, At, Up);

// Initialize the projection matrix
g_Projection = XMMatrixPerspectiveFovLH(XM_PIDIV2, width / (FLOAT)height, 0.01f, 100.0f);
g_Projection = XMMatrixPerspectiveFovLH(XM_PIDIV4, width / (FLOAT)height, 0.01f, 100.0f);

return S_OK;
}

//--------------------------------------------------------------------------------------
// Render a frame
//--------------------------------------------------------------------------------------
Expand All @@ -492,20 +529,31 @@ void Render()
t = (timeCur - timeStart) / 1000.0f;
}

// 1st Cube: Rotate around the origin
g_World1 = XMMatrixRotationY(t);
// Rotate cube around the origin
g_World = XMMatrixRotationY(t);

// 2nd Cube: Rotate around origin
XMMATRIX mSpin = XMMatrixRotationZ(-t);
XMMATRIX mOrbit = XMMatrixRotationY(-t * 2.0f);
XMMATRIX mTranslate = XMMatrixTranslation(-4.0f, 0.0f, 0.0f);
XMMATRIX mScale = XMMatrixScaling(0.3f, 0.3f, 0.3f);
// Setup our lighting parameters
XMFLOAT4 vLightDirs[2] =
{
XMFLOAT4(-0.577f, 0.577f, -0.577f, 1.0f),
XMFLOAT4(0.0f, 0.0f, -1.0f, 1.0f),
};
XMFLOAT4 vLightColors[2] =
{
XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f),
XMFLOAT4(0.5f, 0.0f, 0.0f, 1.0f)
};

g_World2 = mScale * mSpin * mTranslate * mOrbit;
// Rotate the second light around the origin
XMMATRIX mRotate = XMMatrixRotationY(-2.0f * t);
XMVECTOR vLightDir = XMLoadFloat4(&vLightDirs[1]);
vLightDir = XMVector3Transform(vLightDir, mRotate);
XMStoreFloat4(&vLightDirs[1], vLightDir);

//
// Clear the back buffer
//

g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView, Colors::MidnightBlue);

//
Expand All @@ -514,35 +562,45 @@ void Render()
g_pImmediateContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);

//
// Update variables for the first cube
// Update matrix variables and lighting variables
//
ConstantBuffer cb1;
cb1.mWorld = XMMatrixTranspose(g_World1);
cb1.mWorld = XMMatrixTranspose(g_World);
cb1.mView = XMMatrixTranspose(g_View);
cb1.mProjection = XMMatrixTranspose(g_Projection);
cb1.vLightDir[0] = vLightDirs[0];
cb1.vLightDir[1] = vLightDirs[1];
cb1.vLightColor[0] = vLightColors[0];
cb1.vLightColor[1] = vLightColors[1];
cb1.vOutputColor = XMFLOAT4(0, 0, 0, 0);
g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb1, 0, 0);

//
// Render the first cube
// Render the cube
//
g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer);
g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0);
g_pImmediateContext->PSSetConstantBuffers(0, 1, &g_pConstantBuffer);
g_pImmediateContext->DrawIndexed(36, 0, 0);

//
// Update variables for the second cube
// Render each light
//
ConstantBuffer cb2;
cb2.mWorld = XMMatrixTranspose(g_World2);
cb2.mView = XMMatrixTranspose(g_View);
cb2.mProjection = XMMatrixTranspose(g_Projection);
g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb2, 0, 0);
for (int m = 0; m < 2; m++)
{
XMMATRIX mLight = XMMatrixTranslationFromVector(5.0f * XMLoadFloat4(&vLightDirs[m]));
XMMATRIX mLightScale = XMMatrixScaling(0.2f, 0.2f, 0.2f);
mLight = mLightScale * mLight;

//
// Render the second cube
//
g_pImmediateContext->DrawIndexed(36, 0, 0);
// Update the world variable to reflect the current light
cb1.mWorld = XMMatrixTranspose(mLight);
cb1.vOutputColor = vLightColors[m];
g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb1, 0, 0);

g_pImmediateContext->PSSetShader(g_pPixelShaderSolid, nullptr, 0);
g_pImmediateContext->DrawIndexed(36, 0, 0);
}

//
// Present our back buffer to our front buffer
Expand All @@ -562,6 +620,7 @@ void CleanupDevice()
if (g_pIndexBuffer) g_pIndexBuffer->Release();
if (g_pVertexLayout) g_pVertexLayout->Release();
if (g_pVertexShader) g_pVertexShader->Release();
if (g_pPixelShaderSolid) g_pPixelShaderSolid->Release();
if (g_pPixelShader) g_pPixelShader->Release();
if (g_pDepthStencil) g_pDepthStencil->Release();
if (g_pDepthStencilView) g_pDepthStencilView->Release();
Expand Down
9 changes: 6 additions & 3 deletions DirectX11Lab/DirectX11Lab.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ ID3D11Texture2D* g_pDepthStencil = nullptr;
ID3D11DepthStencilView* g_pDepthStencilView = nullptr;
ID3D11VertexShader* g_pVertexShader = nullptr;
ID3D11PixelShader* g_pPixelShader = nullptr;
ID3D11PixelShader* g_pPixelShaderSolid = nullptr;
ID3D11InputLayout* g_pVertexLayout = nullptr;
ID3D11Buffer* g_pVertexBuffer = nullptr;
ID3D11Buffer* g_pIndexBuffer = nullptr;
ID3D11Buffer* g_pConstantBuffer = nullptr;
XMMATRIX g_World1;
XMMATRIX g_World2;
XMMATRIX g_World;
XMMATRIX g_View;
XMMATRIX g_Projection;

Expand All @@ -44,12 +44,15 @@ void Render();
struct SimpleVertex
{
XMFLOAT3 Pos;
XMFLOAT4 Color;
XMFLOAT3 Normal;
};

struct ConstantBuffer
{
XMMATRIX mWorld;
XMMATRIX mView;
XMMATRIX mProjection;
XMFLOAT4 vLightDir[2];
XMFLOAT4 vLightColor[2];
XMFLOAT4 vOutputColor;
};
6 changes: 6 additions & 0 deletions DirectX11Lab/DirectX11Lab.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</FxCompile>
<FxCompile Include="Tutorial06.fx">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</FxCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
1 change: 1 addition & 0 deletions DirectX11Lab/DirectX11Lab.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
<FxCompile Include="Tutorial02.fx" />
<FxCompile Include="Tutorial04.fx" />
<FxCompile Include="Tutorial05.fx" />
<FxCompile Include="Tutorial06.fx" />
</ItemGroup>
</Project>
67 changes: 67 additions & 0 deletions DirectX11Lab/Tutorial06.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
cbuffer ConstantBuffer : register( b0 )
{
matrix World;
matrix View;
matrix Projection;
float4 vLightDir[2];
float4 vLightColor[2];
float4 vOutputColor;
}


//--------------------------------------------------------------------------------------
struct VS_INPUT
{
float4 Pos : POSITION;
float3 Norm : NORMAL;
};

struct PS_INPUT
{
float4 Pos : SV_POSITION;
float3 Norm : TEXCOORD0;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output = (PS_INPUT)0;
output.Pos = mul( input.Pos, World );
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
output.Norm = mul( float4( input.Norm, 1 ), World ).xyz;

return output;
}


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( PS_INPUT input) : SV_Target
{
float4 finalColor = 0;

//do NdotL lighting for 2 lights
for(int i=0; i<2; i++)
{
finalColor += saturate( dot( (float3)vLightDir[i],input.Norm) * vLightColor[i] );
}
finalColor.a = 1;
return finalColor;
}


//--------------------------------------------------------------------------------------
// PSSolid - render a solid color
//--------------------------------------------------------------------------------------
float4 PSSolid( PS_INPUT input) : SV_Target
{
return vOutputColor;
}

0 comments on commit f9f6ff6

Please sign in to comment.