Minor update - Skybox baby

Faudra mettre une texture numéroté pour la skybox afin de verifié l'orientation des plan qui la constitue
This commit is contained in:
CatChow0 2025-01-21 17:43:02 +01:00
parent 172db0b96d
commit 261df5e257
11 changed files with 220 additions and 44 deletions

View File

@ -62,7 +62,6 @@ 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;
@ -992,7 +991,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
m_SceneTexture->SetRenderTarget(m_Direct3D->GetDeviceContext());
m_SceneTexture->ClearRenderTarget(m_Direct3D->GetDeviceContext(), 0.0f, 0.0f, 0.0f, 1.0f);
//Add the 3 first value of the first light position to the TrueLightPosition XMFLOAT3
positionX = lightPosition[0].x;
positionY = lightPosition[0].y;
@ -1014,6 +1012,23 @@ 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);
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.
// -------------------------------------------------------- //
// ------------ Render the object in the queue ------------ //
// -------------------------------------------------------- //
@ -1169,6 +1184,9 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
return false;
}
// turn on the Z buffer
m_Direct3D->TurnZBufferOn();
// Render the model using the multitexture shader.
m_Model->Render(m_Direct3D->GetDeviceContext());
@ -1824,6 +1842,12 @@ bool ApplicationClass::RenderPass(const std::vector<std::reference_wrapper<std::
for (const auto& RenderQueue : RenderQueues)
{
// Désactiver le Z-buffer si la RenderQueue est m_Skybox
if (&RenderQueue.get() == &m_Skybox)
{
m_Direct3D->TurnZBufferOff();
}
for (auto& object : RenderQueue.get())
{
if (object == nullptr)
@ -1891,8 +1915,22 @@ bool ApplicationClass::RenderPass(const std::vector<std::reference_wrapper<std::
return false;
}
break;
case Object::TEXTURE:
result = m_ShaderManager->RenderTextureShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0));
if (!result)
{
Logger::Get().Log("Could not render the model using the texture shader", __FILE__, __LINE__, Logger::LogLevel::Error);
return false;
}
case Object::LIGHTING:
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;
}
break;
default:
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection,
object->GetTexture(0), diffuse, position, ambient);
@ -1912,12 +1950,13 @@ bool ApplicationClass::RenderPass(const std::vector<std::reference_wrapper<std::
}
void ApplicationClass::ConstructSkybox() {
Logger::Get().Log("Constructing skybox", __FILE__, __LINE__);
// Set the file name of the model.
Logger::Get().Log("Initializing skybox", __FILE__, __LINE__, Logger::LogLevel::Initialize);
char modelFilename[128];
strcpy_s(modelFilename, "assets/Model/TXT/cube.txt");
// Liste des fichiers de texture
std::vector<std::wstring> skyboxTexture = {
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",
@ -1925,24 +1964,53 @@ void ApplicationClass::ConstructSkybox() {
L"assets/Skybox/skybox_top.png",
L"assets/Skybox/skybox_bottom.png"
};
textures.clear();
for (const auto& textureFilename : skyboxTexture)
// 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 texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error);
Logger::Get().Log("Failed to load skybox 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);
// Cré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(Object::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()
@ -2059,29 +2127,43 @@ 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"
};
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);
}
}
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);
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;
}
m_SkyboxTextures.push_back(texture);
}
Logger::Get().Log("Loaded " + std::to_string(m_SkyboxTextures.size()) + " skybox textures", __FILE__, __LINE__, Logger::LogLevel::Info);
return true;
}

View File

@ -146,8 +146,9 @@ 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);
bool LoadSkyboxTextures();
void ConstructSkybox();
void ConstructSkybox(); // Construct the skybox
void UpdateSkyboxPosition(); // Update the skybox position
bool RenderSkybox(XMMATRIX view, XMMATRIX projection); // Render the skybox
public :
std::vector<ID3D11ShaderResourceView*> textures;
@ -178,6 +179,7 @@ private :
int m_screenWidth, m_screenHeight;
CameraClass* m_Camera;
PositionClass* m_Position;
std::vector<XMMATRIX> m_SkyboxInitialTranslations;
// ------------------------------------ //
// ------------- OBJECTS -------------- //

View File

