Ajout transparence buggé

This commit is contained in:
GolfOcean334
2024-04-09 13:03:26 +02:00
parent cae1fa326f
commit 1b4762940c
10 changed files with 711 additions and 5 deletions

View File

@@ -5,6 +5,8 @@ ApplicationClass::ApplicationClass()
m_Direct3D = 0;
m_Camera = 0;
m_Model = 0;
m_Model1 = 0;
m_Model2 = 0;
m_LightShader = 0;
m_Light = 0;
m_TextureShader = 0;
@@ -26,6 +28,7 @@ ApplicationClass::ApplicationClass()
m_Frustum = 0;
m_DisplayPlane = 0;
m_ReflectionShader = 0;
m_TransparentShader = 0;
}
@@ -140,6 +143,9 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
return false;
}
// Create and initialize the render to texture object.
m_RenderTexture = new RenderTextureClass;
@@ -233,6 +239,37 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
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.
m_LightShader = new LightShaderClass;
@@ -364,6 +401,29 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
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.
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];
int modelCount, renderCount, i;
bool result, renderModel;
float blendAmount;
// Set the blending amount to 50%.
blendAmount = 0.5f;
// Clear the buffers to begin the scene.
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.
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.
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Camera->GetViewMatrix(viewMatrix);