Specular Lighting
This commit is contained in:
parent
e0c875187b
commit
16db21608a
@ -14,6 +14,8 @@ cbuffer LightBuffer
|
|||||||
float4 diffuseColor;
|
float4 diffuseColor;
|
||||||
float3 lightDirection;
|
float3 lightDirection;
|
||||||
float padding;
|
float padding;
|
||||||
|
float specularPower;
|
||||||
|
float4 specularColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -25,6 +27,7 @@ struct PixelInputType
|
|||||||
float4 position : SV_POSITION;
|
float4 position : SV_POSITION;
|
||||||
float2 tex : TEXCOORD0;
|
float2 tex : TEXCOORD0;
|
||||||
float3 normal : NORMAL;
|
float3 normal : NORMAL;
|
||||||
|
float3 viewDirection : TEXCOORD1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -37,6 +40,8 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
|||||||
float3 lightDir;
|
float3 lightDir;
|
||||||
float lightIntensity;
|
float lightIntensity;
|
||||||
float4 color;
|
float4 color;
|
||||||
|
float3 reflection;
|
||||||
|
float4 specular;
|
||||||
|
|
||||||
|
|
||||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||||
@ -45,6 +50,10 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
|||||||
// Set the default output color to the ambient light value for all pixels.
|
// Set the default output color to the ambient light value for all pixels.
|
||||||
color = ambientColor;
|
color = ambientColor;
|
||||||
|
|
||||||
|
// Initialize the specular color.
|
||||||
|
specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
|
||||||
// Invert the light direction for calculations.
|
// Invert the light direction for calculations.
|
||||||
lightDir = -lightDirection;
|
lightDir = -lightDirection;
|
||||||
|
|
||||||
@ -55,13 +64,22 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
|||||||
{
|
{
|
||||||
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
|
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
|
||||||
color += (diffuseColor * lightIntensity);
|
color += (diffuseColor * lightIntensity);
|
||||||
}
|
|
||||||
|
|
||||||
// Saturate the final light color.
|
// Saturate the ambient and diffuse color.
|
||||||
color = saturate(color);
|
color = saturate(color);
|
||||||
|
|
||||||
|
// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
|
||||||
|
reflection = normalize(2.0f * lightIntensity * input.normal - lightDir);
|
||||||
|
|
||||||
|
// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
|
||||||
|
specular = pow(saturate(dot(reflection, input.viewDirection)), specularPower);
|
||||||
|
}
|
||||||
|
|
||||||
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
|
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
|
||||||
color = color * textureColor;
|
color = color * textureColor;
|
||||||
|
|
||||||
|
// Add the specular component last to the output color.
|
||||||
|
color = saturate(color + specular);
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
@ -13,6 +13,12 @@ cbuffer MatrixBuffer
|
|||||||
matrix projectionMatrix;
|
matrix projectionMatrix;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
cbuffer CameraBuffer
|
||||||
|
{
|
||||||
|
float3 cameraPosition;
|
||||||
|
float padding;
|
||||||
|
};
|
||||||
|
|
||||||
//////////////
|
//////////////
|
||||||
// TYPEDEFS //
|
// TYPEDEFS //
|
||||||
//////////////
|
//////////////
|
||||||
@ -28,6 +34,7 @@ struct PixelInputType
|
|||||||
float4 position : SV_POSITION;
|
float4 position : SV_POSITION;
|
||||||
float2 tex : TEXCOORD0;
|
float2 tex : TEXCOORD0;
|
||||||
float3 normal : NORMAL;
|
float3 normal : NORMAL;
|
||||||
|
float3 viewDirection : TEXCOORD1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -37,7 +44,8 @@ struct PixelInputType
|
|||||||
PixelInputType LightVertexShader(VertexInputType input)
|
PixelInputType LightVertexShader(VertexInputType input)
|
||||||
{
|
{
|
||||||
PixelInputType output;
|
PixelInputType output;
|
||||||
|
float4 worldPosition;
|
||||||
|
|
||||||
|
|
||||||
// Change the position vector to be 4 units for proper matrix calculations.
|
// Change the position vector to be 4 units for proper matrix calculations.
|
||||||
input.position.w = 1.0f;
|
input.position.w = 1.0f;
|
||||||
@ -56,5 +64,14 @@ PixelInputType LightVertexShader(VertexInputType input)
|
|||||||
// Normalize the normal vector.
|
// Normalize the normal vector.
|
||||||
output.normal = normalize(output.normal);
|
output.normal = normalize(output.normal);
|
||||||
|
|
||||||
|
// Calculate the position of the vertex in the world.
|
||||||
|
worldPosition = mul(input.position, worldMatrix);
|
||||||
|
|
||||||
|
// Determine the viewing direction based on the position of the camera and the position of the vertex in the world.
|
||||||
|
output.viewDirection = cameraPosition.xyz - worldPosition.xyz;
|
||||||
|
|
||||||
|
// Normalize the viewing direction vector.
|
||||||
|
output.viewDirection = normalize(output.viewDirection);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
@ -39,6 +39,19 @@ void LightClass::SetDirection(float x, float y, float z)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LightClass::SetSpecularColor(float red, float green, float blue, float alpha)
|
||||||
|
{
|
||||||
|
m_specularColor = XMFLOAT4(red, green, blue, alpha);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LightClass::SetSpecularPower(float power)
|
||||||
|
{
|
||||||
|
m_specularPower = power;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
XMFLOAT4 LightClass::GetAmbientColor()
|
XMFLOAT4 LightClass::GetAmbientColor()
|
||||||
{
|
{
|
||||||
return m_ambientColor;
|
return m_ambientColor;
|
||||||
@ -50,8 +63,18 @@ XMFLOAT4 LightClass::GetDiffuseColor()
|
|||||||
return m_diffuseColor;
|
return m_diffuseColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
XMFLOAT3 LightClass::GetDirection()
|
XMFLOAT3 LightClass::GetDirection()
|
||||||
{
|
{
|
||||||
return m_direction;
|
return m_direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMFLOAT4 LightClass::GetSpecularColor()
|
||||||
|
{
|
||||||
|
return m_specularColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float LightClass::GetSpecularPower()
|
||||||
|
{
|
||||||
|
return m_specularPower;
|
||||||
}
|
}
|
@ -26,15 +26,21 @@ public:
|
|||||||
void SetAmbientColor(float, float, float, float);
|
void SetAmbientColor(float, float, float, float);
|
||||||
void SetDiffuseColor(float, float, float, float);
|
void SetDiffuseColor(float, float, float, float);
|
||||||
void SetDirection(float, float, float);
|
void SetDirection(float, float, float);
|
||||||
|
void SetSpecularColor(float, float, float, float);
|
||||||
|
void SetSpecularPower(float);
|
||||||
|
|
||||||
XMFLOAT4 GetAmbientColor();
|
XMFLOAT4 GetAmbientColor();
|
||||||
XMFLOAT4 GetDiffuseColor();
|
XMFLOAT4 GetDiffuseColor();
|
||||||
XMFLOAT3 GetDirection();
|
XMFLOAT3 GetDirection();
|
||||||
|
XMFLOAT4 GetSpecularColor();
|
||||||
|
float GetSpecularPower();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
XMFLOAT4 m_ambientColor;
|
XMFLOAT4 m_ambientColor;
|
||||||
XMFLOAT4 m_diffuseColor;
|
XMFLOAT4 m_diffuseColor;
|
||||||
XMFLOAT3 m_direction;
|
XMFLOAT3 m_direction;
|
||||||
|
XMFLOAT4 m_specularColor;
|
||||||
|
float m_specularPower;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -11,6 +11,7 @@ LightShaderClass::LightShaderClass()
|
|||||||
m_layout = 0;
|
m_layout = 0;
|
||||||
m_sampleState = 0;
|
m_sampleState = 0;
|
||||||
m_matrixBuffer = 0;
|
m_matrixBuffer = 0;
|
||||||
|
m_cameraBuffer = 0;
|
||||||
m_lightBuffer = 0;
|
m_lightBuffer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,13 +33,6 @@ bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
|||||||
int error;
|
int error;
|
||||||
bool result;
|
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.
|
// Set the filename of the vertex shader.
|
||||||
error = wcscpy_s(vsFilename, 128, L"light.vs");
|
error = wcscpy_s(vsFilename, 128, L"light.vs");
|
||||||
if (error != 0)
|
if (error != 0)
|
||||||
@ -72,13 +66,15 @@ void LightShaderClass::Shutdown()
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
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;
|
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, lightDirection, ambientColor, diffuseColor);
|
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor,
|
||||||
|
cameraPosition, specularColor, specularPower);
|
||||||
if(!result)
|
if(!result)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -97,12 +93,11 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
|||||||
ID3D10Blob* errorMessage;
|
ID3D10Blob* errorMessage;
|
||||||
ID3D10Blob* vertexShaderBuffer;
|
ID3D10Blob* vertexShaderBuffer;
|
||||||
ID3D10Blob* pixelShaderBuffer;
|
ID3D10Blob* pixelShaderBuffer;
|
||||||
|
|
||||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||||
unsigned int numElements;
|
unsigned int numElements;
|
||||||
D3D11_SAMPLER_DESC samplerDesc;
|
D3D11_SAMPLER_DESC samplerDesc;
|
||||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||||
|
D3D11_BUFFER_DESC cameraBufferDesc;
|
||||||
D3D11_BUFFER_DESC lightBufferDesc;
|
D3D11_BUFFER_DESC lightBufferDesc;
|
||||||
|
|
||||||
|
|
||||||
@ -242,10 +237,25 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
|||||||
return false;
|
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.
|
// 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.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
lightBufferDesc.ByteWidth = sizeof(LightBufferType) + 12 ;
|
||||||
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||||
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
lightBufferDesc.MiscFlags = 0;
|
lightBufferDesc.MiscFlags = 0;
|
||||||
@ -271,6 +281,13 @@ void LightShaderClass::ShutdownShader()
|
|||||||
m_lightBuffer = 0;
|
m_lightBuffer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Release the camera constant buffer.
|
||||||
|
if (m_cameraBuffer)
|
||||||
|
{
|
||||||
|
m_cameraBuffer->Release();
|
||||||
|
m_cameraBuffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Release the matrix constant buffer.
|
// Release the matrix constant buffer.
|
||||||
if (m_matrixBuffer)
|
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,
|
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;
|
HRESULT result;
|
||||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||||
unsigned int bufferNumber;
|
unsigned int bufferNumber;
|
||||||
MatrixBufferType* dataPtr;
|
MatrixBufferType* dataPtr;
|
||||||
LightBufferType* dataPtr2;
|
LightBufferType* dataPtr2;
|
||||||
|
CameraBufferType* dataPtr3;
|
||||||
|
|
||||||
// Transpose the matrices to prepare them for the shader.
|
// Transpose the matrices to prepare them for the shader.
|
||||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
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.
|
// 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);
|
||||||
|
|
||||||
|
// 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.
|
// Set shader texture resource in the pixel shader.
|
||||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||||
|
|
||||||
@ -402,7 +443,9 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
|
|||||||
dataPtr2->ambientColor = ambientColor;
|
dataPtr2->ambientColor = ambientColor;
|
||||||
dataPtr2->diffuseColor = diffuseColor;
|
dataPtr2->diffuseColor = diffuseColor;
|
||||||
dataPtr2->lightDirection = lightDirection;
|
dataPtr2->lightDirection = lightDirection;
|
||||||
dataPtr2->padding = 0.0f;
|
dataPtr2->specularColor = specularColor;
|
||||||
|
dataPtr2->specularPower = specularPower;
|
||||||
|
//dataPtr2->padding = 0.0f;
|
||||||
|
|
||||||
// Unlock the constant buffer.
|
// Unlock the constant buffer.
|
||||||
deviceContext->Unmap(m_lightBuffer, 0);
|
deviceContext->Unmap(m_lightBuffer, 0);
|
||||||
|
@ -29,12 +29,20 @@ private:
|
|||||||
XMMATRIX projection;
|
XMMATRIX projection;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct CameraBufferType
|
||||||
|
{
|
||||||
|
XMFLOAT3 cameraPosition;
|
||||||
|
float padding;
|
||||||
|
};
|
||||||
|
|
||||||
struct LightBufferType
|
struct LightBufferType
|
||||||
{
|
{
|
||||||
XMFLOAT4 ambientColor;
|
XMFLOAT4 ambientColor;
|
||||||
XMFLOAT4 diffuseColor;
|
XMFLOAT4 diffuseColor;
|
||||||
XMFLOAT3 lightDirection;
|
XMFLOAT3 lightDirection;
|
||||||
float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements.
|
float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements.
|
||||||
|
float specularPower;
|
||||||
|
XMFLOAT4 specularColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -44,14 +52,14 @@ public:
|
|||||||
|
|
||||||
bool Initialize(ID3D11Device*, HWND);
|
bool Initialize(ID3D11Device*, HWND);
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4);
|
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||||
|
|
||||||
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*, XMFLOAT3, XMFLOAT4, XMFLOAT4);
|
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||||
void RenderShader(ID3D11DeviceContext*, int);
|
void RenderShader(ID3D11DeviceContext*, int);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -60,6 +68,7 @@ private:
|
|||||||
ID3D11InputLayout* m_layout;
|
ID3D11InputLayout* m_layout;
|
||||||
ID3D11SamplerState* m_sampleState;
|
ID3D11SamplerState* m_sampleState;
|
||||||
ID3D11Buffer* m_matrixBuffer;
|
ID3D11Buffer* m_matrixBuffer;
|
||||||
|
ID3D11Buffer* m_cameraBuffer;
|
||||||
ID3D11Buffer* m_lightBuffer;
|
ID3D11Buffer* m_lightBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the file name of the model.
|
// Set the file name of the model.
|
||||||
strcpy_s(modelFilename, "cube.txt");
|
strcpy_s(modelFilename, "sphere.txt");
|
||||||
|
|
||||||
// Set the file name of the textures.
|
// Set the file name of the textures.
|
||||||
strcpy_s(textureFilename1, "stone01.tga");
|
strcpy_s(textureFilename1, "stone01.tga");
|
||||||
@ -146,7 +146,9 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
|||||||
|
|
||||||
m_Light->SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
|
m_Light->SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
|
||||||
m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
|
m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
m_Light->SetDirection(1.0f, 0.0f, 0.0f);
|
m_Light->SetDirection(1.0f, 0.0f, 1.0f);
|
||||||
|
m_Light->SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
m_Light->SetSpecularPower(32.0f);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -356,7 +358,8 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
|||||||
|
|
||||||
// Render the model using the light shader.
|
// Render the model using the light shader.
|
||||||
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
||||||
m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor());
|
m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor(),
|
||||||
|
m_Camera->GetPosition(), m_Light->GetSpecularColor(), m_Light->GetSpecularPower());
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -373,14 +376,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
|||||||
// 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 light shader.
|
|
||||||
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
|
||||||
m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor());
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Turn the Z buffer back on now that all 2D rendering has completed.
|
// Turn the Z buffer back on now that all 2D rendering has completed.
|
||||||
m_Direct3D->TurnZBufferOn();
|
m_Direct3D->TurnZBufferOn();
|
||||||
|
|
||||||
|
@ -89,6 +89,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Text Include="cube.txt" />
|
<Text Include="cube.txt" />
|
||||||
|
<Text Include="sphere.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<PropertyGroup Label="Globals">
|
<PropertyGroup Label="Globals">
|
||||||
<VCProjectVersion>17.0</VCProjectVersion>
|
<VCProjectVersion>17.0</VCProjectVersion>
|
||||||
|
@ -14,6 +14,8 @@ cbuffer LightBuffer
|
|||||||
float4 diffuseColor;
|
float4 diffuseColor;
|
||||||
float3 lightDirection;
|
float3 lightDirection;
|
||||||
float padding;
|
float padding;
|
||||||
|
float specularPower;
|
||||||
|
float4 specularColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -25,6 +27,7 @@ struct PixelInputType
|
|||||||
float4 position : SV_POSITION;
|
float4 position : SV_POSITION;
|
||||||
float2 tex : TEXCOORD0;
|
float2 tex : TEXCOORD0;
|
||||||
float3 normal : NORMAL;
|
float3 normal : NORMAL;
|
||||||
|
float3 viewDirection : TEXCOORD1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -37,6 +40,8 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
|||||||
float3 lightDir;
|
float3 lightDir;
|
||||||
float lightIntensity;
|
float lightIntensity;
|
||||||
float4 color;
|
float4 color;
|
||||||
|
float3 reflection;
|
||||||
|
float4 specular;
|
||||||
|
|
||||||
|
|
||||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||||
@ -45,6 +50,10 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
|||||||
// Set the default output color to the ambient light value for all pixels.
|
// Set the default output color to the ambient light value for all pixels.
|
||||||
color = ambientColor;
|
color = ambientColor;
|
||||||
|
|
||||||
|
// Initialize the specular color.
|
||||||
|
specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
|
||||||
// Invert the light direction for calculations.
|
// Invert the light direction for calculations.
|
||||||
lightDir = -lightDirection;
|
lightDir = -lightDirection;
|
||||||
|
|
||||||
@ -55,13 +64,22 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
|||||||
{
|
{
|
||||||
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
|
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
|
||||||
color += (diffuseColor * lightIntensity);
|
color += (diffuseColor * lightIntensity);
|
||||||
}
|
|
||||||
|
|
||||||
// Saturate the final light color.
|
// Saturate the ambient and diffuse color.
|
||||||
color = saturate(color);
|
color = saturate(color);
|
||||||
|
|
||||||
|
// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
|
||||||
|
reflection = normalize(2.0f * lightIntensity * input.normal - lightDir);
|
||||||
|
|
||||||
|
// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
|
||||||
|
specular = pow(saturate(dot(reflection, input.viewDirection)), specularPower);
|
||||||
|
}
|
||||||
|
|
||||||
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
|
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
|
||||||
color = color * textureColor;
|
color = color * textureColor;
|
||||||
|
|
||||||
|
// Add the specular component last to the output color.
|
||||||
|
color = saturate(color + specular);
|
||||||
|
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
@ -13,6 +13,12 @@ cbuffer MatrixBuffer
|
|||||||
matrix projectionMatrix;
|
matrix projectionMatrix;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
cbuffer CameraBuffer
|
||||||
|
{
|
||||||
|
float3 cameraPosition;
|
||||||
|
float padding;
|
||||||
|
};
|
||||||
|
|
||||||
//////////////
|
//////////////
|
||||||
// TYPEDEFS //
|
// TYPEDEFS //
|
||||||
//////////////
|
//////////////
|
||||||
@ -28,6 +34,7 @@ struct PixelInputType
|
|||||||
float4 position : SV_POSITION;
|
float4 position : SV_POSITION;
|
||||||
float2 tex : TEXCOORD0;
|
float2 tex : TEXCOORD0;
|
||||||
float3 normal : NORMAL;
|
float3 normal : NORMAL;
|
||||||
|
float3 viewDirection : TEXCOORD1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -37,7 +44,8 @@ struct PixelInputType
|
|||||||
PixelInputType LightVertexShader(VertexInputType input)
|
PixelInputType LightVertexShader(VertexInputType input)
|
||||||
{
|
{
|
||||||
PixelInputType output;
|
PixelInputType output;
|
||||||
|
float4 worldPosition;
|
||||||
|
|
||||||
|
|
||||||
// Change the position vector to be 4 units for proper matrix calculations.
|
// Change the position vector to be 4 units for proper matrix calculations.
|
||||||
input.position.w = 1.0f;
|
input.position.w = 1.0f;
|
||||||
@ -56,5 +64,14 @@ PixelInputType LightVertexShader(VertexInputType input)
|
|||||||
// Normalize the normal vector.
|
// Normalize the normal vector.
|
||||||
output.normal = normalize(output.normal);
|
output.normal = normalize(output.normal);
|
||||||
|
|
||||||
|
// Calculate the position of the vertex in the world.
|
||||||
|
worldPosition = mul(input.position, worldMatrix);
|
||||||
|
|
||||||
|
// Determine the viewing direction based on the position of the camera and the position of the vertex in the world.
|
||||||
|
output.viewDirection = cameraPosition.xyz - worldPosition.xyz;
|
||||||
|
|
||||||
|
// Normalize the viewing direction vector.
|
||||||
|
output.viewDirection = normalize(output.viewDirection);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
@ -39,6 +39,19 @@ void LightClass::SetDirection(float x, float y, float z)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LightClass::SetSpecularColor(float red, float green, float blue, float alpha)
|
||||||
|
{
|
||||||
|
m_specularColor = XMFLOAT4(red, green, blue, alpha);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LightClass::SetSpecularPower(float power)
|
||||||
|
{
|
||||||
|
m_specularPower = power;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
XMFLOAT4 LightClass::GetAmbientColor()
|
XMFLOAT4 LightClass::GetAmbientColor()
|
||||||
{
|
{
|
||||||
return m_ambientColor;
|
return m_ambientColor;
|
||||||
@ -50,8 +63,18 @@ XMFLOAT4 LightClass::GetDiffuseColor()
|
|||||||
return m_diffuseColor;
|
return m_diffuseColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
XMFLOAT3 LightClass::GetDirection()
|
XMFLOAT3 LightClass::GetDirection()
|
||||||
{
|
{
|
||||||
return m_direction;
|
return m_direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
XMFLOAT4 LightClass::GetSpecularColor()
|
||||||
|
{
|
||||||
|
return m_specularColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float LightClass::GetSpecularPower()
|
||||||
|
{
|
||||||
|
return m_specularPower;
|
||||||
}
|
}
|
@ -26,15 +26,21 @@ public:
|
|||||||
void SetAmbientColor(float, float, float, float);
|
void SetAmbientColor(float, float, float, float);
|
||||||
void SetDiffuseColor(float, float, float, float);
|
void SetDiffuseColor(float, float, float, float);
|
||||||
void SetDirection(float, float, float);
|
void SetDirection(float, float, float);
|
||||||
|
void SetSpecularColor(float, float, float, float);
|
||||||
|
void SetSpecularPower(float);
|
||||||
|
|
||||||
XMFLOAT4 GetAmbientColor();
|
XMFLOAT4 GetAmbientColor();
|
||||||
XMFLOAT4 GetDiffuseColor();
|
XMFLOAT4 GetDiffuseColor();
|
||||||
XMFLOAT3 GetDirection();
|
XMFLOAT3 GetDirection();
|
||||||
|
XMFLOAT4 GetSpecularColor();
|
||||||
|
float GetSpecularPower();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
XMFLOAT4 m_ambientColor;
|
XMFLOAT4 m_ambientColor;
|
||||||
XMFLOAT4 m_diffuseColor;
|
XMFLOAT4 m_diffuseColor;
|
||||||
XMFLOAT3 m_direction;
|
XMFLOAT3 m_direction;
|
||||||
|
XMFLOAT4 m_specularColor;
|
||||||
|
float m_specularPower;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -11,6 +11,7 @@ LightShaderClass::LightShaderClass()
|
|||||||
m_layout = 0;
|
m_layout = 0;
|
||||||
m_sampleState = 0;
|
m_sampleState = 0;
|
||||||
m_matrixBuffer = 0;
|
m_matrixBuffer = 0;
|
||||||
|
m_cameraBuffer = 0;
|
||||||
m_lightBuffer = 0;
|
m_lightBuffer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,13 +33,6 @@ bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
|||||||
int error;
|
int error;
|
||||||
bool result;
|
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.
|
// Set the filename of the vertex shader.
|
||||||
error = wcscpy_s(vsFilename, 128, L"light.vs");
|
error = wcscpy_s(vsFilename, 128, L"light.vs");
|
||||||
if (error != 0)
|
if (error != 0)
|
||||||
@ -72,13 +66,15 @@ void LightShaderClass::Shutdown()
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
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;
|
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, lightDirection, ambientColor, diffuseColor);
|
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor,
|
||||||
|
cameraPosition, specularColor, specularPower);
|
||||||
if(!result)
|
if(!result)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -97,12 +93,11 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
|||||||
ID3D10Blob* errorMessage;
|
ID3D10Blob* errorMessage;
|
||||||
ID3D10Blob* vertexShaderBuffer;
|
ID3D10Blob* vertexShaderBuffer;
|
||||||
ID3D10Blob* pixelShaderBuffer;
|
ID3D10Blob* pixelShaderBuffer;
|
||||||
|
|
||||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||||
unsigned int numElements;
|
unsigned int numElements;
|
||||||
D3D11_SAMPLER_DESC samplerDesc;
|
D3D11_SAMPLER_DESC samplerDesc;
|
||||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||||
|
D3D11_BUFFER_DESC cameraBufferDesc;
|
||||||
D3D11_BUFFER_DESC lightBufferDesc;
|
D3D11_BUFFER_DESC lightBufferDesc;
|
||||||
|
|
||||||
|
|
||||||
@ -242,10 +237,25 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
|
|||||||
return false;
|
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.
|
// 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.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
lightBufferDesc.ByteWidth = sizeof(LightBufferType) + 12 ;
|
||||||
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||||
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
lightBufferDesc.MiscFlags = 0;
|
lightBufferDesc.MiscFlags = 0;
|
||||||
@ -271,6 +281,13 @@ void LightShaderClass::ShutdownShader()
|
|||||||
m_lightBuffer = 0;
|
m_lightBuffer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Release the camera constant buffer.
|
||||||
|
if (m_cameraBuffer)
|
||||||
|
{
|
||||||
|
m_cameraBuffer->Release();
|
||||||
|
m_cameraBuffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Release the matrix constant buffer.
|
// Release the matrix constant buffer.
|
||||||
if (m_matrixBuffer)
|
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,
|
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;
|
HRESULT result;
|
||||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||||
unsigned int bufferNumber;
|
unsigned int bufferNumber;
|
||||||
MatrixBufferType* dataPtr;
|
MatrixBufferType* dataPtr;
|
||||||
LightBufferType* dataPtr2;
|
LightBufferType* dataPtr2;
|
||||||
|
CameraBufferType* dataPtr3;
|
||||||
|
|
||||||
// Transpose the matrices to prepare them for the shader.
|
// Transpose the matrices to prepare them for the shader.
|
||||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
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.
|
// 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);
|
||||||
|
|
||||||
|
// 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.
|
// Set shader texture resource in the pixel shader.
|
||||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||||
|
|
||||||
@ -402,7 +443,9 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
|
|||||||
dataPtr2->ambientColor = ambientColor;
|
dataPtr2->ambientColor = ambientColor;
|
||||||
dataPtr2->diffuseColor = diffuseColor;
|
dataPtr2->diffuseColor = diffuseColor;
|
||||||
dataPtr2->lightDirection = lightDirection;
|
dataPtr2->lightDirection = lightDirection;
|
||||||
dataPtr2->padding = 0.0f;
|
dataPtr2->specularColor = specularColor;
|
||||||
|
dataPtr2->specularPower = specularPower;
|
||||||
|
//dataPtr2->padding = 0.0f;
|
||||||
|
|
||||||
// Unlock the constant buffer.
|
// Unlock the constant buffer.
|
||||||
deviceContext->Unmap(m_lightBuffer, 0);
|
deviceContext->Unmap(m_lightBuffer, 0);
|
||||||
|
@ -29,12 +29,20 @@ private:
|
|||||||
XMMATRIX projection;
|
XMMATRIX projection;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct CameraBufferType
|
||||||
|
{
|
||||||
|
XMFLOAT3 cameraPosition;
|
||||||
|
float padding;
|
||||||
|
};
|
||||||
|
|
||||||
struct LightBufferType
|
struct LightBufferType
|
||||||
{
|
{
|
||||||
XMFLOAT4 ambientColor;
|
XMFLOAT4 ambientColor;
|
||||||
XMFLOAT4 diffuseColor;
|
XMFLOAT4 diffuseColor;
|
||||||
XMFLOAT3 lightDirection;
|
XMFLOAT3 lightDirection;
|
||||||
float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements.
|
float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements.
|
||||||
|
float specularPower;
|
||||||
|
XMFLOAT4 specularColor;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -44,14 +52,14 @@ public:
|
|||||||
|
|
||||||
bool Initialize(ID3D11Device*, HWND);
|
bool Initialize(ID3D11Device*, HWND);
|
||||||
void Shutdown();
|
void Shutdown();
|
||||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4);
|
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||||
|
|
||||||
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*, XMFLOAT3, XMFLOAT4, XMFLOAT4);
|
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||||
void RenderShader(ID3D11DeviceContext*, int);
|
void RenderShader(ID3D11DeviceContext*, int);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -60,6 +68,7 @@ private:
|
|||||||
ID3D11InputLayout* m_layout;
|
ID3D11InputLayout* m_layout;
|
||||||
ID3D11SamplerState* m_sampleState;
|
ID3D11SamplerState* m_sampleState;
|
||||||
ID3D11Buffer* m_matrixBuffer;
|
ID3D11Buffer* m_matrixBuffer;
|
||||||
|
ID3D11Buffer* m_cameraBuffer;
|
||||||
ID3D11Buffer* m_lightBuffer;
|
ID3D11Buffer* m_lightBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
14704
enginecustom/sphere.txt
Normal file
14704
enginecustom/sphere.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user