Ajout transparence buggé
This commit is contained in:
parent
cae1fa326f
commit
1b4762940c
@ -5,6 +5,8 @@ ApplicationClass::ApplicationClass()
|
|||||||
m_Direct3D = 0;
|
m_Direct3D = 0;
|
||||||
m_Camera = 0;
|
m_Camera = 0;
|
||||||
m_Model = 0;
|
m_Model = 0;
|
||||||
|
m_Model1 = 0;
|
||||||
|
m_Model2 = 0;
|
||||||
m_LightShader = 0;
|
m_LightShader = 0;
|
||||||
m_Light = 0;
|
m_Light = 0;
|
||||||
m_TextureShader = 0;
|
m_TextureShader = 0;
|
||||||
@ -26,6 +28,7 @@ ApplicationClass::ApplicationClass()
|
|||||||
m_Frustum = 0;
|
m_Frustum = 0;
|
||||||
m_DisplayPlane = 0;
|
m_DisplayPlane = 0;
|
||||||
m_ReflectionShader = 0;
|
m_ReflectionShader = 0;
|
||||||
|
m_TransparentShader = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -140,6 +143,9 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Create and initialize the render to texture object.
|
// Create and initialize the render to texture object.
|
||||||
m_RenderTexture = new RenderTextureClass;
|
m_RenderTexture = new RenderTextureClass;
|
||||||
|
|
||||||
@ -233,6 +239,37 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create and initialize the first model object that will use the dirt texture.
|
||||||
|
m_Model1 = new ModelClass;
|
||||||
|
|
||||||
|
result = m_Model1->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3, textureFilename4,
|
||||||
|
textureFilename5, textureFilename6);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and initialize the second model object that will use the stone texture.
|
||||||
|
m_Model2 = new ModelClass;
|
||||||
|
|
||||||
|
result = m_Model2->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3, textureFilename4,
|
||||||
|
textureFilename5, textureFilename6);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and initialize the transparent shader object.
|
||||||
|
m_TransparentShader = new TransparentShaderClass;
|
||||||
|
|
||||||
|
result = m_TransparentShader->Initialize(m_Direct3D->GetDevice(), hwnd);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
MessageBox(hwnd, L"Could not initialize the transparent shader object.", L"Error", MB_OK);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Create and initialize the light shader object.
|
// Create and initialize the light shader object.
|
||||||
m_LightShader = new LightShaderClass;
|
m_LightShader = new LightShaderClass;
|
||||||
|
|
||||||
@ -364,6 +401,29 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
|||||||
|
|
||||||
void ApplicationClass::Shutdown()
|
void ApplicationClass::Shutdown()
|
||||||
{
|
{
|
||||||
|
// Release the transparent shader object.
|
||||||
|
if (m_TransparentShader)
|
||||||
|
{
|
||||||
|
m_TransparentShader->Shutdown();
|
||||||
|
delete m_TransparentShader;
|
||||||
|
m_TransparentShader = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the model object.
|
||||||
|
if (m_Model2)
|
||||||
|
{
|
||||||
|
m_Model2->Shutdown();
|
||||||
|
delete m_Model2;
|
||||||
|
m_Model2 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the model object.
|
||||||
|
if (m_Model1)
|
||||||
|
{
|
||||||
|
m_Model1->Shutdown();
|
||||||
|
delete m_Model1;
|
||||||
|
m_Model1 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Release the shader manager object.
|
// Release the shader manager object.
|
||||||
if (m_ShaderManager)
|
if (m_ShaderManager)
|
||||||
@ -703,6 +763,10 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
|
|||||||
XMFLOAT4 diffuseColor[4], lightPosition[4];
|
XMFLOAT4 diffuseColor[4], lightPosition[4];
|
||||||
int modelCount, renderCount, i;
|
int modelCount, renderCount, i;
|
||||||
bool result, renderModel;
|
bool result, renderModel;
|
||||||
|
float blendAmount;
|
||||||
|
|
||||||
|
// Set the blending amount to 50%.
|
||||||
|
blendAmount = 0.5f;
|
||||||
|
|
||||||
// Clear the buffers to begin the scene.
|
// Clear the buffers to begin the scene.
|
||||||
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
|
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
@ -710,6 +774,38 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
|
|||||||
// Generate the view matrix based on the camera's position.
|
// Generate the view matrix based on the camera's position.
|
||||||
m_Camera->Render();
|
m_Camera->Render();
|
||||||
|
|
||||||
|
// Get the world, view, and projection matrices from the camera and d3d objects.
|
||||||
|
m_Direct3D->GetWorldMatrix(worldMatrix);
|
||||||
|
m_Camera->GetViewMatrix(viewMatrix);
|
||||||
|
m_Direct3D->GetProjectionMatrix(projectionMatrix);
|
||||||
|
|
||||||
|
// Render the first model that is using the dirt texture using the regular texture shader.
|
||||||
|
m_Model1->Render(m_Direct3D->GetDeviceContext());
|
||||||
|
|
||||||
|
result = m_TextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model1->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model1->GetTexture(0));
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Translate to the right by one unit and towards the camera by one unit.
|
||||||
|
worldMatrix = XMMatrixTranslation(1.0f, 0.0f, -1.0f);
|
||||||
|
|
||||||
|
// Turn on alpha blending for the transparency to work.
|
||||||
|
m_Direct3D->EnableAlphaBlending();
|
||||||
|
|
||||||
|
// Render the second square model with the stone texture and use the 50% blending amount for transparency.
|
||||||
|
m_Model2->Render(m_Direct3D->GetDeviceContext());
|
||||||
|
|
||||||
|
result = m_TransparentShader->Render(m_Direct3D->GetDeviceContext(), m_Model2->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model2->GetTexture(5), blendAmount);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Turn off alpha blending.
|
||||||
|
m_Direct3D->DisableAlphaBlending();
|
||||||
|
|
||||||
// Get the world, view, and projection matrices from the camera and d3d objects.
|
// Get the world, view, and projection matrices from the camera and d3d objects.
|
||||||
m_Direct3D->GetWorldMatrix(worldMatrix);
|
m_Direct3D->GetWorldMatrix(worldMatrix);
|
||||||
m_Camera->GetViewMatrix(viewMatrix);
|
m_Camera->GetViewMatrix(viewMatrix);
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#include "rendertextureclass.h"
|
#include "rendertextureclass.h"
|
||||||
#include "displayplaneclass.h"
|
#include "displayplaneclass.h"
|
||||||
#include "reflectionshaderclass.h"
|
#include "reflectionshaderclass.h"
|
||||||
|
#include "transparentshaderclass.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -91,7 +92,9 @@ private :
|
|||||||
D3DClass* m_Direct3D;
|
D3DClass* m_Direct3D;
|
||||||
IDXGISwapChain* m_swapChain;
|
IDXGISwapChain* m_swapChain;
|
||||||
ModelClass* m_Model;
|
ModelClass* m_Model;
|
||||||
|
ModelClass* m_Model1, * m_Model2;
|
||||||
TextureShaderClass* m_TextureShader;
|
TextureShaderClass* m_TextureShader;
|
||||||
|
TransparentShaderClass* m_TransparentShader;
|
||||||
ShaderManagerClass* m_ShaderManager;
|
ShaderManagerClass* m_ShaderManager;
|
||||||
ModelListClass* m_ModelList;
|
ModelListClass* m_ModelList;
|
||||||
|
|
||||||
|
@ -60,6 +60,7 @@
|
|||||||
<ClCompile Include="textureshaderclass.cpp" />
|
<ClCompile Include="textureshaderclass.cpp" />
|
||||||
<ClCompile Include="Timerclass.cpp" />
|
<ClCompile Include="Timerclass.cpp" />
|
||||||
<ClCompile Include="translateshaderclass.cpp" />
|
<ClCompile Include="translateshaderclass.cpp" />
|
||||||
|
<ClCompile Include="transparentshaderclass.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="alphamapshaderclass.h" />
|
<ClInclude Include="alphamapshaderclass.h" />
|
||||||
@ -103,6 +104,7 @@
|
|||||||
<ClInclude Include="textureshaderclass.h" />
|
<ClInclude Include="textureshaderclass.h" />
|
||||||
<ClInclude Include="Timerclass.h" />
|
<ClInclude Include="Timerclass.h" />
|
||||||
<ClInclude Include="translateshaderclass.h" />
|
<ClInclude Include="translateshaderclass.h" />
|
||||||
|
<ClInclude Include="transparentshaderclass.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="alphamap.ps" />
|
<None Include="alphamap.ps" />
|
||||||
@ -124,6 +126,8 @@
|
|||||||
<None Include="specmap.vs" />
|
<None Include="specmap.vs" />
|
||||||
<None Include="texture.ps" />
|
<None Include="texture.ps" />
|
||||||
<None Include="texture.vs" />
|
<None Include="texture.vs" />
|
||||||
|
<None Include="transparent.ps" />
|
||||||
|
<None Include="transparent.vs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="Color.ps">
|
<None Include="Color.ps">
|
||||||
|
@ -156,6 +156,12 @@
|
|||||||
<ClCompile Include="reflectionshaderclass.cpp">
|
<ClCompile Include="reflectionshaderclass.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="shadermanagerclass.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="transparentshaderclass.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="systemclass.h">
|
<ClInclude Include="systemclass.h">
|
||||||
@ -281,6 +287,12 @@
|
|||||||
<ClInclude Include="reflectionshaderclass.h">
|
<ClInclude Include="reflectionshaderclass.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="shadermanagerclass.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="transparentshaderclass.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Image Include="font01.tga">
|
<Image Include="font01.tga">
|
||||||
@ -313,6 +325,9 @@
|
|||||||
<Image Include="..\..\..\..\Downloads\grass.tga">
|
<Image Include="..\..\..\..\Downloads\grass.tga">
|
||||||
<Filter>Assets</Filter>
|
<Filter>Assets</Filter>
|
||||||
</Image>
|
</Image>
|
||||||
|
<Image Include="spec02.tga">
|
||||||
|
<Filter>Assets</Filter>
|
||||||
|
</Image>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
@ -376,8 +391,18 @@
|
|||||||
<None Include="translate.ps">
|
<None Include="translate.ps">
|
||||||
<Filter>shader</Filter>
|
<Filter>shader</Filter>
|
||||||
</None>
|
</None>
|
||||||
<None Include="reflection.vs" />
|
<None Include="reflection.ps">
|
||||||
<None Include="reflection.ps" />
|
<Filter>shader</Filter>
|
||||||
|
</None>
|
||||||
|
<None Include="reflection.vs">
|
||||||
|
<Filter>shader</Filter>
|
||||||
|
</None>
|
||||||
|
<None Include="transparent.ps">
|
||||||
|
<Filter>shader</Filter>
|
||||||
|
</None>
|
||||||
|
<None Include="transparent.vs">
|
||||||
|
<Filter>shader</Filter>
|
||||||
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Text Include="font01.txt">
|
<Text Include="font01.txt">
|
||||||
@ -398,8 +423,5 @@
|
|||||||
<Text Include="square.txt">
|
<Text Include="square.txt">
|
||||||
<Filter>assets</Filter>
|
<Filter>assets</Filter>
|
||||||
</Text>
|
</Text>
|
||||||
<ClCompile Include="shadermanagerclass.cpp" />
|
|
||||||
<ClInclude Include="shadermanagerclass.h" />
|
|
||||||
<Image Include="spec02.tga" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -8,6 +8,7 @@ ShaderManagerClass::ShaderManagerClass()
|
|||||||
m_TranslateShader = 0;
|
m_TranslateShader = 0;
|
||||||
m_AlphaMapShader = 0;
|
m_AlphaMapShader = 0;
|
||||||
m_SpecMapShader = 0;
|
m_SpecMapShader = 0;
|
||||||
|
m_TransparentShader = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -79,6 +80,15 @@ bool ShaderManagerClass::Initialize(ID3D11Device* device, HWND hwnd)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create and initialize the specular map shader object.
|
||||||
|
m_TransparentShader = new TransparentShaderClass;
|
||||||
|
|
||||||
|
result = m_TransparentShader->Initialize(device, hwnd);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,6 +142,14 @@ void ShaderManagerClass::Shutdown()
|
|||||||
m_SpecMapShader = 0;
|
m_SpecMapShader = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Release the transparent shader object.
|
||||||
|
if (m_TransparentShader)
|
||||||
|
{
|
||||||
|
m_TransparentShader->Shutdown();
|
||||||
|
delete m_TransparentShader;
|
||||||
|
m_TransparentShader = 0;
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include "translateshaderclass.h"
|
#include "translateshaderclass.h"
|
||||||
#include "alphamapshaderclass.h"
|
#include "alphamapshaderclass.h"
|
||||||
#include "specmapshaderclass.h"
|
#include "specmapshaderclass.h"
|
||||||
|
#include "transparentshaderclass.h"
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -32,6 +33,7 @@ public:
|
|||||||
bool RenderAlphaMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
bool RenderAlphaMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||||
bool RenderSpecMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*,
|
bool RenderSpecMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*,
|
||||||
XMFLOAT3, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
XMFLOAT3, XMFLOAT4, XMFLOAT3, XMFLOAT4, float);
|
||||||
|
bool RenderTransparentShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TextureShaderClass* m_TextureShader;
|
TextureShaderClass* m_TextureShader;
|
||||||
@ -40,6 +42,7 @@ private:
|
|||||||
TranslateShaderClass* m_TranslateShader;
|
TranslateShaderClass* m_TranslateShader;
|
||||||
AlphaMapShaderClass* m_AlphaMapShader;
|
AlphaMapShaderClass* m_AlphaMapShader;
|
||||||
SpecMapShaderClass* m_SpecMapShader;
|
SpecMapShaderClass* m_SpecMapShader;
|
||||||
|
TransparentShaderClass* m_TransparentShader;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
38
enginecustom/transparent.ps
Normal file
38
enginecustom/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/transparent.vs
Normal file
48
enginecustom/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;
|
||||||
|
}
|
413
enginecustom/transparentshaderclass.cpp
Normal file
413
enginecustom/transparentshaderclass.cpp
Normal file
@ -0,0 +1,413 @@
|
|||||||
|
#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)
|
||||||
|
{
|
||||||
|
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"transparent.vs");
|
||||||
|
if (error != 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the filename of the pixel shader.
|
||||||
|
error = wcscpy_s(psFilename, 128, L"transparent.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 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)
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
{
|
||||||
|
MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
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 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))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TransparentShaderClass::ShutdownShader()
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
61
enginecustom/transparentshaderclass.h
Normal file
61
enginecustom/transparentshaderclass.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#ifndef _TRANSPARENTSHADERCLASS_H_
|
||||||
|
#define _TRANSPARENTSHADERCLASS_H_
|
||||||
|
|
||||||
|
|
||||||
|
//////////////
|
||||||
|
// INCLUDES //
|
||||||
|
//////////////
|
||||||
|
#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
|
Loading…
x
Reference in New Issue
Block a user