@ -0,0 +1,91 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0
[Window][Khaotic Engine]
Pos=1180,27
Size=396,826
Collapsed=0
DockId=0x00000005,0
[Window][Objects]
Pos=8,442
Size=290,411
Collapsed=0
DockId=0x0000000A,0
[Window][Terrain]
Pos=8,27
Size=290,413
Collapsed=0
DockId=0x00000009,0
[Window][Light]
Pos=8,27
Size=330,487
Collapsed=0
DockId=0x00000004,1
[Window][Shader Manager]
Pos=8,27
Size=330,487
Collapsed=0
DockId=0x00000004,2
[Window][Engine Settings]
Pos=1180,27
Size=396,826
Collapsed=0
DockId=0x00000005,1
[Window][DockSpace Demo]
Size=1584,861
Collapsed=0
[Window][Render Window]
Pos=300,27
Size=878,826
Collapsed=0
DockId=0x00000002,0
[Window][DockSpace]
Pos=0,0
Size=1584,861
Collapsed=0
[Window][Add Object]
Pos=1188,0
Size=396,430
Collapsed=0
[Window][Log]
Pos=8,518
Size=1568,335
Collapsed=0
DockId=0x0000000C,0
[Window][Log Window]
Pos=8,627
Size=1568,226
Collapsed=0
DockId=0x0000000E,0
[Docking][Data]
DockSpace ID=0xC0DFADC4 Pos=8,27 Size=1568,826 Split=X
DockNode ID=0x00000001 Parent=0xC0DFADC4 SizeRef=330,1094 Split=Y Selected=0x393905AB
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=330,487 Selected=0x393905AB
DockNode ID=0x00000006 Parent=0x00000001 SizeRef=330,485 Selected=0x031DC75C
DockNode ID=0x00000003 Parent=0xC0DFADC4 SizeRef=1700,1094 CentralNode=1
DockSpace ID=0xCCBD8CF7 Window=0x3DA2F1DE Pos=8,27 Size=1568,826 Split=Y
DockNode ID=0x0000000D Parent=0xCCBD8CF7 SizeRef=1568,598 Split=Y
DockNode ID=0x0000000B Parent=0x0000000D SizeRef=1568,637 Split=X
DockNode ID=0x00000007 Parent=0x0000000B SizeRef=290,826 Split=Y Selected=0x393905AB
DockNode ID=0x00000009 Parent=0x00000007 SizeRef=395,413 Selected=0x393905AB
DockNode ID=0x0000000A Parent=0x00000007 SizeRef=395,411 Selected=0x031DC75C
DockNode ID=0x00000008 Parent=0x0000000B SizeRef=1276,826 Split=X
DockNode ID=0x00000002 Parent=0x00000008 SizeRef=878,826 CentralNode=1 Selected=0x9204953B
DockNode ID=0x00000005 Parent=0x00000008 SizeRef=396,826 Selected=0x9F035453
DockNode ID=0x0000000C Parent=0x0000000D SizeRef=1568,335 Selected=0x139FDA3F
DockNode ID=0x0000000E Parent=0xCCBD8CF7 SizeRef=1568,226 Selected=0xAB74BEE9

Binary file not shown.

Before

Width:  |  Height:  |  Size: 302 KiB

After

Width:  |  Height:  |  Size: 331 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 230 KiB

After

Width:  |  Height:  |  Size: 273 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 441 KiB

After

Width:  |  Height:  |  Size: 493 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 466 KiB

After

Width:  |  Height:  |  Size: 510 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 458 KiB

After

Width:  |  Height:  |  Size: 498 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 352 KiB

After

Width:  |  Height:  |  Size: 422 KiB

View File

@ -10,14 +10,14 @@ Collapsed=0
DockId=0x00000005,0
[Window][Objects]
Pos=8,27
Size=290,826
Pos=8,442
Size=290,411
Collapsed=0
DockId=0x0000000A,0
[Window][Terrain]
Pos=8,27
Size=290,413
Size=290,826
Collapsed=0
DockId=0x00000009,0
@ -35,7 +35,7 @@ DockId=0x00000004,2
[Window][Engine Settings]
Pos=1180,27
Size=396,598
Size=396,826
Collapsed=0
DockId=0x00000005,1

View File

@ -66,7 +66,6 @@ public:
void SetType(ObjectType type) { m_type = type; };
ObjectType GetType() const { return m_type; };
bool LoadTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, const std::wstring& filename);
enum ShaderType
{
@ -75,7 +74,9 @@ public:
NORMAL_MAPPING,
SPECULAR_MAPPING,
REFLECTION,
REFRACTION
REFRACTION,
TEXTURE,
SKYBOX
};
ShaderType GetActiveShader() const { return m_activeShader; };