Major update - Architecture Rework
This commit is contained in:
22
enginecustom/src/hlsl/Color.ps
Normal file
22
enginecustom/src/hlsl/Color.ps
Normal file
@@ -0,0 +1,22 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: color.ps
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float4 color : COLOR;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 ColorPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
return input.color;
|
||||
}
|
||||
50
enginecustom/src/hlsl/Color.vs
Normal file
50
enginecustom/src/hlsl/Color.vs
Normal file
@@ -0,0 +1,50 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: color.vs
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float4 color : COLOR;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float4 color : COLOR;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType ColorVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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 input color for the pixel shader to use.
|
||||
output.color = input.color;
|
||||
|
||||
return output;
|
||||
}
|
||||
45
enginecustom/src/hlsl/Multitexture.ps
Normal file
45
enginecustom/src/hlsl/Multitexture.ps
Normal file
@@ -0,0 +1,45 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: multitexture.ps
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
|
||||
Texture2D shaderTexture1 : register(t0);
|
||||
Texture2D shaderTexture2 : register(t1);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 MultiTexturePixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 color1;
|
||||
float4 color2;
|
||||
float4 blendColor;
|
||||
|
||||
// Sample the pixel color from the textures using the sampler at this texture coordinate location.
|
||||
color1 = shaderTexture1.Sample(SampleType, input.tex);
|
||||
color2 = shaderTexture2.Sample(SampleType, input.tex);
|
||||
|
||||
// Combine the two textures together.
|
||||
blendColor = color1 * color2 * 2.0;
|
||||
|
||||
// Saturate the final color.
|
||||
blendColor = saturate(blendColor);
|
||||
|
||||
return blendColor;
|
||||
}
|
||||
54
enginecustom/src/hlsl/Multitexture.vs
Normal file
54
enginecustom/src/hlsl/Multitexture.vs
Normal file
@@ -0,0 +1,54 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: multitexture.vs
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType MultiTextureVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
return output;
|
||||
}
|
||||
45
enginecustom/src/hlsl/alphamap.ps
Normal file
45
enginecustom/src/hlsl/alphamap.ps
Normal file
@@ -0,0 +1,45 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture1 : register(t0);
|
||||
Texture2D shaderTexture2 : register(t1);
|
||||
Texture2D shaderTexture3 : register(t2);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 AlphaMapPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 color1;
|
||||
float4 color2;
|
||||
float4 alphaValue;
|
||||
float4 blendColor;
|
||||
|
||||
|
||||
// Get the pixel color from the first texture.
|
||||
color1 = shaderTexture1.Sample(SampleType, input.tex);
|
||||
|
||||
// Get the pixel color from the second texture.
|
||||
color2 = shaderTexture2.Sample(SampleType, input.tex);
|
||||
|
||||
// Get the pixel color from the alpha texture.
|
||||
alphaValue = shaderTexture3.Sample(SampleType, input.tex);
|
||||
|
||||
// Combine the two textures based on the alpha value.
|
||||
blendColor = (alphaValue * color1) + ((1.0 - alphaValue) * color2);
|
||||
|
||||
// Saturate the final color value.
|
||||
blendColor = saturate(blendColor);
|
||||
|
||||
return blendColor;
|
||||
}
|
||||
49
enginecustom/src/hlsl/alphamap.vs
Normal file
49
enginecustom/src/hlsl/alphamap.vs
Normal file
@@ -0,0 +1,49 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType AlphaMapVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
return output;
|
||||
}
|
||||
34
enginecustom/src/hlsl/celshading.ps
Normal file
34
enginecustom/src/hlsl/celshading.ps
Normal file
@@ -0,0 +1,34 @@
|
||||
cbuffer LightBuffer
|
||||
{
|
||||
float4 diffuseColor;
|
||||
float3 lightDirection;
|
||||
float padding; // Padding to ensure the structure is a multiple of 16 bytes.
|
||||
float3 lightPosition; // Add light position
|
||||
float padding2; // Padding to ensure the structure is a multiple of 16 bytes.
|
||||
float constantAttenuation;
|
||||
float linearAttenuation;
|
||||
float quadraticAttenuation;
|
||||
float padding3; // Padding to ensure the structure is a multiple of 16 bytes.
|
||||
};
|
||||
|
||||
Texture2D shaderTexture;
|
||||
SamplerState SampleType;
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 worldPos : TEXCOORD1; // Add world position
|
||||
};
|
||||
|
||||
float4 CelShadingPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
// Normalize the normal
|
||||
float3 normal = normalize(input.normal);
|
||||
|
||||
// Convert the normal to a color
|
||||
float4 color = float4((normal + 1.0f) * 0.5f, 1.0f);
|
||||
|
||||
return color;
|
||||
}
|
||||
45
enginecustom/src/hlsl/celshading.vs
Normal file
45
enginecustom/src/hlsl/celshading.vs
Normal file
@@ -0,0 +1,45 @@
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float3 normal : NORMAL;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 worldPos : TEXCOORD1; // Add world position
|
||||
};
|
||||
|
||||
PixelInputType CelShadingVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
// 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.
|
||||
float4 worldPosition = mul(input.position, worldMatrix);
|
||||
output.position = mul(worldPosition, viewMatrix);
|
||||
output.position = mul(output.position, projectionMatrix);
|
||||
|
||||
// Pass the normal to the pixel shader
|
||||
output.normal = mul((float3x3)worldMatrix, input.normal);
|
||||
|
||||
// Pass the world position to the pixel shader
|
||||
output.worldPos = worldPosition.xyz;
|
||||
|
||||
// Store the texture coordinates for the pixel shader.
|
||||
output.tex = input.tex;
|
||||
|
||||
return output;
|
||||
}
|
||||
47
enginecustom/src/hlsl/font.ps
Normal file
47
enginecustom/src/hlsl/font.ps
Normal file
@@ -0,0 +1,47 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture : register(t0);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
cbuffer PixelBuffer
|
||||
{
|
||||
float4 pixelColor;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 FontPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 color;
|
||||
|
||||
|
||||
// Sample the texture pixel at this location.
|
||||
color = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
// If the color is black on the texture then treat this pixel as transparent.
|
||||
if(color.r == 0.0f)
|
||||
{
|
||||
color.a = 0.0f;
|
||||
}
|
||||
|
||||
// If the color is other than black on the texture then this is a pixel in the font so draw it using the font pixel color.
|
||||
else
|
||||
{
|
||||
color.a = 1.0f;
|
||||
color = color * pixelColor;
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
48
enginecustom/src/hlsl/font.vs
Normal file
48
enginecustom/src/hlsl/font.vs
Normal file
@@ -0,0 +1,48 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType FontVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
return output;
|
||||
}
|
||||
84
enginecustom/src/hlsl/light.ps
Normal file
84
enginecustom/src/hlsl/light.ps
Normal file
@@ -0,0 +1,84 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: light.ps
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/////////////
|
||||
// DEFINES //
|
||||
/////////////
|
||||
#define NUM_LIGHTS 4
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture : register(t0);
|
||||
SamplerState SampleType : register(s0);
|
||||
cbuffer LightBuffer
|
||||
{
|
||||
float4 ambientColor;
|
||||
float3 lightDirection;
|
||||
float padding;
|
||||
float specularPower;
|
||||
float4 specularColor;
|
||||
};
|
||||
|
||||
cbuffer LightColorBuffer
|
||||
{
|
||||
float4 diffuseColor[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 lightPos[NUM_LIGHTS] : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
float3 lightDir;
|
||||
float4 color;
|
||||
float3 reflection;
|
||||
float4 specular;
|
||||
float lightIntensity[NUM_LIGHTS];
|
||||
float4 colorArray[NUM_LIGHTS];
|
||||
float4 colorSum;
|
||||
int i;
|
||||
|
||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
for(i=0; i<NUM_LIGHTS; i++)
|
||||
{
|
||||
// Calculate the different amounts of light on this pixel based on the positions of the lights.
|
||||
lightIntensity[i] = saturate(dot(input.normal, input.lightPos[i]));
|
||||
|
||||
// Determine the diffuse color amount of each of the four lights.
|
||||
colorArray[i] = diffuseColor[i] * lightIntensity[i];
|
||||
}
|
||||
|
||||
// Initialize the sum of colors.
|
||||
colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
// Add all of the light colors up.
|
||||
for(i=0; i<NUM_LIGHTS; i++)
|
||||
{
|
||||
colorSum.r += colorArray[i].r;
|
||||
colorSum.g += colorArray[i].g;
|
||||
colorSum.b += colorArray[i].b;
|
||||
}
|
||||
|
||||
// Multiply the texture pixel by the combination of all four light colors to get the final result.
|
||||
color = saturate(colorSum) * textureColor;
|
||||
|
||||
return color;
|
||||
}
|
||||
85
enginecustom/src/hlsl/light.vs
Normal file
85
enginecustom/src/hlsl/light.vs
Normal file
@@ -0,0 +1,85 @@
|
||||
/////////////
|
||||
// DEFINES //
|
||||
/////////////
|
||||
#define NUM_LIGHTS 4
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
cbuffer CameraBuffer
|
||||
{
|
||||
float3 cameraPosition;
|
||||
float padding;
|
||||
};
|
||||
|
||||
cbuffer LightPositionBuffer
|
||||
{
|
||||
float4 lightPosition[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
//////////////
|
||||
// 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 lightPos[NUM_LIGHTS] : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType LightVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
float4 worldPosition;
|
||||
int i;
|
||||
|
||||
// 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);
|
||||
|
||||
for(i=0; i<NUM_LIGHTS; i++)
|
||||
{
|
||||
// Determine the light positions based on the position of the lights and the position of the vertex in the world.
|
||||
output.lightPos[i] = lightPosition[i].xyz - worldPosition.xyz;
|
||||
|
||||
// Normalize the light position vectors.
|
||||
output.lightPos[i] = normalize(output.lightPos[i]);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
38
enginecustom/src/hlsl/lightmap.ps
Normal file
38
enginecustom/src/hlsl/lightmap.ps
Normal file
@@ -0,0 +1,38 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture1 : register(t0);
|
||||
Texture2D shaderTexture2 : register(t1);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 LightMapPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 color;
|
||||
float4 lightColor;
|
||||
float4 finalColor;
|
||||
|
||||
|
||||
// Get the pixel color from the color texture.
|
||||
color = shaderTexture1.Sample(SampleType, input.tex);
|
||||
|
||||
// Get the pixel color from the light map.
|
||||
lightColor = shaderTexture2.Sample(SampleType, input.tex);
|
||||
|
||||
// Blend the two pixels together.
|
||||
finalColor = color * lightColor;
|
||||
|
||||
return finalColor;
|
||||
}
|
||||
49
enginecustom/src/hlsl/lightmap.vs
Normal file
49
enginecustom/src/hlsl/lightmap.vs
Normal file
@@ -0,0 +1,49 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType LightMapVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
return output;
|
||||
}
|
||||
71
enginecustom/src/hlsl/normalmap.ps
Normal file
71
enginecustom/src/hlsl/normalmap.ps
Normal file
@@ -0,0 +1,71 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
|
||||
Texture2D shaderTexture1 : register(t0);
|
||||
Texture2D shaderTexture2 : register(t1);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
cbuffer LightBuffer
|
||||
{
|
||||
float4 diffuseColor;
|
||||
float3 lightDirection;
|
||||
float padding;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 tangent : TANGENT;
|
||||
float3 binormal : BINORMAL;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 NormalMapPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
float4 bumpMap;
|
||||
float3 bumpNormal;
|
||||
float3 lightDir;
|
||||
float lightIntensity;
|
||||
float4 color;
|
||||
|
||||
|
||||
// Sample the pixel color from the color texture at this location.
|
||||
textureColor = shaderTexture1.Sample(SampleType, input.tex);
|
||||
|
||||
// Sample the pixel from the normal map.
|
||||
bumpMap = shaderTexture2.Sample(SampleType, input.tex);
|
||||
|
||||
// Expand the range of the normal value from (0, +1) to (-1, +1).
|
||||
bumpMap = (bumpMap * 2.0f) - 1.0f;
|
||||
|
||||
// Calculate the normal from the data in the normal map.
|
||||
bumpNormal = (bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal);
|
||||
|
||||
// Normalize the resulting bump normal.
|
||||
bumpNormal = normalize(bumpNormal);
|
||||
|
||||
// Invert the light direction for calculations.
|
||||
lightDir = -lightDirection;
|
||||
|
||||
// Calculate the amount of light on this pixel based on the normal map value.
|
||||
lightIntensity = saturate(dot(bumpNormal, lightDir));
|
||||
|
||||
// Determine the final amount of diffuse color based on the diffuse color combined with the light intensity.
|
||||
color = saturate(diffuseColor * lightIntensity);
|
||||
|
||||
// Combine the final light color with the texture color.
|
||||
color = color * textureColor;
|
||||
|
||||
return color;
|
||||
}
|
||||
65
enginecustom/src/hlsl/normalmap.vs
Normal file
65
enginecustom/src/hlsl/normalmap.vs
Normal file
@@ -0,0 +1,65 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 tangent : TANGENT;
|
||||
float3 binormal : BINORMAL;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 tangent : TANGENT;
|
||||
float3 binormal : BINORMAL;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType NormalMapVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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 and then normalize the final value.
|
||||
output.normal = mul(input.normal, (float3x3)worldMatrix);
|
||||
output.normal = normalize(output.normal);
|
||||
|
||||
// Calculate the tangent vector against the world matrix only and then normalize the final value.
|
||||
output.tangent = mul(input.tangent, (float3x3)worldMatrix);
|
||||
output.tangent = normalize(output.tangent);
|
||||
|
||||
// Calculate the binormal vector against the world matrix only and then normalize the final value.
|
||||
output.binormal = mul(input.binormal, (float3x3)worldMatrix);
|
||||
output.binormal = normalize(output.binormal);
|
||||
|
||||
return output;
|
||||
}
|
||||
43
enginecustom/src/hlsl/reflection.ps
Normal file
43
enginecustom/src/hlsl/reflection.ps
Normal file
@@ -0,0 +1,43 @@
|
||||
Texture2D shaderTexture : register(t0);
|
||||
|
||||
Texture2D reflectionTexture : register(t1);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float4 reflectionPosition : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 ReflectionPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
float2 reflectTexCoord;
|
||||
float4 reflectionColor;
|
||||
float4 color;
|
||||
|
||||
|
||||
// Sample the texture pixel at this location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
// Calculate the projected reflection texture coordinates.
|
||||
reflectTexCoord.x = input.reflectionPosition.x / input.reflectionPosition.w / 2.0f + 0.5f;
|
||||
reflectTexCoord.y = -input.reflectionPosition.y / input.reflectionPosition.w / 2.0f + 0.5f;
|
||||
|
||||
// Sample the texture pixel from the reflection texture using the projected texture coordinates.
|
||||
reflectionColor = reflectionTexture.Sample(SampleType, reflectTexCoord);
|
||||
|
||||
// Do a linear interpolation between the two textures for a blend effect.
|
||||
color = lerp(textureColor, reflectionColor, 0.15f);
|
||||
|
||||
return color;
|
||||
}
|
||||
61
enginecustom/src/hlsl/reflection.vs
Normal file
61
enginecustom/src/hlsl/reflection.vs
Normal file
@@ -0,0 +1,61 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
cbuffer ReflectionBuffer
|
||||
{
|
||||
matrix reflectionMatrix;
|
||||
};
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float4 reflectionPosition : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType ReflectionVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
matrix reflectProjectWorld;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
// Create the reflection projection world matrix.
|
||||
reflectProjectWorld = mul(reflectionMatrix, projectionMatrix);
|
||||
reflectProjectWorld = mul(worldMatrix, reflectProjectWorld);
|
||||
|
||||
// Calculate the input position against the reflectProjectWorld matrix.
|
||||
output.reflectionPosition = mul(input.position, reflectProjectWorld);
|
||||
|
||||
return output;
|
||||
}
|
||||
63
enginecustom/src/hlsl/refraction.ps
Normal file
63
enginecustom/src/hlsl/refraction.ps
Normal file
@@ -0,0 +1,63 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
SamplerState SampleType : register(s0);
|
||||
Texture2D shaderTexture : register(t0);
|
||||
|
||||
cbuffer LightBuffer
|
||||
{
|
||||
float4 ambientColor;
|
||||
float4 diffuseColor;
|
||||
float3 lightDirection;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float clip : SV_ClipDistance0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 RefractionPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
float3 lightDir;
|
||||
float lightIntensity;
|
||||
float4 color;
|
||||
|
||||
|
||||
// Sample the texture pixel at this location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
// Set the default output color to the ambient light value for all pixels.
|
||||
color = ambientColor;
|
||||
|
||||
// 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 final light color.
|
||||
color = saturate(color);
|
||||
|
||||
// Multiply the texture pixel and the input color to get the final result.
|
||||
color = color * textureColor;
|
||||
|
||||
return color;
|
||||
}
|
||||
65
enginecustom/src/hlsl/refraction.vs
Normal file
65
enginecustom/src/hlsl/refraction.vs
Normal file
@@ -0,0 +1,65 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
cbuffer ClipPlaneBuffer
|
||||
{
|
||||
float4 clipPlane;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float clip : SV_ClipDistance0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType RefractionVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
// Set the clipping plane.
|
||||
output.clip = dot(mul(input.position, worldMatrix), clipPlane);
|
||||
|
||||
return output;
|
||||
}
|
||||
93
enginecustom/src/hlsl/specmap.ps
Normal file
93
enginecustom/src/hlsl/specmap.ps
Normal file
@@ -0,0 +1,93 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
|
||||
Texture2D shaderTexture1 : register(t0);
|
||||
Texture2D shaderTexture2 : register(t1);
|
||||
Texture2D shaderTexture3 : register(t2);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
cbuffer LightBuffer
|
||||
{
|
||||
float4 diffuseColor;
|
||||
float4 specularColor;
|
||||
float specularPower;
|
||||
float3 lightDirection;
|
||||
};
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 tangent : TANGENT;
|
||||
float3 binormal : BINORMAL;
|
||||
float3 viewDirection : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 SpecMapPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
float4 bumpMap;
|
||||
float3 bumpNormal;
|
||||
float3 lightDir;
|
||||
float lightIntensity;
|
||||
float4 color;
|
||||
float4 specularIntensity;
|
||||
float3 reflection;
|
||||
float4 specular;
|
||||
|
||||
// Sample the pixel color from the color texture at this location.
|
||||
textureColor = shaderTexture1.Sample(SampleType, input.tex);
|
||||
|
||||
// Sample the pixel from the normal map.
|
||||
bumpMap = shaderTexture2.Sample(SampleType, input.tex);
|
||||
|
||||
// Expand the range of the normal value from (0, +1) to (-1, +1).
|
||||
bumpMap = (bumpMap * 2.0f) - 1.0f;
|
||||
|
||||
// Calculate the normal from the data in the normal map.
|
||||
bumpNormal = (bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal);
|
||||
|
||||
// Normalize the resulting bump normal.
|
||||
bumpNormal = normalize(bumpNormal);
|
||||
|
||||
// Invert the light direction for calculations.
|
||||
lightDir = -lightDirection;
|
||||
|
||||
// Calculate the amount of light on this pixel based on the normal map value.
|
||||
lightIntensity = saturate(dot(bumpNormal, lightDir));
|
||||
|
||||
// Determine the final amount of diffuse color based on the diffuse color combined with the light intensity.
|
||||
color = saturate(diffuseColor * lightIntensity);
|
||||
|
||||
// Combine the final light color with the texture color.
|
||||
color = color * textureColor;
|
||||
|
||||
if(lightIntensity > 0.0f)
|
||||
{
|
||||
// Sample the pixel from the specular map texture.
|
||||
specularIntensity = shaderTexture3.Sample(SampleType, input.tex);
|
||||
|
||||
// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
|
||||
reflection = normalize(2 * lightIntensity * bumpNormal - 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);
|
||||
|
||||
// Use the specular map to determine the intensity of specular light at this pixel.
|
||||
specular = specular * specularIntensity;
|
||||
|
||||
// Add the specular component last to the output color.
|
||||
color = saturate(color + specular);
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
83
enginecustom/src/hlsl/specmap.vs
Normal file
83
enginecustom/src/hlsl/specmap.vs
Normal file
@@ -0,0 +1,83 @@
|
||||
/////////////
|
||||
// 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;
|
||||
float3 tangent : TANGENT;
|
||||
float3 binormal : BINORMAL;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 tangent : TANGENT;
|
||||
float3 binormal : BINORMAL;
|
||||
float3 viewDirection : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType SpecMapVertexShader(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 and then normalize the final value.
|
||||
output.normal = mul(input.normal, (float3x3)worldMatrix);
|
||||
output.normal = normalize(output.normal);
|
||||
|
||||
// Calculate the tangent vector against the world matrix only and then normalize the final value.
|
||||
output.tangent = mul(input.tangent, (float3x3)worldMatrix);
|
||||
output.tangent = normalize(output.tangent);
|
||||
|
||||
// Calculate the binormal vector against the world matrix only and then normalize the final value.
|
||||
output.binormal = mul(input.binormal, (float3x3)worldMatrix);
|
||||
output.binormal = normalize(output.binormal);
|
||||
|
||||
// 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;
|
||||
}
|
||||
61
enginecustom/src/hlsl/sunlight.ps
Normal file
61
enginecustom/src/hlsl/sunlight.ps
Normal file
@@ -0,0 +1,61 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture : register(t0);
|
||||
SamplerState SampleType : register(s0);
|
||||
cbuffer SunLightBuffer
|
||||
{
|
||||
float4 ambientColor;
|
||||
float4 diffuseColor;
|
||||
float3 lightDirection;
|
||||
float intensity;
|
||||
};
|
||||
|
||||
cbuffer SunLightColorBuffer
|
||||
{
|
||||
float4 sunColor;
|
||||
};
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 SunLightPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
float4 color;
|
||||
float lightIntensity;
|
||||
float4 colorArray;
|
||||
float4 colorSum;
|
||||
|
||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
// Calculate the different amounts of light on this pixel based on the direction of the light.
|
||||
lightIntensity = saturate(dot(input.normal, -lightDirection));
|
||||
|
||||
// Determine the diffuse color amount of the light.
|
||||
colorArray = (diffuseColor * lightIntensity) * intensity;
|
||||
|
||||
// Initialize the sum of colors.
|
||||
colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
// Add the light color.
|
||||
colorSum.r += colorArray.r;
|
||||
colorSum.g += colorArray.g;
|
||||
colorSum.b += colorArray.b;
|
||||
|
||||
// Multiply the texture pixel by the light color to get the final result.
|
||||
color = saturate(colorSum) * textureColor;
|
||||
|
||||
return color;
|
||||
}
|
||||
67
enginecustom/src/hlsl/sunlight.vs
Normal file
67
enginecustom/src/hlsl/sunlight.vs
Normal file
@@ -0,0 +1,67 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
cbuffer CameraBuffer
|
||||
{
|
||||
float3 cameraPosition;
|
||||
float padding;
|
||||
};
|
||||
|
||||
cbuffer SunLightBuffer
|
||||
{
|
||||
float4 ambientColor;
|
||||
float4 diffuseColor;
|
||||
float3 lightDirection;
|
||||
float intensity;
|
||||
};
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType SunLightVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
// 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);
|
||||
|
||||
return output;
|
||||
}
|
||||
28
enginecustom/src/hlsl/texture.ps
Normal file
28
enginecustom/src/hlsl/texture.ps
Normal file
@@ -0,0 +1,28 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture : register(t0);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 TexturePixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
|
||||
|
||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
return textureColor;
|
||||
}
|
||||
47
enginecustom/src/hlsl/texture.vs
Normal file
47
enginecustom/src/hlsl/texture.vs
Normal file
@@ -0,0 +1,47 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType TextureVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
return output;
|
||||
}
|
||||
37
enginecustom/src/hlsl/translate.ps
Normal file
37
enginecustom/src/hlsl/translate.ps
Normal file
@@ -0,0 +1,37 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture : register(t0);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
cbuffer TranslationBuffer
|
||||
{
|
||||
float textureTranslation;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 TranslatePixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 textureColor;
|
||||
|
||||
|
||||
// Translate the position where we sample the pixel from.
|
||||
input.tex.x += textureTranslation;
|
||||
|
||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
return textureColor;
|
||||
}
|
||||
48
enginecustom/src/hlsl/translate.vs
Normal file
48
enginecustom/src/hlsl/translate.vs
Normal file
@@ -0,0 +1,48 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType TranslateVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
return output;
|
||||
}
|
||||
38
enginecustom/src/hlsl/transparent.ps
Normal file
38
enginecustom/src/hlsl/transparent.ps
Normal file
@@ -0,0 +1,38 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
Texture2D shaderTexture : register(t0);
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
cbuffer TransparentBuffer
|
||||
{
|
||||
float blendAmount;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 TransparentPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float4 color;
|
||||
|
||||
|
||||
// Sample the texture pixel at this location.
|
||||
color = shaderTexture.Sample(SampleType, input.tex);
|
||||
|
||||
// Set the alpha value of this pixel to the blending amount to create the alpha blending effect.
|
||||
color.a = blendAmount;
|
||||
|
||||
return color;
|
||||
}
|
||||
48
enginecustom/src/hlsl/transparent.vs
Normal file
48
enginecustom/src/hlsl/transparent.vs
Normal file
@@ -0,0 +1,48 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType TransparentVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
return output;
|
||||
}
|
||||
72
enginecustom/src/hlsl/water.ps
Normal file
72
enginecustom/src/hlsl/water.ps
Normal file
@@ -0,0 +1,72 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
SamplerState SampleType : register(s0);
|
||||
|
||||
Texture2D reflectionTexture : register(t0);
|
||||
Texture2D refractionTexture : register(t1);
|
||||
Texture2D normalTexture : register(t2);
|
||||
|
||||
cbuffer WaterBuffer
|
||||
{
|
||||
float waterTranslation;
|
||||
float reflectRefractScale;
|
||||
float2 padding;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float4 reflectionPosition : TEXCOORD1;
|
||||
float4 refractionPosition : TEXCOORD2;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Pixel Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
float4 WaterPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
float2 reflectTexCoord;
|
||||
float2 refractTexCoord;
|
||||
float4 normalMap;
|
||||
float3 normal;
|
||||
float4 reflectionColor;
|
||||
float4 refractionColor;
|
||||
float4 color;
|
||||
|
||||
// Move the position the water normal is sampled from to simulate moving water.
|
||||
input.tex.y += waterTranslation;
|
||||
|
||||
// Calculate the projected reflection texture coordinates.
|
||||
reflectTexCoord.x = input.reflectionPosition.x / input.reflectionPosition.w / 2.0f + 0.5f;
|
||||
reflectTexCoord.y = -input.reflectionPosition.y / input.reflectionPosition.w / 2.0f + 0.5f;
|
||||
|
||||
// Calculate the projected refraction texture coordinates.
|
||||
refractTexCoord.x = input.refractionPosition.x / input.refractionPosition.w / 2.0f + 0.5f;
|
||||
refractTexCoord.y = -input.refractionPosition.y / input.refractionPosition.w / 2.0f + 0.5f;
|
||||
|
||||
// Sample the normal from the normal map texture.
|
||||
normalMap = normalTexture.Sample(SampleType, input.tex);
|
||||
|
||||
// Expand the range of the normal from (0,1) to (-1,+1).
|
||||
normal = (normalMap.xyz * 2.0f) - 1.0f;
|
||||
|
||||
// Re-position the texture coordinate sampling position by the normal map value to simulate the rippling wave effect.
|
||||
reflectTexCoord = reflectTexCoord + (normal.xy * reflectRefractScale);
|
||||
refractTexCoord = refractTexCoord + (normal.xy * reflectRefractScale);
|
||||
|
||||
// Sample the texture pixels from the textures using the updated texture coordinates.
|
||||
reflectionColor = reflectionTexture.Sample(SampleType, reflectTexCoord);
|
||||
refractionColor = refractionTexture.Sample(SampleType, refractTexCoord);
|
||||
|
||||
// Combine the reflection and refraction results for the final color.
|
||||
color = lerp(reflectionColor, refractionColor, 0.6f);
|
||||
|
||||
return color;
|
||||
}
|
||||
71
enginecustom/src/hlsl/water.vs
Normal file
71
enginecustom/src/hlsl/water.vs
Normal file
@@ -0,0 +1,71 @@
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
cbuffer MatrixBuffer
|
||||
{
|
||||
matrix worldMatrix;
|
||||
matrix viewMatrix;
|
||||
matrix projectionMatrix;
|
||||
};
|
||||
|
||||
cbuffer ReflectionBuffer
|
||||
{
|
||||
matrix reflectionMatrix;
|
||||
};
|
||||
|
||||
|
||||
//////////////
|
||||
// TYPEDEFS //
|
||||
//////////////
|
||||
struct VertexInputType
|
||||
{
|
||||
float4 position : POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct PixelInputType
|
||||
{
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float4 reflectionPosition : TEXCOORD1;
|
||||
float4 refractionPosition : TEXCOORD2;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Vertex Shader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
PixelInputType WaterVertexShader(VertexInputType input)
|
||||
{
|
||||
PixelInputType output;
|
||||
matrix reflectProjectWorld;
|
||||
matrix viewProjectWorld;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
// Create the reflection projection world matrix.
|
||||
reflectProjectWorld = mul(reflectionMatrix, projectionMatrix);
|
||||
reflectProjectWorld = mul(worldMatrix, reflectProjectWorld);
|
||||
|
||||
// Calculate the input position against the reflectProjectWorld matrix.
|
||||
output.reflectionPosition = mul(input.position, reflectProjectWorld);
|
||||
|
||||
// Create the view projection world matrix for refraction.
|
||||
viewProjectWorld = mul(viewMatrix, projectionMatrix);
|
||||
viewProjectWorld = mul(worldMatrix, viewProjectWorld);
|
||||
|
||||
// Calculate the input position against the viewProjectWorld matrix.
|
||||
output.refractionPosition = mul(input.position, viewProjectWorld);
|
||||
|
||||
return output;
|
||||
}
|
||||
68
enginecustom/src/inc/shader/CelShadingShader.h
Normal file
68
enginecustom/src/inc/shader/CelShadingShader.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef _CELSHADINGSHADER_H_
|
||||
#define _CELSHADINGSHADER_H_
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: CelShadingShader
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class CelShadingShader
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct LightBufferType
|
||||
{
|
||||
XMFLOAT4 diffuseColor;
|
||||
XMFLOAT3 lightDirection;
|
||||
float padding; // Padding to ensure the structure is a multiple of 16 bytes.
|
||||
XMFLOAT3 lightPosition; // Add light position
|
||||
float padding2; // Padding to ensure the structure is a multiple of 16 bytes.
|
||||
float constantAttenuation;
|
||||
float linearAttenuation;
|
||||
float quadraticAttenuation;
|
||||
float padding3; // Padding to ensure the structure is a multiple of 16 bytes.
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
CelShadingShader();
|
||||
CelShadingShader(const CelShadingShader&);
|
||||
~CelShadingShader();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT3);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT3);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_lightBuffer;
|
||||
};
|
||||
|
||||
#endif
|
||||
57
enginecustom/src/inc/shader/Colorshaderclass.h
Normal file
57
enginecustom/src/inc/shader/Colorshaderclass.h
Normal file
@@ -0,0 +1,57 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: colorshaderclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _COLORSHADERCLASS_H_
|
||||
#define _COLORSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: ColorShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class ColorShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
public:
|
||||
ColorShaderClass();
|
||||
ColorShaderClass(const ColorShaderClass&);
|
||||
~ColorShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
};
|
||||
|
||||
#endif
|
||||
59
enginecustom/src/inc/shader/Multitextureshaderclass.h
Normal file
59
enginecustom/src/inc/shader/Multitextureshaderclass.h
Normal file
@@ -0,0 +1,59 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: multitextureshaderclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _MULTITEXTURESHADERCLASS_H_
|
||||
#define _MULTITEXTURESHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: MultiTextureShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class MultiTextureShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
public:
|
||||
MultiTextureShaderClass();
|
||||
MultiTextureShaderClass(const MultiTextureShaderClass&);
|
||||
~MultiTextureShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
55
enginecustom/src/inc/shader/alphamapshaderclass.h
Normal file
55
enginecustom/src/inc/shader/alphamapshaderclass.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef _ALPHAMAPSHADERCLASS_H_
|
||||
#define _ALPHAMAPSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: AlphaMapShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class AlphaMapShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
public:
|
||||
AlphaMapShaderClass();
|
||||
AlphaMapShaderClass(const AlphaMapShaderClass&);
|
||||
~AlphaMapShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
};
|
||||
|
||||
#endif
|
||||
62
enginecustom/src/inc/shader/fontshaderclass.h
Normal file
62
enginecustom/src/inc/shader/fontshaderclass.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef _FONTSHADERCLASS_H_
|
||||
#define _FONTSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: FontShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class FontShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct PixelBufferType
|
||||
{
|
||||
XMFLOAT4 pixelColor;
|
||||
};
|
||||
|
||||
public:
|
||||
FontShaderClass();
|
||||
FontShaderClass(const FontShaderClass&);
|
||||
~FontShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_pixelBuffer;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
57
enginecustom/src/inc/shader/lightmapshaderclass.h
Normal file
57
enginecustom/src/inc/shader/lightmapshaderclass.h
Normal file
@@ -0,0 +1,57 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _LIGHTMAPSHADERCLASS_H_
|
||||
#define _LIGHTMAPSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: LightMapShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class LightMapShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
public:
|
||||
LightMapShaderClass();
|
||||
LightMapShaderClass(const LightMapShaderClass&);
|
||||
~LightMapShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
92
enginecustom/src/inc/shader/lightshaderclass.h
Normal file
92
enginecustom/src/inc/shader/lightshaderclass.h
Normal file
@@ -0,0 +1,92 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: lightshaderclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _LIGHTSHADERCLASS_H_
|
||||
#define _LIGHTSHADERCLASS_H_
|
||||
#pragma once
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
const int NUM_LIGHTS = 4;
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: LightShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class LightShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct CameraBufferType
|
||||
{
|
||||
XMFLOAT3 cameraPosition;
|
||||
float padding;
|
||||
};
|
||||
|
||||
struct LightBufferType
|
||||
{
|
||||
XMFLOAT4 ambientColor;
|
||||
XMFLOAT4 diffuseColor;
|
||||
XMFLOAT3 lightDirection;
|
||||
float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements.
|
||||
float specularPower;
|
||||
XMFLOAT4 specularColor;
|
||||
};
|
||||
|
||||
struct LightColorBufferType
|
||||
{
|
||||
XMFLOAT4 diffuseColor[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
struct LightPositionBufferType
|
||||
{
|
||||
XMFLOAT4 lightPosition[NUM_LIGHTS];
|
||||
};
|
||||
|
||||
public:
|
||||
LightShaderClass();
|
||||
LightShaderClass(const LightShaderClass&);
|
||||
~LightShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[]);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[]);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11Buffer* m_cameraBuffer;
|
||||
ID3D11Buffer* m_lightBuffer;
|
||||
ID3D11Buffer* m_lightColorBuffer;
|
||||
ID3D11Buffer* m_lightPositionBuffer;
|
||||
};
|
||||
|
||||
#endif
|
||||
1
enginecustom/src/inc/shader/lightshaderwaterclass.h
Normal file
1
enginecustom/src/inc/shader/lightshaderwaterclass.h
Normal file
@@ -0,0 +1 @@
|
||||
#pragma once
|
||||
64
enginecustom/src/inc/shader/normalmapshaderclass.h
Normal file
64
enginecustom/src/inc/shader/normalmapshaderclass.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef _NORMALMAPSHADERCLASS_H_
|
||||
#define _NORMALMAPSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: NormalMapShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class NormalMapShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct LightBufferType
|
||||
{
|
||||
XMFLOAT4 diffuseColor;
|
||||
XMFLOAT3 lightDirection;
|
||||
float padding;
|
||||
};
|
||||
|
||||
public:
|
||||
NormalMapShaderClass();
|
||||
NormalMapShaderClass(const NormalMapShaderClass&);
|
||||
~NormalMapShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_lightBuffer;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
65
enginecustom/src/inc/shader/reflectionshaderclass.h
Normal file
65
enginecustom/src/inc/shader/reflectionshaderclass.h
Normal file
@@ -0,0 +1,65 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: reflectionshaderclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _REFLECTIONSHADERCLASS_H_
|
||||
#define _REFLECTIONSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: ReflectionShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class ReflectionShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct ReflectionBufferType
|
||||
{
|
||||
XMMATRIX reflectionMatrix;
|
||||
};
|
||||
|
||||
public:
|
||||
ReflectionShaderClass();
|
||||
ReflectionShaderClass(const ReflectionShaderClass&);
|
||||
~ReflectionShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, XMMATRIX);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, XMMATRIX);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_reflectionBuffer;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
72
enginecustom/src/inc/shader/refractionshaderclass.h
Normal file
72
enginecustom/src/inc/shader/refractionshaderclass.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef _REFRACTIONSHADERCLASS_H_
|
||||
#define _REFRACTIONSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: RefractionShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class RefractionShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct LightBufferType
|
||||
{
|
||||
XMFLOAT4 ambientColor;
|
||||
XMFLOAT4 diffuseColor;
|
||||
XMFLOAT4 lightPosition;
|
||||
XMFLOAT3 lightDirection;
|
||||
float padding;
|
||||
};
|
||||
|
||||
struct ClipPlaneBufferType
|
||||
{
|
||||
XMFLOAT4 clipPlane;
|
||||
};
|
||||
|
||||
public:
|
||||
RefractionShaderClass();
|
||||
RefractionShaderClass(const RefractionShaderClass&);
|
||||
~RefractionShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*,
|
||||
XMFLOAT3, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[], XMFLOAT4);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*,
|
||||
XMFLOAT3, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[], XMFLOAT4);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11Buffer* m_lightBuffer;
|
||||
ID3D11Buffer* m_clipPlaneBuffer;
|
||||
};
|
||||
|
||||
#endif
|
||||
63
enginecustom/src/inc/shader/shadermanagerclass.h
Normal file
63
enginecustom/src/inc/shader/shadermanagerclass.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef _SHADERMANAGERCLASS_H_
|
||||
#define _SHADERMANAGERCLASS_H_
|
||||
|
||||
// Inclure les en-t<>tes n<>cessaires
|
||||
#include <d3d11.h>
|
||||
#include <DirectXMath.h>
|
||||
#include <vector>
|
||||
#include "textureshaderclass.h"
|
||||
#include "normalmapshaderclass.h"
|
||||
#include "multitextureshaderclass.h"
|
||||
#include "translateshaderclass.h"
|
||||
#include "alphamapshaderclass.h"
|
||||
#include "specmapshaderclass.h"
|
||||
#include "transparentshaderclass.h"
|
||||
#include "lightshaderclass.h"
|
||||
#include "lightmapshaderclass.h"
|
||||
#include "refractionshaderclass.h"
|
||||
#include "watershaderclass.h"
|
||||
#include "celshadingshader.h"
|
||||
#include "sunlightshaderclass.h"
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
class ShaderManagerClass
|
||||
{
|
||||
public:
|
||||
ShaderManagerClass();
|
||||
ShaderManagerClass(const ShaderManagerClass&);
|
||||
~ShaderManagerClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
|
||||
bool RenderTextureShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*);
|
||||
bool RenderNormalMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4);
|
||||
bool RenderMultitextureShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
bool RenderTranslateShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float);
|
||||
bool RenderAlphaMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
bool RenderSpecMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||
bool RenderTransparentShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float);
|
||||
bool RenderlightShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[]);
|
||||
bool RenderlightMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||
bool RenderRefractionShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[], XMFLOAT4);
|
||||
bool RenderWaterShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float);
|
||||
bool RenderCelShadingShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT3);
|
||||
bool RenderSunlightShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float);
|
||||
private:
|
||||
TextureShaderClass* m_TextureShader;
|
||||
NormalMapShaderClass* m_NormalMapShader;
|
||||
MultiTextureShaderClass* m_MultitextureShader;
|
||||
TranslateShaderClass* m_TranslateShader;
|
||||
AlphaMapShaderClass* m_AlphaMapShader;
|
||||
SpecMapShaderClass* m_SpecMapShader;
|
||||
TransparentShaderClass* m_TransparentShader;
|
||||
LightShaderClass* m_LightShader;
|
||||
LightMapShaderClass* m_LightMapShader;
|
||||
RefractionShaderClass* m_RefractionShader;
|
||||
WaterShaderClass* m_WaterShader;
|
||||
CelShadingShader* m_CelShadingShader;
|
||||
SunlightShaderClass* m_SunlightShader;
|
||||
};
|
||||
|
||||
#endif
|
||||
72
enginecustom/src/inc/shader/specmapshaderclass.h
Normal file
72
enginecustom/src/inc/shader/specmapshaderclass.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef _SPECMAPSHADERCLASS_H_
|
||||
#define _SPECMAPSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: SpecMapShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class SpecMapShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct LightBufferType
|
||||
{
|
||||
XMFLOAT4 diffuseColor;
|
||||
XMFLOAT4 specularColor;
|
||||
float specularPower;
|
||||
XMFLOAT3 lightDirection;
|
||||
};
|
||||
|
||||
struct CameraBufferType
|
||||
{
|
||||
XMFLOAT3 cameraPosition;
|
||||
float padding;
|
||||
};
|
||||
|
||||
public:
|
||||
SpecMapShaderClass();
|
||||
SpecMapShaderClass(const SpecMapShaderClass&);
|
||||
~SpecMapShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*,
|
||||
XMFLOAT3, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*,
|
||||
XMFLOAT3, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_lightBuffer;
|
||||
ID3D11Buffer* m_cameraBuffer;
|
||||
};
|
||||
|
||||
#endif
|
||||
68
enginecustom/src/inc/shader/sunlightshaderclass.h
Normal file
68
enginecustom/src/inc/shader/sunlightshaderclass.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
class SunlightShaderClass
|
||||
{
|
||||
|
||||
private :
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct CameraBufferType
|
||||
{
|
||||
XMFLOAT3 cameraPosition;
|
||||
float padding;
|
||||
};
|
||||
|
||||
struct SunLightBufferType
|
||||
{
|
||||
XMFLOAT4 diffuseColor;
|
||||
XMFLOAT4 ambientColor;
|
||||
XMFLOAT3 sunDirection;
|
||||
float intensity;
|
||||
};
|
||||
|
||||
struct SunLightColorBufferType
|
||||
{
|
||||
XMFLOAT4 sunColor;
|
||||
};
|
||||
|
||||
public :
|
||||
SunlightShaderClass();
|
||||
SunlightShaderClass(const SunlightShaderClass&);
|
||||
~SunlightShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3,float);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11Buffer* m_cameraBuffer;
|
||||
ID3D11Buffer* m_sunlightBuffer;
|
||||
ID3D11Buffer* m_sunlightColorBuffer;
|
||||
ID3D11Buffer* m_sunlightPositionBuffer;
|
||||
};
|
||||
56
enginecustom/src/inc/shader/textureshaderclass.h
Normal file
56
enginecustom/src/inc/shader/textureshaderclass.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef _TEXTURESHADERCLASS_H_
|
||||
#define _TEXTURESHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: TextureShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class TextureShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
public:
|
||||
TextureShaderClass();
|
||||
TextureShaderClass(const TextureShaderClass&);
|
||||
~TextureShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
63
enginecustom/src/inc/shader/translateshaderclass.h
Normal file
63
enginecustom/src/inc/shader/translateshaderclass.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef _TRANSLATESHADERCLASS_H_
|
||||
#define _TRANSLATESHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: TranslateShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class TranslateShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct TranslateBufferType
|
||||
{
|
||||
float translation;
|
||||
XMFLOAT3 padding;
|
||||
};
|
||||
|
||||
public:
|
||||
TranslateShaderClass();
|
||||
TranslateShaderClass(const TranslateShaderClass&);
|
||||
~TranslateShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_translateBuffer;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
63
enginecustom/src/inc/shader/transparentshaderclass.h
Normal file
63
enginecustom/src/inc/shader/transparentshaderclass.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef _TRANSPARENTSHADERCLASS_H_
|
||||
#define _TRANSPARENTSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: TransparentShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class TransparentShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct TransparentBufferType
|
||||
{
|
||||
float blendAmount;
|
||||
XMFLOAT3 padding;
|
||||
};
|
||||
|
||||
public:
|
||||
TransparentShaderClass();
|
||||
TransparentShaderClass(const TransparentShaderClass&);
|
||||
~TransparentShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_transparentBuffer;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
70
enginecustom/src/inc/shader/watershaderclass.h
Normal file
70
enginecustom/src/inc/shader/watershaderclass.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef _WATERSHADERCLASS_H_
|
||||
#define _WATERSHADERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <d3d11.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: WaterShaderClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class WaterShaderClass
|
||||
{
|
||||
private:
|
||||
struct MatrixBufferType
|
||||
{
|
||||
XMMATRIX world;
|
||||
XMMATRIX view;
|
||||
XMMATRIX projection;
|
||||
};
|
||||
|
||||
struct ReflectionBufferType
|
||||
{
|
||||
XMMATRIX reflection;
|
||||
};
|
||||
|
||||
struct WaterBufferType
|
||||
{
|
||||
float waterTranslation;
|
||||
float reflectRefractScale;
|
||||
XMFLOAT2 padding;
|
||||
};
|
||||
|
||||
public:
|
||||
WaterShaderClass();
|
||||
WaterShaderClass(const WaterShaderClass&);
|
||||
~WaterShaderClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, HWND);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*,
|
||||
ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float);
|
||||
|
||||
private:
|
||||
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||
void ShutdownShader();
|
||||
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||
|
||||
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*,
|
||||
ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float);
|
||||
void RenderShader(ID3D11DeviceContext*, int);
|
||||
|
||||
private:
|
||||
ID3D11VertexShader* m_vertexShader;
|
||||
ID3D11PixelShader* m_pixelShader;
|
||||
ID3D11InputLayout* m_layout;
|
||||
ID3D11Buffer* m_matrixBuffer;
|
||||
ID3D11SamplerState* m_sampleState;
|
||||
ID3D11Buffer* m_reflectionBuffer;
|
||||
ID3D11Buffer* m_waterBuffer;
|
||||
};
|
||||
|
||||
#endif
|
||||
46
enginecustom/src/inc/system/Cameraclass.h
Normal file
46
enginecustom/src/inc/system/Cameraclass.h
Normal file
@@ -0,0 +1,46 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: cameraclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _CAMERACLASS_H_
|
||||
#define _CAMERACLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <directxmath.h>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: CameraClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class CameraClass
|
||||
{
|
||||
public:
|
||||
CameraClass();
|
||||
CameraClass(const CameraClass&);
|
||||
~CameraClass();
|
||||
|
||||
void SetPosition(float, float, float);
|
||||
void SetRotation(float, float, float);
|
||||
|
||||
XMFLOAT3 GetPosition();
|
||||
XMFLOAT3 GetRotation();
|
||||
|
||||
|
||||
void Render();
|
||||
XMMATRIX GetViewMatrix(XMMATRIX& viewMatrix) const;
|
||||
|
||||
void RenderReflection(float);
|
||||
void GetReflectionViewMatrix(XMMATRIX&) const;
|
||||
|
||||
private:
|
||||
float m_positionX, m_positionY, m_positionZ;
|
||||
float m_rotationX, m_rotationY, m_rotationZ;
|
||||
XMMATRIX m_viewMatrix;
|
||||
XMMATRIX m_reflectionViewMatrix;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
226
enginecustom/src/inc/system/Logger.h
Normal file
226
enginecustom/src/inc/system/Logger.h
Normal file
@@ -0,0 +1,226 @@
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <Windows.h>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include <deque>
|
||||
#include <unordered_set>
|
||||
#include <imgui.h>
|
||||
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
|
||||
static Logger& Get()
|
||||
{
|
||||
static Logger instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
Logger(Logger const&) = delete;
|
||||
void operator=(Logger const&) = delete;
|
||||
|
||||
enum class LogLevel
|
||||
{
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
Shutdown,
|
||||
Initialize,
|
||||
Update,
|
||||
Render,
|
||||
Input,
|
||||
Physics,
|
||||
Audio,
|
||||
Network,
|
||||
Scripting,
|
||||
AI,
|
||||
Resource,
|
||||
Memory,
|
||||
Debug,
|
||||
Count // Do not use this, it's just to get the number of log levels it must at the end
|
||||
};
|
||||
|
||||
// Return the size of the enum class LogLevel as a constant integer
|
||||
static constexpr int LogLevelCount = static_cast<int>(LogLevel::Count);
|
||||
|
||||
struct LogEntry
|
||||
{
|
||||
std::string message;
|
||||
LogLevel level;
|
||||
};
|
||||
|
||||
struct LogLevelInfo
|
||||
{
|
||||
const char* name;
|
||||
int value;
|
||||
ImVec4 color;
|
||||
};
|
||||
|
||||
static const LogLevelInfo GetLogLevelInfo(LogLevel level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case LogLevel::Info: return LogLevelInfo{ "Info", 0, ImVec4(0.0f, 1.0f, 0.0f, 1.0f) };
|
||||
case LogLevel::Warning: return LogLevelInfo{ "Warning", 1, ImVec4(1.0f, 1.0f, 0.0f, 1.0f) };
|
||||
case LogLevel::Error: return LogLevelInfo{ "Error", 2, ImVec4(1.0f, 0.0f, 0.0f, 1.0f) };
|
||||
case LogLevel::Shutdown: return LogLevelInfo{ "Shutdown", 3, ImVec4(0.5f, 0.0f, 0.0f, 1.0f) };
|
||||
case LogLevel::Initialize: return LogLevelInfo{ "Initialize", 4, ImVec4(0.0f, 1.0f, 1.0f, 1.0f) };
|
||||
case LogLevel::Update: return LogLevelInfo{ "Update", 5, ImVec4(1.0f, 0.0f, 1.0f, 1.0f) };
|
||||
case LogLevel::Render: return LogLevelInfo{ "Render", 6, ImVec4(1.0f, 1.0f, 1.0f, 1.0f) };
|
||||
case LogLevel::Input: return LogLevelInfo{ "Input", 7, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::Physics: return LogLevelInfo{ "Physics", 8, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::Audio: return LogLevelInfo{ "Audio", 9, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::Network: return LogLevelInfo{ "Network", 10, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::Scripting: return LogLevelInfo{ "Scripting", 11, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::AI: return LogLevelInfo{ "AI", 12, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::Resource: return LogLevelInfo{ "Resource", 13, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::Memory: return LogLevelInfo{ "Memory", 14, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
case LogLevel::Debug: return LogLevelInfo{ "Debug", 15, ImVec4(0.5f, 0.5f, 0.5f, 1.0f) };
|
||||
default: return LogLevelInfo{ "Unknown", 16, ImVec4(1.0f, 1.0f, 1.0f, 1.0f) };
|
||||
}
|
||||
}
|
||||
|
||||
Logger()
|
||||
{
|
||||
char* appdata = nullptr;
|
||||
size_t len;
|
||||
_dupenv_s(&appdata, &len, "APPDATA");
|
||||
if (appdata == nullptr)
|
||||
{
|
||||
m_appdataPath = "log.log";
|
||||
}
|
||||
else
|
||||
{
|
||||
m_appdataPath = appdata;
|
||||
}
|
||||
free(appdata);
|
||||
std::string directoryPath = m_appdataPath + "\\Khaotic Engine";
|
||||
CreateDirectoryA(directoryPath.c_str(), NULL);
|
||||
|
||||
ManageLogFiles(directoryPath);
|
||||
|
||||
m_logFilePath = directoryPath + "\\" + m_logFileName;
|
||||
|
||||
// Enable only the Error warning and shutdown log levels
|
||||
for (int i = 0; i < LogLevelCount; i++)
|
||||
{
|
||||
m_disabledLogLevels[i] = true;
|
||||
|
||||
if (i == static_cast<int>(LogLevel::Error) || i == static_cast<int>(LogLevel::Warning) || i == static_cast<int>(LogLevel::Shutdown))
|
||||
{
|
||||
m_disabledLogLevels[i] = false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ecrit un message dans le fichier de log et le stocke dans le buffer
|
||||
void Log(const std::string& message, const std::string& fileName, int lineNumber, LogLevel level = LogLevel::Info)
|
||||
{
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto in_time_t = std::chrono::system_clock::to_time_t(now);
|
||||
|
||||
std::tm buf;
|
||||
localtime_s(&buf, &in_time_t);
|
||||
|
||||
// Obtenez les millisecondes <20> partir de maintenant
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
||||
|
||||
// Utilisez LogLevelToString pour obtenir la cha<68>ne de caract<63>res du niveau de log
|
||||
std::string levelStr = GetLogLevelInfo(level).name;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "[" << std::put_time(&buf, "%Y-%m-%d") << "] "
|
||||
<< "[" << std::put_time(&buf, "%X") << "." << std::setfill('0') << std::setw(3) << ms.count() << "] "
|
||||
<< "[" << levelStr << "] "
|
||||
<< "[" << fileName << ":" << lineNumber << "] "
|
||||
<< message;
|
||||
|
||||
Log(ss.str(), level);
|
||||
|
||||
std::ofstream file(m_logFilePath, std::ios::app);
|
||||
if (file.is_open())
|
||||
{
|
||||
file << ss.str() << std::endl;
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
// ecrit un message dans la console
|
||||
void Log(const std::string& message, LogLevel level)
|
||||
{
|
||||
|
||||
// Si le niveau de log est d<>sactiv<69>, ne faites rien
|
||||
if (m_disabledLogLevels[GetLogLevelInfo(level).value])
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (logBuffer.size() >= logBufferSize)
|
||||
{
|
||||
logBuffer.pop_front();
|
||||
}
|
||||
logBuffer.push_back({ message, level });
|
||||
}
|
||||
|
||||
const std::deque<LogEntry>& GetLogBuffer() const { return logBuffer; }
|
||||
|
||||
void ManageLogFiles(const std::string& directoryPath)
|
||||
{
|
||||
std::vector<std::filesystem::path> logFiles;
|
||||
|
||||
// Parcourez tous les fichiers dans le dossier
|
||||
for (const auto& entry : std::filesystem::directory_iterator(directoryPath))
|
||||
{
|
||||
// Si le fichier est un fichier de log, ajoutez-le <20> la liste
|
||||
if (entry.path().extension() == ".log")
|
||||
{
|
||||
logFiles.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
// Si nous avons plus de trois fichiers de log, supprimez le plus ancien
|
||||
while (logFiles.size() >= 3)
|
||||
{
|
||||
// Triez les fichiers par date de modification, le plus ancien en premier
|
||||
std::sort(logFiles.begin(), logFiles.end(), [](const std::filesystem::path& a, const std::filesystem::path& b)
|
||||
{
|
||||
return std::filesystem::last_write_time(a) < std::filesystem::last_write_time(b);
|
||||
});
|
||||
|
||||
// Supprimez le fichier le plus ancien
|
||||
std::filesystem::remove(logFiles[0]);
|
||||
|
||||
// Supprimez-le de la liste
|
||||
logFiles.erase(logFiles.begin());
|
||||
}
|
||||
|
||||
// Cr<43>ez un nouveau fichier de log pour cette ex<65>cution
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto in_time_t = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm buf;
|
||||
localtime_s(&buf, &in_time_t);
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "Khaotic_log_" << std::put_time(&buf, "%Y_%m_%d_%Hh%Mm%Ss") << ".log";
|
||||
m_logFileName = ss.str();
|
||||
}
|
||||
|
||||
bool m_disabledLogLevels[LogLevelCount];
|
||||
std::string m_logFilePath;
|
||||
|
||||
private:
|
||||
std::string m_filename;
|
||||
std::string m_appdataPath;
|
||||
std::string m_logFileName;
|
||||
|
||||
std::deque<LogEntry> logBuffer;
|
||||
const size_t logBufferSize = 100;
|
||||
|
||||
};
|
||||
39
enginecustom/src/inc/system/Modellistclass.h
Normal file
39
enginecustom/src/inc/system/Modellistclass.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef _MODELLISTCLASS_H_
|
||||
#define _MODELLISTCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: ModelListClass
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
class ModelListClass
|
||||
{
|
||||
private:
|
||||
struct ModelInfoType
|
||||
{
|
||||
float positionX, positionY, positionZ;
|
||||
};
|
||||
|
||||
public:
|
||||
ModelListClass();
|
||||
ModelListClass(const ModelListClass&);
|
||||
~ModelListClass();
|
||||
|
||||
void Initialize(int);
|
||||
void Shutdown();
|
||||
|
||||
int GetModelCount();
|
||||
void GetData(int, float&, float&, float&);
|
||||
|
||||
private:
|
||||
int m_modelCount;
|
||||
ModelInfoType* m_ModelInfoList;
|
||||
};
|
||||
|
||||
#endif
|
||||
37
enginecustom/src/inc/system/Positionclass.h
Normal file
37
enginecustom/src/inc/system/Positionclass.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef _POSITIONCLASS_H_
|
||||
#define _POSITIONCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <math.h>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: PositionClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class PositionClass
|
||||
{
|
||||
public:
|
||||
PositionClass();
|
||||
PositionClass(const PositionClass&);
|
||||
~PositionClass();
|
||||
|
||||
void SetFrameTime(float);
|
||||
void GetRotation(float&, float&) const;
|
||||
void GetPosition(float&, float&, float&) const;
|
||||
|
||||
void TurnLeft(bool);
|
||||
void TurnRight(bool);
|
||||
void TurnMouse(float, float, float, bool);
|
||||
void MoveCamera(bool, bool, bool, bool, bool, bool, bool, bool, bool);
|
||||
|
||||
private:
|
||||
float m_frameTime;
|
||||
float m_rotationY, m_rotationX;
|
||||
float m_positionX, m_positionY, m_positionZ;
|
||||
float m_leftTurnSpeed, m_rightTurnSpeed, m_horizontalTurnSpeed, m_verticalTurnSpeed, m_cameraSpeed, m_speed;
|
||||
};
|
||||
|
||||
#endif
|
||||
63
enginecustom/src/inc/system/Spriteclass.h
Normal file
63
enginecustom/src/inc/system/Spriteclass.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef _SPRITECLASS_H_
|
||||
#define _SPRITECLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "textureclass.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: SpriteClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class SpriteClass
|
||||
{
|
||||
private:
|
||||
struct VertexType
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT2 texture;
|
||||
};
|
||||
|
||||
public:
|
||||
SpriteClass();
|
||||
SpriteClass(const SpriteClass&);
|
||||
~SpriteClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, int, int, char*, int, int);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*);
|
||||
void Update(float);
|
||||
|
||||
int GetIndexCount();
|
||||
ID3D11ShaderResourceView* GetTexture();
|
||||
|
||||
void SetRenderLocation(int, int);
|
||||
|
||||
private:
|
||||
bool InitializeBuffers(ID3D11Device*);
|
||||
void ShutdownBuffers();
|
||||
bool UpdateBuffers(ID3D11DeviceContext*);
|
||||
void RenderBuffers(ID3D11DeviceContext*);
|
||||
|
||||
bool LoadTextures(ID3D11Device*, ID3D11DeviceContext*, char*);
|
||||
void ReleaseTextures();
|
||||
|
||||
private:
|
||||
ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
|
||||
int m_vertexCount, m_indexCount, m_screenWidth, m_screenHeight, m_bitmapWidth, m_bitmapHeight, m_renderX, m_renderY, m_prevPosX, m_prevPosY;
|
||||
TextureClass* m_Textures;
|
||||
float m_frameTime, m_cycleTime;
|
||||
int m_currentTexture, m_textureCount;
|
||||
};
|
||||
|
||||
#endif
|
||||
34
enginecustom/src/inc/system/Timerclass.h
Normal file
34
enginecustom/src/inc/system/Timerclass.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef _TIMERCLASS_H_
|
||||
#define _TIMERCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: TimerClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class TimerClass
|
||||
{
|
||||
public:
|
||||
TimerClass();
|
||||
TimerClass(const TimerClass&);
|
||||
~TimerClass();
|
||||
|
||||
bool Initialize();
|
||||
void Frame();
|
||||
|
||||
float GetTime();
|
||||
|
||||
private:
|
||||
float m_frequency;
|
||||
INT64 m_startTime;
|
||||
float m_frameTime;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
270
enginecustom/src/inc/system/applicationclass.h
Normal file
270
enginecustom/src/inc/system/applicationclass.h
Normal file
@@ -0,0 +1,270 @@
|
||||
#ifndef _APPLICATIONCLASS_H_
|
||||
#define _APPLICATIONCLASS_H_
|
||||
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "d3dclass.h"
|
||||
#include "cameraclass.h"
|
||||
#include "object.h"
|
||||
#include "lightclass.h"
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
|
||||
#include "bitmapclass.h"
|
||||
#include "spriteclass.h"
|
||||
#include "timerclass.h"
|
||||
#include "fontshaderclass.h"
|
||||
#include "fontclass.h"
|
||||
#include "textclass.h"
|
||||
#include "fpsclass.h"
|
||||
#include "inputclass.h"
|
||||
#include "shadermanagerclass.h"
|
||||
#include "modellistclass.h"
|
||||
#include "positionclass.h"
|
||||
#include "frustumclass.h"
|
||||
#include "rendertextureclass.h"
|
||||
#include "displayplaneclass.h"
|
||||
#include "translateshaderclass.h"
|
||||
#include "reflectionshaderclass.h"
|
||||
#include "physics.h"
|
||||
#include "frustum.h"
|
||||
|
||||
#include <WICTextureLoader.h>
|
||||
#include <comdef.h> // Pour _com_error
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
const bool FULL_SCREEN = false;
|
||||
const float SCREEN_DEPTH = 1000.0f;
|
||||
const float SCREEN_NEAR = 0.3f;
|
||||
|
||||
struct Input
|
||||
{
|
||||
bool m_KeyLeft = false;
|
||||
bool m_KeyRight = false;
|
||||
bool m_KeyUp = false;
|
||||
bool m_KeyDown = false;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: ApplicationClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class ApplicationClass
|
||||
{
|
||||
public:
|
||||
ApplicationClass();
|
||||
~ApplicationClass();
|
||||
D3DClass* GetDirect3D();
|
||||
RenderTextureClass* GetRenderTexture() const { return m_SceneTexture; };
|
||||
|
||||
bool Initialize(int, int, HWND);
|
||||
void Shutdown();
|
||||
bool Frame(InputClass*);
|
||||
void PhysicsThreadFunction();
|
||||
int GetPhysicsTickRate() const { return m_PhysicsTickRate; };
|
||||
void SetPhysicsTickRate(int physicsTickRate) { m_PhysicsTickRate = physicsTickRate; };
|
||||
|
||||
int GetScreenWidth() const;
|
||||
void SetScreenWidth(int screenWidth);
|
||||
int GetScreenHeight() const;
|
||||
void SetScreenHeight(int screenHeight);
|
||||
|
||||
float GetSpeed() const { return m_speed; };
|
||||
void SetSpeed(float speed) { this->m_speed = speed; };
|
||||
|
||||
void AddCube();
|
||||
void DeleteKobject(int index);
|
||||
size_t GetCubeCount() const { return m_cubes.size(); };
|
||||
size_t GetTerrainCubeCount() const { return m_terrainChunk.size(); };
|
||||
std::vector<Object*> GetCubes() const { return m_cubes; };
|
||||
std::vector<Object*> GetTerrainCubes() const { return m_terrainChunk; };
|
||||
std::vector<Object*> GetKobjects() const { return m_object; };
|
||||
void AddKobject(WCHAR* filepath);
|
||||
void SetPath(WCHAR* path) { m_path = path; };
|
||||
void SetWFolder(std::filesystem::path WFolder) { m_WFolder = WFolder; };
|
||||
|
||||
void GenerateTerrain();
|
||||
void DeleteTerrain();
|
||||
|
||||
XMVECTOR GetLightPosition(int index);
|
||||
XMVECTOR GetLightColor(int index);
|
||||
void SetLightPosition(int index, XMVECTOR color);
|
||||
void SetLightColor(int index, XMVECTOR color);
|
||||
void DeleteLight(int index);
|
||||
void AddLight();
|
||||
std::vector<LightClass*> GetLights() const { return m_Lights; };
|
||||
LightClass* GetSunLight() const { return m_SunLight; };
|
||||
|
||||
bool GetShouldQuit() const { return m_ShouldQuit; };
|
||||
void SetShouldQuit(bool shouldQuit) { m_ShouldQuit = shouldQuit; };
|
||||
|
||||
void SetCelShading(bool enable) { m_enableCelShading = enable; };
|
||||
|
||||
void SetVsync(bool vsync);
|
||||
bool GetVsync() const { return VSYNC_ENABLED; };
|
||||
|
||||
HWND GetHwnd() const;
|
||||
void SetHwnd(HWND hwnd);
|
||||
|
||||
bool IsWindowed() const;
|
||||
void SetWindowed(bool windowed);
|
||||
|
||||
void SetWindowSize(ImVec2 size) { windowSize = size; };
|
||||
ImVec2 GetWindowSize() const { return windowSize; };
|
||||
|
||||
Physics* GetPhysics() const { return m_Physics; };
|
||||
|
||||
// ----------------------------------- //
|
||||
// ------------- Culling ------------- //
|
||||
// ----------------------------------- //
|
||||
|
||||
Frustum GetFrustum() const { return m_FrustumCulling; };
|
||||
void SetFrustum(Frustum frustum) { m_FrustumCulling = frustum; };
|
||||
|
||||
void ConstructFrustum();
|
||||
int GetRenderCount() const { return m_renderCount; };
|
||||
void SetRenderCount(int renderCount) { m_renderCount = renderCount; };
|
||||
float GetFrustumTolerance() const { return m_FrustumCullingTolerance; };
|
||||
void SetFrustumTolerance(float frustumTolerance) { m_FrustumCullingTolerance = frustumTolerance; };
|
||||
|
||||
bool GetCanFixedUpdate() const { return CanFixedUpdate; };
|
||||
void SetCanFixedUpdate(bool canFixedUpdate) { CanFixedUpdate = canFixedUpdate; };
|
||||
|
||||
|
||||
private:
|
||||
bool Render(float, float, float, float, float);
|
||||
bool RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bool keyDown, float deltaTime);
|
||||
bool UpdateMouseStrings(int, int, bool);
|
||||
bool UpdateFps();
|
||||
bool UpdateRenderCountString(int);
|
||||
bool RenderSceneToTexture(float);
|
||||
bool RenderRefractionToTexture();
|
||||
bool RenderReflectionToTexture();
|
||||
bool RenderPass(const std::vector<std::reference_wrapper<std::vector<Object*>>>& RenderQueues, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection);
|
||||
|
||||
void ConstructSkybox(); // Construct the skybox
|
||||
void UpdateSkyboxPosition(); // Update the skybox position
|
||||
bool RenderSkybox(XMMATRIX view, XMMATRIX projection); // Render the skybox
|
||||
|
||||
public :
|
||||
std::vector<ID3D11ShaderResourceView*> textures;
|
||||
std::vector<ID3D11ShaderResourceView*> m_SkyboxTextures;
|
||||
|
||||
private :
|
||||
|
||||
// ------------------------------------- //
|
||||
// ------------- DIRECT3D -------------- //
|
||||
// ------------------------------------- //
|
||||
|
||||
D3DClass* m_Direct3D;
|
||||
IDXGISwapChain* m_swapChain;
|
||||
ModelClass* m_Model,* m_GroundModel, * m_WallModel, * m_BathModel, * m_WaterModel;
|
||||
ModelListClass* m_ModelList;
|
||||
bool VSYNC_ENABLED = true;
|
||||
|
||||
HWND m_hwnd;
|
||||
bool m_windowed;
|
||||
|
||||
// ------------------------------------- //
|
||||
// ------------- RENDERING ------------- //
|
||||
// ------------------------------------- //
|
||||
|
||||
XMMATRIX m_baseViewMatrix;
|
||||
RenderTextureClass* m_RenderTexture, * m_RefractionTexture, * m_ReflectionTexture, * m_SceneTexture;
|
||||
DisplayPlaneClass* m_DisplayPlane;
|
||||
int m_screenWidth, m_screenHeight;
|
||||
CameraClass* m_Camera;
|
||||
PositionClass* m_Position;
|
||||
std::vector<XMMATRIX> m_SkyboxInitialTranslations;
|
||||
|
||||
// ------------------------------------ //
|
||||
// ------------- OBJECTS -------------- //
|
||||
// ------------------------------------ //
|
||||
|
||||
Object* m_SelectedObject;
|
||||
std::vector<Object*> m_cubes;
|
||||
std::vector<Object*> m_terrainChunk;
|
||||
float m_speed = 0.1f; // speed for the demo spinning object
|
||||
std::vector<Object*> m_object;
|
||||
int m_ObjectId = 0;
|
||||
std::vector<std::reference_wrapper<std::vector<Object*>>> m_RenderQueues;
|
||||
std::vector<Object*> m_Skybox;
|
||||
|
||||
// ----------------------------------- //
|
||||
// ------------- LIGHTS -------------- //
|
||||
// ----------------------------------- //
|
||||
|
||||
LightClass* m_Light;
|
||||
std::vector<LightClass*> m_Lights;
|
||||
int m_numLights;
|
||||
LightClass* m_SunLight;
|
||||
|
||||
XMFLOAT3 TrueLightPosition;
|
||||
ModelClass* m_LightModel;
|
||||
|
||||
// ----------------------------------- //
|
||||
// ------------- SHADERS ------------- //
|
||||
// ----------------------------------- //
|
||||
|
||||
ShaderManagerClass* m_ShaderManager;
|
||||
FontShaderClass* m_FontShader;
|
||||
BitmapClass* m_Bitmap;
|
||||
SpriteClass* m_Sprite;
|
||||
|
||||
bool m_enableCelShading;
|
||||
|
||||
// ----------------------------------- //
|
||||
// ------------ VARIABLES ------------ //
|
||||
// ----------------------------------- //
|
||||
|
||||
float m_waterHeight, m_waterTranslation;
|
||||
wchar_t* m_path;
|
||||
std::filesystem::path m_WFolder;
|
||||
|
||||
// ------------------------------------------------- //
|
||||
// ------------- FPS AND INFO ON SCREEN ------------ //
|
||||
// ------------------------------------------------- //
|
||||
|
||||
TimerClass* m_Timer;
|
||||
TextClass* m_MouseStrings;
|
||||
TextClass* m_RenderCountString;
|
||||
FontClass* m_Font;
|
||||
FpsClass* m_Fps;
|
||||
TextClass* m_FpsString;
|
||||
int m_previousFps;
|
||||
|
||||
// ------------------------------------------------- //
|
||||
// ------------------- OTHER ----------------------- //
|
||||
// ------------------------------------------------- //
|
||||
|
||||
bool m_ShouldQuit;
|
||||
Physics* m_Physics;
|
||||
float m_gravity;
|
||||
XMVECTOR m_previousPosition;
|
||||
ImVec2 windowSize;
|
||||
int m_PhysicsTickRate = 50;
|
||||
bool CanFixedUpdate = false;
|
||||
std::thread m_PhysicsThread;
|
||||
|
||||
// ------------------------------------------------- //
|
||||
// ------------------- Culling --------------------- //
|
||||
// ------------------------------------------------- //
|
||||
|
||||
Frustum m_FrustumCulling;
|
||||
int m_renderCount;
|
||||
float m_FrustumCullingTolerance = 5.f;
|
||||
|
||||
// ------------------------------------------------- //
|
||||
// -------------------- Input ---------------------- //
|
||||
// ------------------------------------------------- //
|
||||
|
||||
Input m_Inputs;
|
||||
};
|
||||
|
||||
#endif
|
||||
59
enginecustom/src/inc/system/bitmapclass.h
Normal file
59
enginecustom/src/inc/system/bitmapclass.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef _BITMAPCLASS_H_
|
||||
#define _BITMAPCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <directxmath.h>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "textureclass.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: BitmapClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class BitmapClass
|
||||
{
|
||||
private:
|
||||
struct VertexType
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT2 texture;
|
||||
};
|
||||
|
||||
public:
|
||||
BitmapClass();
|
||||
BitmapClass(const BitmapClass&);
|
||||
~BitmapClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, int, int, char*, int, int);
|
||||
void Shutdown();
|
||||
bool Render(ID3D11DeviceContext*);
|
||||
|
||||
int GetIndexCount();
|
||||
ID3D11ShaderResourceView* GetTexture();
|
||||
|
||||
void SetRenderLocation(int, int);
|
||||
|
||||
private:
|
||||
bool InitializeBuffers(ID3D11Device*);
|
||||
void ShutdownBuffers();
|
||||
bool UpdateBuffers(ID3D11DeviceContext*);
|
||||
void RenderBuffers(ID3D11DeviceContext*);
|
||||
|
||||
bool LoadTexture(ID3D11Device*, ID3D11DeviceContext*, char*);
|
||||
void ReleaseTexture();
|
||||
private:
|
||||
ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
|
||||
int m_vertexCount, m_indexCount, m_screenWidth, m_screenHeight, m_bitmapWidth, m_bitmapHeight, m_renderX, m_renderY, m_prevPosX, m_prevPosY;
|
||||
TextureClass* m_Texture;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
90
enginecustom/src/inc/system/d3dclass.h
Normal file
90
enginecustom/src/inc/system/d3dclass.h
Normal file
@@ -0,0 +1,90 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: d3dclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _D3DCLASS_H_
|
||||
#define _D3DCLASS_H_
|
||||
|
||||
|
||||
/////////////
|
||||
// LINKING //
|
||||
/////////////
|
||||
#pragma comment(lib, "d3d11.lib")
|
||||
#pragma comment(lib, "dxgi.lib")
|
||||
#pragma comment(lib, "d3dcompiler.lib")
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "imguiManager.h"
|
||||
#include "d3d11.h"
|
||||
#include "fontshaderclass.h"
|
||||
#include "fontclass.h"
|
||||
#include "textclass.h"
|
||||
|
||||
using namespace DirectX;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: D3DClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class D3DClass
|
||||
{
|
||||
public:
|
||||
D3DClass();
|
||||
D3DClass(const D3DClass&);
|
||||
~D3DClass();
|
||||
|
||||
bool Initialize(int, int, bool, HWND, bool, float, float);
|
||||
void Shutdown();
|
||||
|
||||
void BeginScene(float, float, float, float);
|
||||
void EndScene();
|
||||
|
||||
ID3D11Device* GetDevice();
|
||||
ID3D11DeviceContext* GetDeviceContext();
|
||||
//XMMATRIX GetProjectionMatrix(XMMATRIX& projectionMatrix);
|
||||
IDXGISwapChain* m_swapChain;
|
||||
IDXGISwapChain* GetSwapChain();
|
||||
void ResizeSwapChain(int, int);
|
||||
void SetVsync(bool vsync);
|
||||
|
||||
|
||||
XMMATRIX GetProjectionMatrix() const { return m_projectionMatrix; };
|
||||
XMMATRIX GetWorldMatrix() const { return m_worldMatrix;};
|
||||
XMMATRIX GetOrthoMatrix() const { return m_orthoMatrix; };
|
||||
|
||||
void GetVideoCardInfo(char*, int&);
|
||||
|
||||
void SetBackBufferRenderTarget();
|
||||
void ResetViewport();
|
||||
|
||||
void ReleaseResources();
|
||||
void ResetResources(int newWidth, int newHeight);
|
||||
|
||||
void TurnZBufferOn();
|
||||
void TurnZBufferOff();
|
||||
|
||||
void EnableAlphaBlending();
|
||||
void DisableAlphaBlending();
|
||||
|
||||
private:
|
||||
bool m_vsync_enabled;
|
||||
int m_videoCardMemory;
|
||||
char m_videoCardDescription[128];
|
||||
ID3D11Device* m_device;
|
||||
ID3D11DeviceContext* m_deviceContext;
|
||||
ID3D11RenderTargetView* m_renderTargetView;
|
||||
ID3D11Texture2D* m_depthStencilBuffer;
|
||||
ID3D11DepthStencilState* m_depthStencilState;
|
||||
ID3D11DepthStencilView* m_depthStencilView;
|
||||
ID3D11RasterizerState* m_rasterState;
|
||||
XMMATRIX m_projectionMatrix;
|
||||
XMMATRIX m_worldMatrix;
|
||||
XMMATRIX m_orthoMatrix;
|
||||
D3D11_VIEWPORT m_viewport;
|
||||
ID3D11DepthStencilState* m_depthDisabledStencilState;
|
||||
ID3D11BlendState* m_alphaEnableBlendingState;
|
||||
ID3D11BlendState* m_alphaDisableBlendingState;
|
||||
};
|
||||
|
||||
#endif
|
||||
45
enginecustom/src/inc/system/displayplaneclass.h
Normal file
45
enginecustom/src/inc/system/displayplaneclass.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef _DISPLAYPLANECLASS_H_
|
||||
#define _DISPLAYPLANECLASS_H_
|
||||
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "d3dclass.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: DisplayPlaneClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class DisplayPlaneClass
|
||||
{
|
||||
private:
|
||||
struct VertexType
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT2 texture;
|
||||
};
|
||||
|
||||
public:
|
||||
DisplayPlaneClass();
|
||||
DisplayPlaneClass(const DisplayPlaneClass&);
|
||||
~DisplayPlaneClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, float, float);
|
||||
void Shutdown();
|
||||
void Render(ID3D11DeviceContext*);
|
||||
|
||||
int GetIndexCount();
|
||||
|
||||
private:
|
||||
bool InitializeBuffers(ID3D11Device*, float, float);
|
||||
void ShutdownBuffers();
|
||||
void RenderBuffers(ID3D11DeviceContext*);
|
||||
|
||||
private:
|
||||
ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
|
||||
int m_vertexCount, m_indexCount;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
64
enginecustom/src/inc/system/fontclass.h
Normal file
64
enginecustom/src/inc/system/fontclass.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef _FONTCLASS_H_
|
||||
#define _FONTCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "textureclass.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: FontClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class FontClass
|
||||
{
|
||||
private:
|
||||
struct FontType
|
||||
{
|
||||
float left, right;
|
||||
int size;
|
||||
};
|
||||
|
||||
struct VertexType
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT2 texture;
|
||||
};
|
||||
|
||||
public:
|
||||
FontClass();
|
||||
FontClass(const FontClass&);
|
||||
~FontClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, int);
|
||||
void Shutdown();
|
||||
|
||||
ID3D11ShaderResourceView* GetTexture();
|
||||
|
||||
void BuildVertexArray(void*, char*, float, float);
|
||||
int GetSentencePixelLength(char*);
|
||||
int GetFontHeight();
|
||||
|
||||
private:
|
||||
bool LoadFontData(char*);
|
||||
void ReleaseFontData();
|
||||
bool LoadTexture(ID3D11Device*, ID3D11DeviceContext*, char*);
|
||||
void ReleaseTexture();
|
||||
|
||||
private:
|
||||
FontType* m_Font;
|
||||
TextureClass* m_Texture;
|
||||
float m_fontHeight;
|
||||
int m_spaceSize;
|
||||
};
|
||||
|
||||
#endif
|
||||
36
enginecustom/src/inc/system/fpsclass.h
Normal file
36
enginecustom/src/inc/system/fpsclass.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef _FPSCLASS_H_
|
||||
#define _FPSCLASS_H_
|
||||
|
||||
|
||||
/////////////
|
||||
// LINKING //
|
||||
/////////////
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <windows.h>
|
||||
#include <mmsystem.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: FpsClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class FpsClass
|
||||
{
|
||||
public:
|
||||
FpsClass();
|
||||
FpsClass(const FpsClass&);
|
||||
~FpsClass();
|
||||
|
||||
void Initialize();
|
||||
void Frame();
|
||||
int GetFps();
|
||||
|
||||
private:
|
||||
int m_fps, m_count;
|
||||
unsigned long m_startTime;
|
||||
};
|
||||
|
||||
#endif
|
||||
12
enginecustom/src/inc/system/frustum.h
Normal file
12
enginecustom/src/inc/system/frustum.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <DirectXMath.h>
|
||||
using namespace DirectX;
|
||||
|
||||
class Frustum
|
||||
{
|
||||
public:
|
||||
void ConstructFrustum(float screenDepth, XMMATRIX projectionMatrix, XMMATRIX viewMatrix);
|
||||
bool CheckCube(float xCenter, float yCenter, float zCenter, float radius, float tolerance);
|
||||
|
||||
private:
|
||||
XMVECTOR m_planes[6];
|
||||
};
|
||||
33
enginecustom/src/inc/system/frustumclass.h
Normal file
33
enginecustom/src/inc/system/frustumclass.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef _FRUSTUMCLASS_H_
|
||||
#define _FRUSTUMCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <directxmath.h>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: FrustumClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class FrustumClass
|
||||
{
|
||||
public:
|
||||
FrustumClass();
|
||||
FrustumClass(const FrustumClass&);
|
||||
~FrustumClass();
|
||||
|
||||
void ConstructFrustum(XMMATRIX, XMMATRIX, float);
|
||||
|
||||
bool CheckPoint(float, float, float);
|
||||
bool CheckCube(float, float, float, float);
|
||||
bool CheckSphere(float, float, float, float);
|
||||
bool CheckRectangle(float, float, float, float, float, float);
|
||||
|
||||
private:
|
||||
XMFLOAT4 m_planes[6];
|
||||
};
|
||||
|
||||
#endif
|
||||
69
enginecustom/src/inc/system/imguiManager.h
Normal file
69
enginecustom/src/inc/system/imguiManager.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
#ifndef _IMGUI_MANAGER_H_
|
||||
#define _IMGUI_MANAGER_H_
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_dx11.h>
|
||||
#include <imgui_impl_win32.h>
|
||||
#include <windows.h>
|
||||
#include <deque>
|
||||
|
||||
class ApplicationClass;
|
||||
|
||||
class imguiManager
|
||||
{
|
||||
public:
|
||||
imguiManager();
|
||||
~imguiManager();
|
||||
|
||||
bool Initialize(HWND hwnd, ID3D11Device* device, ID3D11DeviceContext* deviceContext);
|
||||
void Shutdown();
|
||||
void Render();
|
||||
void NewFrame();
|
||||
void SetupDockspace();
|
||||
|
||||
// Widgets
|
||||
void WidgetSpeedSlider(float* speed);
|
||||
void WidgetButton();
|
||||
void WidgetFPS();
|
||||
void WidgetAddObject(ApplicationClass* app);
|
||||
|
||||
void WidgetObjectWindow(ApplicationClass* app);
|
||||
void WidgetTerrainWindow(ApplicationClass* app);
|
||||
void WidgetLightWindow(ApplicationClass* app);
|
||||
void WidgetShaderWindow(ApplicationClass* app);
|
||||
void WidgetEngineSettingsWindow(ApplicationClass* app);
|
||||
void WidgetRenderWindow(ApplicationClass* app, ImVec2 availableSize);
|
||||
void WidgetLogWindow(ApplicationClass* app);
|
||||
|
||||
bool ImGuiWidgetRenderer(ApplicationClass* app);
|
||||
|
||||
void SetWindowSize(ImVec2 size) { windowSize = size; }
|
||||
ImVec2 GetWindowSize() const { return windowSize; }
|
||||
|
||||
// Shader toggles
|
||||
|
||||
bool m_EnableCelShading;
|
||||
|
||||
private:
|
||||
bool showObjectWindow;
|
||||
bool showTerrainWindow;
|
||||
bool showLightWindow;
|
||||
bool showShaderWindow;
|
||||
bool showEngineSettingsWindow;
|
||||
bool showLogWindow;
|
||||
|
||||
bool m_isPhyiscsEnabled = false;
|
||||
|
||||
ImGuiIO* io;
|
||||
|
||||
ID3D11Device* m_device;
|
||||
ID3D11DeviceContext* m_deviceContext;
|
||||
ImVec2 windowSize;
|
||||
|
||||
const std::deque<Logger::LogEntry>& logBuffer = Logger::Get().GetLogBuffer();
|
||||
};
|
||||
|
||||
#endif
|
||||
75
enginecustom/src/inc/system/inputclass.h
Normal file
75
enginecustom/src/inc/system/inputclass.h
Normal file
@@ -0,0 +1,75 @@
|
||||
#ifndef _INPUTCLASS_H_
|
||||
#define _INPUTCLASS_H_
|
||||
|
||||
///////////////////////////////
|
||||
// PRE-PROCESSING DIRECTIVES //
|
||||
///////////////////////////////
|
||||
#define DIRECTINPUT_VERSION 0x0800
|
||||
|
||||
/////////////
|
||||
// LINKING //
|
||||
/////////////
|
||||
#pragma comment(lib, "dinput8.lib")
|
||||
#pragma comment(lib, "dxguid.lib")
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <dinput.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: InputClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class InputClass
|
||||
{
|
||||
public:
|
||||
InputClass();
|
||||
InputClass(const InputClass&);
|
||||
~InputClass();
|
||||
|
||||
bool Initialize(HINSTANCE, HWND, int, int);
|
||||
void Shutdown();
|
||||
bool Frame();
|
||||
|
||||
bool IsEscapePressed() const;
|
||||
void GetMouseLocation(int&, int&) const;
|
||||
bool IsLeftMousePressed() const;
|
||||
bool IsRightMousePressed() const;
|
||||
void KeyDown(unsigned int);
|
||||
void KeyUp(unsigned int);
|
||||
bool IsLeftArrowPressed() const;
|
||||
bool IsRightArrowPressed() const;
|
||||
bool IsScrollUp() const;
|
||||
bool IsScrollDown() const;
|
||||
bool IsUpArrowPressed() const;
|
||||
bool IsDownArrowPressed() const;
|
||||
bool IsAPressed() const;
|
||||
bool IsDPressed() const;
|
||||
bool IsWPressed() const;
|
||||
bool IsSPressed() const;
|
||||
bool IsQPressed() const;
|
||||
bool IsEPressed()const;
|
||||
|
||||
bool IsKeyDown(unsigned int) const;
|
||||
|
||||
private:
|
||||
bool m_keys[256];
|
||||
|
||||
bool ReadKeyboard();
|
||||
bool ReadMouse();
|
||||
void ProcessInput();
|
||||
|
||||
private:
|
||||
IDirectInput8* m_directInput;
|
||||
IDirectInputDevice8* m_keyboard;
|
||||
IDirectInputDevice8* m_mouse;
|
||||
|
||||
unsigned char m_keyboardState[256];
|
||||
DIMOUSESTATE m_mouseState;
|
||||
|
||||
int m_screenWidth, m_screenHeight, m_mouseX, m_mouseY;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
53
enginecustom/src/inc/system/lightclass.h
Normal file
53
enginecustom/src/inc/system/lightclass.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: lightclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _LIGHTCLASS_H_
|
||||
#define _LIGHTCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include <directxmath.h>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: LightClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class LightClass
|
||||
{
|
||||
public:
|
||||
LightClass();
|
||||
LightClass(const LightClass&);
|
||||
~LightClass();
|
||||
|
||||
void SetAmbientColor(float, float, float, float);
|
||||
void SetDiffuseColor(float, float, float, float);
|
||||
void SetDirection(float, float, float);
|
||||
void SetSpecularColor(float, float, float, float);
|
||||
void SetSpecularPower(float);
|
||||
void SetPosition(float, float, float);
|
||||
|
||||
XMFLOAT4 GetAmbientColor();
|
||||
XMFLOAT4 GetDiffuseColor();
|
||||
XMFLOAT3 GetDirection();
|
||||
XMFLOAT4 GetSpecularColor();
|
||||
float GetSpecularPower();
|
||||
XMFLOAT4 GetPosition();
|
||||
|
||||
void SetIntensity(float intensity) { m_intensity = intensity; }
|
||||
float GetIntensity() const { return m_intensity; }
|
||||
|
||||
private:
|
||||
XMFLOAT4 m_ambientColor;
|
||||
XMFLOAT4 m_diffuseColor;
|
||||
XMFLOAT3 m_direction;
|
||||
XMFLOAT4 m_specularColor;
|
||||
float m_intensity;
|
||||
float m_specularPower;
|
||||
XMFLOAT4 m_position;
|
||||
};
|
||||
|
||||
#endif
|
||||
117
enginecustom/src/inc/system/modelclass.h
Normal file
117
enginecustom/src/inc/system/modelclass.h
Normal file
@@ -0,0 +1,117 @@
|
||||
#ifndef _MODELCLASS_H_
|
||||
#define _MODELCLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <directxmath.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <WICTextureLoader.h>
|
||||
using namespace DirectX;
|
||||
using namespace std;
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "textureclass.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: ModelClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class ModelClass
|
||||
{
|
||||
protected:
|
||||
|
||||
struct VertexType
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT2 texture;
|
||||
XMFLOAT3 normal;
|
||||
XMFLOAT3 tangent;
|
||||
XMFLOAT3 binormal;
|
||||
};
|
||||
|
||||
struct ModelType
|
||||
{
|
||||
float x, y, z;
|
||||
float tu, tv;
|
||||
float nx, ny, nz;
|
||||
float tx, ty, tz;
|
||||
float bx, by, bz;
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
struct Texture {
|
||||
float u, v;
|
||||
};
|
||||
|
||||
struct Normal {
|
||||
float nx, ny, nz;
|
||||
};
|
||||
|
||||
struct TempVertexType
|
||||
{
|
||||
float x, y, z;
|
||||
float tu, tv;
|
||||
float nx, ny, nz;
|
||||
};
|
||||
|
||||
struct VectorType
|
||||
{
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
struct Face {
|
||||
int v1, v2, v3;
|
||||
int t1, t2, t3;
|
||||
int n1, n2, n3;
|
||||
};
|
||||
|
||||
public:
|
||||
ModelClass();
|
||||
ModelClass(const ModelClass&);
|
||||
~ModelClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, std::vector<ID3D11ShaderResourceView*>);
|
||||
|
||||
void Shutdown();
|
||||
void Render(ID3D11DeviceContext*);
|
||||
|
||||
int GetIndexCount();
|
||||
ID3D11ShaderResourceView* GetTexture(int index) const;
|
||||
bool ChangeTexture(ID3D11Device*, ID3D11DeviceContext*, std::wstring filename, int index);
|
||||
|
||||
private:
|
||||
bool InitializeBuffers(ID3D11Device*);
|
||||
void ShutdownBuffers();
|
||||
void RenderBuffers(ID3D11DeviceContext*);
|
||||
bool LoadTextures(ID3D11Device*, ID3D11DeviceContext*, vector<string> filename);
|
||||
void ReleaseTextures();
|
||||
|
||||
bool LoadModel(char*);
|
||||
bool LoadObjModel(char*);
|
||||
bool LoadTxtModel(char*);
|
||||
void ReleaseModel();
|
||||
|
||||
void CalculateModelVectors();
|
||||
void CalculateTangentBinormal(TempVertexType, TempVertexType, TempVertexType, VectorType&, VectorType&);
|
||||
|
||||
private:
|
||||
ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
|
||||
int m_vertexCount, m_indexCount;
|
||||
std::vector<ID3D11ShaderResourceView*> m_Textures;
|
||||
ModelType* m_model;
|
||||
};
|
||||
|
||||
#endif
|
||||
112
enginecustom/src/inc/system/object.h
Normal file
112
enginecustom/src/inc/system/object.h
Normal file
@@ -0,0 +1,112 @@
|
||||
#pragma once
|
||||
#include "modelclass.h"
|
||||
#include <WICTextureLoader.h>
|
||||
#include <SimpleMath.h>
|
||||
|
||||
enum class ObjectType
|
||||
{
|
||||
Sphere,
|
||||
Cube,
|
||||
Unknown
|
||||
};
|
||||
|
||||
class Object : public ModelClass
|
||||
{
|
||||
public:
|
||||
Object();
|
||||
~Object();
|
||||
|
||||
void SetScaleMatrix(XMMATRIX scaleMatrix);
|
||||
void SetRotateMatrix(XMMATRIX rotateMatrix);
|
||||
void SetTranslateMatrix(XMMATRIX translateMatrix);
|
||||
void SetSRMatrix(XMMATRIX srMatrix);
|
||||
void SetWorldMatrix(XMMATRIX worldMatrix);
|
||||
|
||||
void SetPosition(XMVECTOR position);
|
||||
void SetRotation(XMVECTOR rotation);
|
||||
void SetScale(XMVECTOR scale);
|
||||
|
||||
XMMATRIX GetScaleMatrix() const;
|
||||
XMMATRIX GetRotateMatrix() const;
|
||||
XMMATRIX GetTranslateMatrix() const;
|
||||
XMMATRIX GetSRMatrix() const;
|
||||
XMMATRIX GetWorldMatrix() const;
|
||||
|
||||
XMVECTOR GetPosition();
|
||||
XMVECTOR GetRotation();
|
||||
XMVECTOR GetScale();
|
||||
|
||||
void SetVelocity(XMVECTOR);
|
||||
void AddVelocity(float deltaTime);
|
||||
XMVECTOR GetVelocity() const;
|
||||
void SetAcceleration(XMVECTOR);
|
||||
XMVECTOR GetAcceleration() const;
|
||||
void SetMass(float);
|
||||
float GetMass() const;
|
||||
void SetGrounded(bool);
|
||||
bool IsGrounded() const;
|
||||
bool IsPhysicsEnabled() const;
|
||||
void SetPhysicsEnabled(bool state);
|
||||
|
||||
|
||||
void UpdateWorldMatrix();
|
||||
void UpdateSRMatrix();
|
||||
void UpdateScaleMatrix();
|
||||
void UpdateRotateMatrix();
|
||||
void UpdateTranslateMatrix();
|
||||
|
||||
void UpdatePosition(float deltaTime);
|
||||
|
||||
void Update();
|
||||
|
||||
std::string GetName();
|
||||
void SetName(std::string name);
|
||||
int SetId(int id);
|
||||
int GetId() const;
|
||||
void SetType(ObjectType type) { m_type = type; };
|
||||
ObjectType GetType() const { return m_type; };
|
||||
|
||||
|
||||
enum ShaderType
|
||||
{
|
||||
CEL_SHADING,
|
||||
LIGHTING,
|
||||
NORMAL_MAPPING,
|
||||
SPECULAR_MAPPING,
|
||||
REFLECTION,
|
||||
REFRACTION,
|
||||
TEXTURE,
|
||||
SKYBOX,
|
||||
SUNLIGHT
|
||||
};
|
||||
|
||||
ShaderType GetActiveShader() const { return m_activeShader; };
|
||||
void SetActiveShader(ShaderType activeShader) { m_activeShader = activeShader; };
|
||||
|
||||
float GetBoundingRadius() const;
|
||||
|
||||
public :
|
||||
bool m_demoSpinning = false;
|
||||
XMVECTOR m_previousPosition;
|
||||
XMVECTOR m_velocity;
|
||||
int m_id;
|
||||
|
||||
private:
|
||||
XMMATRIX m_scaleMatrix;
|
||||
XMMATRIX m_rotateMatrix;
|
||||
XMMATRIX m_translateMatrix;
|
||||
XMMATRIX m_srMatrix;
|
||||
XMMATRIX m_worldMatrix;
|
||||
|
||||
XMVECTOR m_acceleration;
|
||||
float m_mass;
|
||||
bool m_isGrounded;
|
||||
bool m_isPhysicsEnabled;
|
||||
|
||||
std::string m_name;
|
||||
ObjectType m_type = ObjectType::Unknown;
|
||||
|
||||
ShaderType m_activeShader = LIGHTING;
|
||||
|
||||
float m_boundingRadius;
|
||||
};
|
||||
27
enginecustom/src/inc/system/physics.h
Normal file
27
enginecustom/src/inc/system/physics.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef _PHYSICS_H_
|
||||
#define _PHYSICS_H_
|
||||
|
||||
#include "object.h"
|
||||
#include "math.h"
|
||||
|
||||
class Physics : public Object
|
||||
{
|
||||
public:
|
||||
Physics();
|
||||
explicit Physics(const Physics&); // Use explicit to avoid implicit conversion
|
||||
~Physics();
|
||||
|
||||
XMVECTOR GetGravity() const; // Get the gravity value
|
||||
void SetGravity(XMVECTOR gravity); // Define the gravity value
|
||||
void ApplyGravity(Object*, float); // Apply gravity to an object
|
||||
void AddForce(Object*, XMVECTOR);
|
||||
bool IsColliding(Object*, Object*);
|
||||
bool CubesOverlap(Object*, Object*);
|
||||
bool SpheresOverlap(Object*, Object*);
|
||||
bool SphereCubeOverlap(Object*, Object*);
|
||||
|
||||
private:
|
||||
XMVECTOR m_gravity;
|
||||
};
|
||||
|
||||
#endif
|
||||
53
enginecustom/src/inc/system/rendertextureclass.h
Normal file
53
enginecustom/src/inc/system/rendertextureclass.h
Normal file
@@ -0,0 +1,53 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: rendertextureclass.h
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef _RENDERTEXTURECLASS_H_
|
||||
#define _RENDERTEXTURECLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <directxmath.h>
|
||||
using namespace DirectX;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: RenderTextureClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class RenderTextureClass
|
||||
{
|
||||
public:
|
||||
RenderTextureClass();
|
||||
RenderTextureClass(const RenderTextureClass&);
|
||||
~RenderTextureClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, int, int, float, float, int);
|
||||
void Shutdown();
|
||||
|
||||
void SetRenderTarget(ID3D11DeviceContext*);
|
||||
void ClearRenderTarget(ID3D11DeviceContext*, float, float, float, float);
|
||||
ID3D11ShaderResourceView* GetShaderResourceView();
|
||||
|
||||
void GetProjectionMatrix(XMMATRIX&);
|
||||
void GetOrthoMatrix(XMMATRIX&);
|
||||
|
||||
int GetTextureWidth();
|
||||
int GetTextureHeight();
|
||||
|
||||
private:
|
||||
int m_textureWidth, m_textureHeight;
|
||||
ID3D11Texture2D* m_renderTargetTexture;
|
||||
ID3D11RenderTargetView* m_renderTargetView;
|
||||
ID3D11ShaderResourceView* m_shaderResourceView;
|
||||
ID3D11Texture2D* m_depthStencilBuffer;
|
||||
ID3D11DepthStencilView* m_depthStencilView;
|
||||
D3D11_VIEWPORT m_viewport;
|
||||
XMMATRIX m_projectionMatrix;
|
||||
XMMATRIX m_orthoMatrix;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
65
enginecustom/src/inc/system/systemclass.h
Normal file
65
enginecustom/src/inc/system/systemclass.h
Normal file
@@ -0,0 +1,65 @@
|
||||
#ifndef _SYSTEMCLASS_H_
|
||||
#define _SYSTEMCLASS_H_
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
#include "inputclass.h"
|
||||
#include "applicationclass.h"
|
||||
#include "imguiManager.h"
|
||||
#include <mutex>
|
||||
#include "../resources.h"
|
||||
#include <chrono>
|
||||
|
||||
class SystemClass
|
||||
{
|
||||
public:
|
||||
SystemClass();
|
||||
SystemClass(const SystemClass&);
|
||||
~SystemClass();
|
||||
|
||||
bool Initialize();
|
||||
void Shutdown();
|
||||
void Run();
|
||||
|
||||
LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);
|
||||
|
||||
void SendPath(wchar_t* path, std::filesystem::path WFolder);
|
||||
|
||||
private:
|
||||
bool Frame();
|
||||
void InitializeWindows(int&, int&);
|
||||
void ShutdownWindows();
|
||||
|
||||
private:
|
||||
LPCWSTR m_applicationName;
|
||||
HINSTANCE m_hinstance;
|
||||
HWND m_hwnd;
|
||||
|
||||
InputClass* m_Input;
|
||||
ApplicationClass* m_Application;
|
||||
imguiManager* m_imguiManager;
|
||||
|
||||
int m_initialWindowWidth;
|
||||
int m_initialWindowHeight;
|
||||
bool m_isDirect3DInitialized;
|
||||
bool m_isResizing = false;
|
||||
|
||||
std::mutex renderMutex;
|
||||
};
|
||||
|
||||
|
||||
/////////////////////////
|
||||
// FUNCTION PROTOTYPES //
|
||||
/////////////////////////
|
||||
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
|
||||
|
||||
|
||||
/////////////
|
||||
// GLOBALS //
|
||||
/////////////
|
||||
static SystemClass* ApplicationHandle = 0;
|
||||
|
||||
|
||||
#endif
|
||||
48
enginecustom/src/inc/system/textclass.h
Normal file
48
enginecustom/src/inc/system/textclass.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef _TEXTCLASS_H_
|
||||
#define _TEXTCLASS_H_
|
||||
|
||||
|
||||
///////////////////////
|
||||
// MY CLASS INCLUDES //
|
||||
///////////////////////
|
||||
#include "fontclass.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: TextClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class TextClass
|
||||
{
|
||||
private:
|
||||
struct VertexType
|
||||
{
|
||||
XMFLOAT3 position;
|
||||
XMFLOAT2 texture;
|
||||
};
|
||||
|
||||
public:
|
||||
TextClass();
|
||||
TextClass(const TextClass&);
|
||||
~TextClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, int, int, int, FontClass*, char*, int, int, float, float, float);
|
||||
void Shutdown();
|
||||
void Render(ID3D11DeviceContext*);
|
||||
|
||||
int GetIndexCount();
|
||||
|
||||
bool UpdateText(ID3D11DeviceContext*, FontClass*, char*, int, int, float, float, float);
|
||||
XMFLOAT4 GetPixelColor();
|
||||
|
||||
private:
|
||||
bool InitializeBuffers(ID3D11Device*, ID3D11DeviceContext*, FontClass*, char*, int, int, float, float, float);
|
||||
void ShutdownBuffers();
|
||||
void RenderBuffers(ID3D11DeviceContext*);
|
||||
|
||||
private:
|
||||
ID3D11Buffer* m_vertexBuffer, * m_indexBuffer;
|
||||
int m_screenWidth, m_screenHeight, m_maxLength, m_vertexCount, m_indexCount;
|
||||
XMFLOAT4 m_pixelColor;
|
||||
};
|
||||
|
||||
#endif
|
||||
52
enginecustom/src/inc/system/textureclass.h
Normal file
52
enginecustom/src/inc/system/textureclass.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef _TEXTURECLASS_H_
|
||||
#define _TEXTURECLASS_H_
|
||||
|
||||
|
||||
//////////////
|
||||
// INCLUDES //
|
||||
//////////////
|
||||
#include "Logger.h"
|
||||
#include <d3d11.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Class name: TextureClass
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
class TextureClass
|
||||
{
|
||||
private:
|
||||
struct TargaHeader
|
||||
{
|
||||
unsigned char data1[12];
|
||||
unsigned short width;
|
||||
unsigned short height;
|
||||
unsigned char bpp;
|
||||
unsigned char data2;
|
||||
};
|
||||
|
||||
public:
|
||||
TextureClass();
|
||||
TextureClass(const TextureClass&);
|
||||
~TextureClass();
|
||||
|
||||
bool Initialize(ID3D11Device*, ID3D11DeviceContext*, std::string);
|
||||
void Shutdown();
|
||||
|
||||
ID3D11ShaderResourceView* GetTexture();
|
||||
|
||||
int GetWidth();
|
||||
int GetHeight();
|
||||
|
||||
private:
|
||||
bool LoadTarga(std::string);
|
||||
|
||||
private:
|
||||
unsigned char* m_targaData;
|
||||
ID3D11Texture2D* m_texture;
|
||||
ID3D11ShaderResourceView* m_textureView;
|
||||
int m_width, m_height;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
410
enginecustom/src/src/shader/CelShadingShader.cpp
Normal file
410
enginecustom/src/src/shader/CelShadingShader.cpp
Normal file
@@ -0,0 +1,410 @@
|
||||
#include "CelShadingShader.h"
|
||||
#include <iostream>
|
||||
|
||||
CelShadingShader::CelShadingShader()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_lightBuffer = 0;
|
||||
}
|
||||
|
||||
CelShadingShader::CelShadingShader(const CelShadingShader& other)
|
||||
{
|
||||
}
|
||||
|
||||
CelShadingShader::~CelShadingShader()
|
||||
{
|
||||
}
|
||||
|
||||
bool CelShadingShader::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing CelShadingShader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/celshading.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the filename of the vertex shader", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/celshading.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the filename of the pixel shader", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize the vertex and pixel shaders", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Successfully initialized CelShadingShader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CelShadingShader::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
}
|
||||
|
||||
bool CelShadingShader::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 lightPosition)
|
||||
{
|
||||
bool result;
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor, lightPosition);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("CelShading Error", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CelShadingShader::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage = nullptr;
|
||||
ID3D10Blob* vertexShaderBuffer = nullptr;
|
||||
ID3D10Blob* pixelShaderBuffer = nullptr;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC lightBufferDesc;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, nullptr, nullptr, "CelShadingVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have written something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, nullptr, nullptr, "CelShadingPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have written something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), nullptr, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), nullptr, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
// This setup needs to match the VertexType structure in the ModelClass and in the shader.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "NORMAL";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "TEXCOORD";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = nullptr;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = nullptr;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&matrixBufferDesc, nullptr, &m_matrixBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the light dynamic constant buffer that is in the pixel shader.
|
||||
lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
||||
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightBufferDesc.MiscFlags = 0;
|
||||
lightBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&lightBufferDesc, nullptr, &m_lightBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void CelShadingShader::ShutdownShader()
|
||||
{
|
||||
// Release the light constant buffer.
|
||||
if (m_lightBuffer)
|
||||
{
|
||||
m_lightBuffer->Release();
|
||||
m_lightBuffer = nullptr;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = nullptr;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = nullptr;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = nullptr;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = nullptr;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void CelShadingShader::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long bufferSize, i;
|
||||
std::ofstream fout;
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = nullptr;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
}
|
||||
|
||||
|
||||
bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 lightPosition)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
LightBufferType* dataPtr2;
|
||||
unsigned int bufferNumber;
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr2 = (LightBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the lighting variables into the constant buffer.
|
||||
dataPtr2->diffuseColor = diffuseColor;
|
||||
dataPtr2->lightDirection = lightDirection;
|
||||
dataPtr2->lightPosition = lightPosition;
|
||||
dataPtr2->constantAttenuation = 0.5f; // Set your attenuation values here
|
||||
dataPtr2->linearAttenuation = 0.1f;
|
||||
dataPtr2->quadraticAttenuation = 0.01f;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightBuffer, 0);
|
||||
|
||||
// Set the position of the light constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the light constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void CelShadingShader::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, nullptr, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, nullptr, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
}
|
||||
357
enginecustom/src/src/shader/Colorshaderclass.cpp
Normal file
357
enginecustom/src/src/shader/Colorshaderclass.cpp
Normal file
@@ -0,0 +1,357 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: colorshaderclass.cpp
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include "colorshaderclass.h"
|
||||
|
||||
ColorShaderClass::ColorShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
ColorShaderClass::ColorShaderClass(const ColorShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ColorShaderClass::~ColorShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool ColorShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing ColorShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/Color.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/Color.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("ColorShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ColorShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool ColorShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error setting shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ColorShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "ColorVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "ColorPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
// This setup needs to match the VertexType stucture in the ModelClass and in the shader.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "COLOR";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ColorShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
Logger::Get().Log("Releasing matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
Logger::Get().Log("Matrix buffer released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
Logger::Get().Log("Releasing layout", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
Logger::Get().Log("Layout released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
Logger::Get().Log("Releasing pixel shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
Logger::Get().Log("Pixel shader released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
Logger::Get().Log("Releasing vertex shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
Logger::Get().Log("Vertex shader released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader shut down", __FILE__, __LINE__);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ColorShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool ColorShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix)
|
||||
{
|
||||
Logger::Get().Log("Setting shader parameters", __FILE__, __LINE__);
|
||||
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finanly set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ColorShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
395
enginecustom/src/src/shader/Multitextureshaderclass.cpp
Normal file
395
enginecustom/src/src/shader/Multitextureshaderclass.cpp
Normal file
@@ -0,0 +1,395 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: multitextureshaderclass.cpp
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include "multitextureshaderclass.h"
|
||||
|
||||
|
||||
MultiTextureShaderClass::MultiTextureShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
|
||||
MultiTextureShaderClass::MultiTextureShaderClass(const MultiTextureShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
MultiTextureShaderClass::~MultiTextureShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool MultiTextureShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing MultiTextureShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/multitexture.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the filename of the vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/multitexture.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the filename of the pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize the vertex and pixel shaders", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("MultiTextureShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultiTextureShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool MultiTextureShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the shader parameters that it will use for rendering", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MultiTextureShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing the shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "MultiTextureVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile the vertex shader code", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "MultiTexturePixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile the pixel shader code", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the vertex shader from the buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the pixel shader from the buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the vertex input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the constant buffer pointer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the texture sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultiTextureShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down the shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void MultiTextureShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool MultiTextureShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to lock the constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resources in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture1);
|
||||
deviceContext->PSSetShaderResources(1, 1, &texture2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MultiTextureShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
410
enginecustom/src/src/shader/alphamapshaderclass.cpp
Normal file
410
enginecustom/src/src/shader/alphamapshaderclass.cpp
Normal file
@@ -0,0 +1,410 @@
|
||||
#include "alphamapshaderclass.h"
|
||||
|
||||
|
||||
AlphaMapShaderClass::AlphaMapShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
|
||||
AlphaMapShaderClass::AlphaMapShaderClass(const AlphaMapShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AlphaMapShaderClass::~AlphaMapShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool AlphaMapShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing AlphaMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/alphamap.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string ", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/alphamap.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void AlphaMapShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool AlphaMapShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2, texture3);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error setting shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool AlphaMapShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "AlphaMapVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "AlphaMapPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void AlphaMapShaderClass::ShutdownShader()
|
||||
{
|
||||
|
||||
Logger::Get().Log("Shutting down shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
Logger::Get().Log("Releasing sampler state", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
Logger::Get().Log("Sampler state released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
Logger::Get().Log("Releasing constant buffer", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
Logger::Get().Log("Constant buffer released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
Logger::Get().Log("Releasing layout", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
Logger::Get().Log("Layout released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
Logger::Get().Log("Releasing pixel shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
Logger::Get().Log("Pixel shader released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
Logger::Get().Log("Releasing vertex shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
Logger::Get().Log("Vertex shader released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader shutdown complete", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void AlphaMapShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool AlphaMapShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resources in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture1);
|
||||
deviceContext->PSSetShaderResources(1, 1, &texture2);
|
||||
deviceContext->PSSetShaderResources(2, 1, &texture3);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void AlphaMapShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
433
enginecustom/src/src/shader/fontshaderclass.cpp
Normal file
433
enginecustom/src/src/shader/fontshaderclass.cpp
Normal file
@@ -0,0 +1,433 @@
|
||||
#include "fontshaderclass.h"
|
||||
|
||||
|
||||
FontShaderClass::FontShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_pixelBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
FontShaderClass::FontShaderClass(const FontShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
FontShaderClass::~FontShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool FontShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing FontShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/font.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/font.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("FontShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FontShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool FontShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 pixelColor)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, pixelColor);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error setting shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FontShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC pixelBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "FontVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "FontPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the dynamic pixel constant buffer that is in the pixel shader.
|
||||
pixelBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
pixelBufferDesc.ByteWidth = sizeof(PixelBufferType);
|
||||
pixelBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
pixelBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
pixelBufferDesc.MiscFlags = 0;
|
||||
pixelBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the pixel constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&pixelBufferDesc, NULL, &m_pixelBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FontShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the pixel constant buffer.
|
||||
if (m_pixelBuffer)
|
||||
{
|
||||
m_pixelBuffer->Release();
|
||||
m_pixelBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void FontShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool FontShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 pixelColor)
|
||||
{
|
||||
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
PixelBufferType* dataPtr2;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
// Lock the pixel constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_pixelBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the pixel constant buffer.
|
||||
dataPtr2 = (PixelBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the pixel color into the pixel constant buffer.
|
||||
dataPtr2->pixelColor = pixelColor;
|
||||
|
||||
// Unlock the pixel constant buffer.
|
||||
deviceContext->Unmap(m_pixelBuffer, 0);
|
||||
|
||||
// Set the position of the pixel constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Now set the pixel constant buffer in the pixel shader with the updated value.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_pixelBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FontShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
398
enginecustom/src/src/shader/lightmapshaderclass.cpp
Normal file
398
enginecustom/src/src/shader/lightmapshaderclass.cpp
Normal file
@@ -0,0 +1,398 @@
|
||||
#include "lightmapshaderclass.h"
|
||||
|
||||
|
||||
LightMapShaderClass::LightMapShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
|
||||
LightMapShaderClass::LightMapShaderClass(const LightMapShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
LightMapShaderClass::~LightMapShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool LightMapShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing LightMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/lightmap.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/lightmap.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("LightMapShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void LightMapShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool LightMapShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error setting shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool LightMapShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "LightMapVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "LightMapPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void LightMapShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down LightMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("LightMapShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void LightMapShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool LightMapShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resources in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture1);
|
||||
deviceContext->PSSetShaderResources(1, 1, &texture2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void LightMapShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
544
enginecustom/src/src/shader/lightshaderclass.cpp
Normal file
544
enginecustom/src/src/shader/lightshaderclass.cpp
Normal file
@@ -0,0 +1,544 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: lightshaderclass.cpp
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include "lightshaderclass.h"
|
||||
|
||||
|
||||
LightShaderClass::LightShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_sampleState = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_cameraBuffer = 0;
|
||||
m_lightBuffer = 0;
|
||||
m_lightColorBuffer = 0;
|
||||
m_lightPositionBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
LightShaderClass::LightShaderClass(const LightShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
LightShaderClass::~LightShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
bool result;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/light.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/light.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("LightShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void LightShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 ambientClor[])
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, lightPosition, ambientClor);
|
||||
if(!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool LightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
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 lightColorBufferDesc;
|
||||
D3D11_BUFFER_DESC lightPositionBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// 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 the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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 the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
// This setup needs to match the VertexType stucture in the ModelClass and in the shader.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(),
|
||||
&m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create camera buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the dynamic constant buffer that is in the pixel shader.
|
||||
lightColorBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightColorBufferDesc.ByteWidth = sizeof(LightColorBufferType);
|
||||
lightColorBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightColorBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightColorBufferDesc.MiscFlags = 0;
|
||||
lightColorBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&lightColorBufferDesc, NULL, &m_lightColorBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create light color buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the dynamic constant buffer that is in the vertex shader.
|
||||
lightPositionBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightPositionBufferDesc.ByteWidth = sizeof(LightPositionBufferType);
|
||||
lightPositionBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightPositionBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightPositionBufferDesc.MiscFlags = 0;
|
||||
lightPositionBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&lightPositionBufferDesc, NULL, &m_lightPositionBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create light position buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void LightShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the light constant buffers.
|
||||
if (m_lightColorBuffer)
|
||||
{
|
||||
m_lightColorBuffer->Release();
|
||||
m_lightColorBuffer = 0;
|
||||
}
|
||||
|
||||
if (m_lightPositionBuffer)
|
||||
{
|
||||
m_lightPositionBuffer->Release();
|
||||
m_lightPositionBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the light constant buffer.
|
||||
if (m_lightBuffer)
|
||||
{
|
||||
m_lightBuffer->Release();
|
||||
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)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("LightShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned __int64 bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 ambientColor[])
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
unsigned int bufferNumber;
|
||||
MatrixBufferType* dataPtr;
|
||||
LightPositionBufferType* dataPtr2;
|
||||
LightColorBufferType* dataPtr3;
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to map matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to map camera buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Lock the light position constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_lightPositionBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to map light position buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr2 = (LightPositionBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the light position variables into the constant buffer.
|
||||
for (int i = 0; i < NUM_LIGHTS; i++)
|
||||
{
|
||||
dataPtr2->lightPosition[i] = lightPosition[i];
|
||||
}
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightPositionBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 1;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_lightPositionBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
// Lock the light color constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_lightColorBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to map light color buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr3 = (LightColorBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the light color variables into the constant buffer.
|
||||
for (int i = 0; i < NUM_LIGHTS; i++)
|
||||
{
|
||||
dataPtr3->diffuseColor[i] = diffuseColor[i];
|
||||
}
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightColorBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightColorBuffer);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void LightShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
469
enginecustom/src/src/shader/normalmapshaderclass.cpp
Normal file
469
enginecustom/src/src/shader/normalmapshaderclass.cpp
Normal file
@@ -0,0 +1,469 @@
|
||||
#include "normalmapshaderclass.h"
|
||||
|
||||
|
||||
NormalMapShaderClass::NormalMapShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_lightBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
NormalMapShaderClass::NormalMapShaderClass(const NormalMapShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NormalMapShaderClass::~NormalMapShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool NormalMapShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing normal map shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/normalmap.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the filename of the vertex shader", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/normalmap.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the filename of the pixel shader", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize the vertex and pixel shaders", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Successfully initialized normal map shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void NormalMapShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool NormalMapShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2, lightDirection, diffuseColor);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to set the shader parameters that will be used for rendering", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool NormalMapShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing normal map shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[5];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC lightBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "NormalMapVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile the vertex shader code", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "NormalMapPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile the pixel shader code", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the vertex shader from the buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the pixel shader from the buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[3].SemanticName = "TANGENT";
|
||||
polygonLayout[3].SemanticIndex = 0;
|
||||
polygonLayout[3].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[3].InputSlot = 0;
|
||||
polygonLayout[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[3].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[3].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[4].SemanticName = "BINORMAL";
|
||||
polygonLayout[4].SemanticIndex = 0;
|
||||
polygonLayout[4].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[4].InputSlot = 0;
|
||||
polygonLayout[4].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[4].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[4].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the vertex input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the constant buffer pointer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the texture sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the light dynamic constant buffer that is in the pixel shader.
|
||||
lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
||||
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightBufferDesc.MiscFlags = 0;
|
||||
lightBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create the light constant buffer pointer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Successfully initialized normal map shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void NormalMapShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down normal map shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the light constant buffer.
|
||||
if (m_lightBuffer)
|
||||
{
|
||||
m_lightBuffer->Release();
|
||||
m_lightBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Successfully shut down normal map shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void NormalMapShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool NormalMapShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
LightBufferType* dataPtr2;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to lock the constant buffer so it can be written to", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resources in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture1);
|
||||
deviceContext->PSSetShaderResources(1, 1, &texture2);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to lock the light constant buffer so it can be written to", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr2 = (LightBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the lighting variables into the constant buffer.
|
||||
dataPtr2->diffuseColor = diffuseColor;
|
||||
dataPtr2->lightDirection = lightDirection;
|
||||
dataPtr2->padding = 0.0f;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightBuffer, 0);
|
||||
|
||||
// Set the position of the light constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the light constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
|
||||
|
||||
// Set shader texture resources in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture1);
|
||||
deviceContext->PSSetShaderResources(1, 1, &texture2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void NormalMapShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
434
enginecustom/src/src/shader/reflectionshaderclass.cpp
Normal file
434
enginecustom/src/src/shader/reflectionshaderclass.cpp
Normal file
@@ -0,0 +1,434 @@
|
||||
#include "reflectionshaderclass.h"
|
||||
|
||||
ReflectionShaderClass::ReflectionShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_reflectionBuffer = 0;
|
||||
}
|
||||
|
||||
ReflectionShaderClass::ReflectionShaderClass(const ReflectionShaderClass& other)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ReflectionShaderClass::~ReflectionShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool ReflectionShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing reflection shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/reflection.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/reflection.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Reflection shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ReflectionShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool ReflectionShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, ID3D11ShaderResourceView* reflectionTexture, XMMATRIX reflectionMatrix)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, reflectionTexture, reflectionMatrix);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error setting shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReflectionShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing reflection shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC reflectionBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "ReflectionVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "ReflectionPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
// Setup the description of the reflection dynamic constant buffer that is in the vertex shader.
|
||||
reflectionBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
reflectionBufferDesc.ByteWidth = sizeof(ReflectionBufferType);
|
||||
reflectionBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
reflectionBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
reflectionBufferDesc.MiscFlags = 0;
|
||||
reflectionBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&reflectionBufferDesc, NULL, &m_reflectionBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Reflection shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ReflectionShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down reflection shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the reflection constant buffer.
|
||||
if (m_reflectionBuffer)
|
||||
{
|
||||
m_reflectionBuffer->Release();
|
||||
m_reflectionBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Reflection shader shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void ReflectionShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool ReflectionShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, ID3D11ShaderResourceView* reflectionTexture, XMMATRIX reflectionMatrix)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
ReflectionBufferType* dataPtr2;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// Transpose the relfection matrix to prepare it for the shader.
|
||||
reflectionMatrix = XMMatrixTranspose(reflectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Lock the reflection constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_reflectionBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the matrix constant buffer.
|
||||
dataPtr2 = (ReflectionBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrix into the reflection constant buffer.
|
||||
dataPtr2->reflectionMatrix = reflectionMatrix;
|
||||
|
||||
// Unlock the reflection constant buffer.
|
||||
deviceContext->Unmap(m_reflectionBuffer, 0);
|
||||
|
||||
// Set the position of the reflection constant buffer in the vertex shader.
|
||||
bufferNumber = 1;
|
||||
|
||||
// Now set the reflection constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_reflectionBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
// Set the reflection texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(1, 1, &reflectionTexture);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ReflectionShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render the geometry.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the geometry.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
473
enginecustom/src/src/shader/refractionshaderclass.cpp
Normal file
473
enginecustom/src/src/shader/refractionshaderclass.cpp
Normal file
@@ -0,0 +1,473 @@
|
||||
#include "refractionshaderclass.h"
|
||||
|
||||
|
||||
RefractionShaderClass::RefractionShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_lightBuffer = 0;
|
||||
m_clipPlaneBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
RefractionShaderClass::RefractionShaderClass(const RefractionShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
RefractionShaderClass::~RefractionShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool RefractionShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/refraction.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/refraction.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void RefractionShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool RefractionShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor[], XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 clipPlane)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor, lightPosition, clipPlane);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool RefractionShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC lightBufferDesc;
|
||||
D3D11_BUFFER_DESC clipPlaneBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "RefractionVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "RefractionPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
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.
|
||||
lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
||||
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightBufferDesc.MiscFlags = 0;
|
||||
lightBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the clip plane dynamic constant buffer that is in the vertex shader.
|
||||
clipPlaneBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
clipPlaneBufferDesc.ByteWidth = sizeof(ClipPlaneBufferType);
|
||||
clipPlaneBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
clipPlaneBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
clipPlaneBufferDesc.MiscFlags = 0;
|
||||
clipPlaneBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&clipPlaneBufferDesc, NULL, &m_clipPlaneBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void RefractionShaderClass::ShutdownShader()
|
||||
{
|
||||
// Release the clip plane constant buffer.
|
||||
if (m_clipPlaneBuffer)
|
||||
{
|
||||
m_clipPlaneBuffer->Release();
|
||||
m_clipPlaneBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the light constant buffer.
|
||||
if (m_lightBuffer)
|
||||
{
|
||||
m_lightBuffer->Release();
|
||||
m_lightBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void RefractionShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool RefractionShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor[], XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 clipPlane)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
ClipPlaneBufferType* dataPtr2;
|
||||
LightBufferType* dataPtr3;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Lock the clip plane constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_clipPlaneBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if(FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the clip plane constant buffer.
|
||||
dataPtr2 = (ClipPlaneBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the clip plane into the clip plane constant buffer.
|
||||
dataPtr2->clipPlane = clipPlane;
|
||||
|
||||
// Unlock the buffer.
|
||||
deviceContext->Unmap(m_clipPlaneBuffer, 0);
|
||||
|
||||
// Set the position of the clip plane constant buffer in the vertex shader.
|
||||
bufferNumber = 1;
|
||||
|
||||
// Now set the clip plane constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_clipPlaneBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr3 = (LightBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the lighting variables into the constant buffer.
|
||||
dataPtr3->ambientColor = ambientColor[0];
|
||||
dataPtr3->diffuseColor = diffuseColor[0];
|
||||
dataPtr3->lightPosition = lightPosition[0];
|
||||
dataPtr3->lightDirection = lightDirection;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightBuffer, 0);
|
||||
|
||||
// Set the position of the light constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the light constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void RefractionShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render the geometry.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the geometry.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
450
enginecustom/src/src/shader/shadermanagerclass.cpp
Normal file
450
enginecustom/src/src/shader/shadermanagerclass.cpp
Normal file
@@ -0,0 +1,450 @@
|
||||
#include "shadermanagerclass.h"
|
||||
|
||||
ShaderManagerClass::ShaderManagerClass()
|
||||
{
|
||||
m_TextureShader = 0;
|
||||
m_NormalMapShader = 0;
|
||||
m_MultitextureShader = 0;
|
||||
m_TranslateShader = 0;
|
||||
m_AlphaMapShader = 0;
|
||||
m_SpecMapShader = 0;
|
||||
m_TransparentShader = 0;
|
||||
m_LightShader = 0;
|
||||
m_LightMapShader = 0;
|
||||
m_RefractionShader = 0;
|
||||
m_WaterShader = 0;
|
||||
m_CelShadingShader = 0;
|
||||
}
|
||||
|
||||
ShaderManagerClass::ShaderManagerClass(const ShaderManagerClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
ShaderManagerClass::~ShaderManagerClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing ShaderManagerClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
|
||||
// Create and initialize the texture shader object.
|
||||
m_TextureShader = new TextureShaderClass;
|
||||
result = m_TextureShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing TextureShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the normal map shader object.
|
||||
m_NormalMapShader = new NormalMapShaderClass;
|
||||
result = m_NormalMapShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing NormalMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the multitexture shader object.
|
||||
m_MultitextureShader = new MultiTextureShaderClass;
|
||||
result = m_MultitextureShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing MultiTextureShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the translate shader object.
|
||||
m_TranslateShader = new TranslateShaderClass;
|
||||
result = m_TranslateShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing TranslateShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the alpha map shader object.
|
||||
m_AlphaMapShader = new AlphaMapShaderClass;
|
||||
result = m_AlphaMapShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing AlphaMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the specular map shader object.
|
||||
m_SpecMapShader = new SpecMapShaderClass;
|
||||
result = m_SpecMapShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing SpecMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the transparent shader object.
|
||||
m_TransparentShader = new TransparentShaderClass;
|
||||
result = m_TransparentShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing TransparentShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the light shader object.
|
||||
m_LightShader = new LightShaderClass;
|
||||
result = m_LightShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the light map shader object.
|
||||
m_LightMapShader = new LightMapShaderClass;
|
||||
result = m_LightMapShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing LightMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the refraction shader object.
|
||||
m_RefractionShader = new RefractionShaderClass;
|
||||
result = m_RefractionShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the water shader object.
|
||||
m_WaterShader = new WaterShaderClass;
|
||||
result = m_WaterShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_CelShadingShader = new CelShadingShader;
|
||||
result = m_CelShadingShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_SunlightShader = new SunlightShaderClass;
|
||||
result = m_SunlightShader->Initialize(device, hwnd);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("ShaderManagerClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ShaderManagerClass::Shutdown()
|
||||
{
|
||||
Logger::Get().Log("Shutting down ShaderManagerClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the normal map shader object.
|
||||
if (m_NormalMapShader)
|
||||
{
|
||||
m_NormalMapShader->Shutdown();
|
||||
delete m_NormalMapShader;
|
||||
m_NormalMapShader = 0;
|
||||
}
|
||||
|
||||
// Release the texture shader object.
|
||||
if (m_TextureShader)
|
||||
{
|
||||
m_TextureShader->Shutdown();
|
||||
delete m_TextureShader;
|
||||
m_TextureShader = 0;
|
||||
}
|
||||
|
||||
// Release the multitexture shader object.
|
||||
if (m_MultitextureShader)
|
||||
{
|
||||
m_MultitextureShader->Shutdown();
|
||||
delete m_MultitextureShader;
|
||||
m_MultitextureShader = 0;
|
||||
}
|
||||
|
||||
// Release the translate shader object.
|
||||
if (m_TranslateShader)
|
||||
{
|
||||
m_TranslateShader->Shutdown();
|
||||
delete m_TranslateShader;
|
||||
m_TranslateShader = 0;
|
||||
}
|
||||
|
||||
// Release the alpha map shader object.
|
||||
if (m_AlphaMapShader)
|
||||
{
|
||||
m_AlphaMapShader->Shutdown();
|
||||
delete m_AlphaMapShader;
|
||||
m_AlphaMapShader = 0;
|
||||
}
|
||||
|
||||
// Release the specular map shader object.
|
||||
if (m_SpecMapShader)
|
||||
{
|
||||
m_SpecMapShader->Shutdown();
|
||||
delete m_SpecMapShader;
|
||||
m_SpecMapShader = 0;
|
||||
}
|
||||
|
||||
// Release the transparent shader object.
|
||||
if (m_TransparentShader)
|
||||
{
|
||||
m_TransparentShader->Shutdown();
|
||||
delete m_TransparentShader;
|
||||
m_TransparentShader = 0;
|
||||
}
|
||||
|
||||
// Release the light shader object.
|
||||
if (m_LightShader)
|
||||
{
|
||||
m_LightShader->Shutdown();
|
||||
delete m_LightShader;
|
||||
m_LightShader = 0;
|
||||
}
|
||||
|
||||
// Release the light map shader object.
|
||||
if (m_LightMapShader)
|
||||
{
|
||||
m_LightMapShader->Shutdown();
|
||||
delete m_LightMapShader;
|
||||
m_LightMapShader = 0;
|
||||
}
|
||||
|
||||
// Release the refraction shader object.
|
||||
if (m_RefractionShader)
|
||||
{
|
||||
m_RefractionShader->Shutdown();
|
||||
delete m_RefractionShader;
|
||||
m_RefractionShader = 0;
|
||||
}
|
||||
|
||||
// Release the water shader object.
|
||||
if (m_WaterShader)
|
||||
{
|
||||
m_WaterShader->Shutdown();
|
||||
delete m_WaterShader;
|
||||
m_WaterShader = 0;
|
||||
}
|
||||
|
||||
// Release the cel shading shader object.
|
||||
if (m_CelShadingShader)
|
||||
{
|
||||
m_CelShadingShader->Shutdown();
|
||||
delete m_CelShadingShader;
|
||||
m_CelShadingShader = 0;
|
||||
}
|
||||
|
||||
if (m_SunlightShader)
|
||||
{
|
||||
m_SunlightShader->Shutdown();
|
||||
delete m_SunlightShader;
|
||||
m_SunlightShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("ShaderManagerClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderTextureShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_TextureShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error rendering TextureShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderNormalMapShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* colorTexture, ID3D11ShaderResourceView* normalTexture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_NormalMapShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, colorTexture, normalTexture, lightDirection, diffuseColor);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error rendering NormalMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderMultitextureShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_MultitextureShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error rendering MultiTextureShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderTranslateShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, float valeur)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_TranslateShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, valeur);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error rendering TranslateShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderAlphaMapShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_AlphaMapShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2, texture3);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error rendering AlphaMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderSpecMapShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3,
|
||||
XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 cameraPosition, XMFLOAT4 specularColor, float specularPower)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_SpecMapShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2, texture3, lightDirection,
|
||||
diffuseColor, cameraPosition, specularColor, specularPower);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error rendering SpecMapShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderTransparentShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, float blendAmount)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_TransparentShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, blendAmount);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error rendering TransparentShaderClass", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderlightShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 ambientColor[])
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_LightShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, lightPosition, ambientColor);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderlightMapShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_LightMapShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderRefractionShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor[], XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 clipPlane)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_RefractionShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor, lightPosition, clipPlane);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderWaterShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
XMMATRIX reflectionMatrix, ID3D11ShaderResourceView* reflectionTexture, ID3D11ShaderResourceView* refractionTexture,
|
||||
ID3D11ShaderResourceView* normalTexture, float waterTranslation, float reflectRefractScale)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_WaterShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, reflectionMatrix, reflectionTexture,
|
||||
refractionTexture, normalTexture, waterTranslation, reflectRefractScale);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderCelShadingShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 lightPosition)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_CelShadingShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor, lightPosition);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShaderManagerClass::RenderSunlightShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection, float sunIntensity)
|
||||
{
|
||||
bool result;
|
||||
|
||||
result = m_SunlightShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, sunIntensity);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
492
enginecustom/src/src/shader/specmapshaderclass.cpp
Normal file
492
enginecustom/src/src/shader/specmapshaderclass.cpp
Normal file
@@ -0,0 +1,492 @@
|
||||
#include "specmapshaderclass.h"
|
||||
|
||||
|
||||
SpecMapShaderClass::SpecMapShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_lightBuffer = 0;
|
||||
m_cameraBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
SpecMapShaderClass::SpecMapShaderClass(const SpecMapShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SpecMapShaderClass::~SpecMapShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool SpecMapShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/specmap.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/specmap.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpecMapShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool SpecMapShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3,
|
||||
XMFLOAT3 lightDirection, 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, texture1, texture2, texture3, lightDirection, diffuseColor,
|
||||
cameraPosition, specularColor, specularPower);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SpecMapShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[5];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC lightBufferDesc;
|
||||
D3D11_BUFFER_DESC cameraBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "SpecMapVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "SpecMapPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[3].SemanticName = "TANGENT";
|
||||
polygonLayout[3].SemanticIndex = 0;
|
||||
polygonLayout[3].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[3].InputSlot = 0;
|
||||
polygonLayout[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[3].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[3].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[4].SemanticName = "BINORMAL";
|
||||
polygonLayout[4].SemanticIndex = 0;
|
||||
polygonLayout[4].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[4].InputSlot = 0;
|
||||
polygonLayout[4].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[4].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[4].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the light dynamic constant buffer that is in the pixel shader.
|
||||
lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
||||
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
lightBufferDesc.MiscFlags = 0;
|
||||
lightBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpecMapShaderClass::ShutdownShader()
|
||||
{
|
||||
// Release the camera constant buffer.
|
||||
if (m_cameraBuffer)
|
||||
{
|
||||
m_cameraBuffer->Release();
|
||||
m_cameraBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the light constant buffer.
|
||||
if (m_lightBuffer)
|
||||
{
|
||||
m_lightBuffer->Release();
|
||||
m_lightBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void SpecMapShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool SpecMapShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3,
|
||||
XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 cameraPosition, XMFLOAT4 specularColor, float specularPower)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
LightBufferType* dataPtr2;
|
||||
CameraBufferType* dataPtr3;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resources in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture1);
|
||||
deviceContext->PSSetShaderResources(1, 1, &texture2);
|
||||
deviceContext->PSSetShaderResources(2, 1, &texture3);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr2 = (LightBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the lighting variables into the constant buffer.
|
||||
dataPtr2->diffuseColor = diffuseColor;
|
||||
dataPtr2->lightDirection = lightDirection;
|
||||
dataPtr2->specularColor = specularColor;
|
||||
dataPtr2->specularPower = specularPower;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_lightBuffer, 0);
|
||||
|
||||
// Set the position of the light constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the light constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
|
||||
|
||||
// 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;
|
||||
|
||||
// Unlock the camera constant buffer.
|
||||
deviceContext->Unmap(m_cameraBuffer, 0);
|
||||
|
||||
// Set the position of the camera constant buffer in the vertex shader as the second buffer.
|
||||
bufferNumber = 1;
|
||||
|
||||
// Now set the camera constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_cameraBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpecMapShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this model.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the model.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
462
enginecustom/src/src/shader/sunlightshaderclass.cpp
Normal file
462
enginecustom/src/src/shader/sunlightshaderclass.cpp
Normal file
@@ -0,0 +1,462 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: lightshaderclass.cpp
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include "sunlightshaderclass.h"
|
||||
|
||||
|
||||
SunlightShaderClass::SunlightShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_sampleState = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_cameraBuffer = 0;
|
||||
m_sunlightBuffer = 0;
|
||||
m_sunlightColorBuffer = 0;
|
||||
m_sunlightPositionBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
SunlightShaderClass::SunlightShaderClass(const SunlightShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SunlightShaderClass::~SunlightShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool SunlightShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
bool result;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/sunlight.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/sunlight.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("SunLightShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SunlightShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool SunlightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection, float sunIntensity)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, sunIntensity);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool SunlightShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
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 sunlightBufferDesc;
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "SunLightVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "SunLightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[2].SemanticName = "NORMAL";
|
||||
polygonLayout[2].SemanticIndex = 0;
|
||||
polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[2].InputSlot = 0;
|
||||
polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[2].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the dynamic sunlight constant buffer that is in the pixel shader.
|
||||
sunlightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
sunlightBufferDesc.ByteWidth = sizeof(SunLightBufferType);
|
||||
sunlightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
sunlightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
sunlightBufferDesc.MiscFlags = 0;
|
||||
sunlightBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&sunlightBufferDesc, NULL, &m_sunlightBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create sunlight buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void SunlightShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down SunLightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the light constant buffers.
|
||||
if (m_sunlightColorBuffer)
|
||||
{
|
||||
m_sunlightColorBuffer->Release();
|
||||
m_sunlightColorBuffer = 0;
|
||||
}
|
||||
|
||||
if (m_sunlightPositionBuffer)
|
||||
{
|
||||
m_sunlightPositionBuffer->Release();
|
||||
m_sunlightPositionBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the light constant buffer.
|
||||
if (m_sunlightBuffer)
|
||||
{
|
||||
m_sunlightBuffer->Release();
|
||||
m_sunlightBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the camera constant buffer.
|
||||
if (m_cameraBuffer)
|
||||
{
|
||||
m_cameraBuffer->Release();
|
||||
m_cameraBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("SunLightShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void SunlightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned __int64 bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool SunlightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor, XMFLOAT3 lightDirection, float sunIntensity)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
CameraBufferType* dataPtr2;
|
||||
SunLightBufferType* dataPtr3;
|
||||
unsigned int bufferNumber;
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Lock the sunlight constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_sunlightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr3 = (SunLightBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the lighting variables into the constant buffer.
|
||||
dataPtr3->ambientColor = ambientColor;
|
||||
dataPtr3->diffuseColor = diffuseColor;
|
||||
dataPtr3->sunDirection = lightDirection;
|
||||
dataPtr3->intensity = sunIntensity;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_sunlightBuffer, 0);
|
||||
|
||||
// Set the position of the sunlight constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the sunlight constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_sunlightBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SunlightShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
378
enginecustom/src/src/shader/textureshaderclass.cpp
Normal file
378
enginecustom/src/src/shader/textureshaderclass.cpp
Normal file
@@ -0,0 +1,378 @@
|
||||
#include "textureshaderclass.h"
|
||||
|
||||
|
||||
TextureShaderClass::TextureShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
|
||||
TextureShaderClass::TextureShaderClass(const TextureShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TextureShaderClass::~TextureShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool TextureShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing texture shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/texture.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying stirng", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/texture.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Error copying stirng", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error initializing shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Texture shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TextureShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool TextureShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Error setting shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TextureShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "TextureVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "TexturePixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Error compiling shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
// Create the vertex input layout description.
|
||||
// This setup needs to match the VertexType stucture in the ModelClass and in the shader.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error creating constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Error creating sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TextureShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Shader shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void TextureShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool TextureShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Error mapping constant buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finanly set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TextureShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the triangle.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
438
enginecustom/src/src/shader/translateshaderclass.cpp
Normal file
438
enginecustom/src/src/shader/translateshaderclass.cpp
Normal file
@@ -0,0 +1,438 @@
|
||||
#include "translateshaderclass.h"
|
||||
|
||||
|
||||
TranslateShaderClass::TranslateShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_translateBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
TranslateShaderClass::TranslateShaderClass(const TranslateShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TranslateShaderClass::~TranslateShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool TranslateShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initilaizing TranslateShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/translate.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy vsFilename", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/translate.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy psFilename", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("TranslateShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TranslateShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool TranslateShaderClass::Render(ID3D11DeviceContext * deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView * texture, float translation)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, translation);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool TranslateShaderClass::InitializeShader(ID3D11Device * device, HWND hwnd, WCHAR * vsFilename, WCHAR * psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing translate shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC translateBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "TranslateVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "TranslatePixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the texture translation dynamic constant buffer that is in the pixel shader.
|
||||
translateBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
translateBufferDesc.ByteWidth = sizeof(TranslateBufferType);
|
||||
translateBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
translateBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
translateBufferDesc.MiscFlags = 0;
|
||||
translateBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&translateBufferDesc, NULL, &m_translateBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create translate buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Translate shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TranslateShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down translate shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the texture translation constant buffer.
|
||||
if (m_translateBuffer)
|
||||
{
|
||||
m_translateBuffer->Release();
|
||||
m_translateBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Translate shader shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void TranslateShaderClass::OutputShaderErrorMessage(ID3D10Blob * errorMessage, HWND hwnd, WCHAR * shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool TranslateShaderClass::SetShaderParameters(ID3D11DeviceContext * deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView * texture, float translation)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
TranslateBufferType* dataPtr2;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to map matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
// Lock the texture translation constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_translateBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to map translate buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the texture translation constant buffer.
|
||||
dataPtr2 = (TranslateBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the translation value into the texture translation constant buffer.
|
||||
dataPtr2->translation = translation;
|
||||
|
||||
// Unlock the buffer.
|
||||
deviceContext->Unmap(m_translateBuffer, 0);
|
||||
|
||||
// Set the position of the texture translation constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Now set the texture translation constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_translateBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TranslateShaderClass::RenderShader(ID3D11DeviceContext * deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the geometry.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
437
enginecustom/src/src/shader/transparentshaderclass.cpp
Normal file
437
enginecustom/src/src/shader/transparentshaderclass.cpp
Normal file
@@ -0,0 +1,437 @@
|
||||
#include "transparentshaderclass.h"
|
||||
|
||||
|
||||
TransparentShaderClass::TransparentShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_sampleState = 0;
|
||||
m_transparentBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
TransparentShaderClass::TransparentShaderClass(const TransparentShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TransparentShaderClass::~TransparentShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool TransparentShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
Logger::Get().Log("Initializing TransparentShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/transparent.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy vertex shader filename", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/transparent.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
Logger::Get().Log("Failed to copy pixel shader filename", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("TransparentShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TransparentShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool TransparentShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, float blend)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, blend);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool TransparentShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
Logger::Get().Log("Initializing transparent shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC transparentBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "TransparentVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "TransparentPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
Logger::Get().Log("Failed to compile pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the pixel shader from the buffer.
|
||||
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the transparent dynamic constant buffer that is in the pixel shader.
|
||||
transparentBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
transparentBufferDesc.ByteWidth = sizeof(TransparentBufferType);
|
||||
transparentBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
transparentBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
transparentBufferDesc.MiscFlags = 0;
|
||||
transparentBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&transparentBufferDesc, NULL, &m_transparentBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create transparent buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Transparent shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TransparentShaderClass::ShutdownShader()
|
||||
{
|
||||
Logger::Get().Log("Shutting down transparent shader", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the transparent constant buffer.
|
||||
if (m_transparentBuffer)
|
||||
{
|
||||
m_transparentBuffer->Release();
|
||||
m_transparentBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Transparent shader shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void TransparentShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool TransparentShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix,
|
||||
XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, float blend)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
TransparentBufferType* dataPtr2;
|
||||
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
Logger::Get().Log("Failed to map matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Set shader texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||
|
||||
// Lock the transparent constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_transparentBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to map transparent buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the transparent constant buffer.
|
||||
dataPtr2 = (TransparentBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the alpha blending value into the transparent constant buffer.
|
||||
dataPtr2->blendAmount = blend;
|
||||
|
||||
// Unlock the buffer.
|
||||
deviceContext->Unmap(m_transparentBuffer, 0);
|
||||
|
||||
// Set the position of the transparent constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Now set the transparent constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_transparentBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void TransparentShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render the geometry.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the geometry.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
470
enginecustom/src/src/shader/watershaderclass.cpp
Normal file
470
enginecustom/src/src/shader/watershaderclass.cpp
Normal file
@@ -0,0 +1,470 @@
|
||||
#include "watershaderclass.h"
|
||||
|
||||
|
||||
WaterShaderClass::WaterShaderClass()
|
||||
{
|
||||
m_vertexShader = 0;
|
||||
m_pixelShader = 0;
|
||||
m_layout = 0;
|
||||
m_sampleState = 0;
|
||||
m_matrixBuffer = 0;
|
||||
m_reflectionBuffer = 0;
|
||||
m_waterBuffer = 0;
|
||||
}
|
||||
|
||||
|
||||
WaterShaderClass::WaterShaderClass(const WaterShaderClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
WaterShaderClass::~WaterShaderClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool WaterShaderClass::Initialize(ID3D11Device* device, HWND hwnd)
|
||||
{
|
||||
bool result;
|
||||
wchar_t vsFilename[128];
|
||||
wchar_t psFilename[128];
|
||||
int error;
|
||||
|
||||
// Set the filename of the vertex shader.
|
||||
error = wcscpy_s(vsFilename, 128, L"src/hlsl/water.vs");
|
||||
if (error != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the filename of the pixel shader.
|
||||
error = wcscpy_s(psFilename, 128, L"src/hlsl/water.ps");
|
||||
if (error != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize the vertex and pixel shaders.
|
||||
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void WaterShaderClass::Shutdown()
|
||||
{
|
||||
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||
ShutdownShader();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool WaterShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||
XMMATRIX reflectionMatrix, ID3D11ShaderResourceView* reflectionTexture, ID3D11ShaderResourceView* refractionTexture,
|
||||
ID3D11ShaderResourceView* normalTexture, float waterTranslation, float reflectRefractScale)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Set the shader parameters that it will use for rendering.
|
||||
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, reflectionMatrix, reflectionTexture,
|
||||
refractionTexture, normalTexture, waterTranslation, reflectRefractScale);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now render the prepared buffers with the shader.
|
||||
RenderShader(deviceContext, indexCount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool WaterShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||
{
|
||||
HRESULT result;
|
||||
ID3D10Blob* errorMessage;
|
||||
ID3D10Blob* vertexShaderBuffer;
|
||||
ID3D10Blob* pixelShaderBuffer;
|
||||
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||
unsigned int numElements;
|
||||
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
D3D11_BUFFER_DESC reflectionBufferDesc;
|
||||
D3D11_BUFFER_DESC waterBufferDesc;
|
||||
|
||||
|
||||
// Initialize the pointers this function will use to null.
|
||||
errorMessage = 0;
|
||||
vertexShaderBuffer = 0;
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Compile the vertex shader code.
|
||||
result = D3DCompileFromFile(vsFilename, NULL, NULL, "WaterVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&vertexShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compile the pixel shader code.
|
||||
result = D3DCompileFromFile(psFilename, NULL, NULL, "WaterPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
|
||||
&pixelShaderBuffer, &errorMessage);
|
||||
if (FAILED(result))
|
||||
{
|
||||
// If the shader failed to compile it should have writen something to the error message.
|
||||
if (errorMessage)
|
||||
{
|
||||
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||
}
|
||||
// If there was nothing in the error message then it simply could not find the file itself.
|
||||
else
|
||||
{
|
||||
MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex shader from the buffer.
|
||||
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
|
||||
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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the vertex input layout description.
|
||||
polygonLayout[0].SemanticName = "POSITION";
|
||||
polygonLayout[0].SemanticIndex = 0;
|
||||
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||
polygonLayout[0].InputSlot = 0;
|
||||
polygonLayout[0].AlignedByteOffset = 0;
|
||||
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[0].InstanceDataStepRate = 0;
|
||||
|
||||
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||
polygonLayout[1].SemanticIndex = 0;
|
||||
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||
polygonLayout[1].InputSlot = 0;
|
||||
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||
polygonLayout[1].InstanceDataStepRate = 0;
|
||||
|
||||
// Get a count of the elements in the layout.
|
||||
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||
|
||||
// Create the vertex input layout.
|
||||
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
|
||||
vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||
vertexShaderBuffer->Release();
|
||||
vertexShaderBuffer = 0;
|
||||
|
||||
pixelShaderBuffer->Release();
|
||||
pixelShaderBuffer = 0;
|
||||
|
||||
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
matrixBufferDesc.MiscFlags = 0;
|
||||
matrixBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a texture sampler state description.
|
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||
samplerDesc.MipLODBias = 0.0f;
|
||||
samplerDesc.MaxAnisotropy = 1;
|
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||
samplerDesc.BorderColor[0] = 0;
|
||||
samplerDesc.BorderColor[1] = 0;
|
||||
samplerDesc.BorderColor[2] = 0;
|
||||
samplerDesc.BorderColor[3] = 0;
|
||||
samplerDesc.MinLOD = 0;
|
||||
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||
|
||||
// Create the texture sampler state.
|
||||
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the reflection dynamic constant buffer that is in the vertex shader.
|
||||
reflectionBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
reflectionBufferDesc.ByteWidth = sizeof(ReflectionBufferType);
|
||||
reflectionBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
reflectionBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
reflectionBufferDesc.MiscFlags = 0;
|
||||
reflectionBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&reflectionBufferDesc, NULL, &m_reflectionBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup the description of the water dynamic constant buffer that is in the pixel shader.
|
||||
waterBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
waterBufferDesc.ByteWidth = sizeof(WaterBufferType);
|
||||
waterBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
waterBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
waterBufferDesc.MiscFlags = 0;
|
||||
waterBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||
result = device->CreateBuffer(&waterBufferDesc, NULL, &m_waterBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void WaterShaderClass::ShutdownShader()
|
||||
{
|
||||
// Release the water constant buffer.
|
||||
if (m_waterBuffer)
|
||||
{
|
||||
m_waterBuffer->Release();
|
||||
m_waterBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the reflection constant buffer.
|
||||
if (m_reflectionBuffer)
|
||||
{
|
||||
m_reflectionBuffer->Release();
|
||||
m_reflectionBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the sampler state.
|
||||
if (m_sampleState)
|
||||
{
|
||||
m_sampleState->Release();
|
||||
m_sampleState = 0;
|
||||
}
|
||||
|
||||
// Release the matrix constant buffer.
|
||||
if (m_matrixBuffer)
|
||||
{
|
||||
m_matrixBuffer->Release();
|
||||
m_matrixBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the layout.
|
||||
if (m_layout)
|
||||
{
|
||||
m_layout->Release();
|
||||
m_layout = 0;
|
||||
}
|
||||
|
||||
// Release the pixel shader.
|
||||
if (m_pixelShader)
|
||||
{
|
||||
m_pixelShader->Release();
|
||||
m_pixelShader = 0;
|
||||
}
|
||||
|
||||
// Release the vertex shader.
|
||||
if (m_vertexShader)
|
||||
{
|
||||
m_vertexShader->Release();
|
||||
m_vertexShader = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void WaterShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||
{
|
||||
char* compileErrors;
|
||||
unsigned long long bufferSize, i;
|
||||
ofstream fout;
|
||||
|
||||
|
||||
// Get a pointer to the error message text buffer.
|
||||
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||
|
||||
// Get the length of the message.
|
||||
bufferSize = errorMessage->GetBufferSize();
|
||||
|
||||
// Open a file to write the error message to.
|
||||
fout.open("shader-error.txt");
|
||||
|
||||
// Write out the error message.
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
fout << compileErrors[i];
|
||||
}
|
||||
|
||||
// Close the file.
|
||||
fout.close();
|
||||
|
||||
// Release the error message.
|
||||
errorMessage->Release();
|
||||
errorMessage = 0;
|
||||
|
||||
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool WaterShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, XMMATRIX reflectionMatrix,
|
||||
ID3D11ShaderResourceView* reflectionTexture, ID3D11ShaderResourceView* refractionTexture, ID3D11ShaderResourceView* normalTexture,
|
||||
float waterTranslation, float reflectRefractScale)
|
||||
{
|
||||
HRESULT result;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
MatrixBufferType* dataPtr;
|
||||
unsigned int bufferNumber;
|
||||
ReflectionBufferType* dataPtr2;
|
||||
WaterBufferType* dataPtr3;
|
||||
|
||||
// Transpose the matrices to prepare them for the shader.
|
||||
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||
reflectionMatrix = XMMatrixTranspose(reflectionMatrix);
|
||||
|
||||
// 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))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the matrices into the constant buffer.
|
||||
dataPtr->world = worldMatrix;
|
||||
dataPtr->view = viewMatrix;
|
||||
dataPtr->projection = projectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||
|
||||
// Set the position of the constant buffer in the vertex shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finanly set the constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||
|
||||
// Lock the reflection constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_reflectionBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr2 = (ReflectionBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the reflection matrix into the constant buffer.
|
||||
dataPtr2->reflection = reflectionMatrix;
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_reflectionBuffer, 0);
|
||||
|
||||
// Set the position of the reflection constant buffer in the vertex shader.
|
||||
bufferNumber = 1;
|
||||
|
||||
// Finally set the reflection constant buffer in the vertex shader with the updated values.
|
||||
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_reflectionBuffer);
|
||||
|
||||
// Set the reflection texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(0, 1, &reflectionTexture);
|
||||
|
||||
// Set the refraction texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(1, 1, &refractionTexture);
|
||||
|
||||
// Set the normal map texture resource in the pixel shader.
|
||||
deviceContext->PSSetShaderResources(2, 1, &normalTexture);
|
||||
|
||||
// Lock the water constant buffer so it can be written to.
|
||||
result = deviceContext->Map(m_waterBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr3 = (WaterBufferType*)mappedResource.pData;
|
||||
|
||||
// Copy the water data into the constant buffer.
|
||||
dataPtr3->waterTranslation = waterTranslation;
|
||||
dataPtr3->reflectRefractScale = reflectRefractScale;
|
||||
dataPtr3->padding = XMFLOAT2(0.0f, 0.0f);
|
||||
|
||||
// Unlock the constant buffer.
|
||||
deviceContext->Unmap(m_waterBuffer, 0);
|
||||
|
||||
// Set the position of the water constant buffer in the pixel shader.
|
||||
bufferNumber = 0;
|
||||
|
||||
// Finally set the water constant buffer in the pixel shader with the updated values.
|
||||
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_waterBuffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WaterShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||
{
|
||||
// Set the vertex input layout.
|
||||
deviceContext->IASetInputLayout(m_layout);
|
||||
|
||||
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
|
||||
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
|
||||
|
||||
// Set the sampler state in the pixel shader.
|
||||
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||
|
||||
// Render the geometry.
|
||||
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
171
enginecustom/src/src/system/Cameraclass.cpp
Normal file
171
enginecustom/src/src/system/Cameraclass.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Filename: cameraclass.cpp
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include "cameraclass.h"
|
||||
|
||||
CameraClass::CameraClass()
|
||||
{
|
||||
m_positionX = 0.0f;
|
||||
m_positionY = 0.0f;
|
||||
m_positionZ = 0.0f;
|
||||
|
||||
m_rotationX = 0.0f;
|
||||
m_rotationY = 0.0f;
|
||||
m_rotationZ = 0.0f;
|
||||
}
|
||||
|
||||
|
||||
CameraClass::CameraClass(const CameraClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CameraClass::~CameraClass()
|
||||
{
|
||||
}
|
||||
|
||||
void CameraClass::SetPosition(float x, float y, float z)
|
||||
{
|
||||
m_positionX = x;
|
||||
m_positionY = y;
|
||||
m_positionZ = z;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void CameraClass::SetRotation(float x, float y, float z)
|
||||
{
|
||||
m_rotationX = x;
|
||||
m_rotationY = y;
|
||||
m_rotationZ = z;
|
||||
return;
|
||||
}
|
||||
|
||||
XMFLOAT3 CameraClass::GetPosition()
|
||||
{
|
||||
return XMFLOAT3(m_positionX, m_positionY, m_positionZ);
|
||||
}
|
||||
|
||||
|
||||
XMFLOAT3 CameraClass::GetRotation()
|
||||
{
|
||||
return XMFLOAT3(m_rotationX, m_rotationY, m_rotationZ);
|
||||
}
|
||||
|
||||
void CameraClass::Render()
|
||||
{
|
||||
XMFLOAT3 up, position, lookAt;
|
||||
XMVECTOR upVector, positionVector, lookAtVector;
|
||||
float yaw, pitch, roll;
|
||||
XMMATRIX rotationMatrix;
|
||||
|
||||
|
||||
// Setup the vector that points upwards.
|
||||
up.x = 0.0f;
|
||||
up.y = 1.0f;
|
||||
up.z = 0.0f;
|
||||
|
||||
// Load it into a XMVECTOR structure.
|
||||
upVector = XMLoadFloat3(&up);
|
||||
|
||||
// Setup the position of the camera in the world.
|
||||
position.x = m_positionX;
|
||||
position.y = m_positionY;
|
||||
position.z = m_positionZ;
|
||||
|
||||
// Load it into a XMVECTOR structure.
|
||||
positionVector = XMLoadFloat3(&position);
|
||||
|
||||
// Setup where the camera is looking by default.
|
||||
lookAt.x = 0.0f;
|
||||
lookAt.y = 0.0f;
|
||||
lookAt.z = 1.0f;
|
||||
|
||||
// Load it into a XMVECTOR structure.
|
||||
lookAtVector = XMLoadFloat3(&lookAt);
|
||||
|
||||
// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
|
||||
pitch = m_rotationX * 0.0174532925f;
|
||||
yaw = m_rotationY * 0.0174532925f;
|
||||
roll = m_rotationZ * 0.0174532925f;
|
||||
|
||||
// Create the rotation matrix from the yaw, pitch, and roll values.
|
||||
rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
|
||||
|
||||
// Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin.
|
||||
lookAtVector = XMVector3TransformCoord(lookAtVector, rotationMatrix);
|
||||
upVector = XMVector3TransformCoord(upVector, rotationMatrix);
|
||||
|
||||
// Translate the rotated camera position to the location of the viewer.
|
||||
lookAtVector = XMVectorAdd(positionVector, lookAtVector);
|
||||
|
||||
// Finally create the view matrix from the three updated vectors.
|
||||
m_viewMatrix = XMMatrixLookAtLH(positionVector, lookAtVector, upVector);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
XMMATRIX CameraClass::GetViewMatrix(XMMATRIX& viewMatrix) const
|
||||
{
|
||||
viewMatrix = m_viewMatrix;
|
||||
return viewMatrix;
|
||||
}
|
||||
|
||||
void CameraClass::RenderReflection(float height)
|
||||
{
|
||||
XMFLOAT3 up, position, lookAt;
|
||||
XMVECTOR upVector, positionVector, lookAtVector;
|
||||
float yaw, pitch, roll;
|
||||
XMMATRIX rotationMatrix;
|
||||
|
||||
|
||||
// Setup the vector that points upwards.
|
||||
up.x = 0.0f;
|
||||
up.y = 1.0f;
|
||||
up.z = 0.0f;
|
||||
|
||||
// Load it into a XMVECTOR structure.
|
||||
upVector = XMLoadFloat3(&up);
|
||||
|
||||
// Setup the position of the camera in the world.
|
||||
position.x = m_positionX;
|
||||
position.y = -m_positionY + (height * 2.0f);
|
||||
position.z = m_positionZ;
|
||||
|
||||
// Load it into a XMVECTOR structure.
|
||||
positionVector = XMLoadFloat3(&position);
|
||||
|
||||
// Setup where the camera is looking by default.
|
||||
lookAt.x = 0.0f;
|
||||
lookAt.y = 0.0f;
|
||||
lookAt.z = 1.0f;
|
||||
|
||||
// Load it into a XMVECTOR structure.
|
||||
lookAtVector = XMLoadFloat3(&lookAt);
|
||||
|
||||
// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
|
||||
pitch = (-1.0f * m_rotationX) * 0.0174532925f; // Invert for reflection
|
||||
yaw = m_rotationY * 0.0174532925f;
|
||||
roll = m_rotationZ * 0.0174532925f;
|
||||
|
||||
// Create the rotation matrix from the yaw, pitch, and roll values.
|
||||
rotationMatrix = XMMatrixRotationRollPitchYaw(pitch, yaw, roll);
|
||||
|
||||
// Transform the lookAt and up vector by the rotation matrix so the view is correctly rotated at the origin.
|
||||
lookAtVector = XMVector3TransformCoord(lookAtVector, rotationMatrix);
|
||||
upVector = XMVector3TransformCoord(upVector, rotationMatrix);
|
||||
|
||||
// Translate the rotated camera position to the location of the viewer.
|
||||
lookAtVector = XMVectorAdd(positionVector, lookAtVector);
|
||||
|
||||
// Finally create the view matrix from the three updated vectors.
|
||||
m_reflectionViewMatrix = XMMatrixLookAtLH(positionVector, lookAtVector, upVector);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void CameraClass::GetReflectionViewMatrix(XMMATRIX& reflectionViewMatrix) const
|
||||
{
|
||||
reflectionViewMatrix = m_reflectionViewMatrix;
|
||||
return;
|
||||
}
|
||||
37
enginecustom/src/src/system/Main.cpp
Normal file
37
enginecustom/src/src/system/Main.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "systemclass.h"
|
||||
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pScmdline, int iCmdshow)
|
||||
{
|
||||
SystemClass* System;
|
||||
bool result;
|
||||
|
||||
wchar_t path[MAX_PATH];
|
||||
HMODULE hmodule = GetModuleHandle(NULL);
|
||||
if (hmodule != NULL)
|
||||
{
|
||||
GetModuleFileName(hmodule, path, (sizeof(path) / sizeof(wchar_t)));
|
||||
}
|
||||
|
||||
std::filesystem::path exePath(path);
|
||||
std::filesystem::path WFolder = exePath.parent_path();
|
||||
|
||||
// Create the system object.
|
||||
System = new SystemClass;
|
||||
|
||||
// Initialize and run the system object.
|
||||
result = System->Initialize();
|
||||
if (result)
|
||||
{
|
||||
Logger::Get().Log("System initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
System->SendPath(path,WFolder);
|
||||
System->Run();
|
||||
}
|
||||
|
||||
// Shutdown and release the system object.
|
||||
System->Shutdown();
|
||||
delete System;
|
||||
System = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
69
enginecustom/src/src/system/Modellistclass.cpp
Normal file
69
enginecustom/src/src/system/Modellistclass.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "modellistclass.h"
|
||||
|
||||
|
||||
ModelListClass::ModelListClass()
|
||||
{
|
||||
m_ModelInfoList = 0;
|
||||
}
|
||||
|
||||
|
||||
ModelListClass::ModelListClass(const ModelListClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
ModelListClass::~ModelListClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void ModelListClass::Initialize(int numModels)
|
||||
{
|
||||
|
||||
int i;
|
||||
|
||||
// Store the number of models.
|
||||
m_modelCount = numModels;
|
||||
|
||||
// Create a list array of the model information.
|
||||
m_ModelInfoList = new ModelInfoType[m_modelCount];
|
||||
|
||||
// Seed the random generator with the current time.
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
// Go through all the models and randomly generate the position.
|
||||
for (i = 0; i < m_modelCount; i++)
|
||||
{
|
||||
// Generate a random position in front of the viewer for the mode.
|
||||
m_ModelInfoList[i].positionX = (((float)rand() - (float)rand()) / RAND_MAX) * 10.0f;
|
||||
m_ModelInfoList[i].positionY = (((float)rand() - (float)rand()) / RAND_MAX) * 10.0f;
|
||||
m_ModelInfoList[i].positionZ = ((((float)rand() - (float)rand()) / RAND_MAX) * 10.0f) + 5.0f;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ModelListClass::Shutdown()
|
||||
{
|
||||
// Release the model information list.
|
||||
if (m_ModelInfoList)
|
||||
{
|
||||
delete[] m_ModelInfoList;
|
||||
m_ModelInfoList = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int ModelListClass::GetModelCount()
|
||||
{
|
||||
return m_modelCount;
|
||||
}
|
||||
|
||||
void ModelListClass::GetData(int index, float& positionX, float& positionY, float& positionZ)
|
||||
{
|
||||
positionX = m_ModelInfoList[index].positionX;
|
||||
positionY = m_ModelInfoList[index].positionY;
|
||||
positionZ = m_ModelInfoList[index].positionZ;
|
||||
return;
|
||||
}
|
||||
242
enginecustom/src/src/system/Positionclass.cpp
Normal file
242
enginecustom/src/src/system/Positionclass.cpp
Normal file
@@ -0,0 +1,242 @@
|
||||
#include "positionclass.h"
|
||||
|
||||
PositionClass::PositionClass()
|
||||
{
|
||||
m_frameTime = 0.0f;
|
||||
m_rotationY = 0.0f;
|
||||
m_rotationX = 0.0f;
|
||||
m_positionX = 0.0f;
|
||||
m_positionY = 0.0f;
|
||||
m_positionZ = 0.0f;
|
||||
m_leftTurnSpeed = 0.0f;
|
||||
m_rightTurnSpeed = 0.0f;
|
||||
m_horizontalTurnSpeed = 0.0f;
|
||||
m_verticalTurnSpeed = 0.0f;
|
||||
m_verticalTurnSpeed = 0.0f;
|
||||
m_cameraSpeed = 4.0f;
|
||||
m_speed = m_cameraSpeed;
|
||||
}
|
||||
|
||||
|
||||
PositionClass::PositionClass(const PositionClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
PositionClass::~PositionClass()
|
||||
{
|
||||
}
|
||||
|
||||
void PositionClass::SetFrameTime(float time)
|
||||
{
|
||||
m_frameTime = time;
|
||||
return;
|
||||
}
|
||||
|
||||
void PositionClass::GetRotation(float& y, float& x) const
|
||||
{
|
||||
y = m_rotationY;
|
||||
x = m_rotationX;
|
||||
return;
|
||||
}
|
||||
|
||||
void PositionClass::GetPosition(float& x, float& y, float& z) const
|
||||
{
|
||||
x = m_positionX;
|
||||
y = m_positionY;
|
||||
z = m_positionZ;
|
||||
return;
|
||||
}
|
||||
|
||||
void PositionClass::TurnLeft(bool keydown)
|
||||
{
|
||||
// If the key is pressed increase the speed at which the camera turns left. If not slow down the turn speed.
|
||||
if (keydown)
|
||||
{
|
||||
m_leftTurnSpeed += m_frameTime * 1.5f;
|
||||
|
||||
if (m_leftTurnSpeed > (m_frameTime * 200.0f))
|
||||
{
|
||||
m_leftTurnSpeed = m_frameTime * 200.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_leftTurnSpeed -= m_frameTime * 1.0f;
|
||||
|
||||
if (m_leftTurnSpeed < 0.0f)
|
||||
{
|
||||
m_leftTurnSpeed = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the rotation using the turning speed.
|
||||
m_rotationY -= m_leftTurnSpeed;
|
||||
if (m_rotationY < 0.0f)
|
||||
{
|
||||
m_rotationY += 360.0f;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void PositionClass::TurnRight(bool keydown)
|
||||
{
|
||||
// If the key is pressed increase the speed at which the camera turns right. If not slow down the turn speed.
|
||||
if (keydown)
|
||||
{
|
||||
m_rightTurnSpeed += m_frameTime * 1.5f;
|
||||
|
||||
if (m_rightTurnSpeed > (m_frameTime * 200.0f))
|
||||
{
|
||||
m_rightTurnSpeed = m_frameTime * 200.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_rightTurnSpeed -= m_frameTime * 1.0f;
|
||||
|
||||
if (m_rightTurnSpeed < 0.0f)
|
||||
{
|
||||
m_rightTurnSpeed = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the rotation using the turning speed.
|
||||
m_rotationY += m_rightTurnSpeed;
|
||||
if (m_rotationY > 360.0f)
|
||||
{
|
||||
m_rotationY -= 360.0f;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void PositionClass::TurnMouse(float deltaX, float deltaY, float sensitivity, bool rightMouseDown)
|
||||
{
|
||||
// The turning speed is proportional to the horizontal mouse movement
|
||||
m_horizontalTurnSpeed = deltaX * sensitivity;
|
||||
|
||||
if (rightMouseDown)
|
||||
{
|
||||
// Update the rotation using the turning speed
|
||||
m_rotationY += m_horizontalTurnSpeed;
|
||||
if (m_rotationY < 0.0f)
|
||||
{
|
||||
m_rotationY += 360.0f;
|
||||
}
|
||||
else if (m_rotationY > 360.0f)
|
||||
{
|
||||
m_rotationY -= 360.0f;
|
||||
}
|
||||
|
||||
// The turning speed is proportional to the vertical mouse movement
|
||||
m_verticalTurnSpeed = deltaY * sensitivity;
|
||||
|
||||
// Update the rotation using the turning speed
|
||||
m_rotationX += m_verticalTurnSpeed;
|
||||
if (m_rotationX < -90.0f)
|
||||
{
|
||||
m_rotationX = -90.0f;
|
||||
}
|
||||
else if (m_rotationX > 90.0f)
|
||||
{
|
||||
m_rotationX = 90.0f;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void PositionClass::MoveCamera(bool forward, bool backward, bool left, bool right, bool up, bool down, bool scrollUp, bool scrollDown, bool rightClick)
|
||||
{
|
||||
float radiansY, radiansX, speed;
|
||||
|
||||
// Set the speed of the camera if the right click is down.
|
||||
if (scrollUp && rightClick)
|
||||
{
|
||||
m_cameraSpeed *= 1.1f;
|
||||
}
|
||||
if (scrollDown && rightClick)
|
||||
{
|
||||
m_cameraSpeed *= 0.9f;
|
||||
|
||||
if (m_cameraSpeed < 0.25f) // Minimum speed.
|
||||
{
|
||||
m_cameraSpeed = 0.25f;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert degrees to radians.
|
||||
radiansY = m_rotationY * 0.0174532925f;
|
||||
radiansX = m_rotationX * 0.0174532925f;
|
||||
|
||||
//////////////////////////
|
||||
// Update the position. //
|
||||
//////////////////////////
|
||||
|
||||
// Moves the camera forward on a greater scale than the arrows.
|
||||
if (scrollUp && !rightClick)
|
||||
{
|
||||
speed = m_speed * 20 * m_frameTime;
|
||||
m_positionX += sinf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionZ += cosf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionY -= sinf(radiansX) * speed;
|
||||
}
|
||||
|
||||
// Moves the camera backward on a greater scale than the arrows.
|
||||
if (scrollDown && !rightClick)
|
||||
{
|
||||
speed = m_speed * 20 * m_frameTime;
|
||||
m_positionX -= sinf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionZ -= cosf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionY += sinf(radiansX) * speed;
|
||||
}
|
||||
|
||||
// Set the speed of the camera.
|
||||
speed = m_cameraSpeed * m_frameTime;
|
||||
|
||||
// If moving forward, the position moves in the direction of the camera and accordingly to its angle.
|
||||
if (forward)
|
||||
{
|
||||
m_positionX += sinf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionZ += cosf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionY -= sinf(radiansX) * speed;
|
||||
}
|
||||
|
||||
// If moving backward, the position moves in the opposite direction of the camera and accordingly to its angle.
|
||||
if (backward)
|
||||
{
|
||||
m_positionX -= sinf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionZ -= cosf(radiansY) * cosf(radiansX) * speed;
|
||||
m_positionY += sinf(radiansX) * speed;
|
||||
}
|
||||
|
||||
// If moving left, the position moves to the left of the camera and accordingly to its angle.
|
||||
if (left)
|
||||
{
|
||||
m_positionX -= cosf(radiansY) * speed;
|
||||
m_positionZ += sinf(radiansY) * speed;
|
||||
}
|
||||
|
||||
// If moving right, the position moves to the right of the camera and accordingly to its angle.
|
||||
if (right)
|
||||
{
|
||||
m_positionX += cosf(radiansY) * speed;
|
||||
m_positionZ -= sinf(radiansY) * speed;
|
||||
}
|
||||
|
||||
// If moving up, the position moves up.
|
||||
if (up)
|
||||
{
|
||||
m_positionY += speed;
|
||||
}
|
||||
|
||||
// If moving down, the position moves down.
|
||||
if (down)
|
||||
{
|
||||
m_positionY -= speed;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
421
enginecustom/src/src/system/Spriteclass.cpp
Normal file
421
enginecustom/src/src/system/Spriteclass.cpp
Normal file
@@ -0,0 +1,421 @@
|
||||
#include "spriteclass.h"
|
||||
|
||||
|
||||
SpriteClass::SpriteClass()
|
||||
{
|
||||
m_vertexBuffer = 0;
|
||||
m_indexBuffer = 0;
|
||||
m_Textures = 0;
|
||||
}
|
||||
|
||||
|
||||
SpriteClass::SpriteClass(const SpriteClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SpriteClass::~SpriteClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool SpriteClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, int screenWidth, int screenHeight, char* spriteFilename, int renderX, int renderY)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Store the screen size.
|
||||
m_screenWidth = screenWidth;
|
||||
m_screenHeight = screenHeight;
|
||||
|
||||
// Store where the sprite should be rendered to.
|
||||
m_renderX = renderX;
|
||||
m_renderY = renderY;
|
||||
|
||||
// Initialize the frame time for this sprite object.
|
||||
m_frameTime = 0;
|
||||
|
||||
// Initialize the vertex and index buffer that hold the geometry for the sprite bitmap.
|
||||
result = InitializeBuffers(device);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load the textures for this sprite.
|
||||
result = LoadTextures(device, deviceContext, spriteFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpriteClass::Shutdown()
|
||||
{
|
||||
// Release the textures used for this sprite.
|
||||
ReleaseTextures();
|
||||
|
||||
// Release the vertex and index buffers.
|
||||
ShutdownBuffers();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool SpriteClass::Render(ID3D11DeviceContext* deviceContext)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Update the buffers if the position of the sprite has changed from its original position.
|
||||
result = UpdateBuffers(deviceContext);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
RenderBuffers(deviceContext);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpriteClass::Update(float frameTime)
|
||||
{
|
||||
// Increment the frame time each frame.
|
||||
m_frameTime += frameTime;
|
||||
|
||||
// Check if the frame time has reached the cycle time.
|
||||
if (m_frameTime >= m_cycleTime)
|
||||
{
|
||||
// If it has then reset the frame time and cycle to the next sprite in the texture array.
|
||||
m_frameTime -= m_cycleTime;
|
||||
|
||||
m_currentTexture++;
|
||||
|
||||
// If we are at the last sprite texture then go back to the beginning of the texture array to the first texture again.
|
||||
if (m_currentTexture == m_textureCount)
|
||||
{
|
||||
m_currentTexture = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int SpriteClass::GetIndexCount()
|
||||
{
|
||||
return m_indexCount;
|
||||
}
|
||||
|
||||
ID3D11ShaderResourceView* SpriteClass::GetTexture()
|
||||
{
|
||||
return m_Textures[m_currentTexture].GetTexture();
|
||||
}
|
||||
|
||||
|
||||
bool SpriteClass::InitializeBuffers(ID3D11Device* device)
|
||||
{
|
||||
VertexType* vertices;
|
||||
unsigned long* indices;
|
||||
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
|
||||
D3D11_SUBRESOURCE_DATA vertexData, indexData;
|
||||
HRESULT result;
|
||||
int i;
|
||||
|
||||
|
||||
// Initialize the previous rendering position to negative one.
|
||||
m_prevPosX = -1;
|
||||
m_prevPosY = -1;
|
||||
|
||||
// Set the number of vertices in the vertex array.
|
||||
m_vertexCount = 6;
|
||||
|
||||
// Set the number of indices in the index array.
|
||||
m_indexCount = m_vertexCount;
|
||||
|
||||
// Create the vertex array.
|
||||
vertices = new VertexType[m_vertexCount];
|
||||
|
||||
// Create the index array.
|
||||
indices = new unsigned long[m_indexCount];
|
||||
|
||||
// Initialize vertex array to zeros at first.
|
||||
memset(vertices, 0, (sizeof(VertexType) * m_vertexCount));
|
||||
|
||||
// Load the index array with data.
|
||||
for (i = 0; i < m_indexCount; i++)
|
||||
{
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
// Set up the description of the dynamic vertex buffer.
|
||||
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
|
||||
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
vertexBufferDesc.MiscFlags = 0;
|
||||
vertexBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Give the subresource structure a pointer to the vertex data.
|
||||
vertexData.pSysMem = vertices;
|
||||
vertexData.SysMemPitch = 0;
|
||||
vertexData.SysMemSlicePitch = 0;
|
||||
|
||||
// Now finally create the vertex buffer.
|
||||
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set up the description of the index buffer.
|
||||
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
|
||||
indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
|
||||
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||
indexBufferDesc.CPUAccessFlags = 0;
|
||||
indexBufferDesc.MiscFlags = 0;
|
||||
indexBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Give the subresource structure a pointer to the index data.
|
||||
indexData.pSysMem = indices;
|
||||
indexData.SysMemPitch = 0;
|
||||
indexData.SysMemSlicePitch = 0;
|
||||
|
||||
// Create the index buffer.
|
||||
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the arrays now that the vertex and index buffers have been created and loaded.
|
||||
delete[] vertices;
|
||||
vertices = 0;
|
||||
|
||||
delete[] indices;
|
||||
indices = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpriteClass::ShutdownBuffers()
|
||||
{
|
||||
// Release the index buffer.
|
||||
if (m_indexBuffer)
|
||||
{
|
||||
m_indexBuffer->Release();
|
||||
m_indexBuffer = 0;
|
||||
}
|
||||
|
||||
// Release the vertex buffer.
|
||||
if (m_vertexBuffer)
|
||||
{
|
||||
m_vertexBuffer->Release();
|
||||
m_vertexBuffer = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool SpriteClass::UpdateBuffers(ID3D11DeviceContext* deviceContent)
|
||||
{
|
||||
float left, right, top, bottom;
|
||||
VertexType* vertices;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
VertexType* dataPtr;
|
||||
HRESULT result;
|
||||
|
||||
|
||||
// If the position we are rendering this bitmap to hasn't changed then don't update the vertex buffer.
|
||||
if ((m_prevPosX == m_renderX) && (m_prevPosY == m_renderY))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// If the rendering location has changed then store the new position and update the vertex buffer.
|
||||
m_prevPosX = m_renderX;
|
||||
m_prevPosY = m_renderY;
|
||||
|
||||
// Create the vertex array.
|
||||
vertices = new VertexType[m_vertexCount];
|
||||
|
||||
// Calculate the screen coordinates of the left side of the bitmap.
|
||||
left = (float)((m_screenWidth / 2) * -1) + (float)m_renderX;
|
||||
|
||||
// Calculate the screen coordinates of the right side of the bitmap.
|
||||
right = left + (float)m_bitmapWidth;
|
||||
|
||||
// Calculate the screen coordinates of the top of the bitmap.
|
||||
top = (float)(m_screenHeight / 2) - (float)m_renderY;
|
||||
|
||||
// Calculate the screen coordinates of the bottom of the bitmap.
|
||||
bottom = top - (float)m_bitmapHeight;
|
||||
|
||||
// Load the vertex array with data.
|
||||
// First triangle.
|
||||
vertices[0].position = XMFLOAT3(left, top, 0.0f); // Top left.
|
||||
vertices[0].texture = XMFLOAT2(0.0f, 0.0f);
|
||||
|
||||
vertices[1].position = XMFLOAT3(right, bottom, 0.0f); // Bottom right.
|
||||
vertices[1].texture = XMFLOAT2(1.0f, 1.0f);
|
||||
|
||||
vertices[2].position = XMFLOAT3(left, bottom, 0.0f); // Bottom left.
|
||||
vertices[2].texture = XMFLOAT2(0.0f, 1.0f);
|
||||
|
||||
// Second triangle.
|
||||
vertices[3].position = XMFLOAT3(left, top, 0.0f); // Top left.
|
||||
vertices[3].texture = XMFLOAT2(0.0f, 0.0f);
|
||||
|
||||
vertices[4].position = XMFLOAT3(right, top, 0.0f); // Top right.
|
||||
vertices[4].texture = XMFLOAT2(1.0f, 0.0f);
|
||||
|
||||
vertices[5].position = XMFLOAT3(right, bottom, 0.0f); // Bottom right.
|
||||
vertices[5].texture = XMFLOAT2(1.0f, 1.0f);
|
||||
|
||||
// Lock the vertex buffer.
|
||||
result = deviceContent->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (VertexType*)mappedResource.pData;
|
||||
|
||||
// Copy the data into the vertex buffer.
|
||||
memcpy(dataPtr, (void*)vertices, (sizeof(VertexType) * m_vertexCount));
|
||||
|
||||
// Unlock the vertex buffer.
|
||||
deviceContent->Unmap(m_vertexBuffer, 0);
|
||||
|
||||
// Release the pointer reference.
|
||||
dataPtr = 0;
|
||||
|
||||
// Release the vertex array as it is no longer needed.
|
||||
delete[] vertices;
|
||||
vertices = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void SpriteClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
|
||||
{
|
||||
unsigned int stride;
|
||||
unsigned int offset;
|
||||
|
||||
|
||||
// Set vertex buffer stride and offset.
|
||||
stride = sizeof(VertexType);
|
||||
offset = 0;
|
||||
|
||||
// Set the vertex buffer to active in the input assembler so it can be rendered.
|
||||
deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
|
||||
|
||||
// Set the index buffer to active in the input assembler so it can be rendered.
|
||||
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
|
||||
|
||||
// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
|
||||
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool SpriteClass::LoadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename)
|
||||
{
|
||||
std::string textureFilename(256, '0');
|
||||
std::ifstream fin;
|
||||
int i, j;
|
||||
char input;
|
||||
bool result;
|
||||
|
||||
|
||||
// Open the sprite info data file.
|
||||
fin.open(filename);
|
||||
if (fin.fail())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read in the number of textures.
|
||||
fin >> m_textureCount;
|
||||
|
||||
// Create and initialize the texture array with the texture count from the file.
|
||||
m_Textures = new TextureClass[m_textureCount];
|
||||
|
||||
// Read to start of next line.
|
||||
fin.get(input);
|
||||
|
||||
// Read in each texture file name.
|
||||
for (i = 0; i < m_textureCount; i++)
|
||||
{
|
||||
j = 0;
|
||||
fin.get(input);
|
||||
while (input != '\n')
|
||||
{
|
||||
textureFilename[j] = input;
|
||||
j++;
|
||||
fin.get(input);
|
||||
}
|
||||
textureFilename[j] = '\0';
|
||||
|
||||
// Once you have the filename then load the texture in the texture array.
|
||||
result = m_Textures[i].Initialize(device, deviceContext, textureFilename);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Read in the cycle time.
|
||||
fin >> m_cycleTime;
|
||||
|
||||
// Convert the integer milliseconds to float representation.
|
||||
m_cycleTime = m_cycleTime * 0.001f;
|
||||
|
||||
// Close the file.
|
||||
fin.close();
|
||||
|
||||
// Get the dimensions of the first texture and use that as the dimensions of the 2D sprite images.
|
||||
m_bitmapWidth = m_Textures[0].GetWidth();
|
||||
m_bitmapHeight = m_Textures[0].GetHeight();
|
||||
|
||||
// Set the starting texture in the cycle to be the first one in the list.
|
||||
m_currentTexture = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpriteClass::ReleaseTextures()
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
// Release the texture objects.
|
||||
if (m_Textures)
|
||||
{
|
||||
for (i = 0; i < m_textureCount; i++)
|
||||
{
|
||||
m_Textures[i].Shutdown();
|
||||
}
|
||||
|
||||
delete[] m_Textures;
|
||||
m_Textures = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void SpriteClass::SetRenderLocation(int x, int y)
|
||||
{
|
||||
m_renderX = x;
|
||||
m_renderY = y;
|
||||
return;
|
||||
}
|
||||
469
enginecustom/src/src/system/Systemclass.cpp
Normal file
469
enginecustom/src/src/system/Systemclass.cpp
Normal file
@@ -0,0 +1,469 @@
|
||||
#include "systemclass.h"
|
||||
#include <iostream>
|
||||
#include <shellapi.h> // Include for DragAcceptFiles and DragQueryFile
|
||||
#include <windows.h>
|
||||
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
||||
|
||||
SystemClass::SystemClass()
|
||||
{
|
||||
m_Input = 0;
|
||||
m_Application = 0;
|
||||
m_imguiManager = 0;
|
||||
m_applicationName = 0;
|
||||
m_hinstance = 0;
|
||||
m_hwnd = 0;
|
||||
m_initialWindowWidth = 0;
|
||||
m_initialWindowHeight = 0;
|
||||
m_isDirect3DInitialized = false;
|
||||
|
||||
}
|
||||
|
||||
SystemClass::~SystemClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool SystemClass::Initialize()
|
||||
{
|
||||
int screenHeight, screenWidth = 0;
|
||||
bool result;
|
||||
|
||||
Logger::Get().Log("Initializing system class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
try
|
||||
{
|
||||
// Initialize the windows api.
|
||||
InitializeWindows(screenWidth, screenHeight);
|
||||
|
||||
// Create and initialize the input object. This object will be used to handle reading the keyboard input from the user.
|
||||
m_Input = new InputClass;
|
||||
|
||||
result = m_Input->Initialize(m_hinstance, m_hwnd, screenWidth, screenHeight);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize input class", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the application class object. This object will handle rendering all the graphics for this application.
|
||||
m_Application = new ApplicationClass;
|
||||
|
||||
result = m_Application->Initialize(screenWidth, screenHeight, m_hwnd);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_isDirect3DInitialized = true;
|
||||
|
||||
// If we received a WM_SIZE message before Direct3D was initialized, resize the swap chain now
|
||||
if (m_initialWindowWidth > 0 && m_initialWindowHeight > 0)
|
||||
{
|
||||
m_Application->GetDirect3D()->ResizeSwapChain(m_initialWindowWidth, m_initialWindowHeight);
|
||||
}
|
||||
|
||||
// Initialize imgui
|
||||
m_imguiManager = new imguiManager;
|
||||
result = m_imguiManager->Initialize(m_hwnd, m_Application->GetDirect3D()->GetDevice(), m_Application->GetDirect3D()->GetDeviceContext());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
Logger::Get().Log(std::string("Exception caught during initialization: ") + e.what(), __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("System class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SystemClass::Shutdown()
|
||||
{
|
||||
Logger::Get().Log("Shutting down system class", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
std::lock_guard<std::mutex> guard(renderMutex);
|
||||
|
||||
// Shutdown imgui
|
||||
if (m_imguiManager)
|
||||
{
|
||||
Logger::Get().Log("Shutting down imgui manager", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
m_imguiManager->Shutdown();
|
||||
delete m_imguiManager;
|
||||
m_imguiManager = 0;
|
||||
|
||||
Logger::Get().Log("Imgui manager shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the application class object.
|
||||
if (m_Application)
|
||||
{
|
||||
Logger::Get().Log("Shutting down application", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
m_Application->Shutdown();
|
||||
delete m_Application;
|
||||
m_Application = 0;
|
||||
|
||||
Logger::Get().Log("Application shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the input object.
|
||||
if (m_Input)
|
||||
{
|
||||
Logger::Get().Log("Shutting down input", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
delete m_Input;
|
||||
m_Input = 0;
|
||||
|
||||
Logger::Get().Log("Input shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Shutdown the window.
|
||||
ShutdownWindows();
|
||||
|
||||
Logger::Get().Log("System class shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void SystemClass::Run()
|
||||
{
|
||||
MSG msg;
|
||||
bool done, result;
|
||||
|
||||
Logger::Get().Log("Running the system", __FILE__, __LINE__);
|
||||
|
||||
// Initialize the message structure.
|
||||
ZeroMemory(&msg, sizeof(MSG));
|
||||
|
||||
// Loop until there is a quit message from the window or the user.
|
||||
done = false;
|
||||
|
||||
while (!done)
|
||||
{
|
||||
// Handle the windows messages.
|
||||
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
||||
{
|
||||
if(msg.message == WM_QUIT)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
// If windows signals to end the application then exit out.
|
||||
if (m_Application != nullptr && m_Application->GetShouldQuit())
|
||||
{
|
||||
Logger::Get().Log("Received quit signal from application", __FILE__, __LINE__);
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise do the frame processing.
|
||||
result = Frame();
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to process frame", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool SystemClass::Frame()
|
||||
{
|
||||
// Clear the buffers to begin the scene.
|
||||
m_Application->GetDirect3D()->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
std::lock_guard<std::mutex> guard(renderMutex);
|
||||
bool result;
|
||||
|
||||
// Do the input frame processing.
|
||||
result = m_Input->Frame();
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to process input frame", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Do the frame processing for the application class object.
|
||||
result = m_Application->Frame(m_Input);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to process application frame", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Render ImGui
|
||||
result = m_imguiManager->ImGuiWidgetRenderer(m_Application);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to render ImGui widgets", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_Application->GetDirect3D()->EndScene();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
|
||||
if (ImGui_ImplWin32_WndProcHandler(hwnd, umsg, wparam, lparam))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (umsg)
|
||||
{
|
||||
// Check if a key has been pressed on the keyboard.
|
||||
case WM_KEYDOWN:
|
||||
{
|
||||
// If a key is pressed send it to the input object so it can record that state.
|
||||
m_Input->KeyDown((unsigned int)wparam);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check if a key has been released on the keyboard.
|
||||
case WM_KEYUP:
|
||||
{
|
||||
// If a key is released then send it to the input object so it can unset the state for that key.
|
||||
m_Input->KeyUp((unsigned int)wparam);
|
||||
return 0;
|
||||
}
|
||||
case WM_SIZE:
|
||||
{
|
||||
int newWidth = LOWORD(lparam);
|
||||
int newHeight = HIWORD(lparam);
|
||||
|
||||
// If Direct3D is initialized, update the swap chain. Otherwise, store the window dimensions
|
||||
if (m_isDirect3DInitialized && m_Application && m_Application->GetDirect3D())
|
||||
{
|
||||
m_Application->SetScreenWidth(newWidth);
|
||||
m_Application->SetScreenHeight(newHeight);
|
||||
m_Application->GetDirect3D()->ResizeSwapChain(newWidth, newHeight);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_initialWindowWidth = newWidth;
|
||||
m_initialWindowHeight = newHeight;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
case WM_ENTERSIZEMOVE:
|
||||
{
|
||||
m_isResizing = true;
|
||||
break;
|
||||
}
|
||||
case WM_EXITSIZEMOVE:
|
||||
{
|
||||
m_isResizing = false;
|
||||
break;
|
||||
}
|
||||
case WM_DROPFILES:
|
||||
{
|
||||
HDROP hDrop = reinterpret_cast<HDROP>(wparam);
|
||||
UINT numFiles = DragQueryFile(hDrop, 0xFFFFFFFF, nullptr, 0);
|
||||
|
||||
if (numFiles > 0) {
|
||||
for (UINT i = 0; i < numFiles; ++i) {
|
||||
WCHAR filePath[MAX_PATH];
|
||||
DragQueryFile(hDrop, i, filePath, MAX_PATH);
|
||||
|
||||
// Get the file extension
|
||||
std::wstring fileName = filePath;
|
||||
std::wstring extension = fileName.substr(fileName.find_last_of(L".") + 1);
|
||||
|
||||
// Check if the file has a valid extension
|
||||
if (extension == L"txt" || extension == L"kobj") {
|
||||
// Handle dropped files with valid extensions
|
||||
std::wcout << L"File dropped: " << filePath << std::endl;
|
||||
m_Application->AddKobject(filePath);
|
||||
}
|
||||
else {
|
||||
// Handle files with invalid extensions (optional)
|
||||
std::wcout << L"Ignored file: " << filePath << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DragFinish(hDrop);
|
||||
return 0;
|
||||
}
|
||||
case WM_CLOSE:
|
||||
{
|
||||
Logger::Get().Log("WM_CLOSE message received", __FILE__, __LINE__);
|
||||
m_Application->SetShouldQuit(true);
|
||||
return 0;
|
||||
}
|
||||
// Any other messages send to the default message handler as our application won't make use of them.
|
||||
default:
|
||||
{
|
||||
return DefWindowProc(hwnd, umsg, wparam, lparam);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SystemClass::InitializeWindows(int& screenWidth, int& screenHeight)
|
||||
{
|
||||
WNDCLASSEX wc;
|
||||
DEVMODE dmScreenSettings;
|
||||
int posX, posY;
|
||||
|
||||
Logger::Get().Log("Initializing windows", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
// Get an external pointer to this object.
|
||||
ApplicationHandle = this;
|
||||
|
||||
// Get the instance of this application.
|
||||
m_hinstance = GetModuleHandle(NULL);
|
||||
|
||||
// Give the application a name.
|
||||
m_applicationName = L"Khaotic Engine";
|
||||
|
||||
// Setup the windows class with default settings.
|
||||
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
|
||||
wc.lpfnWndProc = WndProc;
|
||||
wc.cbClsExtra = 0;
|
||||
wc.cbWndExtra = 0;
|
||||
wc.hInstance = m_hinstance;
|
||||
wc.hIcon = LoadIcon(m_hinstance,MAKEINTRESOURCE(IDI_ICON1));
|
||||
wc.hIconSm = LoadIcon(m_hinstance, MAKEINTRESOURCE(IDI_ICON1));
|
||||
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
|
||||
wc.lpszMenuName = NULL;
|
||||
wc.lpszClassName = m_applicationName;
|
||||
wc.cbSize = sizeof(WNDCLASSEX);
|
||||
|
||||
// Register the window class.
|
||||
RegisterClassEx(&wc);
|
||||
|
||||
// Determine the resolution of the clients desktop screen.
|
||||
screenWidth = GetSystemMetrics(SM_CXSCREEN);
|
||||
screenHeight = GetSystemMetrics(SM_CYSCREEN);
|
||||
|
||||
// Setup the screen settings depending on whether it is running in full screen or in windowed mode.
|
||||
if (FULL_SCREEN)
|
||||
{
|
||||
// If full screen set the screen to maximum size of the users desktop and 32bit.
|
||||
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
|
||||
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
|
||||
dmScreenSettings.dmPelsWidth = (unsigned long)screenWidth;
|
||||
dmScreenSettings.dmPelsHeight = (unsigned long)screenHeight;
|
||||
dmScreenSettings.dmBitsPerPel = 32;
|
||||
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
|
||||
|
||||
// Change the display settings to full screen.
|
||||
ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
|
||||
|
||||
// Set the position of the window to the top left corner.
|
||||
posX = posY = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If windowed then set it to 1600x900 resolution.
|
||||
screenWidth = 1600;
|
||||
screenHeight = 900;
|
||||
|
||||
// Place the window in the middle of the screen.
|
||||
posX = (GetSystemMetrics(SM_CXSCREEN) - screenWidth) / 2;
|
||||
posY = (GetSystemMetrics(SM_CYSCREEN) - screenHeight) / 2;
|
||||
}
|
||||
|
||||
// Create the window with the screen settings and get the handle to it.
|
||||
m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, m_applicationName, m_applicationName,
|
||||
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
|
||||
posX, posY, screenWidth, screenHeight, NULL, NULL, m_hinstance, NULL);
|
||||
|
||||
// Bring the window up on the screen and set it as main focus.
|
||||
ShowWindow(m_hwnd, SW_SHOW);
|
||||
SetForegroundWindow(m_hwnd);
|
||||
SetFocus(m_hwnd);
|
||||
|
||||
// Hide the mouse cursor.
|
||||
ShowCursor(true);
|
||||
|
||||
//drag and drop
|
||||
DragAcceptFiles(m_hwnd, TRUE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void SystemClass::ShutdownWindows()
|
||||
{
|
||||
Logger::Get().Log("Shutting down the windows", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
// Show the mouse cursor.
|
||||
ShowCursor(true);
|
||||
|
||||
// Fix the display settings if leaving full screen mode.
|
||||
if (FULL_SCREEN)
|
||||
{
|
||||
ChangeDisplaySettings(NULL, 0);
|
||||
}
|
||||
|
||||
// Remove the window.
|
||||
DestroyWindow(m_hwnd);
|
||||
m_hwnd = NULL;
|
||||
|
||||
// Remove the application instance.
|
||||
UnregisterClass(m_applicationName, m_hinstance);
|
||||
m_hinstance = NULL;
|
||||
|
||||
// Release the pointer to this class.
|
||||
ApplicationHandle = NULL;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
switch (umessage)
|
||||
{
|
||||
// Check if the window is being destroyed.
|
||||
case WM_DESTROY:
|
||||
{
|
||||
PostQuitMessage(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Check if the window is being closed.
|
||||
case WM_CLOSE:
|
||||
{
|
||||
PostQuitMessage(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case WM_DROPFILES:
|
||||
{
|
||||
ApplicationHandle->MessageHandler(hwnd, umessage, wparam, lparam);
|
||||
return(0);
|
||||
}
|
||||
|
||||
// All other messages pass to the message handler in the system class.
|
||||
default:
|
||||
{
|
||||
return ApplicationHandle->MessageHandler(hwnd, umessage, wparam, lparam);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SystemClass::SendPath(wchar_t* path, std::filesystem::path WFolder)
|
||||
{
|
||||
m_Application->SetPath(path);
|
||||
m_Application->SetWFolder(WFolder);
|
||||
}
|
||||
68
enginecustom/src/src/system/Timerclass.cpp
Normal file
68
enginecustom/src/src/system/Timerclass.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "timerclass.h"
|
||||
|
||||
|
||||
TimerClass::TimerClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TimerClass::TimerClass(const TimerClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
TimerClass::~TimerClass()
|
||||
{
|
||||
}
|
||||
|
||||
bool TimerClass::Initialize()
|
||||
{
|
||||
Logger::Get().Log("Initilazing timer class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
INT64 frequency;
|
||||
|
||||
|
||||
// Get the cycles per second speed for this system.
|
||||
QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
|
||||
if (frequency == 0)
|
||||
{
|
||||
Logger::Get().Log("QueryPerformanceFrequency failed", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store it in floating point.
|
||||
m_frequency = (float)frequency;
|
||||
|
||||
// Get the initial start time.
|
||||
QueryPerformanceCounter((LARGE_INTEGER*)&m_startTime);
|
||||
|
||||
Logger::Get().Log("Timer class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TimerClass::Frame()
|
||||
{
|
||||
INT64 currentTime;
|
||||
INT64 elapsedTicks;
|
||||
|
||||
|
||||
// Query the current time.
|
||||
QueryPerformanceCounter((LARGE_INTEGER*)¤tTime);
|
||||
|
||||
// Calculate the difference in time since the last time we queried for the current time.
|
||||
elapsedTicks = currentTime - m_startTime;
|
||||
|
||||
// Calculate the frame time.
|
||||
m_frameTime = (float)elapsedTicks / m_frequency;
|
||||
|
||||
// Restart the timer.
|
||||
m_startTime = currentTime;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
float TimerClass::GetTime()
|
||||
{
|
||||
return m_frameTime;
|
||||
}
|
||||
2191
enginecustom/src/src/system/applicationclass.cpp
Normal file
2191
enginecustom/src/src/system/applicationclass.cpp
Normal file
File diff suppressed because it is too large
Load Diff
361
enginecustom/src/src/system/bitmapclass.cpp
Normal file
361
enginecustom/src/src/system/bitmapclass.cpp
Normal file
@@ -0,0 +1,361 @@
|
||||
#include "bitmapclass.h"
|
||||
|
||||
BitmapClass::BitmapClass()
|
||||
{
|
||||
m_vertexBuffer = 0;
|
||||
m_indexBuffer = 0;
|
||||
m_Texture = 0;
|
||||
}
|
||||
|
||||
|
||||
BitmapClass::BitmapClass(const BitmapClass& other)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
BitmapClass::~BitmapClass()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
bool BitmapClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, int screenWidth, int screenHeight, char* textureFilename, int renderX, int renderY)
|
||||
{
|
||||
Logger::Get().Log("Initializing bitmap class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
bool result;
|
||||
|
||||
// Store the screen size.
|
||||
m_screenWidth = screenWidth;
|
||||
m_screenHeight = screenHeight;
|
||||
|
||||
// Store where the bitmap should be rendered to.
|
||||
m_renderX = renderX;
|
||||
m_renderY = renderY;
|
||||
|
||||
// Initialize the vertex and index buffer that hold the geometry for the bitmap quad.
|
||||
result = InitializeBuffers(device);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize buffers", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load the texture for this bitmap.
|
||||
result = LoadTexture(device, deviceContext, textureFilename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to load texture", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger::Get().Log("Bitmap class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BitmapClass::Shutdown()
|
||||
{
|
||||
// Release the bitmap texture.
|
||||
ReleaseTexture();
|
||||
|
||||
// Release the vertex and index buffers.
|
||||
ShutdownBuffers();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool BitmapClass::Render(ID3D11DeviceContext* deviceContext)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Update the buffers if the position of the bitmap has changed from its original position.
|
||||
result = UpdateBuffers(deviceContext);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to update buffers", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
RenderBuffers(deviceContext);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int BitmapClass::GetIndexCount()
|
||||
{
|
||||
return m_indexCount;
|
||||
}
|
||||
|
||||
ID3D11ShaderResourceView* BitmapClass::GetTexture()
|
||||
{
|
||||
return m_Texture->GetTexture();
|
||||
}
|
||||
|
||||
bool BitmapClass::InitializeBuffers(ID3D11Device* device)
|
||||
{
|
||||
Logger::Get().Log("Initializing buffers", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
VertexType* vertices;
|
||||
unsigned long* indices;
|
||||
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
|
||||
D3D11_SUBRESOURCE_DATA vertexData, indexData;
|
||||
HRESULT result;
|
||||
int i;
|
||||
|
||||
// Initialize the previous rendering position to negative one.
|
||||
m_prevPosX = -1;
|
||||
m_prevPosY = -1;
|
||||
|
||||
// Set the number of vertices in the vertex array.
|
||||
m_vertexCount = 6;
|
||||
|
||||
// Set the number of indices in the index array.
|
||||
m_indexCount = m_vertexCount;
|
||||
|
||||
// Create the vertex array.
|
||||
vertices = new VertexType[m_vertexCount];
|
||||
|
||||
// Create the index array.
|
||||
indices = new unsigned long[m_indexCount];
|
||||
|
||||
// Initialize vertex array to zeros at first.
|
||||
memset(vertices, 0, (sizeof(VertexType) * m_vertexCount));
|
||||
|
||||
// Load the index array with data.
|
||||
for (i = 0; i < m_indexCount; i++)
|
||||
{
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
// Set up the description of the dynamic vertex buffer.
|
||||
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
|
||||
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||
vertexBufferDesc.MiscFlags = 0;
|
||||
vertexBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Give the subresource structure a pointer to the vertex data.
|
||||
vertexData.pSysMem = vertices;
|
||||
vertexData.SysMemPitch = 0;
|
||||
vertexData.SysMemSlicePitch = 0;
|
||||
|
||||
// Now finally create the vertex buffer.
|
||||
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create vertex buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set up the description of the index buffer.
|
||||
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
|
||||
indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
|
||||
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||
indexBufferDesc.CPUAccessFlags = 0;
|
||||
indexBufferDesc.MiscFlags = 0;
|
||||
indexBufferDesc.StructureByteStride = 0;
|
||||
|
||||
// Give the subresource structure a pointer to the index data.
|
||||
indexData.pSysMem = indices;
|
||||
indexData.SysMemPitch = 0;
|
||||
indexData.SysMemSlicePitch = 0;
|
||||
|
||||
// Create the index buffer.
|
||||
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to create index buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Release the arrays now that the vertex and index buffers have been created and loaded.
|
||||
delete[] vertices;
|
||||
vertices = 0;
|
||||
|
||||
delete[] indices;
|
||||
indices = 0;
|
||||
|
||||
Logger::Get().Log("Buffers initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BitmapClass::ShutdownBuffers()
|
||||
{
|
||||
Logger::Get().Log("Shutting down buffers", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
// Release the index buffer.
|
||||
if (m_indexBuffer)
|
||||
{
|
||||
Logger::Get().Log("Releasing index buffer", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_indexBuffer->Release();
|
||||
m_indexBuffer = 0;
|
||||
Logger::Get().Log("Index buffer released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
// Release the vertex buffer.
|
||||
if (m_vertexBuffer)
|
||||
{
|
||||
Logger::Get().Log("Releasing vertex buffer", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_vertexBuffer->Release();
|
||||
m_vertexBuffer = 0;
|
||||
Logger::Get().Log("Vertex buffer released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
Logger::Get().Log("Buffers shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool BitmapClass::UpdateBuffers(ID3D11DeviceContext* deviceContent)
|
||||
{
|
||||
float left, right, top, bottom;
|
||||
VertexType* vertices;
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||
VertexType* dataPtr;
|
||||
HRESULT result;
|
||||
|
||||
// If the position we are rendering this bitmap to hasn't changed then don't update the vertex buffer.
|
||||
if ((m_prevPosX == m_renderX) && (m_prevPosY == m_renderY))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// If the rendering location has changed then store the new position and update the vertex buffer.
|
||||
m_prevPosX = m_renderX;
|
||||
m_prevPosY = m_renderY;
|
||||
|
||||
// Create the vertex array.
|
||||
vertices = new VertexType[m_vertexCount];
|
||||
|
||||
// Calculate the screen coordinates of the left side of the bitmap.
|
||||
left = (float)((m_screenWidth / 2) * -1) + (float)m_renderX;
|
||||
|
||||
// Calculate the screen coordinates of the right side of the bitmap.
|
||||
right = left + (float)m_bitmapWidth;
|
||||
|
||||
// Calculate the screen coordinates of the top of the bitmap.
|
||||
top = (float)(m_screenHeight / 2) - (float)m_renderY;
|
||||
|
||||
// Calculate the screen coordinates of the bottom of the bitmap.
|
||||
bottom = top - (float)m_bitmapHeight;
|
||||
|
||||
// Load the vertex array with data.
|
||||
// First triangle.
|
||||
vertices[0].position = XMFLOAT3(left, top, 0.0f); // Top left.
|
||||
vertices[0].texture = XMFLOAT2(0.0f, 0.0f);
|
||||
|
||||
vertices[1].position = XMFLOAT3(right, bottom, 0.0f); // Bottom right.
|
||||
vertices[1].texture = XMFLOAT2(1.0f, 1.0f);
|
||||
|
||||
vertices[2].position = XMFLOAT3(left, bottom, 0.0f); // Bottom left.
|
||||
vertices[2].texture = XMFLOAT2(0.0f, 1.0f);
|
||||
|
||||
// Second triangle.
|
||||
vertices[3].position = XMFLOAT3(left, top, 0.0f); // Top left.
|
||||
vertices[3].texture = XMFLOAT2(0.0f, 0.0f);
|
||||
|
||||
vertices[4].position = XMFLOAT3(right, top, 0.0f); // Top right.
|
||||
vertices[4].texture = XMFLOAT2(1.0f, 0.0f);
|
||||
|
||||
vertices[5].position = XMFLOAT3(right, bottom, 0.0f); // Bottom right.
|
||||
vertices[5].texture = XMFLOAT2(1.0f, 1.0f);
|
||||
|
||||
// Lock the vertex buffer.
|
||||
result = deviceContent->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||
if (FAILED(result))
|
||||
{
|
||||
Logger::Get().Log("Failed to map vertex buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a pointer to the data in the constant buffer.
|
||||
dataPtr = (VertexType*)mappedResource.pData;
|
||||
|
||||
// Copy the data into the vertex buffer.
|
||||
memcpy(dataPtr, (void*)vertices, (sizeof(VertexType) * m_vertexCount));
|
||||
|
||||
// Unlock the vertex buffer.
|
||||
deviceContent->Unmap(m_vertexBuffer, 0);
|
||||
|
||||
// Release the pointer reference.
|
||||
dataPtr = 0;
|
||||
|
||||
// Release the vertex array as it is no longer needed.
|
||||
delete[] vertices;
|
||||
vertices = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BitmapClass::RenderBuffers(ID3D11DeviceContext* deviceContext)
|
||||
{
|
||||
unsigned int stride;
|
||||
unsigned int offset;
|
||||
|
||||
|
||||
// Set vertex buffer stride and offset.
|
||||
stride = sizeof(VertexType);
|
||||
offset = 0;
|
||||
|
||||
// Set the vertex buffer to active in the input assembler so it can be rendered.
|
||||
deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
|
||||
|
||||
// Set the index buffer to active in the input assembler so it can be rendered.
|
||||
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
|
||||
|
||||
// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
|
||||
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool BitmapClass::LoadTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename)
|
||||
{
|
||||
bool result;
|
||||
|
||||
|
||||
// Create and initialize the texture object.
|
||||
m_Texture = new TextureClass;
|
||||
|
||||
result = m_Texture->Initialize(device, deviceContext, filename);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Failed to initialize texture object", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store the size in pixels that this bitmap should be rendered at.
|
||||
m_bitmapWidth = m_Texture->GetWidth();
|
||||
m_bitmapHeight = m_Texture->GetHeight();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BitmapClass::ReleaseTexture()
|
||||
{
|
||||
Logger::Get().Log("Releasing texture", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
// Release the texture object.
|
||||
if (m_Texture)
|
||||
{
|
||||
Logger::Get().Log("Releasing texture object", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
m_Texture->Shutdown();
|
||||
delete m_Texture;
|
||||
m_Texture = 0;
|
||||
Logger::Get().Log("Texture object released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
}
|
||||
|
||||
Logger::Get().Log("Texture released", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void BitmapClass::SetRenderLocation(int x, int y)
|
||||
{
|
||||
m_renderX = x;
|
||||
m_renderY = y;
|
||||
return;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user