Skybox WIP

This commit is contained in:
2025-01-21 15:28:37 +01:00
parent 296e04692a
commit 172db0b96d
16 changed files with 177 additions and 88 deletions

View File

@@ -62,6 +62,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;
@@ -424,6 +425,8 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
m_PhysicsThread = std::thread(&ApplicationClass::PhysicsThreadFunction, this);
ConstructSkybox();
}
catch (const std::exception& e)
{
@@ -791,10 +794,6 @@ bool ApplicationClass::Frame(InputClass* Input)
m_Inputs.m_KeyUp = Input->IsUpArrowPressed();
m_Inputs.m_KeyDown = Input->IsDownArrowPressed();
//RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, frameTime);
// Render the scene to a render texture.
result = RenderSceneToTexture(rotation);
if (!result)
@@ -967,6 +966,9 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
projectionMatrix = m_Direct3D->GetProjectionMatrix();
orthoMatrix = m_Direct3D->GetOrthoMatrix();
//Render Sky box
//RenderSkybox(viewMatrix, projectionMatrix);
// Get the light properties.
for (i = 0; i < m_numLights; i++)
{
@@ -1012,7 +1014,6 @@ 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);
// -------------------------------------------------------- //
// ------------ Render the object in the queue ------------ //
// -------------------------------------------------------- //
@@ -1837,6 +1838,12 @@ bool ApplicationClass::RenderPass(const std::vector<std::reference_wrapper<std::
float z = XMVectorGetZ(objposition);
float radius = object->GetBoundingRadius();
// Check if the object has physics enabled
if (object->IsPhysicsEnabled())
{
object->UpdatePosition(m_Timer->GetTime());
}
// V<>rifie si l'objet est dans le frustum
if (!m_FrustumCulling.CheckCube(x, y, z, radius, GetFrustumTolerance()))
{
@@ -1904,6 +1911,40 @@ bool ApplicationClass::RenderPass(const std::vector<std::reference_wrapper<std::
return true;
}
void ApplicationClass::ConstructSkybox() {
Logger::Get().Log("Constructing skybox", __FILE__, __LINE__);
// Set the file name of the model.
char modelFilename[128];
strcpy_s(modelFilename, "assets/Model/TXT/cube.txt");
// Liste des fichiers de texture
std::vector<std::wstring> skyboxTexture = {
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"
};
textures.clear();
for (const auto& textureFilename : skyboxTexture)
{
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 texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
return;
}
textures.push_back(texture);
}
Object* newSkybox = new Object();
newSkybox->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textures);
newSkybox->SetScaleMatrix(XMMatrixScaling(100.0f, 100.0f, 100.0f));
newSkybox->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f));
newSkybox->SetType(ObjectType::Cube);
m_object.push_back(newSkybox);
}
void ApplicationClass::ConstructFrustum()
{
XMMATRIX projectionMatrix = m_Direct3D->GetProjectionMatrix();
@@ -1980,10 +2021,6 @@ bool ApplicationClass::RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bo
object->SetVelocity(velocity);
}
XMVECTOR position = object->GetPosition();
position = position + object->GetVelocity() * deltaTime;
object->SetPosition(position);
m_Physics->ApplyGravity(object, deltaTime);
if (XMVectorGetY(object->GetPosition()) < -30.0f) {
@@ -2022,3 +2059,29 @@ void ApplicationClass::PhysicsThreadFunction()
}
}
bool ApplicationClass::LoadSkyboxTextures()
{
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"
};
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 false;
}
m_SkyboxTextures.push_back(texture);
}
Logger::Get().Log("Loaded " + std::to_string(m_SkyboxTextures.size()) + " skybox textures", __FILE__, __LINE__, Logger::LogLevel::Info);
return true;
}