diffuse light

This commit is contained in:
Mamitiana RASOLOJAONA 2024-03-22 15:48:47 +01:00
parent e0d6eb025c
commit 5c9ee0180f
14 changed files with 453 additions and 219 deletions

View File

@ -5,7 +5,8 @@ ApplicationClass::ApplicationClass()
m_Direct3D = 0; m_Direct3D = 0;
m_Camera = 0; m_Camera = 0;
m_Model = 0; m_Model = 0;
m_TextureShader = 0; m_LightShader = 0;
m_Light = 0;
} }
@ -49,7 +50,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
// Set the initial position of the camera. // Set the initial position of the camera.
m_Camera->SetPosition(0.0f, 0.0f, -5.0f); m_Camera->SetPosition(0.0f, 0.0f, -5.0f);
m_Camera->SetRotation(0.0f, 0.0f, 0.0f); m_Camera->SetRotation(0.0f, 0.0f, 10.0f);
// Create and initialize the model object. // Create and initialize the model object.
m_Model = new ModelClass; m_Model = new ModelClass;
@ -62,15 +63,21 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK); MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
return false; return false;
} }
// Create and initialize the texture shader object.
m_TextureShader = new TextureShaderClass;
result = m_TextureShader->Initialize(m_Direct3D->GetDevice(), hwnd); // Create and initialize the light shader object.
m_LightShader = new LightShaderClass;
result = m_LightShader->Initialize(m_Direct3D->GetDevice(), hwnd);
if (!result) if (!result)
{ {
MessageBox(hwnd, L"Could not initialize the texture shader object.", L"Error", MB_OK); MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
return false; return false;
} }
// Create and initialize the light object.
m_Light = new LightClass;
m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
m_Light->SetDirection(0.0f, 0.0f, 1.0f);
return true; return true;
} }
@ -78,12 +85,19 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
void ApplicationClass::Shutdown() void ApplicationClass::Shutdown()
{ {
// Release the texture shader object. // Release the light object.
if (m_TextureShader) if (m_Light)
{ {
m_TextureShader->Shutdown(); delete m_Light;
delete m_TextureShader; m_Light = 0;
m_TextureShader = 0; }
// Release the light shader object.
if (m_LightShader)
{
m_LightShader->Shutdown();
delete m_LightShader;
m_LightShader = 0;
} }
// Release the model object. // Release the model object.
@ -115,11 +129,19 @@ void ApplicationClass::Shutdown()
bool ApplicationClass::Frame() bool ApplicationClass::Frame()
{ {
static float rotation = 0.0f;
bool result; bool result;
// Update the rotation variable each frame.
rotation -= 0.0174532925f * 0.1f;
if (rotation < 0.0f)
{
rotation += 360.0f;
}
// Render the graphics scene. // Render the graphics scene.
result = Render(); result = Render(rotation);
if (!result) if (!result)
{ {
return false; return false;
@ -129,7 +151,7 @@ bool ApplicationClass::Frame()
} }
bool ApplicationClass::Render() bool ApplicationClass::Render(float rotation)
{ {
XMMATRIX worldMatrix, viewMatrix, projectionMatrix; XMMATRIX worldMatrix, viewMatrix, projectionMatrix;
bool result; bool result;
@ -145,16 +167,19 @@ bool ApplicationClass::Render()
m_Direct3D->GetWorldMatrix(worldMatrix); m_Direct3D->GetWorldMatrix(worldMatrix);
m_Camera->GetViewMatrix(viewMatrix); m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix); m_Direct3D->GetProjectionMatrix(projectionMatrix);
// Rotate the world matrix by the rotation value so that the triangle will spin.
worldMatrix = XMMatrixRotationY(rotation);
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing. // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
m_Model->Render(m_Direct3D->GetDeviceContext()); m_Model->Render(m_Direct3D->GetDeviceContext());
// Render the model using the texture shader.
result = m_TextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture()); // Render the model using the light shader.
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(),
m_Light->GetDirection(), m_Light->GetDiffuseColor());
if (!result) if (!result)
{ {
return false; return false;
} }
// Present the rendered scene to the screen. // Present the rendered scene to the screen.
m_Direct3D->EndScene(); m_Direct3D->EndScene();

View File

@ -8,9 +8,8 @@
#include "d3dclass.h" #include "d3dclass.h"
#include "cameraclass.h" #include "cameraclass.h"
#include "modelclass.h" #include "modelclass.h"
#include "lightshaderclass.h"
#include "textureshaderclass.h" #include "lightclass.h"
///////////// /////////////
// GLOBALS // // GLOBALS //
@ -36,13 +35,14 @@ public:
bool Frame(); bool Frame();
private: private:
bool Render(); bool Render(float);
private: private:
D3DClass* m_Direct3D; D3DClass* m_Direct3D;
CameraClass* m_Camera; CameraClass* m_Camera;
ModelClass* m_Model; ModelClass* m_Model;
TextureShaderClass* m_TextureShader; LightShaderClass* m_LightShader;
LightClass* m_Light;
}; };
#endif #endif

View File

@ -25,27 +25,29 @@
<ClCompile Include="Colorshaderclass.cpp" /> <ClCompile Include="Colorshaderclass.cpp" />
<ClCompile Include="d3dclass.cpp" /> <ClCompile Include="d3dclass.cpp" />
<ClCompile Include="inputclass.cpp" /> <ClCompile Include="inputclass.cpp" />
<ClCompile Include="lightclass.cpp" />
<ClCompile Include="lightshaderclass.cpp" />
<ClCompile Include="Main.cpp" /> <ClCompile Include="Main.cpp" />
<ClCompile Include="modelclass.cpp" /> <ClCompile Include="modelclass.cpp" />
<ClCompile Include="Systemclass.cpp" /> <ClCompile Include="Systemclass.cpp" />
<ClCompile Include="textureclass.cpp" /> <ClCompile Include="textureclass.cpp" />
<ClCompile Include="textureshaderclass.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\dx11win10tut06_src\source\lightclass.h" />
<ClInclude Include="applicationclass.h" /> <ClInclude Include="applicationclass.h" />
<ClInclude Include="Cameraclass.h" /> <ClInclude Include="Cameraclass.h" />
<ClInclude Include="Colorshaderclass.h" /> <ClInclude Include="Colorshaderclass.h" />
<ClInclude Include="d3dclass.h" /> <ClInclude Include="d3dclass.h" />
<ClInclude Include="inputclass.h" /> <ClInclude Include="inputclass.h" />
<ClInclude Include="lightshaderclass.h" />
<ClInclude Include="modelclass.h" /> <ClInclude Include="modelclass.h" />
<ClInclude Include="systemclass.h" /> <ClInclude Include="systemclass.h" />
<ClInclude Include="textureclass.h" /> <ClInclude Include="textureclass.h" />
<ClInclude Include="textureshaderclass.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="light.ps" />
<None Include="light.vs" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="texture.ps" />
<None Include="texture.vs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Color.ps"> <None Include="Color.ps">

View File

@ -48,10 +48,13 @@
<ClCompile Include="Cameraclass.cpp"> <ClCompile Include="Cameraclass.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="textureshaderclass.cpp"> <ClCompile Include="textureclass.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="textureclass.cpp"> <ClCompile Include="lightshaderclass.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="lightclass.cpp">
<Filter>Fichiers sources</Filter> <Filter>Fichiers sources</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -77,10 +80,13 @@
<ClInclude Include="Cameraclass.h"> <ClInclude Include="Cameraclass.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="textureshaderclass.h"> <ClInclude Include="textureclass.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="textureclass.h"> <ClInclude Include="..\..\dx11win10tut06_src\source\lightclass.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="lightshaderclass.h">
<Filter>Fichiers d%27en-tête</Filter> <Filter>Fichiers d%27en-tête</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
@ -92,10 +98,10 @@
<None Include="Color.vs"> <None Include="Color.vs">
<Filter>shader</Filter> <Filter>shader</Filter>
</None> </None>
<None Include="texture.vs"> <None Include="light.vs">
<Filter>texture</Filter> <Filter>texture</Filter>
</None> </None>
<None Include="texture.ps"> <None Include="light.ps">
<Filter>texture</Filter> <Filter>texture</Filter>
</None> </None>
</ItemGroup> </ItemGroup>

61
enginecustom/light.ps Normal file
View File

@ -0,0 +1,61 @@
////////////////////////////////////////////////////////////////////////////////
// Filename: light.ps
////////////////////////////////////////////////////////////////////////////////
/////////////
// GLOBALS //
/////////////
Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0);
cbuffer LightBuffer
{
float4 diffuseColor;
float3 lightDirection;
float padding;
};
//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};
////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
float4 textureColor;
float3 lightDir;
float lightIntensity;
float4 color;
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
textureColor = shaderTexture.Sample(SampleType, input.tex);
// Invert the light direction for calculations.
lightDir = -lightDirection;
// Calculate the amount of light on this pixel.
lightIntensity = saturate(dot(input.normal, lightDir));
// Change the diffuse color to red (0, 1, 0)
float3 greenDiffuseColor = float3(1, 0, 0);
// Determine the final amount of diffuse color based on the diffuse color combined with the light intensity.
color = saturate(diffuseColor * lightIntensity);
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
color = color * textureColor;
return color;
}

