351 lines
7.8 KiB
C++
351 lines
7.8 KiB
C++
#include "applicationclass.h"
|
|
|
|
ApplicationClass::ApplicationClass()
|
|
{
|
|
m_Direct3D = 0;
|
|
m_Camera = 0;
|
|
m_Model = 0;
|
|
m_LightShader = 0;
|
|
m_Lights = 0;
|
|
}
|
|
|
|
|
|
ApplicationClass::ApplicationClass(const ApplicationClass& other)
|
|
{
|
|
}
|
|
|
|
|
|
ApplicationClass::~ApplicationClass()
|
|
{
|
|
}
|
|
|
|
|
|
bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
|
{
|
|
char modelFilename[128];
|
|
char textureFilename[128];
|
|
bool result;
|
|
|
|
// Create the Direct3D object.
|
|
m_Direct3D = new D3DClass;
|
|
if (!m_Direct3D)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
result = m_Direct3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);
|
|
if (!result)
|
|
{
|
|
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, -10.0f);
|
|
m_Camera->SetRotation(0.0f, 0.0f, 0.0f);
|
|
|
|
// Set the file name of the model.
|
|
strcpy_s(modelFilename, "cube.txt");
|
|
|
|
// Set the name of the texture file that we will be loading.
|
|
strcpy_s(textureFilename, "stone01.tga");
|
|
|
|
// Create and initialize the model object.
|
|
m_Model = new ModelClass;
|
|
|
|
result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename);
|
|
if (!result)
|
|
{
|
|
MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
|
|
return false;
|
|
}
|
|
|
|
// Create and initialize the light shader object.
|
|
m_LightShader = new LightShaderClass;
|
|
|
|
result = m_LightShader->Initialize(m_Direct3D->GetDevice(), hwnd);
|
|
if (!result)
|
|
{
|
|
MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
|
|
return false;
|
|
}
|
|
// Set the number of lights we will use.
|
|
m_numLights = 4;
|
|
|
|
// Create and initialize the light objects array.
|
|
m_Lights = new LightClass[m_numLights];
|
|
|
|
// Manually set the color and position of each light.
|
|
m_Lights[0].SetDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); // Red
|
|
m_Lights[0].SetPosition(-3.0f, 1.0f, 3.0f);
|
|
|
|
m_Lights[1].SetDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); // Green
|
|
m_Lights[1].SetPosition(3.0f, 1.0f, 3.0f);
|
|
|
|
m_Lights[2].SetDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); // Blue
|
|
m_Lights[2].SetPosition(-3.0f, 1.0f, -3.0f);
|
|
|
|
m_Lights[3].SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); // White
|
|
m_Lights[3].SetPosition(3.0f, 1.0f, -3.0f);
|
|
// 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, -1.0f, 1.0f);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void ApplicationClass::Shutdown()
|
|
{
|
|
// Release the light object.
|
|
if (m_Lights)
|
|
{
|
|
delete m_Lights;
|
|
m_Lights = 0;
|
|
}
|
|
|
|
// Release the light shader object.
|
|
if (m_LightShader)
|
|
{
|
|
m_LightShader->Shutdown();
|
|
delete m_LightShader;
|
|
m_LightShader = 0;
|
|
}
|
|
|
|
// Release the model object.
|
|
if (m_Model)
|
|
{
|
|
m_Model->Shutdown();
|
|
delete m_Model;
|
|
m_Model = 0;
|
|
}
|
|
|
|
// Release the camera object.
|
|
if (m_Camera)
|
|
{
|
|
delete m_Camera;
|
|
m_Camera = 0;
|
|
}
|
|
|
|
// Release the D3D object.
|
|
if (m_Direct3D)
|
|
{
|
|
m_Direct3D->Shutdown();
|
|
delete m_Direct3D;
|
|
m_Direct3D = 0;
|
|
}
|
|
|
|
// Liberez la memoire pour chaque cube
|
|
for (auto cube : m_cubes)
|
|
{
|
|
cube->Shutdown();
|
|
delete cube;
|
|
}
|
|
m_cubes.clear();
|
|
|
|
// Liberez la memoire pour chaque cube du terrain
|
|
for (auto cube : m_terrainChunk)
|
|
{
|
|
cube->Shutdown();
|
|
delete cube;
|
|
}
|
|
m_terrainChunk.clear();
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
bool ApplicationClass::Frame()
|
|
{
|
|
static float rotation = 0.0f;
|
|
bool result;
|
|
|
|
// Update the rotation variable each frame.
|
|
rotation -= 0.0174532925f * speed;
|
|
if (rotation < 0.0f)
|
|
{
|
|
rotation += 360.0f;
|
|
}
|
|
|
|
// Render the graphics scene.
|
|
result = Render(rotation);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool ApplicationClass::Render(float rotation)
|
|
{
|
|
XMMATRIX worldMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix, viewMatrix, projectionMatrix;;
|
|
XMFLOAT4 diffuseColor[4], lightPosition[4];
|
|
int i;
|
|
bool result;
|
|
|
|
// Clear the buffers to begin the scene.
|
|
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
// 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);
|
|
viewMatrix = m_Camera->GetViewMatrix();
|
|
m_Direct3D->GetProjectionMatrix(projectionMatrix);
|
|
|
|
// Get the light properties.
|
|
for (i = 0; i < m_numLights; i++)
|
|
{
|
|
// Create the diffuse color array from the four light colors.
|
|
diffuseColor[i] = m_Lights[i].GetDiffuseColor();
|
|
|
|
// Create the light position array from the four light positions.
|
|
lightPosition[i] = m_Lights[i].GetPosition();
|
|
}
|
|
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
|
m_Model->Render(m_Direct3D->GetDeviceContext());
|
|
|
|
|
|
for (auto cube : m_cubes)
|
|
{
|
|
cube->Render(m_Direct3D->GetDeviceContext());
|
|
|
|
scaleMatrix = cube->GetScaleMatrix();
|
|
|
|
if (cube->m_demoSpinning)
|
|
rotateMatrix = XMMatrixRotationY(rotation);
|
|
else
|
|
{
|
|
rotateMatrix = cube->GetRotateMatrix();
|
|
}
|
|
|
|
|
|
translateMatrix = cube->GetTranslateMatrix();
|
|
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
|
|
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
|
|
|
|
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), cube->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, cube->GetTexture(),
|
|
diffuseColor, lightPosition);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Render terrain
|
|
for (auto chunk : m_terrainChunk)
|
|
{
|
|
chunk->Render(m_Direct3D->GetDeviceContext());
|
|
|
|
scaleMatrix = chunk->GetScaleMatrix();
|
|
rotateMatrix = chunk->GetRotateMatrix();
|
|
translateMatrix = chunk->GetTranslateMatrix();
|
|
|
|
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
|
|
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
|
|
|
|
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(),
|
|
diffuseColor, lightPosition);
|
|
if (!result)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
// Present the rendered scene to the screen.
|
|
m_Direct3D->EndScene();
|
|
|
|
return true;
|
|
}
|
|
|
|
D3DClass* ApplicationClass::GetDirect3D()
|
|
{
|
|
return m_Direct3D;
|
|
}
|
|
|
|
int ApplicationClass::GetScreenWidth() const
|
|
{
|
|
return GetSystemMetrics(SM_CXSCREEN);
|
|
}
|
|
|
|
int ApplicationClass::GetScreenHeight() const
|
|
{
|
|
return GetSystemMetrics(SM_CYSCREEN);
|
|
}
|
|
|
|
void ApplicationClass::GenerateTerrain()
|
|
{
|
|
// Set the file name of the model.
|
|
char modelFilename[128];
|
|
|
|
// check if a chunk file already exists
|
|
strcpy_s(modelFilename, "chunk.txt");
|
|
|
|
// Set the name of the texture file that we will be loading.
|
|
char textureFilename[128];
|
|
strcpy_s(textureFilename, "stone01.tga");
|
|
|
|
Object* newTerrain = new Object();
|
|
newTerrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename);
|
|
|
|
newTerrain->SetTranslateMatrix(XMMatrixTranslation(0.0f, -1.0f, 0.0f));
|
|
newTerrain->SetRotateMatrix(XMMatrixRotationY(180.0f));
|
|
|
|
m_cubes.push_back(newTerrain);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplicationClass::AddCube()
|
|
{
|
|
char modelFilename[128];
|
|
char textureFilename[128];
|
|
|
|
// Set the file name of the model.
|
|
strcpy_s(modelFilename, "cube.txt");
|
|
|
|
// Set the name of the texture file that we will be loading.
|
|
strcpy_s(textureFilename, "stone01.tga");
|
|
static int cubeCount = 0;
|
|
float position = cubeCount * 2.0f;
|
|
Object* newCube = new Object();
|
|
newCube->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename);
|
|
|
|
newCube->SetTranslateMatrix(XMMatrixTranslation(position, 0.0f, 0.0f));
|
|
|
|
m_cubes.push_back(newCube);
|
|
}
|
|
|
|
void ApplicationClass::DeleteCube(int index)
|
|
{
|
|
if (index < m_cubes.size())
|
|
{
|
|
m_cubes[index]->Shutdown();
|
|
delete m_cubes[index];
|
|
m_cubes.erase(m_cubes.begin() + index);
|
|
}
|
|
}
|
|
|
|
void ApplicationClass::DeleteTerrain()
|
|
{
|
|
for (auto cube : m_terrainChunk)
|
|
{
|
|
cube->Shutdown();
|
|
delete cube;
|
|
}
|
|
m_terrainChunk.clear();
|
|
} |