merge 3d diffuse lighting

This commit is contained in:
StratiX0 2024-03-26 12:02:03 +01:00
parent deea6fa3ba
commit ccb666faaf
7 changed files with 1674 additions and 1679 deletions

View File

@ -8,7 +8,6 @@
/////////////
Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0);
cbuffer LightBuffer
{
float4 diffuseColor;
@ -48,9 +47,6 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
// 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);

View File

@ -13,7 +13,6 @@ cbuffer MatrixBuffer
matrix projectionMatrix;
};
//////////////
// TYPEDEFS //
//////////////

View File

@ -1,3 +1,4 @@
#pragma once
////////////////////////////////////////////////////////////////////////////////
// Filename: lightclass.h
////////////////////////////////////////////////////////////////////////////////
@ -22,11 +23,11 @@ public:
LightClass(const LightClass&);
~LightClass();
void SetDiffuseColor(float, float, float, float);
void SetDirection(float, float, float);
void SetDiffuseColor(float, float, float, float);
XMFLOAT4 GetDiffuseColor();
XMFLOAT3 GetDirection();
XMFLOAT4 GetDiffuseColor();
private:
XMFLOAT4 m_diffuseColor;

View File

@ -32,16 +32,15 @@ bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
int error;
bool result;
// Set the filename of the vertex shader.
error = wcscpy_s(vsFilename, 128, L"../enginecustom/light.vs");
error = wcscpy_s(vsFilename, 128, L"./Light.vs");
if (error != 0)
{
return false;
}
// Set the filename of the pixel shader.
error = wcscpy_s(psFilename, 128, L"../enginecustom/light.ps");
error = wcscpy_s(psFilename, 128, L"./Light.ps");
if (error != 0)
{
return false;
@ -49,7 +48,7 @@ bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
// Initialize the vertex and pixel shaders.
result = InitializeShader(device, hwnd, vsFilename, psFilename);
if(!result)
if (!result)
{
return false;
}
@ -66,7 +65,6 @@ void LightShaderClass::Shutdown()
return;
}
bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
{
@ -75,7 +73,7 @@ bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount
// Set the shader parameters that it will use for rendering.
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor);
if(!result)
if (!result)
{
return false;
}
@ -93,10 +91,12 @@ 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 lightBufferDesc;
@ -107,10 +107,10 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
// Compile the vertex shader code.
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(errorMessage)
if (errorMessage)
{
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
}
@ -125,10 +125,10 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
// Compile the pixel shader code.
result = D3DCompileFromFile(psFilename, NULL, NULL, "LightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
if(FAILED(result))
if (FAILED(result))
{
// If the shader failed to compile it should have writen something to the error message.
if(errorMessage)
if (errorMessage)
{
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
}
@ -143,14 +143,14 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
// Create the vertex shader from the buffer.
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
if(FAILED(result))
if (FAILED(result))
{
return false;
}
// Create the pixel shader from the buffer.
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
if(FAILED(result))
if (FAILED(result))
{
return false;
}
@ -187,7 +187,7 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
// Create the vertex input layout.
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(),
&m_layout);
if(FAILED(result))
if (FAILED(result))
{
return false;
}
@ -216,7 +216,7 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
// Create the texture sampler state.
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
if(FAILED(result))
if (FAILED(result))
{
return false;
}
@ -231,7 +231,7 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
// 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);
if(FAILED(result))
if (FAILED(result))
{
return false;
}
@ -247,7 +247,7 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
// 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))
if (FAILED(result))
{
return false;
}
@ -259,42 +259,42 @@ bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR*
void LightShaderClass::ShutdownShader()
{
// Release the light constant buffer.
if(m_lightBuffer)
if (m_lightBuffer)
{
m_lightBuffer->Release();
m_lightBuffer = 0;
}
// Release the matrix constant buffer.
if(m_matrixBuffer)
if (m_matrixBuffer)
{
m_matrixBuffer->Release();
m_matrixBuffer = 0;
}
// Release the sampler state.
if(m_sampleState)
if (m_sampleState)
{
m_sampleState->Release();
m_sampleState = 0;
}
// Release the layout.
if(m_layout)
if (m_layout)
{
m_layout->Release();
m_layout = 0;
}
// Release the pixel shader.
if(m_pixelShader)
if (m_pixelShader)
{
m_pixelShader->Release();
m_pixelShader = 0;
}
// Release the vertex shader.
if(m_vertexShader)
if (m_vertexShader)
{
m_vertexShader->Release();
m_vertexShader = 0;
@ -321,7 +321,7 @@ void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND h
fout.open("shader-error.txt");
// Write out the error message.
for(i=0; i<bufferSize; i++)
for (i = 0; i < bufferSize; i++)
{
fout << compileErrors[i];
}
@ -339,7 +339,6 @@ void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND h
return;
}
bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
{
@ -357,7 +356,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;
}
@ -384,7 +383,7 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
// 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))
if (FAILED(result))
{
return false;
}

File diff suppressed because it is too large Load Diff