61
enginecustom/light.vs Normal file
View File

@ -0,0 +1,61 @@
////////////////////////////////////////////////////////////////////////////////
// Filename: light.vs
////////////////////////////////////////////////////////////////////////////////
/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};
////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType LightVertexShader(VertexInputType input)
{
PixelInputType output;
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
// Store the texture coordinates for the pixel shader.
output.tex = input.tex;
// Calculate the normal vector against the world matrix only.
output.normal = mul(input.normal, (float3x3)worldMatrix);
// Normalize the normal vector.
output.normal = normalize(output.normal);
return output;
}

View File

@ -0,0 +1,45 @@
////////////////////////////////////////////////////////////////////////////////
// Filename: lightclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "lightclass.h"
LightClass::LightClass()
{
}
LightClass::LightClass(const LightClass& other)
{
}
LightClass::~LightClass()
{
}
void LightClass::SetDiffuseColor(float red, float green, float blue, float alpha)
{
m_diffuseColor = XMFLOAT4(red, green, blue, alpha);
return;
}
void LightClass::SetDirection(float x, float y, float z)
{
m_direction = XMFLOAT3(x, y, z);
return;
}
XMFLOAT4 LightClass::GetDiffuseColor()
{
return m_diffuseColor;
}
XMFLOAT3 LightClass::GetDirection()
{
return m_direction;
}

