From af2231210c2e8ecc29a7414cf7364f1d94b383cf Mon Sep 17 00:00:00 2001 From: Harpie94 Date: Fri, 22 Mar 2024 18:00:55 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20Tuto=2010?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- enginecustom/Light.ps | 79 +++++++++++++++++++++++ enginecustom/Light.vs | 73 +++++++++++++++++++++ enginecustom/enginecustom.vcxproj | 1 + enginecustom/enginecustom.vcxproj.filters | 3 + 4 files changed, 156 insertions(+) create mode 100644 enginecustom/Light.ps create mode 100644 enginecustom/Light.vs diff --git a/enginecustom/Light.ps b/enginecustom/Light.ps new file mode 100644 index 0000000..b8add43 --- /dev/null +++ b/enginecustom/Light.ps @@ -0,0 +1,79 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: light.ps +//////////////////////////////////////////////////////////////////////////////// + + +///////////// +// GLOBALS // +///////////// +Texture2D shaderTexture : register(t0); +SamplerState SampleType : register(s0); +cbuffer LightBuffer +{ + float4 ambientColor; + float4 diffuseColor; + float3 lightDirection; + float specularPower; + float4 specularColor; +}; +////////////// +// TYPEDEFS // +////////////// +struct PixelInputType +{ + float4 position : SV_POSITION; + float2 tex : TEXCOORD0; + float3 normal : NORMAL; + float3 viewDirection : TEXCOORD1; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Pixel Shader +//////////////////////////////////////////////////////////////////////////////// +float4 LightPixelShader(PixelInputType input) : SV_TARGET +{ + float4 textureColor; + float3 lightDir; + float lightIntensity; + float4 color; + float3 reflection; + float4 specular; + + + // Sample the pixel color from the texture using the sampler at this texture coordinate location. + textureColor = shaderTexture.Sample(SampleType, input.tex); + + // Set the default output color to the ambient light value for all pixels. + color = ambientColor; + + // Initialize the specular color. + specular = float4(0.0f, 0.0f, 0.0f, 0.0f); + + // Invert the light direction for calculations. + lightDir = -lightDirection; + + // Calculate the amount of light on this pixel. + lightIntensity = saturate(dot(input.normal, lightDir)); + + if(lightIntensity > 0.0f) + { + // Determine the final diffuse color based on the diffuse color and the amount of light intensity. + color += (diffuseColor * lightIntensity); + + // Saturate the ambient and diffuse 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. + color = color * textureColor; + // Add the specular component last to the output color. + color = saturate(color + specular); + + return color; +} diff --git a/enginecustom/Light.vs b/enginecustom/Light.vs new file mode 100644 index 0000000..15843cb --- /dev/null +++ b/enginecustom/Light.vs @@ -0,0 +1,73 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: light.vs +//////////////////////////////////////////////////////////////////////////////// + + +///////////// +// GLOBALS // +///////////// +cbuffer MatrixBuffer +{ + matrix worldMatrix; + matrix viewMatrix; + matrix projectionMatrix; +}; +cbuffer CameraBuffer +{ + float3 cameraPosition; + float padding; +}; +////////////// +// TYPEDEFS // +////////////// +struct VertexInputType +{ + float4 position : POSITION; + float2 tex : TEXCOORD0; + float3 normal : NORMAL; +}; +struct PixelInputType +{ + float4 position : SV_POSITION; + float2 tex : TEXCOORD0; + float3 normal : NORMAL; + float3 viewDirection : TEXCOORD1; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Vertex Shader +//////////////////////////////////////////////////////////////////////////////// +PixelInputType LightVertexShader(VertexInputType input) +{ + PixelInputType output; + float4 worldPosition; + + + // Change the position vector to be 4 units for proper matrix calculations. + input.position.w = 1.0f; + + // Calculate the position of the vertex against the world, view, and projection matrices. + output.position = mul(input.position, worldMatrix); + output.position = mul(output.position, viewMatrix); + output.position = mul(output.position, projectionMatrix); + + // Store the texture coordinates for the pixel shader. + output.tex = input.tex; + + // Calculate the normal vector against the world matrix only. + output.normal = mul(input.normal, (float3x3)worldMatrix); + + // Normalize the normal vector. + 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; +} \ No newline at end of file diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj index ba1ba40..a1722bf 100644 --- a/enginecustom/enginecustom.vcxproj +++ b/enginecustom/enginecustom.vcxproj @@ -43,6 +43,7 @@ + diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index cc96857..cf581b5 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -98,6 +98,9 @@ texture + + shader +