Multiple Point Lights
This commit is contained in:
parent
8d56c159c6
commit
ee4564560d
@ -28,6 +28,7 @@ public:
|
||||
XMFLOAT3 GetPosition();
|
||||
XMFLOAT3 GetRotation();
|
||||
|
||||
|
||||
void Render();
|
||||
XMMATRIX GetViewMatrix();
|
||||
|
||||
|
@ -6,7 +6,7 @@ ApplicationClass::ApplicationClass()
|
||||
m_Camera = 0;
|
||||
m_Model = 0;
|
||||
m_LightShader = 0;
|
||||
m_Light = 0;
|
||||
m_Lights = 0;
|
||||
}
|
||||
|
||||
|
||||
@ -48,7 +48,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
}
|
||||
|
||||
// Set the initial position of the camera.
|
||||
m_Camera->SetPosition(0.0f, 0.0f, -10.0f);
|
||||
m_Camera->SetPosition(0.0f, 2.0f, -12.0f);
|
||||
m_Camera->SetRotation(0.0f, 0.0f, 10.0f);
|
||||
|
||||
// Set the file name of the model.
|
||||
@ -76,6 +76,24 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
|
||||
return false;
|
||||
}
|
||||
// Set the number of lights we will use.
|
||||
m_numLights = 4;
|
||||
|
||||
// Create and initialize the light objects array.
|
||||
m_Lights = new LightClass[m_numLights];
|
||||
|
||||
// Manually set the color and position of each light.
|
||||
m_Lights[0].SetDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); // Red
|
||||
m_Lights[0].SetPosition(-3.0f, 1.0f, 3.0f);
|
||||
|
||||
m_Lights[1].SetDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); // Green
|
||||
m_Lights[1].SetPosition(3.0f, 1.0f, 3.0f);
|
||||
|
||||
m_Lights[2].SetDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); // Blue
|
||||
m_Lights[2].SetPosition(-3.0f, 1.0f, -3.0f);
|
||||
|
||||
m_Lights[3].SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); // White
|
||||
m_Lights[3].SetPosition(3.0f, 1.0f, -3.0f);
|
||||
// Create and initialize the light object.
|
||||
m_Light = new LightClass;
|
||||
|
||||
@ -89,10 +107,10 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
void ApplicationClass::Shutdown()
|
||||
{
|
||||
// Release the light object.
|
||||
if (m_Light)
|
||||
if (m_Lights)
|
||||
{
|
||||
delete m_Light;
|
||||
m_Light = 0;
|
||||
delete m_Lights;
|
||||
m_Lights = 0;
|
||||
}
|
||||
|
||||
// Release the light shader object.
|
||||
@ -163,7 +181,9 @@ bool ApplicationClass::Frame()
|
||||
|
||||
bool ApplicationClass::Render(float rotation)
|
||||
{
|
||||
XMMATRIX worldMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix;
|
||||
XMMATRIX worldMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix, viewMatrix, projectionMatrix;;
|
||||
XMFLOAT4 diffuseColor[4], lightPosition[4];
|
||||
int i;
|
||||
bool result;
|
||||
|
||||
|
||||
@ -175,6 +195,21 @@ bool ApplicationClass::Render(float rotation)
|
||||
|
||||
// Get the world, view, and projection matrices from the camera and d3d objects.
|
||||
m_Direct3D->GetWorldMatrix(worldMatrix);
|
||||
viewMatrix = m_Camera->GetViewMatrix();
|
||||
m_Direct3D->GetProjectionMatrix(projectionMatrix);
|
||||
|
||||
// Get the light properties.
|
||||
for (i = 0; i < m_numLights; i++)
|
||||
{
|
||||
// Create the diffuse color array from the four light colors.
|
||||
diffuseColor[i] = m_Lights[i].GetDiffuseColor();
|
||||
|
||||
// Create the light position array from the four light positions.
|
||||
lightPosition[i] = m_Lights[i].GetPosition();
|
||||
}
|
||||
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
m_Model->Render(m_Direct3D->GetDeviceContext());
|
||||
|
||||
|
||||
for (auto cube : m_cubes)
|
||||
{
|
||||
@ -186,8 +221,8 @@ bool ApplicationClass::Render(float rotation)
|
||||
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
|
||||
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
|
||||
|
||||
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), cube->GetIndexCount(), worldMatrix, m_Camera->GetViewMatrix(), m_Direct3D->GetProjectionMatrix(), cube->GetTexture(),
|
||||
m_Light->GetDirection(), m_Light->GetDiffuseColor());
|
||||
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(),
|
||||
diffuseColor, lightPosition);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
|
@ -59,6 +59,8 @@ private:
|
||||
float speed = 0.1f;
|
||||
std::vector<Object*> m_cubes;
|
||||
Object* m_SelectedObject;
|
||||
LightClass* m_Lights;
|
||||
int m_numLights;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -463,8 +463,9 @@ ID3D11DeviceContext* D3DClass::GetDeviceContext()
|
||||
}
|
||||
|
||||
|
||||
XMMATRIX D3DClass::GetProjectionMatrix()
|
||||
XMMATRIX D3DClass::GetProjectionMatrix(XMMATRIX& projectionMatrix)
|
||||
{
|
||||
projectionMatrix = m_projectionMatrix;
|
||||
return m_projectionMatrix;
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,7 @@ public:
|
||||
|
||||
ID3D11Device* GetDevice();
|
||||
ID3D11DeviceContext* GetDeviceContext();
|
||||
XMMATRIX GetProjectionMatrix(XMMATRIX& projectionMatrix);
|
||||
IDXGISwapChain* m_swapChain;
|
||||
IDXGISwapChain* GetSwapChain();
|
||||
void ResizeSwapChain(int, int);
|
||||
|
@ -3,10 +3,10 @@ Pos=60,60
|
||||
Size=400,400
|
||||
|
||||
[Window][Khaotic Engine]
|
||||
Pos=765,19
|
||||
Pos=500,27
|
||||
Size=694,367
|
||||
|
||||
[Window][Objects]
|
||||
Pos=44,57
|
||||
Size=492,353
|
||||
Pos=8,44
|
||||
Size=298,297
|
||||
|
||||
|
@ -3,17 +3,21 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/////////////
|
||||
// DEFINES //
|
||||
/////////////
|
||||
#define NUM_LIGHTS 4
|
||||
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture : register(t0);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
cbuffer LightBuffer
|
||||
cbuffer LightColorBuffer
|
||||
{
|
||||
float4 diffuseColor;
|
||||
float3 lightDirection;
|
||||
float padding;
|
||||
float4 diffuseColor[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
|
||||
@ -24,7 +28,8 @@ struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 normal : NORMAL;
|
||||
float3 lightPos[NUM_LIGHTS] : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
@ -34,28 +39,38 @@ struct PixelInputType
|
||||
float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
float3 lightDir;
|
||||
float lightIntensity;
|
||||
float4 color;
|
||||
float lightIntensity[NUM_LIGHTS];
|
||||
float4 colorArray[NUM_LIGHTS];
|
||||
float4 colorSum;
|
||||
float4 color;
|
||||
int i;
|
||||
|
||||
|
||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
// Sample the texture pixel at this location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
// Invert the light direction for calculations.
|
||||
lightDir = -lightDirection;
|
||||
for(i=0; i<NUM_LIGHTS; i++)
|
||||
{
|
||||
// Calculate the different amounts of light on this pixel based on the positions of the lights.
|
||||
lightIntensity[i] = saturate(dot(input.normal, input.lightPos[i]));
|
||||
|
||||
// Calculate the amount of light on this pixel.
|
||||
lightIntensity = saturate(dot(input.normal, lightDir));
|
||||
// Determine the diffuse color amount of each of the four lights.
|
||||
colorArray[i] = diffuseColor[i] * lightIntensity[i];
|
||||
}
|
||||
|
||||
// Change the diffuse color to red (0, 1, 0)
|
||||
float3 greenDiffuseColor = float3(1, 0, 0);
|
||||
// Initialize the sum of colors.
|
||||
colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
// Determine the final amount of diffuse color based on the diffuse color combined with the light intensity.
|
||||
color = saturate(diffuseColor * lightIntensity);
|
||||
// Add all of the light colors up.
|
||||
for(i=0; i<NUM_LIGHTS; i++)
|
||||
{
|
||||
colorSum.r += colorArray[i].r;
|
||||
colorSum.g += colorArray[i].g;
|
||||
colorSum.b += colorArray[i].b;
|
||||
}
|
||||
|
||||
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
|
||||
color = color * textureColor;
|
||||
// Multiply the texture pixel by the combination of all four light colors to get the final result.
|
||||
color = saturate(colorSum) * textureColor;
|
||||
|
||||
return color;
|
||||
}
|
||||
|
@ -3,6 +3,12 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/////////////
|
||||
// DEFINES //
|
||||
/////////////
|
||||
#define NUM_LIGHTS 4
|
||||
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
@ -13,6 +19,11 @@ cbuffer MatrixBuffer
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
cbuffer LightPositionBuffer
|
||||
{
|
||||
float4 lightPosition[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
@ -29,7 +40,8 @@ struct PixelInputType
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
float3 lightPos[NUM_LIGHTS] : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@ -38,7 +50,9 @@ struct PixelInputType
|
||||
PixelInputType LightVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
float4 worldPosition;
|
||||
int i;
|
||||
|
||||
|
||||
// Change the position vector to be 4 units for proper matrix calculations.
|
||||
input.position.w = 1.0f;
|
||||
@ -57,5 +71,17 @@ PixelInputType LightVertexShader(VertexInputType input)
|
||||
// Normalize the normal vector.
|
||||
output.normal = normalize(output.normal);
|
||||
|
||||
// Calculate the position of the vertex in the world.
|
||||
worldPosition = mul(input.position, worldMatrix);
|
||||
|
||||
for(i=0; i<NUM_LIGHTS; i++)
|
||||
{
|
||||
// Determine the light positions based on the position of the lights and the position of the vertex in the world.
|
||||
output.lightPos[i] = lightPosition[i].xyz - worldPosition.xyz;
|
||||
|
||||
// Normalize the light position vectors.
|
||||
output.lightPos[i] = normalize(output.lightPos[i]);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
@ -18,6 +18,11 @@ LightClass::~LightClass()
|
||||
{
|
||||
}
|
||||
|
||||
void LightClass::SetAmbientColor(float red, float green, float blue, float alpha)
|
||||
{
|
||||
m_ambientColor = XMFLOAT4(red, green, blue, alpha);
|
||||
return;
|
||||
}
|
||||
|
||||
void LightClass::SetDiffuseColor(float red, float green, float blue, float alpha)
|
||||
{
|
||||
@ -32,6 +37,23 @@ void LightClass::SetDirection(float x, float y, float z)
|
||||
return;
|
||||
}
|
||||
|
||||
void LightClass::SetSpecularColor(float red, float green, float blue, float alpha)
|
||||
{
|
||||
m_specularColor = XMFLOAT4(red, green, blue, alpha);
|
||||
return;
|
||||
}
|
||||
|
||||
void LightClass::SetPosition(float x, float y, float z)
|
||||
{
|
||||
m_position = XMFLOAT4(x, y, z, 1.0f);
|
||||
return;
|
||||
}
|
||||
|
||||
XMFLOAT4 LightClass::GetAmbientColor()
|
||||
{
|
||||
return m_ambientColor;
|
||||
}
|
||||
|
||||
|
||||
XMFLOAT4 LightClass::GetDiffuseColor()
|
||||
{
|
||||
@ -42,4 +64,22 @@ XMFLOAT4 LightClass::GetDiffuseColor()
|
||||
XMFLOAT3 LightClass::GetDirection()
|
||||
{
|
||||
return m_direction;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
XMFLOAT4 LightClass::GetSpecularColor()
|
||||
{
|
||||
return m_specularColor;
|
||||
}
|
||||
|
||||
|
||||
float LightClass::GetSpecularPower()
|
||||
{
|
||||
return m_specularPower;
|
||||
}
|
||||
|
||||
|
||||
XMFLOAT4 LightClass::GetPosition()
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
@ -22,15 +22,26 @@ public:
|
||||
LightClass(const LightClass&);
|
||||
~LightClass();
|
||||
|
||||
void SetAmbientColor(float, float, float, float);
|
||||
void SetDiffuseColor(float, float, float, float);
|
||||
void SetDirection(float, float, float);
|
||||
|
||||
void SetSpecularColor(float, float, float, float);
|
||||
void SetSpecularPower(float);
|
||||
void SetPosition(float, float, float);
|
||||
|
||||
XMFLOAT4 GetAmbientColor();
|
||||
XMFLOAT4 GetDiffuseColor();
|
||||
XMFLOAT3 GetDirection();
|
||||
|
||||
XMFLOAT4 GetSpecularColor();
|
||||
float GetSpecularPower();
|
||||
XMFLOAT4 GetPosition();
|
||||
private:
|
||||
XMFLOAT4 m_ambientColor;
|
||||
XMFLOAT4 m_diffuseColor;
|
||||
XMFLOAT3 m_direction;
|
||||
XMFLOAT4 m_specularColor;
|
||||
float m_specularPower;
|
||||
XMFLOAT4 m_position;
|
||||
};
|
||||
|
||||
#endif
|
@ -12,6 +12,9 @@ LightShaderClass::LightShaderClass()
|
||||
m_sampleState = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_lightBuffer = 0;
|
||||
|
||||
m_lightColorBuffer = 0;
|
||||
m_lightPositionBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
@ -68,13 +71,13 @@ void LightShaderClass::Shutdown()
|
||||
|
||||
|
||||
bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[])
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor);
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, lightPosition);
|
||||
if(!result)
|
||||
{
|
||||
return false;
|
||||
@ -98,6 +101,8 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_BUFFER_DESC lightBufferDesc;
|
||||
D3D11_BUFFER_DESC lightColorBufferDesc;
|
||||
D3D11_BUFFER_DESC lightPositionBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
@ -235,6 +240,34 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Setup the description of the dynamic constant buffer that is in the pixel shader.
|
||||
lightColorBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightColorBufferDesc.ByteWidth = sizeof(LightColorBufferType);
|
||||
lightColorBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightColorBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightColorBufferDesc.MiscFlags = 0;
|
||||
lightColorBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&lightColorBufferDesc, NULL, &m_lightColorBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// Setup the description of the dynamic constant buffer that is in the vertex shader.
|
||||
lightPositionBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightPositionBufferDesc.ByteWidth = sizeof(LightPositionBufferType);
|
||||
lightPositionBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightPositionBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightPositionBufferDesc.MiscFlags = 0;
|
||||
lightPositionBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&lightPositionBufferDesc, NULL, &m_lightPositionBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the light dynamic constant buffer that is in the pixel shader.
|
||||
// Note that ByteWidth always needs to be a multiple of 16 if using D3D11_BIND_CONSTANT_BUFFER or CreateBuffer will fail.
|
||||
@ -258,6 +291,18 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
||||
|
||||
void LightShaderClass::ShutdownShader()
|
||||
{
|
||||
// Release the light constant buffers.
|
||||
if (m_lightColorBuffer)
|
||||
{
|
||||
m_lightColorBuffer->Release();
|
||||
m_lightColorBuffer = 0;
|
||||
}
|
||||
|
||||
if (m_lightPositionBuffer)
|
||||
{
|
||||
m_lightPositionBuffer->Release();
|
||||
m_lightPositionBuffer = 0;
|
||||
}
|
||||
// Release the light constant buffer.
|
||||
if(m_lightBuffer)
|
||||
{
|
||||
@ -340,14 +385,15 @@ void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND h
|
||||
}
|
||||
|
||||
|
||||
bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
|
||||
bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[])
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
unsigned int bufferNumber;
|
||||
MatrixBufferType* dataPtr;
|
||||
LightBufferType* dataPtr2;
|
||||
LightPositionBufferType* dataPtr2;
|
||||
LightColorBufferType* dataPtr3;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
@ -357,7 +403,7 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
|
||||
|
||||
// Lock the constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if(FAILED(result))
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -371,40 +417,66 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// 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.
|
||||
bufferNumber = 0;
|
||||
|
||||
// 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.
|
||||
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))
|
||||
// Lock the light position constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_lightPositionBuffer, 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;
|
||||
dataPtr2 = (LightPositionBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the lighting variables into the constant buffer.
|
||||
dataPtr2->diffuseColor = diffuseColor;
|
||||
dataPtr2->lightDirection = lightDirection;
|
||||
dataPtr2->padding = 0.0f;
|
||||
// Copy the light position variables into the constant buffer.
|
||||
dataPtr2->lightPosition[0] = lightPosition[0];
|
||||
dataPtr2->lightPosition[1] = lightPosition[1];
|
||||
dataPtr2->lightPosition[2] = lightPosition[2];
|
||||
dataPtr2->lightPosition[3] = lightPosition[3];
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightBuffer, 0);
|
||||
deviceContext->Unmap(m_lightPositionBuffer, 0);
|
||||
|
||||
// Set the position of the light constant buffer in the pixel shader.
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 1;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_lightPositionBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
// Lock the light color constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_lightColorBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr3 = (LightColorBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the light color variables into the constant buffer.
|
||||
dataPtr3->diffuseColor[0] = diffuseColor[0];
|
||||
dataPtr3->diffuseColor[1] = diffuseColor[1];
|
||||
dataPtr3->diffuseColor[2] = diffuseColor[2];
|
||||
dataPtr3->diffuseColor[3] = diffuseColor[3];
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightColorBuffer, 0);
|
||||
|
||||
// Set the position of the 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);
|
||||
// Finally set the constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightColorBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -4,6 +4,11 @@
|
||||
#ifndef _LIGHTSHADERCLASS_H_
|
||||
#define _LIGHTSHADERCLASS_H_
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
const int NUM_LIGHTS = 4;
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
@ -28,6 +33,16 @@ private:
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
struct LightColorBufferType
|
||||
{
|
||||
XMFLOAT4 diffuseColor[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
struct LightPositionBufferType
|
||||
{
|
||||
XMFLOAT4 lightPosition[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
|
||||
struct LightBufferType
|
||||
{
|
||||
@ -43,14 +58,15 @@ public:
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4);
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4[], XMFLOAT4[]);
|
||||
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4);
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4[], XMFLOAT4[]);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
@ -60,6 +76,9 @@ private:
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11Buffer* m_lightBuffer;
|
||||
|
||||
ID3D11Buffer* m_lightColorBuffer;
|
||||
ID3D11Buffer* m_lightPositionBuffer;
|
||||
};
|
||||
|
||||
#endif
|
BIN
enginecustom/shader-error.txt
Normal file
BIN
enginecustom/shader-error.txt
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user