Minor Update - Skybox Update - V9.3.0

This commit is contained in:
2025-05-01 16:41:33 +02:00
parent 0e45e0688c
commit e34b44996a
10 changed files with 172 additions and 228 deletions

View File

@@ -0,0 +1,30 @@
#pragma once
#include <vector>
#include "d3dclass.h"
#include "object.h"
class Skybox
{
public:
Skybox();
~Skybox();
void Initialize(D3DClass* d3dClassRef); // Get all the required references
Object* ConstructSkybox();
// Variables
std::vector<ID3D11ShaderResourceView*> textures;
std::vector<XMMATRIX> translations;
private:
D3DClass* m_d3dClassRef; // Reference to the D3DClass instance
Object* m_Skybox;
};

View File

@@ -30,8 +30,10 @@
#include "reflectionshaderclass.h"
#include "physics.h"
#include "frustum.h"
#include <fstream>
#include "skybox.h"
#include <fstream>
#include <WICTextureLoader.h>
#include <comdef.h> // Pour _com_error
#include <chrono>
@@ -167,13 +169,10 @@ private:
bool RenderReflectionToTexture();
bool RenderPass(const std::vector<std::reference_wrapper<std::vector<Object*>>>& RenderQueues, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection);
void ConstructSkybox(); // Construct the skybox
void UpdateSkyboxPosition(); // Update the skybox position
bool RenderSkybox(XMMATRIX view, XMMATRIX projection); // Render the skybox
void UpdateSkyboxPosition();
public :
std::vector<ID3D11ShaderResourceView*> textures;
std::vector<ID3D11ShaderResourceView*> m_SkyboxTextures;
private :
@@ -203,7 +202,6 @@ private :
int m_screenWidth, m_screenHeight;
CameraClass* m_Camera;
PositionClass* m_Position;
std::vector<XMMATRIX> m_SkyboxInitialTranslations;
// ------------------------------------ //
// ------------- OBJECTS -------------- //

View File

@@ -12,7 +12,6 @@ PositionClass::PositionClass()
m_rightTurnSpeed = 0.0f;
m_horizontalTurnSpeed = 0.0f;
m_verticalTurnSpeed = 0.0f;
m_verticalTurnSpeed = 0.0f;
m_cameraSpeed = 4.0f;
m_speed = m_cameraSpeed;
}

View File

@@ -0,0 +1,69 @@
#include "Skybox.h"
Skybox::Skybox()
{
}
Skybox::~Skybox()
{
for (auto& texture : textures)
{
if (texture)
{
texture->Release();
texture = nullptr;
}
}
textures.clear();
translations.clear();
}
void Skybox::Initialize(D3DClass* d3dClassRef)
{
m_d3dClassRef = d3dClassRef;
}
Object* Skybox::ConstructSkybox()
{
Logger::Get().Log("Construct skybox", __FILE__, __LINE__, Logger::LogLevel::Initialize);
char modelFilename[128];
strcpy_s(modelFilename, "assets/Model/OBJ/invertcube.obj");
textures.clear();
std::vector<std::wstring> skyboxTextures = {
L"assets/Skybox/skybox_front.png",
L"assets/Skybox/skybox_back.png",
L"assets/Skybox/skybox_left.png",
L"assets/Skybox/skybox_right.png",
L"assets/Skybox/skybox_top.png",
L"assets/Skybox/skybox_bottom.png"
};
// Load the textures
for (const auto& textureFilename : skyboxTextures)
{
ID3D11ShaderResourceView* texture = nullptr;
HRESULT result = DirectX::CreateWICTextureFromFile(m_d3dClassRef->GetDevice(), m_d3dClassRef->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture);
if (FAILED(result))
{
Logger::Get().Log("Failed to load texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
return nullptr;
}
textures.push_back(texture);
}
// Create the model object
m_Skybox = new Object();
HRESULT result = m_Skybox->Initialize(m_d3dClassRef->GetDevice(), m_d3dClassRef->GetDeviceContext(), modelFilename, textures);
if (!result)
{
Logger::Get().Log("Could not initialize the skybox model object", __FILE__, __LINE__, Logger::LogLevel::Error);
delete m_Skybox;
return nullptr;
}
m_Skybox->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f)); // Set the initial translation of the skybox
m_Skybox->SetActiveShader(ShaderType::SKYBOX);
return m_Skybox; // Return the constructed skybox object
}

