merge avec Sprite

en espérant que ça a rien kapout pt kc
This commit is contained in:
StratiX0 2024-03-27 12:01:50 +01:00
parent a39859b251
commit f736a6bb54
6 changed files with 75 additions and 41 deletions

View File

@ -10,6 +10,7 @@ Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0); SamplerState SampleType : register(s0);
cbuffer LightBuffer cbuffer LightBuffer
{ {
float4 ambientColor;
float4 diffuseColor; float4 diffuseColor;
float3 lightDirection; float3 lightDirection;
float padding; float padding;
@ -41,14 +42,23 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
// 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.
textureColor = shaderTexture.Sample(SampleType, input.tex); textureColor = shaderTexture.Sample(SampleType, input.tex);
// Invert the light direction for calculations. // Set the default output color to the ambient light value for all pixels.
color = ambientColor;
// Invert the light direction for calculations.
lightDir = -lightDirection; lightDir = -lightDirection;
// Calculate the amount of light on this pixel. // Calculate the amount of light on this pixel.
lightIntensity = saturate(dot(input.normal, lightDir)); lightIntensity = saturate(dot(input.normal, lightDir));
// Determine the final amount of diffuse color based on the diffuse color combined with the light intensity. if(lightIntensity > 0.0f)
color = saturate(diffuseColor * lightIntensity); {
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
color += (diffuseColor * lightIntensity);
}
// Saturate the final light color.
color = saturate(color);
// 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;

View File

@ -49,7 +49,7 @@ PixelInputType LightVertexShader(VertexInputType input)
// Store the texture coordinates for the pixel shader. // Store the texture coordinates for the pixel shader.
output.tex = input.tex; output.tex = input.tex;
// Calculate the normal vector against the world matrix only. // Calculate the normal vector against the world matrix only.
output.normal = mul(input.normal, (float3x3)worldMatrix); output.normal = mul(input.normal, (float3x3)worldMatrix);

View File

@ -18,6 +18,13 @@ 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) void LightClass::SetDiffuseColor(float red, float green, float blue, float alpha)
{ {
@ -32,6 +39,11 @@ void LightClass::SetDirection(float x, float y, float z)
return; return;
} }
XMFLOAT4 LightClass::GetAmbientColor()
{
return m_ambientColor;
}
XMFLOAT4 LightClass::GetDiffuseColor() XMFLOAT4 LightClass::GetDiffuseColor()
{ {

View File

@ -23,15 +23,18 @@ public:
LightClass(const LightClass&); LightClass(const LightClass&);
~LightClass(); ~LightClass();
void SetDirection(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);
XMFLOAT3 GetDirection();
XMFLOAT4 GetDiffuseColor(); XMFLOAT4 GetAmbientColor();
XMFLOAT4 GetDiffuseColor();
XMFLOAT3 GetDirection();
private: private:
XMFLOAT4 m_diffuseColor; XMFLOAT4 m_ambientColor;
XMFLOAT3 m_direction; XMFLOAT4 m_diffuseColor;
XMFLOAT3 m_direction;
}; };
#endif #endif

View File

@ -39,13 +39,19 @@ bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
return false; return false;
} }
// Set the filename of the pixel shader. // Set the filename of the vertex shader.
error = wcscpy_s(psFilename, 128, L"./Light.ps"); error = wcscpy_s(vsFilename, 128, L"light.vs");
if (error != 0) if (error != 0)
{ {
return false; return false;
} }
// Set the filename of the pixel shader.
error = wcscpy_s(psFilename, 128, L"light.ps");
if (error != 0)
{
return false;
}
// 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)
@ -66,17 +72,17 @@ 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 diffuseColor) ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, 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, lightDirection, diffuseColor); result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor);
if (!result) if(!result)
{ {
return false; return false;
} }
// Now render the prepared buffers with the shader. // Now render the prepared buffers with the shader.
RenderShader(deviceContext, indexCount); RenderShader(deviceContext, indexCount);
@ -339,8 +345,9 @@ void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND h
return; return;
} }
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 diffuseColor) ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor)
{ {
HRESULT result; HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource; D3D11_MAPPED_SUBRESOURCE mappedResource;
@ -391,10 +398,11 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
// Get a pointer to the data in the constant buffer. // Get a pointer to the data in the constant buffer.
dataPtr2 = (LightBufferType*)mappedResource.pData; dataPtr2 = (LightBufferType*)mappedResource.pData;
// Copy the lighting variables into the constant buffer. // Copy the lighting variables into the constant buffer.
dataPtr2->diffuseColor = diffuseColor; dataPtr2->ambientColor = ambientColor;
dataPtr2->lightDirection = lightDirection; dataPtr2->diffuseColor = diffuseColor;
dataPtr2->padding = 0.0f; dataPtr2->lightDirection = lightDirection;
dataPtr2->padding = 0.0f;
// Unlock the constant buffer. // Unlock the constant buffer.
deviceContext->Unmap(m_lightBuffer, 0); deviceContext->Unmap(m_lightBuffer, 0);

View File

@ -29,29 +29,30 @@ private:
XMMATRIX projection; XMMATRIX projection;
}; };
struct LightBufferType struct LightBufferType
{ {
XMFLOAT4 diffuseColor; XMFLOAT4 ambientColor;
XMFLOAT3 lightDirection; XMFLOAT4 diffuseColor;
float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements. XMFLOAT3 lightDirection;
}; float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements.
};
public: public:
LightShaderClass(); LightShaderClass();
LightShaderClass(const LightShaderClass&); LightShaderClass(const LightShaderClass&);
~LightShaderClass(); ~LightShaderClass();
bool Initialize(ID3D11Device*, HWND); bool Initialize(ID3D11Device*, HWND);
void Shutdown(); void Shutdown();
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4); bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, 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*, XMFLOAT3, XMFLOAT4); bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4);
void RenderShader(ID3D11DeviceContext*, int); void RenderShader(ID3D11DeviceContext*, int);
private: private:
ID3D11VertexShader* m_vertexShader; ID3D11VertexShader* m_vertexShader;