Specular Lighting
This commit is contained in:
@@ -11,6 +11,7 @@ LightShaderClass::LightShaderClass()
|
||||
m_layout = 0;
|
||||
m_sampleState = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_cameraBuffer = 0;
|
||||
m_lightBuffer = 0;
|
||||
}
|
||||
|
||||
@@ -32,13 +33,6 @@ bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
int error;
|
||||
bool result;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"./Light.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"light.vs");
|
||||
if (error != 0)
|
||||
@@ -72,13 +66,15 @@ void LightShaderClass::Shutdown()
|
||||
}
|
||||
|
||||
bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor)
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor,
|
||||
XMFLOAT3 cameraPosition, XMFLOAT4 specularColor, float specularPower)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor);
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor,
|
||||
cameraPosition, specularColor, specularPower);
|
||||
if(!result)
|
||||
{
|
||||
return false;
|
||||
@@ -97,12 +93,11 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||
unsigned int numElements;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
|
||||
D3D11_BUFFER_DESC cameraBufferDesc;
|
||||
D3D11_BUFFER_DESC lightBufferDesc;
|
||||
|
||||
|
||||
@@ -242,10 +237,25 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the camera dynamic constant buffer that is in the vertex shader.
|
||||
cameraBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
cameraBufferDesc.ByteWidth = sizeof(CameraBufferType);
|
||||
cameraBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
cameraBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
cameraBufferDesc.MiscFlags = 0;
|
||||
cameraBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the camera constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&cameraBufferDesc, NULL, &m_cameraBuffer);
|
||||
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.
|
||||
// Note that ByteWidth always needs to be a multiple of 16 if using D3D11_BIND_CONSTANT_BUFFER or CreateBuffer will fail.
|
||||
lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType) + 12 ;
|
||||
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightBufferDesc.MiscFlags = 0;
|
||||
@@ -271,6 +281,13 @@ void LightShaderClass::ShutdownShader()
|
||||
m_lightBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the camera constant buffer.
|
||||
if (m_cameraBuffer)
|
||||
{
|
||||
m_cameraBuffer->Release();
|
||||
m_cameraBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
@@ -347,14 +364,15 @@ void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND h
|
||||
|
||||
|
||||
bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor)
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor,
|
||||
XMFLOAT3 cameraPosition, XMFLOAT4 specularColor, float specularPower)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
unsigned int bufferNumber;
|
||||
MatrixBufferType* dataPtr;
|
||||
LightBufferType* dataPtr2;
|
||||
|
||||
CameraBufferType* dataPtr3;
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
@@ -385,6 +403,29 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
|
||||
// Now set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Lock the camera constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_cameraBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr3 = (CameraBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the camera position into the constant buffer.
|
||||
dataPtr3->cameraPosition = cameraPosition;
|
||||
dataPtr3->padding = 0.0f;
|
||||
|
||||
// Unlock the camera constant buffer.
|
||||
deviceContext->Unmap(m_cameraBuffer, 0);
|
||||
|
||||
// Set the position of the camera constant buffer in the vertex shader.
|
||||
bufferNumber = 1;
|
||||
|
||||
// Now set the camera constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_cameraBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
@@ -402,7 +443,9 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
|
||||
dataPtr2->ambientColor = ambientColor;
|
||||
dataPtr2->diffuseColor = diffuseColor;
|
||||
dataPtr2->lightDirection = lightDirection;
|
||||
dataPtr2->padding = 0.0f;
|
||||
dataPtr2->specularColor = specularColor;
|
||||
dataPtr2->specularPower = specularPower;
|
||||
//dataPtr2->padding = 0.0f;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightBuffer, 0);
|
||||
|
||||
Reference in New Issue
Block a user