View File

@@ -81,6 +81,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd,
m_RenderQueues.push_back(std::ref(m_object));
m_RenderQueues.push_back(std::ref(m_cubes));
m_RenderQueues.push_back(std::ref(m_terrainChunk));
m_RenderQueues.push_back(std::ref(m_Skybox));
m_screenWidth = screenWidth;
m_screenHeight = screenHeight;
@@ -454,7 +455,10 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd,
m_PhysicsThread = std::thread(&ApplicationClass::PhysicsThreadFunction, this);
ConstructSkybox();
//ConstructSkyboxWithPlanes();
Skybox* skybox = new Skybox;
skybox->Initialize(m_Direct3D);
m_Skybox.push_back(skybox->ConstructSkybox());
}
catch (const std::exception& e)
@@ -1026,20 +1030,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
diffuseColor, lightPosition, ambientColor);
UpdateSkyboxPosition(); // Update the position of the skybox to match the camera position.
// ------------------------------------------------------------- //
// --------------------- Render the skybox --------------------- //
// ------------------------------------------------------------- //
m_Direct3D->TurnZBufferOff(); // Disable the Z buffer for rendering the skybox.
result = RenderSkybox(viewMatrix, projectionMatrix); // Render the skybox.
if (!result)
{
Logger::Get().Log("Could not render the skybox", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
m_Direct3D->TurnZBufferOn(); // Enable the Z buffer after rendering the skybox.
// -------------------------------------------------------- //
@@ -2010,70 +2001,6 @@ bool ApplicationClass::RenderPass(const std::vector<std::reference_wrapper<std::
return true;
}
void ApplicationClass::ConstructSkybox() {
Logger::Get().Log("Initializing skybox", __FILE__, __LINE__, Logger::LogLevel::Initialize);
char modelFilename[128];
strcpy_s(modelFilename, "assets/Model/OBJ/plane.obj");
// Liste des fichiers de texture pour chaque face du skybox
std::vector<std::wstring> skyboxTextures = {
L"assets/Skybox/skybox_front.png",
L"assets/Skybox/skybox_back.png",
L"assets/Skybox/skybox_left.png",
L"assets/Skybox/skybox_right.png",
L"assets/Skybox/skybox_top.png",
L"assets/Skybox/skybox_bottom.png"
};
// Charger les textures
std::vector<ID3D11ShaderResourceView*> textures;
for (const auto& textureFilename : skyboxTextures)
{
ID3D11ShaderResourceView* texture = nullptr;
HRESULT result = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture);
if (FAILED(result))
{
Logger::Get().Log("Failed to load skybox texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
return;
}
textures.push_back(texture);
}
// Cr<43>er les 6 faces du skybox
std::vector<XMMATRIX> translations = {
XMMatrixTranslation(0.0f, 0.0f, 1.0f), // Front
XMMatrixTranslation(0.0f, 0.0f, -1.0f), // Back
XMMatrixTranslation(-1.0f, 0.0f, 0.0f), // Left
XMMatrixTranslation(1.0f, 0.0f, 0.0f), // Right
XMMatrixTranslation(0.0f, 1.0f, 0.0f), // Top
XMMatrixTranslation(0.0f, -1.0f, 0.0f) // Bottom
};
std::vector<XMMATRIX> rotations = {
XMMatrixRotationRollPitchYaw(-XM_PIDIV2, 0, 0.0f), // Front
XMMatrixRotationRollPitchYaw(XM_PIDIV2, 0, 0.0f), // Back
XMMatrixRotationRollPitchYaw(XM_PIDIV2, XM_PIDIV2, 0.0f), // Left
XMMatrixRotationRollPitchYaw(XM_PIDIV2, -XM_PIDIV2, 0.0f), // Right
XMMatrixRotationRollPitchYaw(XM_PI, 0.0f, 0.0f), // top
XMMatrixRotationRollPitchYaw(0.0f, 0.0f, 0.0f) // Bottom
};
for (int i = 0; i < 6; ++i)
{
Object* face = new Object();
face->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, { textures[i] });
face->SetScaleMatrix(XMMatrixScaling(2.01f, 2.01f, 2.01f));
face->SetRotateMatrix(rotations[i]);
face->SetTranslateMatrix(translations[i]);
face->SetActiveShader(ShaderType::SKYBOX);
m_Skybox.push_back(face);
m_SkyboxInitialTranslations.push_back(translations[i]);
}
Logger::Get().Log("Skybox initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
}
void ApplicationClass::ConstructFrustum()
{
XMMATRIX projectionMatrix = m_Direct3D->GetProjectionMatrix();
@@ -2083,6 +2010,20 @@ void ApplicationClass::ConstructFrustum()
m_FrustumCulling.ConstructFrustum(SCREEN_DEPTH, projectionMatrix, viewMatrix);
}
// Update the position of the skybox based on the camera position
void ApplicationClass::UpdateSkyboxPosition()
{
if (m_Skybox.empty())
{
Logger::Get().Log("Skybox is empty", __FILE__, __LINE__, Logger::LogLevel::Error);
return;
}
m_Skybox[0]->SetTranslateMatrix(XMMatrixTranslation(m_Camera->GetPosition().x, m_Camera->GetPosition().y, m_Camera->GetPosition().z));
}
bool ApplicationClass::RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bool keyDown, float deltaTime) {
const float maxSpeed = 50.0f; // Limite de vitesse maximale
@@ -2190,47 +2131,6 @@ void ApplicationClass::PhysicsThreadFunction()
}
}
void ApplicationClass::UpdateSkyboxPosition() {
XMFLOAT3 cameraPositionFloat3 = m_Camera->GetPosition();
XMVECTOR cameraPosition = XMLoadFloat3(&cameraPositionFloat3);
for (size_t i = 0; i < m_Skybox.size(); ++i) {
Object* face = m_Skybox[i];
XMMATRIX initialTranslation = m_SkyboxInitialTranslations[i];
XMVECTOR initialTranslationVector = initialTranslation.r[3];
XMVECTOR newTranslation = XMVectorAdd(cameraPosition, initialTranslationVector);
XMMATRIX translateMatrix = XMMatrixTranslationFromVector(newTranslation);
face->SetTranslateMatrix(translateMatrix);
}
}
bool ApplicationClass::RenderSkybox(XMMATRIX view, XMMATRIX projection) {
bool result;
XMMATRIX worldMatrix, scaleMatrix, rotateMatrix, translateMatrix, srMatrix;
for (auto& face : m_Skybox) {
if (face == nullptr) {
Logger::Get().Log("Skybox face is null", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
scaleMatrix = face->GetScaleMatrix();
rotateMatrix = face->GetRotateMatrix();
translateMatrix = face->GetTranslateMatrix();
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
face->Render(m_Direct3D->GetDeviceContext());
result = m_ShaderManager->RenderTextureShader(m_Direct3D->GetDeviceContext(), face->GetIndexCount(), worldMatrix, view, projection, face->GetTexture(0));
if (!result)
{
Logger::Get().Log("Could not render the model using the texture shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
}
return true;
}
std::string ApplicationClass::ConvertWStringToString(const std::wstring& wstr)
{
if (wstr.empty()) return std::string();