36
enginecustom/lightclass.h Normal file
View File

@ -0,0 +1,36 @@
////////////////////////////////////////////////////////////////////////////////
// Filename: lightclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _LIGHTCLASS_H_
#define _LIGHTCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <directxmath.h>
using namespace DirectX;
////////////////////////////////////////////////////////////////////////////////
// Class name: LightClass
////////////////////////////////////////////////////////////////////////////////
class LightClass
{
public:
LightClass();
LightClass(const LightClass&);
~LightClass();
void SetDiffuseColor(float, float, float, float);
void SetDirection(float, float, float);
XMFLOAT4 GetDiffuseColor();
XMFLOAT3 GetDirection();
private:
XMFLOAT4 m_diffuseColor;
XMFLOAT3 m_direction;
};
#endif

View File

@ -1,41 +1,47 @@
#include "textureshaderclass.h" ////////////////////////////////////////////////////////////////////////////////
// Filename: lightshaderclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "lightshaderclass.h"
TextureShaderClass::TextureShaderClass() LightShaderClass::LightShaderClass()
{ {
m_vertexShader = 0; m_vertexShader = 0;
m_pixelShader = 0; m_pixelShader = 0;
m_layout = 0; m_layout = 0;
m_matrixBuffer = 0;
m_sampleState = 0; m_sampleState = 0;
m_matrixBuffer = 0;
m_lightBuffer = 0;
} }
TextureShaderClass::TextureShaderClass(const TextureShaderClass& other) LightShaderClass::LightShaderClass(const LightShaderClass& other)
{ {
} }
TextureShaderClass::~TextureShaderClass() LightShaderClass::~LightShaderClass()
{ {
} }
bool TextureShaderClass::Initialize(ID3D11Device* device, HWND hwnd) bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
{ {
bool result;
wchar_t vsFilename[128]; wchar_t vsFilename[128];
wchar_t psFilename[128]; wchar_t psFilename[128];
int error; int error;
bool result;
// Set the filename of the vertex shader. // Set the filename of the vertex shader.
error = wcscpy_s(vsFilename, 128, L"texture.vs"); error = wcscpy_s(vsFilename, 128, L"../enginecustom/light.vs");
if (error != 0) if (error != 0)
{ {
return false; return false;
} }
// Set the filename of the pixel shader. // Set the filename of the pixel shader.
error = wcscpy_s(psFilename, 128, L"texture.ps"); error = wcscpy_s(psFilename, 128, L"../enginecustom/light.ps");
if (error != 0) if (error != 0)
{ {
return false; return false;
@ -43,7 +49,7 @@ bool TextureShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
// Initialize the vertex and pixel shaders. // Initialize the vertex and pixel shaders.
result = InitializeShader(device, hwnd, vsFilename, psFilename); result = InitializeShader(device, hwnd, vsFilename, psFilename);
if (!result) if(!result)
{ {
return false; return false;
} }
@ -51,7 +57,8 @@ bool TextureShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
return true; return true;
} }
void TextureShaderClass::Shutdown()
void LightShaderClass::Shutdown()
{ {
// Shutdown the vertex and pixel shaders as well as the related objects. // Shutdown the vertex and pixel shaders as well as the related objects.
ShutdownShader(); ShutdownShader();
@ -59,15 +66,16 @@ void TextureShaderClass::Shutdown()
return; return;
} }
bool TextureShaderClass::Render(ID3D11DeviceContext * deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
XMMATRIX projectionMatrix, ID3D11ShaderResourceView * texture) bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
{ {
bool result; bool result;
// Set the shader parameters that it will use for rendering. // Set the shader parameters that it will use for rendering.
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture); result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor);
if (!result) if(!result)
{ {
return false; return false;
} }
@ -78,29 +86,31 @@ bool TextureShaderClass::Render(ID3D11DeviceContext * deviceContext, int indexCo
return true; return true;
} }
bool TextureShaderClass::InitializeShader(ID3D11Device * device, HWND hwnd, WCHAR * vsFilename, WCHAR * psFilename)
bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
{ {
HRESULT result; HRESULT result;
ID3D10Blob* errorMessage; ID3D10Blob* errorMessage;
ID3D10Blob* vertexShaderBuffer; ID3D10Blob* vertexShaderBuffer;
ID3D10Blob* pixelShaderBuffer; ID3D10Blob* pixelShaderBuffer;
D3D11_INPUT_ELEMENT_DESC polygonLayout[2]; D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
unsigned int numElements; unsigned int numElements;
D3D11_SAMPLER_DESC samplerDesc;
D3D11_BUFFER_DESC matrixBufferDesc; D3D11_BUFFER_DESC matrixBufferDesc;
D3D11_SAMPLER_DESC samplerDesc; D3D11_BUFFER_DESC lightBufferDesc;
// Initialize the pointers this function will use to null. // Initialize the pointers this function will use to null.
errorMessage = 0; errorMessage = 0;
vertexShaderBuffer = 0; vertexShaderBuffer = 0;
pixelShaderBuffer = 0; pixelShaderBuffer = 0;
// Compile the vertex shader code.
result = D3DCompileFromFile(vsFilename, NULL, NULL, "TextureVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, // Compile the vertex shader code.
&vertexShaderBuffer, &errorMessage); result = D3DCompileFromFile(vsFilename, NULL, NULL, "LightVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
if (FAILED(result)) if(FAILED(result))
{ {
// If the shader failed to compile it should have writen something to the error message. // If the shader failed to compile it should have writen something to the error message.
if (errorMessage) if(errorMessage)
{ {
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename); OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
} }
@ -113,13 +123,12 @@ bool TextureShaderClass::InitializeShader(ID3D11Device * device, HWND hwnd, WCHA
return false; return false;
} }
// Compile the pixel shader code. // Compile the pixel shader code.
result = D3DCompileFromFile(psFilename, NULL, NULL, "TexturePixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, result = D3DCompileFromFile(psFilename, NULL, NULL, "LightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
&pixelShaderBuffer, &errorMessage); if(FAILED(result))
if (FAILED(result))
{ {
// If the shader failed to compile it should have writen something to the error message. // If the shader failed to compile it should have writen something to the error message.
if (errorMessage) if(errorMessage)
{ {
OutputShaderErrorMessage(errorMessage, hwnd, psFilename); OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
} }
@ -132,19 +141,20 @@ bool TextureShaderClass::InitializeShader(ID3D11Device * device, HWND hwnd, WCHA
return false; return false;
} }
// Create the vertex shader from the buffer. // Create the vertex shader from the buffer.
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader); result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
if (FAILED(result)) if(FAILED(result))
{ {
return false; return false;
} }
// Create the pixel shader from the buffer. // Create the pixel shader from the buffer.
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader); result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
if (FAILED(result)) if(FAILED(result))
{ {
return false; return false;
} }
// Create the vertex input layout description. // Create the vertex input layout description.
// This setup needs to match the VertexType stucture in the ModelClass and in the shader. // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
polygonLayout[0].SemanticName = "POSITION"; polygonLayout[0].SemanticName = "POSITION";
@ -163,13 +173,21 @@ bool TextureShaderClass::InitializeShader(ID3D11Device * device, HWND hwnd, WCHA
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[1].InstanceDataStepRate = 0; polygonLayout[1].InstanceDataStepRate = 0;
polygonLayout[2].SemanticName = "NORMAL";
polygonLayout[2].SemanticIndex = 0;
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
polygonLayout[2].InputSlot = 0;
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
polygonLayout[2].InstanceDataStepRate = 0;
// Get a count of the elements in the layout. // Get a count of the elements in the layout.
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]); numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
// Create the vertex input layout. // Create the vertex input layout.
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(),
vertexShaderBuffer->GetBufferSize(), &m_layout); &m_layout);
if (FAILED(result)) if(FAILED(result))
{ {
return false; return false;
} }
@ -181,38 +199,55 @@ bool TextureShaderClass::InitializeShader(ID3D11Device * device, HWND hwnd, WCHA
pixelShaderBuffer->Release(); pixelShaderBuffer->Release();
pixelShaderBuffer = 0; pixelShaderBuffer = 0;
// Create a texture sampler state description.
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 0;
samplerDesc.BorderColor[1] = 0;
samplerDesc.BorderColor[2] = 0;
samplerDesc.BorderColor[3] = 0;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
// Create the texture sampler state.
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
if(FAILED(result))
{
return false;
}
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader. // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC; matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType); matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
matrixBufferDesc.MiscFlags = 0; matrixBufferDesc.MiscFlags = 0;
matrixBufferDesc.StructureByteStride = 0; matrixBufferDesc.StructureByteStride = 0;
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class. // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer); result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
if (FAILED(result)) if(FAILED(result))
{ {
return false; return false;
} }
// Create a texture sampler state description.
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
samplerDesc.BorderColor[0] = 0;
samplerDesc.BorderColor[1] = 0;
samplerDesc.BorderColor[2] = 0;
samplerDesc.BorderColor[3] = 0;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
// Create the texture sampler state. // Setup the description of the light dynamic constant buffer that is in the pixel shader.
result = device->CreateSamplerState(&samplerDesc, &m_sampleState); // Note that ByteWidth always needs to be a multiple of 16 if using D3D11_BIND_CONSTANT_BUFFER or CreateBuffer will fail.
if (FAILED(result)) lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
lightBufferDesc.MiscFlags = 0;
lightBufferDesc.StructureByteStride = 0;
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
result = device->CreateBuffer(&lightBufferDesc, NULL, &m_lightBuffer);
if(FAILED(result))
{ {
return false; return false;
} }
@ -220,39 +255,46 @@ bool TextureShaderClass::InitializeShader(ID3D11Device * device, HWND hwnd, WCHA
return true; return true;
} }
void TextureShaderClass::ShutdownShader()
{
// Release the sampler state. void LightShaderClass::ShutdownShader()
if (m_sampleState) {
// Release the light constant buffer.
if(m_lightBuffer)
{ {
m_sampleState->Release(); m_lightBuffer->Release();
m_sampleState = 0; m_lightBuffer = 0;
} }
// Release the matrix constant buffer. // Release the matrix constant buffer.
if (m_matrixBuffer) if(m_matrixBuffer)
{ {
m_matrixBuffer->Release(); m_matrixBuffer->Release();
m_matrixBuffer = 0; m_matrixBuffer = 0;
} }
// Release the sampler state.
if(m_sampleState)
{
m_sampleState->Release();
m_sampleState = 0;
}
// Release the layout. // Release the layout.
if (m_layout) if(m_layout)
{ {
m_layout->Release(); m_layout->Release();
m_layout = 0; m_layout = 0;
} }
// Release the pixel shader. // Release the pixel shader.
if (m_pixelShader) if(m_pixelShader)
{ {
m_pixelShader->Release(); m_pixelShader->Release();
m_pixelShader = 0; m_pixelShader = 0;
} }
// Release the vertex shader. // Release the vertex shader.
if (m_vertexShader) if(m_vertexShader)
{ {
m_vertexShader->Release(); m_vertexShader->Release();
m_vertexShader = 0; m_vertexShader = 0;
@ -261,10 +303,11 @@ void TextureShaderClass::ShutdownShader()
return; return;
} }
void TextureShaderClass::OutputShaderErrorMessage(ID3D10Blob * errorMessage, HWND hwnd, WCHAR * shaderFilename)
void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
{ {
char* compileErrors; char* compileErrors;
unsigned long long bufferSize, i; unsigned __int64 bufferSize, i;
ofstream fout; ofstream fout;
@ -278,7 +321,7 @@ void TextureShaderClass::OutputShaderErrorMessage(ID3D10Blob * errorMessage, HWN
fout.open("shader-error.txt"); fout.open("shader-error.txt");
// Write out the error message. // Write out the error message.
for (i = 0; i < bufferSize; i++) for(i=0; i<bufferSize; i++)
{ {
fout << compileErrors[i]; fout << compileErrors[i];
} }
@ -296,13 +339,15 @@ void TextureShaderClass::OutputShaderErrorMessage(ID3D10Blob * errorMessage, HWN
return; return;
} }
bool TextureShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture) bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
{ {
HRESULT result; HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource; D3D11_MAPPED_SUBRESOURCE mappedResource;
MatrixBufferType* dataPtr;
unsigned int bufferNumber; unsigned int bufferNumber;
MatrixBufferType* dataPtr;
LightBufferType* dataPtr2;
// Transpose the matrices to prepare them for the shader. // Transpose the matrices to prepare them for the shader.
@ -312,7 +357,7 @@ bool TextureShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext,
// Lock the constant buffer so it can be written to. // Lock the constant buffer so it can be written to.
result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(result)) if(FAILED(result))
{ {
return false; return false;
} }
@ -326,27 +371,54 @@ bool TextureShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext,
dataPtr->projection = projectionMatrix; dataPtr->projection = projectionMatrix;
// Unlock the constant buffer. // Unlock the constant buffer.
deviceContext->Unmap(m_matrixBuffer, 0); deviceContext->Unmap(m_matrixBuffer, 0);
// Set the position of the constant buffer in the vertex shader. // Set the position of the constant buffer in the vertex shader.
bufferNumber = 0; bufferNumber = 0;
// Finanly set the constant buffer in the vertex shader with the updated values. // Now set the constant buffer in the vertex shader with the updated values.
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer); deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
// Set shader texture resource in the pixel shader. // Set shader texture resource in the pixel shader.
deviceContext->PSSetShaderResources(0, 1, &texture); deviceContext->PSSetShaderResources(0, 1, &texture);
// Lock the light constant buffer so it can be written to.
result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if(FAILED(result))
{
return false;
}
// Get a pointer to the data in the constant buffer.
dataPtr2 = (LightBufferType*)mappedResource.pData;
// Copy the lighting variables into the constant buffer.
dataPtr2->diffuseColor = diffuseColor;
dataPtr2->lightDirection = lightDirection;
dataPtr2->padding = 0.0f;
// Unlock the constant buffer.
deviceContext->Unmap(m_lightBuffer, 0);
// Set the position of the light constant buffer in the pixel shader.
bufferNumber = 0;
// Finally set the light constant buffer in the pixel shader with the updated values.
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
return true; return true;
} }
void TextureShaderClass::RenderShader(ID3D11DeviceContext * deviceContext, int indexCount)
void LightShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
{ {
// Set the vertex input layout. // Set the vertex input layout.
deviceContext->IASetInputLayout(m_layout); deviceContext->IASetInputLayout(m_layout);
// Set the vertex and pixel shaders that will be used to render this triangle. // Set the vertex and pixel shaders that will be used to render this triangle.
deviceContext->VSSetShader(m_vertexShader, NULL, 0); deviceContext->VSSetShader(m_vertexShader, NULL, 0);
deviceContext->PSSetShader(m_pixelShader, NULL, 0); deviceContext->PSSetShader(m_pixelShader, NULL, 0);
// Set the sampler state in the pixel shader. // Set the sampler state in the pixel shader.
deviceContext->PSSetSamplers(0, 1, &m_sampleState); deviceContext->PSSetSamplers(0, 1, &m_sampleState);

View File

@ -1,5 +1,8 @@
#ifndef _TEXTURESHADERCLASS_H_ ////////////////////////////////////////////////////////////////////////////////
#define _TEXTURESHADERCLASS_H_ // Filename: lightshaderclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _LIGHTSHADERCLASS_H_
#define _LIGHTSHADERCLASS_H_
////////////// //////////////
@ -14,9 +17,9 @@ using namespace std;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Class name: TextureShaderClass // Class name: LightShaderClass
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
class TextureShaderClass class LightShaderClass
{ {
private: private:
struct MatrixBufferType struct MatrixBufferType
@ -26,29 +29,37 @@ private:
XMMATRIX projection; XMMATRIX projection;
}; };
struct LightBufferType
{
XMFLOAT4 diffuseColor;
XMFLOAT3 lightDirection;
float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements.
};
public: public:
TextureShaderClass(); LightShaderClass();
TextureShaderClass(const TextureShaderClass&); LightShaderClass(const LightShaderClass&);
~TextureShaderClass(); ~LightShaderClass();
bool Initialize(ID3D11Device*, HWND); bool Initialize(ID3D11Device*, HWND);
void Shutdown(); void Shutdown();
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*); bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4);
private: private:
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*); bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
void ShutdownShader(); void ShutdownShader();
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*); void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*); bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4);
void RenderShader(ID3D11DeviceContext*, int); void RenderShader(ID3D11DeviceContext*, int);
private: private:
ID3D11VertexShader* m_vertexShader; ID3D11VertexShader* m_vertexShader;
ID3D11PixelShader* m_pixelShader; ID3D11PixelShader* m_pixelShader;
ID3D11InputLayout* m_layout; ID3D11InputLayout* m_layout;
ID3D11Buffer* m_matrixBuffer;
ID3D11SamplerState* m_sampleState; ID3D11SamplerState* m_sampleState;
ID3D11Buffer* m_matrixBuffer;
ID3D11Buffer* m_lightBuffer;
}; };
#endif #endif

