Boutton "Add object" ajoute un cube qui tourne

This commit is contained in:
CatChow0 2024-03-25 17:38:34 +01:00
parent 7ad8ed3483
commit 7ef3237d88
9 changed files with 156 additions and 30 deletions

View File

@ -163,6 +163,7 @@ bool SystemClass::Frame()
m_Application->SetSpeed(speed);
m_imguiManager->WidgetButton();
m_imguiManager->WidgetFPS();
m_imguiManager->WidgetAddObject(m_Application);
ImGui::End();

View File

@ -126,6 +126,14 @@ void ApplicationClass::Shutdown()
m_Direct3D = 0;
}
// Libérez la mémoire pour chaque cube
for (auto cube : m_cubes)
{
cube->Shutdown();
delete cube;
}
m_cubes.clear();
return;
}
@ -170,34 +178,22 @@ bool ApplicationClass::Render(float rotation)
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);
// Render the first cube
scaleMatrix = XMMatrixScaling(0.5f, 0.5f, 0.5f);
rotateMatrix = XMMatrixRotationX(rotation);
translateMatrix = XMMatrixTranslation(-2.0f, 0.0f, 0.0f);
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
m_Model->Render(m_Direct3D->GetDeviceContext());
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(),
m_Light->GetDirection(), m_Light->GetDiffuseColor());
if (!result)
for (auto cube : m_cubes)
{
return false;
}
cube->Render(m_Direct3D->GetDeviceContext());
// Render the second cube
scaleMatrix = XMMatrixScaling(1.5f, 1.5f, 1.5f);
rotateMatrix = XMMatrixRotationY(rotation);
translateMatrix = XMMatrixTranslation(2.0f, 0.0f, 0.0f);
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
scaleMatrix = cube->GetScaleMatrix();
rotateMatrix = XMMatrixRotationY(rotation);
translateMatrix = cube->GetTranslateMatrix();
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
m_Model->Render(m_Direct3D->GetDeviceContext());
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;
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), cube->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, cube->GetTexture(),
m_Light->GetDirection(), m_Light->GetDiffuseColor());
if (!result)
{
return false;
}
}
// Present the rendered scene to the screen.
@ -220,3 +216,22 @@ int ApplicationClass::GetScreenHeight() const
{
return GetSystemMetrics(SM_CYSCREEN);
}
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");
Object* newCube = new Object();
newCube->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename);
newCube->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f));
m_cubes.push_back(newCube);
}

View File

@ -7,9 +7,10 @@
///////////////////////
#include "d3dclass.h"
#include "cameraclass.h"
#include "modelclass.h"
#include "object.h"
#include "lightshaderclass.h"
#include "lightclass.h"
#include <vector>
/////////////
// GLOBALS //
@ -41,6 +42,9 @@ public:
float GetSpeed() const { return speed; };
void SetSpeed(float speed) { this->speed = speed; };
void AddCube();
int GetCubeCount() const { return m_cubes.size(); };
private:
bool Render(float);
@ -52,6 +56,7 @@ private:
LightShaderClass* m_LightShader;
LightClass* m_Light;
float speed = 0.1f;
std::vector<Object*> m_cubes;
};
#endif

View File

@ -37,6 +37,7 @@
<ClCompile Include="lightshaderclass.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="modelclass.cpp" />
<ClCompile Include="object.cpp" />
<ClCompile Include="Systemclass.cpp" />
<ClCompile Include="textureclass.cpp" />
</ItemGroup>
@ -58,6 +59,7 @@
<ClInclude Include="inputclass.h" />
<ClInclude Include="lightshaderclass.h" />
<ClInclude Include="modelclass.h" />
<ClInclude Include="object.h" />
<ClInclude Include="systemclass.h" />
<ClInclude Include="textureclass.h" />
</ItemGroup>

View File

@ -3,6 +3,6 @@ Pos=60,60
Size=400,400
[Window][Khaotic Engine]
Pos=21,21
Pos=522,44
Size=694,367

View File

@ -1,4 +1,5 @@
#include "imguiManager.h"
#include "applicationclass.h"
imguiManager::imguiManager()
{
@ -62,7 +63,15 @@ void imguiManager::WidgetFPS()
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate);
}
void imguiManager::WidgetFullscreen(bool* fullscreen)
void imguiManager::WidgetAddObject(ApplicationClass* app)
{
ImGui::Checkbox("Fullscreen", fullscreen);
if (ImGui::CollapsingHeader("Objects"))
{
if (ImGui::Button("Add Cube"))
{
app->AddCube();
}
ImGui::SameLine();
ImGui::Text("Number of cubes: %d", app->GetCubeCount());
}
}

View File

@ -7,6 +7,8 @@
#include <imgui_impl_win32.h>
#include <windows.h>
class ApplicationClass;
class imguiManager
{
public:
@ -22,7 +24,7 @@ public:
void WidgetSpeedSlider(float* speed);
void WidgetButton();
void WidgetFPS();
void WidgetFullscreen(bool* fullscreen);
void WidgetAddObject(ApplicationClass* app);
private:
ImGuiIO* io;

64
enginecustom/object.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "object.h"
Object::Object() : ModelClass()
{
m_scaleMatrix = XMMatrixIdentity();
m_rotateMatrix = XMMatrixIdentity();
m_translateMatrix = XMMatrixIdentity();
m_srMatrix = XMMatrixIdentity();
m_worldMatrix = XMMatrixIdentity();
}
Object::~Object()
{
}
void Object::SetScaleMatrix(XMMATRIX scaleMatrix)
{
m_scaleMatrix = scaleMatrix;
}
void Object::SetRotateMatrix(XMMATRIX rotateMatrix)
{
m_rotateMatrix = rotateMatrix;
}
void Object::SetTranslateMatrix(XMMATRIX translateMatrix)
{
m_translateMatrix = translateMatrix;
}
void Object::SetSRMatrix(XMMATRIX srMatrix)
{
m_srMatrix = srMatrix;
}
void Object::SetWorldMatrix(XMMATRIX worldMatrix)
{
m_worldMatrix = worldMatrix;
}
XMMATRIX Object::GetScaleMatrix()
{
return m_scaleMatrix;
}
XMMATRIX Object::GetRotateMatrix()
{
return m_rotateMatrix;
}
XMMATRIX Object::GetTranslateMatrix()
{
return m_translateMatrix;
}
XMMATRIX Object::GetSRMatrix()
{
return m_srMatrix;
}
XMMATRIX Object::GetWorldMatrix()
{
return m_worldMatrix;
}

28
enginecustom/object.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include "modelclass.h"
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);
XMMATRIX GetScaleMatrix();
XMMATRIX GetRotateMatrix();
XMMATRIX GetTranslateMatrix();
XMMATRIX GetSRMatrix();
XMMATRIX GetWorldMatrix();
private:
XMMATRIX m_scaleMatrix;
XMMATRIX m_rotateMatrix;
XMMATRIX m_translateMatrix;
XMMATRIX m_srMatrix;
XMMATRIX m_worldMatrix;
};