Merge branch 'main' into Imgui

This commit is contained in:
2024-03-22 17:19:06 +01:00
15 changed files with 1119 additions and 55 deletions

View File

@@ -1,15 +1,12 @@
////////////////////////////////////////////////////////////////////////////////
// Filename: applicationclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "applicationclass.h"
ApplicationClass::ApplicationClass()
{
m_Direct3D = 0;
m_Camera = 0;
m_Model = 0;
m_ColorShader = 0;
m_LightShader = 0;
m_Light = 0;
}
@@ -25,44 +22,61 @@ ApplicationClass::~ApplicationClass()
bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
char textureFilename[128];
bool result;
// Create and initialize the Direct3D object.
// Create the Direct3D object.
m_Direct3D = new D3DClass;
if (!m_Direct3D)
{
return false;
}
result = m_Direct3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, m_fullscreen, SCREEN_DEPTH, SCREEN_NEAR);
if (!result)
{
MessageBox(hwnd, L"Could not initialize Direct3D", L"Error", MB_OK);
MessageBox(hwnd, L"Could not initialize Direct3D.", L"Error", MB_OK);
return false;
}
// Create the camera object.
m_Camera = new CameraClass;
if (!m_Camera)
{
return false;
}
// Set the initial position of the camera.
m_Camera->SetPosition(0.0f, 0.0f, -5.0f);
m_Camera->SetRotation(0.0f, 0.0f, 10.0f);
// Create and initialize the model object.
m_Model = new ModelClass;
// Set the name of the texture file that we will be loading.
strcpy_s(textureFilename, "stone01.tga");
result = m_Model->Initialize(m_Direct3D->GetDevice());
result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
return false;
}
// Create and initialize the color shader object.
m_ColorShader = new ColorShaderClass;
// Create and initialize the light shader object.
m_LightShader = new LightShaderClass;
result = m_ColorShader->Initialize(m_Direct3D->GetDevice(), hwnd);
result = m_LightShader->Initialize(m_Direct3D->GetDevice(), hwnd);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the color shader object.", L"Error", MB_OK);
MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
return false;
}
// Create and initialize the light object.
m_Light = new LightClass;
m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
m_Light->SetDirection(0.0f, 0.0f, 1.0f);
return true;
}
@@ -70,12 +84,19 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
void ApplicationClass::Shutdown()
{
// Release the color shader object.
if (m_ColorShader)
// Release the light object.
if (m_Light)
{
m_ColorShader->Shutdown();
delete m_ColorShader;
m_ColorShader = 0;
delete m_Light;
m_Light = 0;
}
// Release the light shader object.
if (m_LightShader)
{
m_LightShader->Shutdown();
delete m_LightShader;
m_LightShader = 0;
}
// Release the model object.
@@ -93,7 +114,7 @@ void ApplicationClass::Shutdown()
m_Camera = 0;
}
// Release the Direct3D object.
// Release the D3D object.
if (m_Direct3D)
{
m_Direct3D->Shutdown();
@@ -107,11 +128,19 @@ void ApplicationClass::Shutdown()
bool ApplicationClass::Frame()
{
static float rotation = 0.0f;
bool result;
// Update the rotation variable each frame.
rotation -= 0.0174532925f * 0.1f;
if (rotation < 0.0f)
{
rotation += 360.0f;
}
// Render the graphics scene.
result = Render();
result = Render(rotation);
if (!result)
{
return false;
@@ -121,7 +150,7 @@ bool ApplicationClass::Frame()
}
bool ApplicationClass::Render()
bool ApplicationClass::Render(float rotation)
{
XMMATRIX worldMatrix, viewMatrix, projectionMatrix;
bool result;
@@ -137,17 +166,19 @@ bool ApplicationClass::Render()
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);
// Rotate the world matrix by the rotation value so that the triangle will spin.
worldMatrix = XMMatrixRotationY(rotation);
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
m_Model->Render(m_Direct3D->GetDeviceContext());
// Render the model using the color shader.
result = m_ColorShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix);
// Render the model using the light shader.
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(),
m_Light->GetDirection(), m_Light->GetDiffuseColor());
if (!result)
{
return false;
}
// Present the rendered scene to the screen.
m_Direct3D->EndScene();