View File

@ -94,33 +94,22 @@ bool ModelClass::InitializeBuffers(ID3D11Device* device)
indices = new unsigned long[m_indexCount]; indices = new unsigned long[m_indexCount];
// Load the vertex array with data. // Load the vertex array with data.
// Triangle 1 vertices[0].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); // Bottom left.
vertices[0].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); // Bottom Left.
vertices[0].texture = XMFLOAT2(0.0f, 1.0f); vertices[0].texture = XMFLOAT2(0.0f, 1.0f);
vertices[0].normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
vertices[1].position = XMFLOAT3(1.0f, 1.0f, 0.0f); // Top Right. vertices[1].position = XMFLOAT3(0.0f, 1.0f, 0.0f); // Top middle.
vertices[1].texture = XMFLOAT2(1.0f, 0.0f); vertices[1].texture = XMFLOAT2(0.5f, 0.0f);
vertices[1].normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
vertices[2].position = XMFLOAT3(1.0f, -1.0f, 0.0f); // Bottom Right. vertices[2].position = XMFLOAT3(1.0f, -1.0f, 0.0f); // Bottom right.
vertices[2].texture = XMFLOAT2(1.0f, 1.0f); vertices[2].texture = XMFLOAT2(1.0f, 1.0f);
vertices[2].normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
// Triangle 2
vertices[3].position = XMFLOAT3(-1.0f, 1.0f, 0.0f); // Top Left.
vertices[3].texture = XMFLOAT2(0.0f, 0.0f);
vertices[4].position = XMFLOAT3(1.0f, 1.0f, 0.0f); // Top Right.
vertices[4].texture = XMFLOAT2(1.0f, 0.0f);
vertices[5].position = XMFLOAT3(-1.0f, -1.0f, 0.0f); // Bottom Left.
vertices[5].texture = XMFLOAT2(0.0f, 1.0f);
// Load the index array with data. // Load the index array with data.
indices[0] = 0; // Bottom left. indices[0] = 0; // Bottom left.
indices[1] = 1; // Top middle. indices[1] = 1; // Top middle.
indices[2] = 2; // Bottom right. indices[2] = 2; // Bottom right.
indices[3] = 3; // Top left.
indices[4] = 4; // Top right.
indices[5] = 5; // Bottom left.
// Set up the description of the static vertex buffer. // Set up the description of the static vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT; vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;

View File

@ -26,6 +26,7 @@ private:
{ {
XMFLOAT3 position; XMFLOAT3 position;
XMFLOAT2 texture; XMFLOAT2 texture;
XMFLOAT3 normal;
}; };
public: public:

View File

@ -1,28 +0,0 @@
/////////////
// GLOBALS //
/////////////
Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0);
//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
};
////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 TexturePixelShader(PixelInputType input) : SV_TARGET
{
float4 textureColor;
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
textureColor = shaderTexture.Sample(SampleType, input.tex);
return textureColor;
}

View File

@ -1,47 +0,0 @@
/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
};
struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
};
////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType TextureVertexShader(VertexInputType input)
{
PixelInputType output;
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
// Store the texture coordinates for the pixel shader.
output.tex = input.tex;
return output;
}