Minor Update - Object Rendering change

+ RenderPass (render object in a vector)
This commit is contained in:
CatChow0 2025-01-11 13:53:35 +01:00
parent bce659e55d
commit 915c0cdd7f
9 changed files with 220 additions and 155 deletions

View File

@ -9,34 +9,28 @@ SystemClass::SystemClass()
m_Input = 0;
m_Application = 0;
m_imguiManager = 0;
m_applicationName = 0;
m_hinstance = 0;
m_hwnd = 0;
m_initialWindowWidth = 0;
m_initialWindowHeight = 0;
m_isDirect3DInitialized = false;
}
SystemClass::SystemClass(const SystemClass& other)
{
}
SystemClass::~SystemClass()
{
}
bool SystemClass::Initialize()
{
int screenWidth, screenHeight;
int screenHeight, screenWidth = 0;
bool result;
Logger::Get().Log("Initializing system class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
try
{
// Initialize the width and height of the screen to zero before sending the variables into the function.
screenWidth = 0;
screenHeight = 0;
m_initialWindowWidth = 0;
m_initialWindowHeight = 0;
m_isDirect3DInitialized = false;
// Initialize the windows api.
InitializeWindows(screenWidth, screenHeight);
@ -149,6 +143,10 @@ void SystemClass::Run()
// Loop until there is a quit message from the window or the user.
done = false;
auto fixedUpdateInterval = std::chrono::milliseconds(16);
auto m_lastFixedUpdateTime = std::chrono::steady_clock::now();
while (!done)
{
// Handle the windows messages.
@ -180,6 +178,13 @@ void SystemClass::Run()
Logger::Get().Log("Failed to process frame", __FILE__, __LINE__, Logger::LogLevel::Error);
done = true;
}
auto now = std::chrono::steady_clock::now();
if (now - m_lastFixedUpdateTime >= fixedUpdateInterval)
{
FixedUpdate();
m_lastFixedUpdateTime = now;
}
}
}
@ -266,6 +271,7 @@ LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam
m_initialWindowWidth = newWidth;
m_initialWindowHeight = newHeight;
}
return 0;
}
case WM_ENTERSIZEMOVE:
{
@ -470,4 +476,9 @@ void SystemClass::SendPath(wchar_t* path, std::filesystem::path WFolder)
{
m_Application->SetPath(path);
m_Application->SetWFolder(WFolder);
}
void SystemClass::FixedUpdate()
{
m_Application->GetPhysics()->Update();
}

View File

@ -27,12 +27,6 @@ ApplicationClass::ApplicationClass() : m_ShouldQuit(false)
m_Physics = 0;
}
ApplicationClass::ApplicationClass(const ApplicationClass& other)
{
}
ApplicationClass::~ApplicationClass()
{
}
@ -725,7 +719,7 @@ bool ApplicationClass::Frame(InputClass* Input)
m_Camera->SetRotation(rotationX, rotationY, 0.0f);
m_Camera->Render();
// Render the graphics scene.
// Render the static graphics scene.
result = Render(rotation, x, y, z, textureTranslation);
if (!result)
{
@ -851,7 +845,7 @@ bool ApplicationClass::Frame(InputClass* Input)
position = position + object->GetVelocity() * frameTime;
object->SetPosition(position);
m_Physics->ApplyGravity(object, 1.0f, frameTime);
m_Physics->ApplyGravity(object, 1.0f);
// Check if the object has fallen below a certain position
if (XMVectorGetY(object->GetPosition()) < -30.0f)
@ -1074,134 +1068,15 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
diffuseColor, lightPosition, ambientColor);
for (auto cube : m_cubes)
// -------------------------------------------------------- //
// ------------ Render the object in the queue ------------ //
// -------------------------------------------------------- //
result = RenderPass(m_terrainChunk, diffuseColor, lightPosition, ambientColor, viewMatrix, projectionMatrix);
if (!result)
{
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);
cube->Render(m_Direct3D->GetDeviceContext());
if (!m_enableCelShading) {
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), cube->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, cube->GetTexture(0),
diffuseColor, lightPosition, ambientColor);
if (!result)
{
Logger::Get().Log("Could not render the cube model using the light shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
}
// Render cel shading globally to the scene using the cel shader if the checkbox is checked.
if (m_enableCelShading) {
result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), cube->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, cube->GetTexture(0),
m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), TrueLightPosition);
if (!result)
{
Logger::Get().Log("Could not render the model using the cel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
}
}
for (auto& object : m_object)
{
scaleMatrix = object->GetScaleMatrix();
if (object->m_demoSpinning)
rotateMatrix = XMMatrixRotationY(rotation);
else
{
rotateMatrix = object->GetRotateMatrix();
}
translateMatrix = object->GetTranslateMatrix();
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
object->Render(m_Direct3D->GetDeviceContext());
if (!m_enableCelShading) {
// check if m_object is null or not
if (object == nullptr) {
Logger::Get().Log("Object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
object->GetTexture(0), diffuseColor, lightPosition, ambientColor);
if (!result)
{
Logger::Get().Log("Could not render the object model using the light shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
}
// Render cel shading globally to the scene using the cel shader if the checkbox is checked.
if (m_enableCelShading) {
result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, object->GetTexture(0),
m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), TrueLightPosition);
if (!result)
{
Logger::Get().Log("Could not render the model using the cel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
}
}
// Render terrain
for (auto& chunk : m_terrainChunk)
{
scaleMatrix = chunk->GetScaleMatrix();
rotateMatrix = chunk->GetRotateMatrix();
translateMatrix = chunk->GetTranslateMatrix();
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
chunk->Render(m_Direct3D->GetDeviceContext());
if (!m_enableCelShading) {
if (chunk->GetTexture(0) == nullptr)
{
Logger::Get().Log("Could not render the terrain model using the light shader, texture is null", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), chunk->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, chunk->GetTexture(0), diffuseColor, lightPosition, ambientColor );
if (!result)
{
Logger::Get().Log("Could not render the terrain model using the light shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
}
// Render cel shading globally to the scene using the cel shader if the checkbox is checked.
if (m_enableCelShading) {
result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), chunk->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, chunk->GetTexture(0),
m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), TrueLightPosition);
if (!result)
{
Logger::Get().Log("Could not render the model using the cel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
}
Logger::Get().Log("Could not render the model using any shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
// Translate to where the bath model will be rendered.
@ -1720,6 +1595,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath)
m_object.push_back(newObject);
// Vérifiez que l'objet a bien reçu les textures
if (newObject->GetTexture(0) == nullptr)
{
@ -2026,4 +1902,83 @@ void ApplicationClass::SetScreenWidth(int width)
// log the new screen width
Logger::Get().Log("Setting screen width to " + std::to_string(width), __FILE__, __LINE__);
m_screenWidth = width;
}
bool ApplicationClass::RenderPass(std::vector<Object*> RenderQueue, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection)
{
XMMATRIX worldMatrix, scaleMatrix, rotateMatrix, translateMatrix, srMatrix;
bool result;
for (auto& object : RenderQueue)
{
if (object == nullptr)
{
Logger::Get().Log("Object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
scaleMatrix = object->GetScaleMatrix();
rotateMatrix = object->GetRotateMatrix();
translateMatrix = object->GetTranslateMatrix();
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
object->Render(m_Direct3D->GetDeviceContext());
// Render cel shading.
if (object->GetCelShading()) {
result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0),
m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), TrueLightPosition);
if (!result)
{
Logger::Get().Log("Could not render the model using the cel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
continue;
}
// Render normal mapping.
if (object->GetNormalMappingEnabled()) {
result = m_ShaderManager->RenderNormalMapShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0), object->GetTexture(1), m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor());
if (!result)
{
Logger::Get().Log("Could not render the model using the normal map shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
continue;
}
// Render Specular mapping.
if (object->GetSpecularMappingEnabled()) {
result = m_ShaderManager->RenderSpecMapShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0), object->GetTexture(1), object->GetTexture(2), m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), m_Camera->GetPosition(), m_Lights[0]->GetSpecularColor(), m_Lights[0]->GetSpecularPower());
if (!result)
{
Logger::Get().Log("Could not render the model using the specular map shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
continue;
}
// By default, render the object using the light shader.
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection,
object->GetTexture(0) ,diffuse, position, ambient);
if (!result)
{
Logger::Get().Log("Could not render the object model using the light shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
continue;
}
return true;
}

View File

@ -102,14 +102,18 @@ public:
bool IsWindowed() const;
void SetWindowed(bool windowed);
Physics* GetPhysics() const { return m_Physics; };
private:
bool Render(float, float, float, float, float);
bool RenderPhysics(float x, float y, float z);
bool UpdateMouseStrings(int, int, bool);
bool UpdateFps();
bool UpdateRenderCountString(int);
bool RenderSceneToTexture(float);
bool RenderRefractionToTexture();
bool RenderReflectionToTexture();
bool RenderPass(std::vector<Object*> RenderQueue, XMFLOAT4* diffuse,XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX proijection);
private :

View File

@ -13,7 +13,6 @@ Size=457,294
[Window][Terrain]
Pos=60,60
Size=342,82
Collapsed=1
[Window][Light]
Pos=1548,17
@ -24,6 +23,6 @@ Pos=471,90
Size=180,79
[Window][Engine Settings]
Pos=561,352
Pos=106,213
Size=168,77

View File

@ -225,3 +225,72 @@ int Object::GetId() const
return m_id;
}
bool Object::IsPhysicsEnabled() const
{
return m_isPhysicsEnabled;
}
void Object::SetPhysicsEnabled(bool state)
{
m_isPhysicsEnabled = state;
}
void Object::SetCelShading(bool state)
{
isCelShading = state;
}
bool Object::GetCelShading() const
{
return isCelShading;
}
void Object::SetLightingEnabled(bool state)
{
isLightingEnabled = state;
}
bool Object::GetLightingEnabled() const
{
return isLightingEnabled;
}
void Object::SetNormalMappingEnabled(bool state)
{
isNormalMappingEnabled = state;
}
bool Object::GetNormalMappingEnabled() const
{
return isNormalMappingEnabled;
}
void Object::SetSpecularMappingEnabled(bool state)
{
isSpecularMappingEnabled = state;
}
bool Object::GetSpecularMappingEnabled() const
{
return isSpecularMappingEnabled;
}
void Object::SetReflectionEnabled(bool state)
{
isReflectionEnabled = state;
}
bool Object::GetReflectionEnabled() const
{
return isReflectionEnabled;
}
void Object::SetRefractionEnabled(bool state)
{
isRefractionEnabled = state;
}
bool Object::GetRefractionEnabled() const
{
return isRefractionEnabled;
}

View File

@ -38,6 +38,9 @@ public:
float GetMass() const;
void SetGrounded(bool);
bool IsGrounded() const;
bool IsPhysicsEnabled() const;
void SetPhysicsEnabled(bool state);
void UpdateWorldMatrix();
void UpdateSRMatrix();
@ -54,6 +57,20 @@ public:
bool LoadTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, const std::wstring& filename);
// Setters and getters for the shader to use
void SetCelShading(bool state);
bool GetCelShading() const;
void SetLightingEnabled(bool state);
bool GetLightingEnabled() const;
void SetNormalMappingEnabled(bool state);
bool GetNormalMappingEnabled() const;
void SetSpecularMappingEnabled(bool state);
bool GetSpecularMappingEnabled() const;
void SetReflectionEnabled(bool state);
bool GetReflectionEnabled() const;
void SetRefractionEnabled(bool state);
bool GetRefractionEnabled() const;
public :
bool m_demoSpinning = false;
XMVECTOR m_previousPosition;
@ -70,6 +87,15 @@ private:
XMVECTOR m_acceleration;
float m_mass;
bool m_isGrounded;
bool m_isPhysicsEnabled;
std::string m_name;
// Shader to use for rendering
bool isCelShading = false;
bool isLightingEnabled = true;
bool isNormalMappingEnabled = false;
bool isSpecularMappingEnabled = false;
bool isReflectionEnabled = false;
bool isRefractionEnabled = false;
};

View File

@ -28,7 +28,7 @@ void Physics::SetGravity(XMVECTOR gravity)
}
// Apply gravity to an object
void Physics::ApplyGravity(Object* object, float dragValue, float frameTime)
void Physics::ApplyGravity(Object* object, float dragValue)
{
if (object == nullptr) // Verify if the object is not null
{
@ -53,7 +53,7 @@ void Physics::ApplyGravity(Object* object, float dragValue, float frameTime)
XMVECTOR velocity = object->GetVelocity();
// Update the velocity with the object's acceleration
velocity += object->GetAcceleration() * frameTime;
velocity += object->GetAcceleration();
// Set the new velocity
object->SetVelocity(velocity);
@ -174,5 +174,4 @@ bool Physics::SphereCubeOverlap(Object* cube, Object* sphere)
float radius = XMVectorGetX(XMVector3Length(scale2));
return distance < radius;
}
}

View File

@ -13,7 +13,7 @@ public:
XMVECTOR GetGravity() const; // Get the gravity value
void SetGravity(XMVECTOR gravity); // Define the gravity value
void ApplyGravity(Object*, float, float); // Apply gravity to an object
void ApplyGravity(Object*, float); // Apply gravity to an object
void AddForce(Object*, XMVECTOR);
bool IsColliding(Object*, Object*);
bool CubesOverlap(Object*, Object*);

View File

@ -10,6 +10,7 @@
#include "imguiManager.h"
#include <mutex>
#include "resources.h"
#include <chrono>
class SystemClass
{
@ -21,6 +22,7 @@ public:
bool Initialize();
void Shutdown();
void Run();
void FixedUpdate();
LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM);