diff --git a/README.md b/README.md index 3167f41..547db4a 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,105 @@ +# Khaotic Engine - C++ Custom Engine -# Khaotic Engine - C++ Custom Engine +Khaotic Engine est un moteur de rendu fait en **C++** réalisé par une petite équipe dans le but d'apprendre à utiliser les API de rendu (OpenGL,DirectX 11/12 et Vulkan). -Khaotic Engine est un moteur de rendu fait en c++ réalisé par une petite équipe dans le but d'apprendre a utilisé les API de rendu (OpenGL,DirectX 11/12 et Vulkan). - -Ce moteur est basé sur DirectX11 utilise ImGui avec une couche d'abstraction pour permetre son usage avec d'autre API. +Ce moteur est basé sur **DirectX11** utilise **ImGui** avec une couche d'abstraction pour permetre son usage avec d'autres API. --- -Khaotic Engine is a rendering engine made in C++ by a small team with the aim of learning how to use rendering APIs (OpenGL, DirectX 11/12 and Vulkan). +Khaotic Engine is a rendering engine made in **C++** by a small team with the aim of learning how to use rendering APIs (OpenGL, DirectX 11/12 and Vulkan). -This DirectX11-based engine uses ImGui with an abstraction layer to enable its use with other APIs. +This **DirectX11** based engine uses **ImGui** with an abstraction layer to enable its use with other APIs. + + +## Installation + +**Par la solution + Debogueur :** + + 1. Télécharger la branche Main + 2. Dezip le Zip + 3. Ouvrir le fichier en .sln + 4. Lancer le déboguage + +---- + +**From solution + Debugger** + + 1. Download the Main branch + 2. Unzip the Zip file + 3. Open the .sln flie + 4. Launch the debugger -## Demo +## Shaders list: -Insert gif or link to demo + + - **Diffuse Lighting** + - **Ambiant Lighting** + - **Specular Lighting** + - **Alpha Mapping** + - **Normal Mapping** + - **Specular Mapping** + - **Clipping Planes** + - **Texture Translation** + - **Transparency** + + +*Plus de shaders seront disponibles dans le futur* + +--- + +*More shaders will be added in the future* + + + +## ImGui: + +*Cette partie du moteur est encore en développement, d'autres fonctionnalitées seront ajoutées dans le futur* + +La librairie ImGui est utilisée afin d'interragir avec les éléments du moteur comme les objets. + +### Fonctionnalitées: +- Importer des objets 3D sous format **.obj** +- Ajout d'un cube à la scène +- Modifier les propriétées d'un objet (Position, Rotation, Taille) +- Modifier les objets présents dans la scène +- Création d'un terrain +- Modifier les propriétées des lumières (Position, Couleur RVB) + +---- +*This part of the engine is still in developpement, other features will be added in the future* + +ImGui is used here to allow interraction between the user and the objects in the scene. + +### Features: +- Import 3D objects with the **.obj** format +- Can add a cube tu the scne +- Edit an object prorpieties (Position, Rotation, Scale) +- Edit objects in the current scene +- Terrain generation +- Edit lights proprieties (Position, RGB Color) + + + +## Demo : + +[![Demo Video](https://img.youtube.com/vi/qCOCTyB_97c/0.jpg)](https://www.youtube.com/watch?v=qCOCTyB_97c) ## Engine Build by : -- [@CatChow0](https://github.com/CatChow0) -- [@miragefr0st](https://github.com/miragefr0st) -- [@StratiX0](https://github.com/StratiX0) -- [@Kagutsuchi84](https://github.com/Mattys8423) -- [@Harpie94](https://github.com/Harpie94) -- [@axelpicou](https://github.com/axelpicou) -- [@GolfOcean334](https://github.com/GolfOcean334) +[](https://github.com/GamingCampus-AdrienBourgois/khaotic-engine?tab=readme-ov-file#engine-build-by-) + +- [@CatChow0](https://github.com/CatChow0) +- [@miragefr0st](https://github.com/miragefr0st) +- [@StratiX0](https://github.com/StratiX0) +- [@Kagutsuchi84](https://github.com/Mattys8423) +- [@Harpie94](https://github.com/Harpie94) +- [@axelpicou](https://github.com/axelpicou) +- [@GolfOcean334](https://github.com/GolfOcean334) +- [@sutabasuto](https://github.com/sutabasuto) + + + + diff --git a/enginecustom/Light.vs b/enginecustom/Light.vs index 81923e6..8e32839 100644 --- a/enginecustom/Light.vs +++ b/enginecustom/Light.vs @@ -1,7 +1,3 @@ -//////////////////////////////////////////////////////////////////////////////// -// Filename: light.vs -//////////////////////////////////////////////////////////////////////////////// - ///////////// // DEFINES // ///////////// diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 795935f..f606dfa 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -3,12 +3,9 @@ ApplicationClass::ApplicationClass() { m_Direct3D = 0; - m_Camera = 0; - m_MultiTextureShader = 0; - m_AlphaMapShader = 0; + m_Camera = 0; m_Model = 0; m_LightShader = 0; - m_LightMapShader = 0; m_Light = 0; m_TextureShader = 0; m_Bitmap = 0; @@ -19,8 +16,7 @@ ApplicationClass::ApplicationClass() m_Font = 0; m_Fps = 0; m_FpsString = 0; - m_NormalMapShader = 0; - m_SpecMapShader = 0; + m_ShaderManager = 0; m_RenderCountString = 0; m_ModelList = 0; m_Position = 0; @@ -43,7 +39,7 @@ ApplicationClass::~ApplicationClass() bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) { char mouseString1[32], mouseString2[32], mouseString3[32]; - char modelFilename[128], textureFilename1[128], textureFilename2[128], textureFilename3[128], renderString[32]; + char modelFilename[128], textureFilename1[128], textureFilename2[128], textureFilename3[128], textureFilename4[128], textureFilename5[128], textureFilename6[128], renderString[32]; char bitmapFilename[128]; char spriteFilename[128]; char fpsString[32]; @@ -79,26 +75,6 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_Camera->Render(); m_Camera->GetViewMatrix(m_baseViewMatrix); - // Create and initialize the specular map shader object. - m_SpecMapShader = new SpecMapShaderClass; - - result = m_SpecMapShader->Initialize(m_Direct3D->GetDevice(), hwnd); - if (!result) - { - MessageBox(hwnd, L"Could not initialize the specular map shader object.", L"Error", MB_OK); - return false; - } - - // Create and initialize the normal map shader object. - m_NormalMapShader = new NormalMapShaderClass; - - result = m_NormalMapShader->Initialize(m_Direct3D->GetDevice(), hwnd); - if (!result) - { - MessageBox(hwnd, L"Could not initialize the normal map shader object.", L"Error", MB_OK); - return false; - } - // Create and initialize the font shader object. m_FontShader = new FontShaderClass; @@ -128,6 +104,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) return false; } + // Create and initialize the render to texture object. m_RenderTexture = new RenderTextureClass; @@ -197,36 +174,28 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) return false; } - // Create and initialize the multitexture shader object. - m_MultiTextureShader = new MultiTextureShaderClass; - - result = m_MultiTextureShader->Initialize(m_Direct3D->GetDevice(), hwnd); - if (!result) - { - MessageBox(hwnd, L"Could not initialize the multitexture shader object.", L"Error", MB_OK); - return false; - } - // Set the file name of the model. strcpy_s(modelFilename, "cube.txt"); // Set the file name of the textures. strcpy_s(textureFilename1, "stone01.tga"); strcpy_s(textureFilename2, "normal01.tga"); - strcpy_s(textureFilename3, "alpha01.tga"); - // A FAIRE: Ajouter une nouvelle texture pour le multitexturing + strcpy_s(textureFilename3, "spec02.tga"); + strcpy_s(textureFilename4, "alpha01.tga"); + strcpy_s(textureFilename5, "light01.tga"); + strcpy_s(textureFilename6, "moss01.tga"); // Create and initialize the model object. m_Model = new ModelClass; - result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3); + result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3, textureFilename4, + textureFilename5, textureFilename6); if (!result) { MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK); return false; } - // Create and initialize the light shader object. m_LightShader = new LightShaderClass; @@ -241,7 +210,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_Light = new LightClass; m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); - m_Light->SetDirection(0.0f, 0.0f, 1.0f); + m_Light->SetDirection(0.0f, 0.0f, -1.0f); m_Light->SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); m_Light->SetSpecularPower(16.0f); @@ -251,42 +220,46 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) // Create and initialize the light objects array. m_Lights.resize(m_numLights); - // Définissez manuellement la couleur et la position de chaque lumière. m_Lights[0] = new LightClass; - m_Lights[0]->SetDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); // Rouge - m_Lights[0]->SetPosition(-3.0f, 1.0f, 3.0f); + m_Lights[0]->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); // White + m_Lights[0]->SetDirection(0.0f, 0.0f, -1.0f); + m_Lights[0]->SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); + m_Lights[0]->SetSpecularPower(16.0f); + m_Lights[0]->SetPosition(0.0f, 0.0f, 6.0f); + // Manually set the color and position of each light. m_Lights[1] = new LightClass; - m_Lights[1]->SetDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); // Vert - m_Lights[1]->SetPosition(3.0f, 1.0f, 3.0f); + m_Lights[1]->SetDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); // Red + m_Lights[1]->SetDirection(0.0f, 0.0f, 1.0f); + m_Lights[1]->SetSpecularColor(1.0f, 0.0f, 0.0f, 1.0f); + m_Lights[1]->SetSpecularPower(16.0f); + m_Lights[1]->SetPosition(0.0f, 0.0f, 6.0f); m_Lights[2] = new LightClass; - m_Lights[2]->SetDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); // Bleu - m_Lights[2]->SetPosition(-3.0f, 1.0f, -3.0f); - + m_Lights[2]->SetDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); // Green + m_Lights[2]->SetDirection(0.0f, 0.0f, 1.0f); + m_Lights[2]->SetSpecularColor(0.0f, 1.0f, 0.0f, 1.0f); + m_Lights[2]->SetSpecularPower(16.0f); + m_Lights[2]->SetPosition(0.0f, 0.0f, 6.0f); + m_Lights[3] = new LightClass; - m_Lights[3]->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); // Blanc - m_Lights[3]->SetPosition(3.0f, 1.0f, -3.0f); + m_Lights[3]->SetDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); // Blue + m_Lights[3]->SetDirection(0.0f, 0.0f, 1.0f); + m_Lights[3]->SetSpecularColor(0.0f, 0.0f, 1.0f, 1.0f); + m_Lights[3]->SetSpecularPower(16.0f); + m_Lights[3]->SetPosition(0.0f, 0.0f, 6.0f); - // Create and initialize the light map shader object. - m_LightMapShader = new LightMapShaderClass; + + // Create and initialize the normal map shader object. + m_ShaderManager = new ShaderManagerClass; - result = m_LightMapShader->Initialize(m_Direct3D->GetDevice(), hwnd); + result = m_ShaderManager->Initialize(m_Direct3D->GetDevice(), hwnd); if (!result) { - MessageBox(hwnd, L"Could not initialize the light map shader object.", L"Error", MB_OK); return false; } - // Create and initialize the alpha map shader object. - m_AlphaMapShader = new AlphaMapShaderClass; - - result = m_AlphaMapShader->Initialize(m_Direct3D->GetDevice(), hwnd); - if (!result) - { - MessageBox(hwnd, L"Could not initialize the alpha map shader object.", L"Error", MB_OK); - return false; - } + // Create and initialize the font shader object. m_FontShader = new FontShaderClass; @@ -362,6 +335,14 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) void ApplicationClass::Shutdown() { + // Release the shader manager object. + if (m_ShaderManager) + { + m_ShaderManager->Shutdown(); + delete m_ShaderManager; + m_ShaderManager = 0; + } + // Release the frustum class object. if (m_Frustum) { @@ -457,11 +438,20 @@ void ApplicationClass::Shutdown() m_Sprite = 0; } - // Supprimez tous les objets de lumière. - for (int i = 0; i < m_Lights.size(); i++) + for (auto light : m_Lights) { - delete m_Lights[i]; - m_Lights[i] = 0; + if (light) + { + delete light; + light = 0; + } + } + + // Release the light object. + if (m_Light) + { + delete m_Light; + m_Light = 0; } // Release the light shader object. @@ -472,91 +462,12 @@ void ApplicationClass::Shutdown() m_LightShader = 0; } - // Release the specular map shader object. - if (m_SpecMapShader) + // Release the model object. + if (m_Model) { - m_SpecMapShader->Shutdown(); - delete m_SpecMapShader; - m_SpecMapShader = 0; - } - - // Release the normal map shader object. - if (m_NormalMapShader) - { - m_NormalMapShader->Shutdown(); - delete m_NormalMapShader; - m_NormalMapShader = 0; - } - - // Liberez la memoire pour chaque cube - for (auto cube : m_cubes) - { - cube->Shutdown(); - delete cube; - } - m_cubes.clear(); - - // Liberez la memoire pour chaque cube du terrain - for (auto cube : m_terrainChunk) - { - cube->Shutdown(); - delete cube; - } - m_terrainChunk.clear(); - - for (auto object : m_object) - { - object->Shutdown(); - delete object; - } - m_object.clear(); - - // Release the multitexture shader object. - if (m_MultiTextureShader) - { - m_MultiTextureShader->Shutdown(); - delete m_MultiTextureShader; - m_MultiTextureShader = 0; - // Release the bitmap object. - if (m_Bitmap) - { - m_Bitmap->Shutdown(); - delete m_Bitmap; - m_Bitmap = 0; - } - - // Release the texture shader object. - if (m_TextureShader) - { - m_TextureShader->Shutdown(); - delete m_TextureShader; - m_TextureShader = 0; - } - - // Release the camera object. - if (m_Camera) - { - delete m_Camera; - m_Camera = 0; - } - - // Release the D3D object. - if (m_Direct3D) - { - m_Direct3D->Shutdown(); - delete m_Direct3D; - m_Direct3D = 0; - } - - return; - } - - // Release the alpha map shader object. - if (m_AlphaMapShader) - { - m_AlphaMapShader->Shutdown(); - delete m_AlphaMapShader; - m_AlphaMapShader = 0; + m_Model->Shutdown(); + delete m_Model; + m_Model = 0; } } @@ -566,15 +477,16 @@ bool ApplicationClass::Frame(InputClass* Input) int mouseX, mouseY, currentMouseX, currentMouseY; bool result, leftMouseDown, rightMouseDown, keyDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE; float rotationY, rotationX, positionX, positionY, positionZ; + static float textureTranslation = 0.0f; float frameTime; static int lastMouseX = 0, lastMouseY = 0; static float rotation = 360.0f; - static float x = 6.f; - static float y = 3.f; - static float z = 0.f; + static float x = 0.f; + static float y = 0.f; + static float z = -8.f; // Update the system stats. m_Timer->Frame(); @@ -636,7 +548,7 @@ bool ApplicationClass::Frame(InputClass* Input) m_Camera->Render(); // Render the graphics scene. - result = Render(rotation, x, y, z); + result = Render(rotation, x, y, z, textureTranslation); if (!result) { return false; @@ -656,13 +568,13 @@ bool ApplicationClass::Frame(InputClass* Input) rotation += 360.0f; } - // Update the x position variable each frame. - x -= 0.0174532925f * 0.6f; + //// Update the x position variable each frame. + //x -= 0.0174532925f * 0.6f; - y -= 0.0174532925f * 0.2f; + //y -= 0.0174532925f * 0.2f; - // Update the z position variable each frame. - z -= 0.0174532925f * 0.2f; + //// Update the z position variable each frame. + //z -= 0.0174532925f * 0.2f; // Render the scene to a render texture. result = RenderSceneToTexture(rotation); @@ -681,6 +593,24 @@ bool ApplicationClass::Frame(InputClass* Input) // Update the sprite object using the frame time. m_Sprite->Update(frameTime); + // Increment the texture translation. + textureTranslation += 0.01f; + if (textureTranslation > 1.0f) + { + textureTranslation -= 1.0f; + } + + ///////////////////////////// + // C en commentaire pcl kc // + ///////////////////////////// + + //// Render the graphics scene. + //result = Render(rotation, x, y, z, textureTranslation); + //if (!result) + //{ + // return false; + //} + return true; } @@ -720,19 +650,24 @@ bool ApplicationClass::RenderSceneToTexture(float rotation) return true; } -bool ApplicationClass::Render(float rotation, float x, float y, float z) +bool ApplicationClass::Render(float rotation, float x, float y, float z, float textureTranslation) { XMMATRIX worldMatrix, viewMatrix, orthoMatrix, projectionMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix; float positionX, positionY, positionZ, radius; XMFLOAT4 diffuseColor[4], lightPosition[4]; int modelCount, renderCount, i; bool result, renderModel; + float blendAmount; + + // Set the blending amount to 10%. + blendAmount = 0.1f; // Clear the buffers to begin the scene. m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); // Generate the view matrix based on the camera's position. m_Camera->Render(); + // Get the world, view, and projection matrices from the camera and d3d objects. m_Direct3D->GetWorldMatrix(worldMatrix); @@ -1014,6 +949,117 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) //// Render the model using the multitexture shader. //m_Model->Render(m_Direct3D->GetDeviceContext()); + // Setup matrices. + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = XMMatrixTranslation(-5.0f, 1.0f, -20.0f); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); + + // Render the model using the alpha map shader. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + result = m_ShaderManager->RenderAlphaMapShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, + m_Model->GetTexture(0), m_Model->GetTexture(5), m_Model->GetTexture(3)); + if (!result) + { + return false; + } + + // Setup matrices. + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = XMMatrixTranslation(-5.0f, -5.0f, -20.0f); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); + + // Render the model using the texture shader. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + result = m_ShaderManager->RenderTextureShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, + m_Model->GetTexture(0)); + if (!result) + { + return false; + } + + // Setup matrices. + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = XMMatrixTranslation(0.0f, 1.0f, -20.0f); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); + + // Render the model using the render map shader. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + result = m_ShaderManager->RenderNormalMapShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, + m_Model->GetTexture(0), m_Model->GetTexture(1), m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor()); + if (!result) + { + return false; + } + + + // Setup matrices. + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = XMMatrixTranslation(0.0f, -2.0f, -20.0f); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); + + // Render the model using the multitexture shader. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + result = m_ShaderManager->RenderMultitextureShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, + m_Model->GetTexture(0), m_Model->GetTexture(5)); + if (!result) + { + return false; + } + + + // Setup matrices. + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = XMMatrixTranslation(0.0f, -5.0f, -20.0f); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); + + // Render the model using the translate shader. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + result = m_ShaderManager->RenderTranslateShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, + m_Model->GetTexture(0), textureTranslation); + if (!result) + { + return false; + } + + // Setup matrices. + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = XMMatrixTranslation(-5.0f, -2.0f, -20.0f); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); + + // Render the model using the specular map shader. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + result = m_ShaderManager->RenderSpecMapShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, + m_Model->GetTexture(0), m_Model->GetTexture(1), m_Model->GetTexture(2), m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), + m_Camera->GetPosition(), m_Lights[0]->GetSpecularColor(), m_Lights[0]->GetSpecularPower()); + if (!result) + { + return false; + } + + // Setup matrices. + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = XMMatrixTranslation(-10.0f, -5.0f, -20.0f); + worldMatrix = XMMatrixMultiply(rotateMatrix, translateMatrix); + + // Render the model using the transparent shader. + m_Model->Render(m_Direct3D->GetDeviceContext()); + + result = m_ShaderManager->RenderTransparentShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0), blendAmount); + if (!result) + { + return false; + } + + // Turn off alpha blending. + m_Direct3D->DisableAlphaBlending(); + + // Lighting, utilise plusieurs lights donc Multiple Points Lighting //result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0), // diffuseColor, lightPosition); @@ -1030,21 +1076,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) // return false; //} - // MultiTexturing - //result = m_MultiTextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, - // m_Model->GetTexture(0), m_Model->GetTexture(1)); - //if (!result) - //{ - // return false; - //} - - // Alphamapping - /*result = m_AlphaMapShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, - m_Model->GetTexture(0), m_Model->GetTexture(1), m_Model->GetTexture(2)); - if (!result) - { - return false; - }*/ // Enable the Z buffer and disable alpha blending now that 2D rendering is complete. m_Direct3D->TurnZBufferOn(); @@ -1074,9 +1105,12 @@ int ApplicationClass::GetScreenHeight() const void ApplicationClass::GenerateTerrain() { char modelFilename[128]; - char textureFilename[128]; + char textureFilename1[128]; char textureFilename2[128]; char textureFilename3[128]; + char textureFilename4[128]; + char textureFilename5[128]; + char textureFilename6[128]; XMMATRIX scaleMatrix; float scaleX, scaleY, scaleZ; @@ -1089,9 +1123,12 @@ void ApplicationClass::GenerateTerrain() // Set the file name of the model. strcpy_s(modelFilename, "plane.txt"); - strcpy_s(textureFilename, "stone01.tga"); - strcpy_s(textureFilename2, "moss01.tga"); - strcpy_s(textureFilename3, "alpha01.tga"); + strcpy_s(textureFilename1, "stone01.tga"); + strcpy_s(textureFilename2, "normal01.tga"); + strcpy_s(textureFilename3, "spec02.tga"); + strcpy_s(textureFilename4, "alpha01.tga"); + strcpy_s(textureFilename5, "light01.tga"); + strcpy_s(textureFilename6, "moss01.tga"); // for loop to generate terrain chunks for a 10x10 grid for (int i = 0; i < 10; i++) @@ -1099,7 +1136,7 @@ void ApplicationClass::GenerateTerrain() for (int j = 0; j < 10; j++) { Object* newTerrain = new Object(); - newTerrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3); + newTerrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3, textureFilename4, textureFilename5, textureFilename6); newTerrain->SetScaleMatrix(scaleMatrix); @@ -1115,9 +1152,12 @@ void ApplicationClass::GenerateTerrain() void ApplicationClass::AddKobject(WCHAR* filepath) { char modelFilename[128]; - char textureFilename[128]; + char textureFilename1[128]; char textureFilename2[128]; char textureFilename3[128]; + char textureFilename4[128]; + char textureFilename5[128]; + char textureFilename6[128]; filesystem::path p(filepath); string filename = p.stem().string(); @@ -1127,12 +1167,15 @@ void ApplicationClass::AddKobject(WCHAR* filepath) // Set the name of the texture file that we will be loading. - strcpy_s(textureFilename, "stone01.tga"); - strcpy_s(textureFilename2, "moss01.tga"); - strcpy_s(textureFilename3, "alpha01.tga"); + strcpy_s(textureFilename1, "stone01.tga"); + strcpy_s(textureFilename2, "normal01.tga"); + strcpy_s(textureFilename3, "spec02.tga"); + strcpy_s(textureFilename4, "alpha01.tga"); + strcpy_s(textureFilename5, "light01.tga"); + strcpy_s(textureFilename6, "moss01.tga"); Object* newObject = new Object(); - newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3); + newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3, textureFilename4, textureFilename5, textureFilename6); newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f)); newObject->SetName(filename); @@ -1143,21 +1186,27 @@ void ApplicationClass::AddKobject(WCHAR* filepath) void ApplicationClass::AddCube() { char modelFilename[128]; - char textureFilename[128]; + char textureFilename1[128]; char textureFilename2[128]; char textureFilename3[128]; + char textureFilename4[128]; + char textureFilename5[128]; + char textureFilename6[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"); - strcpy_s(textureFilename2, "moss01.tga"); - strcpy_s(textureFilename3, "alpha01.tga"); + strcpy_s(textureFilename1, "stone01.tga"); + strcpy_s(textureFilename2, "normal01.tga"); + strcpy_s(textureFilename3, "spec02.tga"); + strcpy_s(textureFilename4, "alpha01.tga"); + strcpy_s(textureFilename5, "light01.tga"); + strcpy_s(textureFilename6, "moss01.tga"); static int cubeCount = 0; float position = cubeCount * 2.0f; Object* newCube = new Object(); - newCube->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3); + newCube->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3, textureFilename4, textureFilename5, textureFilename6); newCube->SetTranslateMatrix(XMMatrixTranslation(position, 0.0f, 0.0f)); diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 8c5c32c..3aaad14 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -14,26 +14,24 @@ #include #include "lightmapshaderclass.h" -#include "multitextureshaderclass.h" -#include "alphamapshaderclass.h" #include "bitmapclass.h" -#include "textureshaderclass.h" #include "spriteclass.h" +#include "textureshaderclass.h" #include "timerclass.h" #include "fontshaderclass.h" #include "fontclass.h" #include "textclass.h" #include "fpsclass.h" #include "inputclass.h" -#include "normalmapshaderclass.h" -#include "specmapshaderclass.h" +#include "shadermanagerclass.h" #include "modellistclass.h" #include "positionclass.h" #include "frustumclass.h" #include "rendertextureclass.h" #include "displayplaneclass.h" -#include "translateshaderclass.h" #include "reflectionshaderclass.h" +#include "transparentshaderclass.h" + ///////////// @@ -89,7 +87,7 @@ public: std::vector GetLights() const { return m_Lights; }; private: - bool Render(float, float, float, float); + bool Render(float, float, float, float, float); bool UpdateMouseStrings(int, int, bool); bool UpdateFps(); bool UpdateRenderCountString(int); @@ -104,6 +102,9 @@ private : D3DClass* m_Direct3D; IDXGISwapChain* m_swapChain; ModelClass* m_Model; + TextureShaderClass* m_TextureShader; + /*TransparentShaderClass* m_TransparentShader;*/ + ShaderManagerClass* m_ShaderManager; ModelListClass* m_ModelList; // ------------------------------------- // @@ -142,13 +143,7 @@ private : LightShaderClass* m_LightShader; LightMapShaderClass* m_LightMapShader; - MultiTextureShaderClass* m_MultiTextureShader; - AlphaMapShaderClass* m_AlphaMapShader; - TextureShaderClass* m_TextureShader; FontShaderClass* m_FontShader; - NormalMapShaderClass* m_NormalMapShader; - SpecMapShaderClass* m_SpecMapShader; - TranslateShaderClass* m_TranslateShader; ReflectionShaderClass* m_ReflectionShader; BitmapClass* m_Bitmap; @@ -165,7 +160,6 @@ private : FpsClass* m_Fps; TextClass* m_FpsString; int m_previousFps; - }; #endif \ No newline at end of file diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj index 4d01a56..40cd905 100644 --- a/enginecustom/enginecustom.vcxproj +++ b/enginecustom/enginecustom.vcxproj @@ -48,6 +48,7 @@ + @@ -59,6 +60,7 @@ + @@ -90,6 +92,7 @@ + @@ -102,6 +105,7 @@ + @@ -127,6 +131,8 @@ + + @@ -134,7 +140,6 @@ Pixel Pixel Pixel - false Document @@ -142,7 +147,6 @@ Vertex Vertex Vertex - false Document @@ -156,6 +160,7 @@ + @@ -184,7 +189,6 @@ 17.0 Win32Proj {92cf56c4-76bb-40d4-8fe5-36c15f5f127a} - enginecustom 10.0 diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index f876445..1d7464f 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -156,6 +156,12 @@ Fichiers sources + + Fichiers sources + + + Fichiers sources + diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 9d3f9bb..9e8787c 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,18 +3,20 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=260,25 +Pos=24,182 Size=694,210 [Window][Objects] -Pos=994,39 +Pos=35,13 Size=492,353 +Collapsed=1 [Window][Terrain] -Pos=1039,19 +Pos=1123,109 Size=418,94 +Collapsed=1 [Window][Light] -Pos=185,468 +Pos=58,418 Size=418,240 diff --git a/enginecustom/light.vs b/enginecustom/light.vs index 81923e6..8e32839 100644 --- a/enginecustom/light.vs +++ b/enginecustom/light.vs @@ -1,7 +1,3 @@ -//////////////////////////////////////////////////////////////////////////////// -// Filename: light.vs -//////////////////////////////////////////////////////////////////////////////// - ///////////// // DEFINES // ///////////// diff --git a/enginecustom/modelclass.cpp b/enginecustom/modelclass.cpp index 38dcfa7..c47f3b1 100644 --- a/enginecustom/modelclass.cpp +++ b/enginecustom/modelclass.cpp @@ -19,7 +19,8 @@ ModelClass::~ModelClass() { } -bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, char* textureFilename1, char* textureFilename2, char* textureFilename3) +bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* modelFilename, char* textureFilename1, char* textureFilename2, char* textureFilename3, + char* textureFilename4, char* textureFilename5, char* textureFilename6) { bool result; @@ -40,7 +41,7 @@ bool ModelClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceCon return false; } // Load the textures for this model. - result = LoadTextures(device, deviceContext, textureFilename1, textureFilename2, textureFilename3); + result = LoadTextures(device, deviceContext, textureFilename1, textureFilename2, textureFilename3, textureFilename4, textureFilename5, textureFilename6); if (!result) { return false; @@ -206,13 +207,14 @@ void ModelClass::RenderBuffers(ID3D11DeviceContext* deviceContext) } -bool ModelClass::LoadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename1, char* filename2, char* filename3) +bool ModelClass::LoadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename1, char* filename2, char* filename3, char* filename4, char* filename5, + char* filename6) { bool result; // Create and initialize the texture object array. - m_Textures = new TextureClass[3]; + m_Textures = new TextureClass[6]; result = m_Textures[0].Initialize(device, deviceContext, filename1); if (!result) @@ -232,6 +234,24 @@ bool ModelClass::LoadTextures(ID3D11Device* device, ID3D11DeviceContext* deviceC return false; } + result = m_Textures[3].Initialize(device, deviceContext, filename4); + if (!result) + { + return false; + } + + result = m_Textures[4].Initialize(device, deviceContext, filename5); + if (!result) + { + return false; + } + + result = m_Textures[5].Initialize(device, deviceContext, filename6); + if (!result) + { + return false; + } + return true; } @@ -244,6 +264,9 @@ void ModelClass::ReleaseTextures() m_Textures[0].Shutdown(); m_Textures[1].Shutdown(); m_Textures[2].Shutdown(); + m_Textures[3].Shutdown(); + m_Textures[4].Shutdown(); + m_Textures[5].Shutdown(); delete[] m_Textures; m_Textures = 0; diff --git a/enginecustom/modelclass.h b/enginecustom/modelclass.h index 70eed55..3db14c8 100644 --- a/enginecustom/modelclass.h +++ b/enginecustom/modelclass.h @@ -80,7 +80,7 @@ public: ModelClass(const ModelClass&); ~ModelClass(); - bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, char*, char*, char*); + bool Initialize(ID3D11Device*, ID3D11DeviceContext*, char*, char*, char*, char*, char*, char*, char*); void Shutdown(); void Render(ID3D11DeviceContext*); @@ -91,7 +91,7 @@ private: bool InitializeBuffers(ID3D11Device*); void ShutdownBuffers(); void RenderBuffers(ID3D11DeviceContext*); - bool LoadTextures(ID3D11Device*, ID3D11DeviceContext*, char*, char*, char*); + bool LoadTextures(ID3D11Device*, ID3D11DeviceContext*, char*, char*, char*, char*, char*, char*); void ReleaseTextures(); bool LoadModel(char*); diff --git a/enginecustom/packages.config b/enginecustom/packages.config deleted file mode 100644 index cbf6205..0000000 --- a/enginecustom/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/enginecustom/papier.tga b/enginecustom/papier.tga deleted file mode 100644 index 4f0b9b9..0000000 Binary files a/enginecustom/papier.tga and /dev/null differ diff --git a/enginecustom/shadermanagerclass.cpp b/enginecustom/shadermanagerclass.cpp new file mode 100644 index 0000000..79baf23 --- /dev/null +++ b/enginecustom/shadermanagerclass.cpp @@ -0,0 +1,262 @@ +#include "shadermanagerclass.h" + +ShaderManagerClass::ShaderManagerClass() +{ + m_TextureShader = 0; + m_NormalMapShader = 0; + m_MultitextureShader = 0; + m_TranslateShader = 0; + m_AlphaMapShader = 0; + m_SpecMapShader = 0; + m_TransparentShader = 0; +} + + +ShaderManagerClass::ShaderManagerClass(const ShaderManagerClass& other) +{ +} + + +ShaderManagerClass::~ShaderManagerClass() +{ +} + + +bool ShaderManagerClass::Initialize(ID3D11Device* device, HWND hwnd) +{ + bool result; + + // Create and initialize the texture shader object. + m_TextureShader = new TextureShaderClass; + + result = m_TextureShader->Initialize(device, hwnd); + if (!result) + { + return false; + } + + // Create and initialize the normal map shader object. + m_NormalMapShader = new NormalMapShaderClass; + + result = m_NormalMapShader->Initialize(device, hwnd); + if (!result) + { + return false; + } + + // Create and initialize the multitexture shader object. + m_MultitextureShader = new MultiTextureShaderClass; + + result = m_MultitextureShader->Initialize(device, hwnd); + if (!result) + { + return false; + } + + // Create and initialize the translate shader object. + m_TranslateShader = new TranslateShaderClass; + + result = m_TranslateShader->Initialize(device, hwnd); + if (!result) + { + return false; + } + + // Create and initialize the alpha map shader object. + m_AlphaMapShader = new AlphaMapShaderClass; + + result = m_AlphaMapShader->Initialize(device, hwnd); + if (!result) + { + return false; + } + + // Create and initialize the specular map shader object. + m_SpecMapShader = new SpecMapShaderClass; + + result = m_SpecMapShader->Initialize(device, hwnd); + if (!result) + { + return false; + } + + // Create and initialize the specular map shader object. + m_TransparentShader = new TransparentShaderClass; + + result = m_TransparentShader->Initialize(device, hwnd); + if (!result) + { + return false; + } + + return true; +} + +void ShaderManagerClass::Shutdown() +{ + // Release the normal map shader object. + if (m_NormalMapShader) + { + m_NormalMapShader->Shutdown(); + delete m_NormalMapShader; + m_NormalMapShader = 0; + } + + // Release the texture shader object. + if (m_TextureShader) + { + m_TextureShader->Shutdown(); + delete m_TextureShader; + m_TextureShader = 0; + } + + // Release the multitexture shader object. + if (m_MultitextureShader) + { + m_MultitextureShader->Shutdown(); + delete m_MultitextureShader; + m_MultitextureShader = 0; + } + + // Release the multitexture shader object. + if (m_TranslateShader) + { + m_TranslateShader->Shutdown(); + delete m_TranslateShader; + m_TranslateShader = 0; + } + + // Release the alpha map shader object. + if (m_AlphaMapShader) + { + m_AlphaMapShader->Shutdown(); + delete m_AlphaMapShader; + m_AlphaMapShader = 0; + } + + // Release the specular map shader object. + if (m_SpecMapShader) + { + m_SpecMapShader->Shutdown(); + delete m_SpecMapShader; + m_SpecMapShader = 0; + } + + // Release the transparent shader object. + if (m_TransparentShader) + { + m_TransparentShader->Shutdown(); + delete m_TransparentShader; + m_TransparentShader = 0; + } + + return; + +} + +bool ShaderManagerClass::RenderTextureShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture) +{ + bool result; + + + result = m_TextureShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture); + if (!result) + { + return false; + } + + return true; +} + +bool ShaderManagerClass::RenderNormalMapShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* colorTexture, ID3D11ShaderResourceView* normalTexture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor) +{ + bool result; + + + result = m_NormalMapShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, colorTexture, normalTexture, lightDirection, diffuseColor); + if (!result) + { + return false; + } + + return true; +} + +bool ShaderManagerClass::RenderMultitextureShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2) +{ + bool result; + + + result = m_MultitextureShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2); + if (!result) + { + return false; + } + + return true; +} + +bool ShaderManagerClass::RenderTranslateShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture1, float valeur) +{ + bool result; + + + result = m_TranslateShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, valeur); + if (!result) + { + return false; + } + + return true; +} + +bool ShaderManagerClass::RenderAlphaMapShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3) +{ + bool result; + + + result = m_AlphaMapShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2, texture3); + if (!result) + { + return false; + } + + return true; +} + +bool ShaderManagerClass::RenderSpecMapShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture1, ID3D11ShaderResourceView* texture2, ID3D11ShaderResourceView* texture3, + XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 cameraPosition, XMFLOAT4 specularColor, float specularPower) +{ + bool result; + + + result = m_SpecMapShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, texture2, texture3, lightDirection, + diffuseColor, cameraPosition, specularColor, specularPower); + if (!result) + { + return false; + } + + return true; +} + +bool ShaderManagerClass::RenderTransparentShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture1, float blendAmount) +{ + bool result; + + + result = m_TransparentShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture1, blendAmount); + if (!result) + { + return false; + } + + return true; +} \ No newline at end of file diff --git a/enginecustom/shadermanagerclass.h b/enginecustom/shadermanagerclass.h new file mode 100644 index 0000000..542093d --- /dev/null +++ b/enginecustom/shadermanagerclass.h @@ -0,0 +1,47 @@ +#ifndef _SHADERMANAGERCLASS_H_ +#define _SHADERMANAGERCLASS_H_ + +/////////////////////// +// MY CLASS INCLUDES // +/////////////////////// +#include "textureshaderclass.h" +#include "normalmapshaderclass.h" +#include "Multitextureshaderclass.h" +#include "translateshaderclass.h" +#include "alphamapshaderclass.h" +#include "specmapshaderclass.h" +#include "transparentshaderclass.h" + + +//////////////////////////////////////////////////////////////////////////////// +// Class name: ShaderManagerClass +//////////////////////////////////////////////////////////////////////////////// +class ShaderManagerClass +{ +public: + ShaderManagerClass(); + ShaderManagerClass(const ShaderManagerClass&); + ~ShaderManagerClass(); + + bool Initialize(ID3D11Device*, HWND); + void Shutdown(); + bool RenderTextureShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*); + bool RenderNormalMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4); + bool RenderMultitextureShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*); + bool RenderTranslateShader(ID3D11DeviceContext*,int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float); + bool RenderAlphaMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*); + bool RenderSpecMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, + XMFLOAT3, XMFLOAT4, XMFLOAT3, XMFLOAT4, float); + bool RenderTransparentShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float); + +private: + TextureShaderClass* m_TextureShader; + NormalMapShaderClass* m_NormalMapShader; + MultiTextureShaderClass* m_MultitextureShader; + TranslateShaderClass* m_TranslateShader; + AlphaMapShaderClass* m_AlphaMapShader; + SpecMapShaderClass* m_SpecMapShader; + TransparentShaderClass* m_TransparentShader; +}; + +#endif \ No newline at end of file diff --git a/enginecustom/spec02.tga b/enginecustom/spec02.tga new file mode 100644 index 0000000..b5085a7 Binary files /dev/null and b/enginecustom/spec02.tga differ diff --git a/enginecustom/translateshaderclass.cpp b/enginecustom/translateshaderclass.cpp index 47692f3..6eca67c 100644 --- a/enginecustom/translateshaderclass.cpp +++ b/enginecustom/translateshaderclass.cpp @@ -8,7 +8,6 @@ TranslateShaderClass::TranslateShaderClass() m_layout = 0; m_matrixBuffer = 0; m_sampleState = 0; - m_translateBuffer = 0; } @@ -31,14 +30,14 @@ bool TranslateShaderClass::Initialize(ID3D11Device* device, HWND hwnd) int error; // Set the filename of the vertex shader. - error = wcscpy_s(vsFilename, 128, L"../Engine/translate.vs"); + error = wcscpy_s(vsFilename, 128, L"translate.vs"); if (error != 0) { return false; } // Set the filename of the pixel shader. - error = wcscpy_s(psFilename, 128, L"../Engine/translate.ps"); + error = wcscpy_s(psFilename, 128, L"translate.ps"); if (error != 0) { return false; diff --git a/enginecustom/translateshaderclass.h b/enginecustom/translateshaderclass.h index a51f1ab..779c535 100644 --- a/enginecustom/translateshaderclass.h +++ b/enginecustom/translateshaderclass.h @@ -55,7 +55,6 @@ private: ID3D11InputLayout* m_layout; ID3D11Buffer* m_matrixBuffer; ID3D11SamplerState* m_sampleState; - ID3D11Buffer* m_translateBuffer; }; diff --git a/enginecustom/transparent.ps b/enginecustom/transparent.ps new file mode 100644 index 0000000..47e0417 --- /dev/null +++ b/enginecustom/transparent.ps @@ -0,0 +1,38 @@ +///////////// +// GLOBALS // +///////////// +Texture2D shaderTexture : register(t0); +SamplerState SampleType : register(s0); + +cbuffer TransparentBuffer +{ + float blendAmount; +}; + + +////////////// +// TYPEDEFS // +////////////// +struct PixelInputType +{ + float4 position : SV_POSITION; + float2 tex : TEXCOORD0; +}; + + +//////////////////////////////////////////////////////////////////////////////// +// Pixel Shader +//////////////////////////////////////////////////////////////////////////////// +float4 TransparentPixelShader(PixelInputType input) : SV_TARGET +{ + float4 color; + + + // Sample the texture pixel at this location. + color = shaderTexture.Sample(SampleType, input.tex); + + // Set the alpha value of this pixel to the blending amount to create the alpha blending effect. + color.a = blendAmount; + + return color; +} \ No newline at end of file diff --git a/enginecustom/transparent.vs b/enginecustom/transparent.vs new file mode 100644 index 0000000..3c73098 --- /dev/null +++ b/enginecustom/transparent.vs @@ -0,0 +1,48 @@ +///////////// +// GLOBALS // +///////////// +cbuffer MatrixBuffer +{ + matrix worldMatrix; + matrix viewMatrix; + matrix projectionMatrix; +}; + + +////////////// +// TYPEDEFS // +////////////// +struct VertexInputType +{ + float4 position : POSITION; + float2 tex : TEXCOORD0; +}; + +struct PixelInputType +{ + float4 position : SV_POSITION; + float2 tex : TEXCOORD0; +}; + + +//////////////////////////////////////////////////////////////////////////////// +// Vertex Shader +//////////////////////////////////////////////////////////////////////////////// +PixelInputType TransparentVertexShader(VertexInputType input) +{ + PixelInputType output; + + + // Change the position vector to be 4 units for proper matrix calculations. + input.position.w = 1.0f; + + // Calculate the position of the vertex against the world, view, and projection matrices. + output.position = mul(input.position, worldMatrix); + output.position = mul(output.position, viewMatrix); + output.position = mul(output.position, projectionMatrix); + + // Store the texture coordinates for the pixel shader. + output.tex = input.tex; + + return output; +} diff --git a/enginecustom/transparentshaderclass.cpp b/enginecustom/transparentshaderclass.cpp new file mode 100644 index 0000000..e257abc --- /dev/null +++ b/enginecustom/transparentshaderclass.cpp @@ -0,0 +1,413 @@ +#include "transparentshaderclass.h" + + +TransparentShaderClass::TransparentShaderClass() +{ + m_vertexShader = 0; + m_pixelShader = 0; + m_layout = 0; + m_matrixBuffer = 0; + m_sampleState = 0; + m_transparentBuffer = 0; +} + + +TransparentShaderClass::TransparentShaderClass(const TransparentShaderClass& other) +{ +} + + +TransparentShaderClass::~TransparentShaderClass() +{ +} + + +bool TransparentShaderClass::Initialize(ID3D11Device* device, HWND hwnd) +{ + bool result; + wchar_t vsFilename[128]; + wchar_t psFilename[128]; + int error; + + // Set the filename of the vertex shader. + error = wcscpy_s(vsFilename, 128, L"transparent.vs"); + if (error != 0) + { + return false; + } + + // Set the filename of the pixel shader. + error = wcscpy_s(psFilename, 128, L"transparent.ps"); + if (error != 0) + { + return false; + } + + // Initialize the vertex and pixel shaders. + result = InitializeShader(device, hwnd, vsFilename, psFilename); + if (!result) + { + return false; + } + + return true; +} + + +void TransparentShaderClass::Shutdown() +{ + // Shutdown the vertex and pixel shaders as well as the related objects. + ShutdownShader(); + + return; +} + +bool TransparentShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, + XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, float blend) +{ + bool result; + + + // Set the shader parameters that it will use for rendering. + result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, blend); + if (!result) + { + return false; + } + + // Now render the prepared buffers with the shader. + RenderShader(deviceContext, indexCount); + + return true; +} + + +bool TransparentShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename) +{ + HRESULT result; + ID3D10Blob* errorMessage; + ID3D10Blob* vertexShaderBuffer; + ID3D10Blob* pixelShaderBuffer; + D3D11_INPUT_ELEMENT_DESC polygonLayout[2]; + unsigned int numElements; + D3D11_BUFFER_DESC matrixBufferDesc; + D3D11_SAMPLER_DESC samplerDesc; + D3D11_BUFFER_DESC transparentBufferDesc; + + + // Initialize the pointers this function will use to null. + errorMessage = 0; + vertexShaderBuffer = 0; + pixelShaderBuffer = 0; + + // Compile the vertex shader code. + result = D3DCompileFromFile(vsFilename, NULL, NULL, "TransparentVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, + &vertexShaderBuffer, &errorMessage); + if (FAILED(result)) + { + // If the shader failed to compile it should have writen something to the error message. + if (errorMessage) + { + OutputShaderErrorMessage(errorMessage, hwnd, vsFilename); + } + // If there was nothing in the error message then it simply could not find the shader file itself. + else + { + MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK); + } + + return false; + } + + // Compile the pixel shader code. + result = D3DCompileFromFile(psFilename, NULL, NULL, "TransparentPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, + &pixelShaderBuffer, &errorMessage); + if (FAILED(result)) + { + // If the shader failed to compile it should have writen something to the error message. + if (errorMessage) + { + OutputShaderErrorMessage(errorMessage, hwnd, psFilename); + } + // If there was nothing in the error message then it simply could not find the file itself. + else + { + MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK); + } + + return false; + } + + // Create the vertex shader from the buffer. + result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader); + if (FAILED(result)) + { + return false; + } + + // Create the pixel shader from the buffer. + result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader); + if (FAILED(result)) + { + return false; + } + + // Create the vertex input layout description. + polygonLayout[0].SemanticName = "POSITION"; + polygonLayout[0].SemanticIndex = 0; + polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; + polygonLayout[0].InputSlot = 0; + polygonLayout[0].AlignedByteOffset = 0; + polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; + polygonLayout[0].InstanceDataStepRate = 0; + + polygonLayout[1].SemanticName = "TEXCOORD"; + polygonLayout[1].SemanticIndex = 0; + polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT; + polygonLayout[1].InputSlot = 0; + polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; + polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; + polygonLayout[1].InstanceDataStepRate = 0; + + // Get a count of the elements in the layout. + numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]); + + // Create the vertex input layout. + result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), + vertexShaderBuffer->GetBufferSize(), &m_layout); + if (FAILED(result)) + { + return false; + } + + // Release the vertex shader buffer and pixel shader buffer since they are no longer needed. + vertexShaderBuffer->Release(); + vertexShaderBuffer = 0; + + pixelShaderBuffer->Release(); + pixelShaderBuffer = 0; + + // Setup the description of the dynamic matrix constant buffer that is in the vertex shader. + matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC; + matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType); + matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + matrixBufferDesc.MiscFlags = 0; + matrixBufferDesc.StructureByteStride = 0; + + // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class. + result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer); + if (FAILED(result)) + { + return false; + } + + // Create a texture sampler state description. + samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + samplerDesc.MipLODBias = 0.0f; + samplerDesc.MaxAnisotropy = 1; + samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; + samplerDesc.BorderColor[0] = 0; + samplerDesc.BorderColor[1] = 0; + samplerDesc.BorderColor[2] = 0; + samplerDesc.BorderColor[3] = 0; + samplerDesc.MinLOD = 0; + samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; + + // Create the texture sampler state. + result = device->CreateSamplerState(&samplerDesc, &m_sampleState); + if (FAILED(result)) + { + return false; + } + + // Setup the description of the transparent dynamic constant buffer that is in the pixel shader. + transparentBufferDesc.Usage = D3D11_USAGE_DYNAMIC; + transparentBufferDesc.ByteWidth = sizeof(TransparentBufferType); + transparentBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + transparentBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + transparentBufferDesc.MiscFlags = 0; + transparentBufferDesc.StructureByteStride = 0; + + // Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class. + result = device->CreateBuffer(&transparentBufferDesc, NULL, &m_transparentBuffer); + if (FAILED(result)) + { + return false; + } + + return true; +} + + +void TransparentShaderClass::ShutdownShader() +{ + // Release the transparent constant buffer. + if (m_transparentBuffer) + { + m_transparentBuffer->Release(); + m_transparentBuffer = 0; + } + + // Release the sampler state. + if (m_sampleState) + { + m_sampleState->Release(); + m_sampleState = 0; + } + + // Release the matrix constant buffer. + if (m_matrixBuffer) + { + m_matrixBuffer->Release(); + m_matrixBuffer = 0; + } + + // Release the layout. + if (m_layout) + { + m_layout->Release(); + m_layout = 0; + } + + // Release the pixel shader. + if (m_pixelShader) + { + m_pixelShader->Release(); + m_pixelShader = 0; + } + + // Release the vertex shader. + if (m_vertexShader) + { + m_vertexShader->Release(); + m_vertexShader = 0; + } + + return; +} + + +void TransparentShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename) +{ + char* compileErrors; + unsigned long long bufferSize, i; + ofstream fout; + + + // Get a pointer to the error message text buffer. + compileErrors = (char*)(errorMessage->GetBufferPointer()); + + // Get the length of the message. + bufferSize = errorMessage->GetBufferSize(); + + // Open a file to write the error message to. + fout.open("shader-error.txt"); + + // Write out the error message. + for (i = 0; i < bufferSize; i++) + { + fout << compileErrors[i]; + } + + // Close the file. + fout.close(); + + // Release the error message. + errorMessage->Release(); + errorMessage = 0; + + // Pop a message up on the screen to notify the user to check the text file for compile errors. + MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK); + + return; +} + +bool TransparentShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, + XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, float blend) +{ + HRESULT result; + D3D11_MAPPED_SUBRESOURCE mappedResource; + MatrixBufferType* dataPtr; + unsigned int bufferNumber; + TransparentBufferType* dataPtr2; + + + // Transpose the matrices to prepare them for the shader. + worldMatrix = XMMatrixTranspose(worldMatrix); + viewMatrix = XMMatrixTranspose(viewMatrix); + projectionMatrix = XMMatrixTranspose(projectionMatrix); + + // Lock the constant buffer so it can be written to. + result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); + if (FAILED(result)) + { + return false; + } + + // Get a pointer to the data in the constant buffer. + dataPtr = (MatrixBufferType*)mappedResource.pData; + + // Copy the matrices into the constant buffer. + dataPtr->world = worldMatrix; + dataPtr->view = viewMatrix; + dataPtr->projection = projectionMatrix; + + // Unlock the constant buffer. + deviceContext->Unmap(m_matrixBuffer, 0); + + // Set the position of the constant buffer in the vertex shader. + bufferNumber = 0; + + // Finally set the constant buffer in the vertex shader with the updated values. + deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer); + + // Set shader texture resource in the pixel shader. + deviceContext->PSSetShaderResources(0, 1, &texture); + + // Lock the transparent constant buffer so it can be written to. + result = deviceContext->Map(m_transparentBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); + if (FAILED(result)) + { + return false; + } + + // Get a pointer to the data in the transparent constant buffer. + dataPtr2 = (TransparentBufferType*)mappedResource.pData; + + // Copy the alpha blending value into the transparent constant buffer. + dataPtr2->blendAmount = blend; + + // Unlock the buffer. + deviceContext->Unmap(m_transparentBuffer, 0); + + // Set the position of the transparent constant buffer in the pixel shader. + bufferNumber = 0; + + // Now set the transparent constant buffer in the pixel shader with the updated values. + deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_transparentBuffer); + + return true; +} + + +void TransparentShaderClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount) +{ + // Set the vertex input layout. + deviceContext->IASetInputLayout(m_layout); + + // Set the vertex and pixel shaders that will be used to render the geometry. + deviceContext->VSSetShader(m_vertexShader, NULL, 0); + deviceContext->PSSetShader(m_pixelShader, NULL, 0); + + // Set the sampler state in the pixel shader. + deviceContext->PSSetSamplers(0, 1, &m_sampleState); + + // Render the geometry. + deviceContext->DrawIndexed(indexCount, 0, 0); + + return; +} \ No newline at end of file diff --git a/enginecustom/transparentshaderclass.h b/enginecustom/transparentshaderclass.h new file mode 100644 index 0000000..aa0ba22 --- /dev/null +++ b/enginecustom/transparentshaderclass.h @@ -0,0 +1,61 @@ +#ifndef _TRANSPARENTSHADERCLASS_H_ +#define _TRANSPARENTSHADERCLASS_H_ + + +////////////// +// INCLUDES // +////////////// +#include +#include +#include +#include +using namespace DirectX; +using namespace std; + + +//////////////////////////////////////////////////////////////////////////////// +// Class name: TransparentShaderClass +//////////////////////////////////////////////////////////////////////////////// +class TransparentShaderClass +{ +private: + struct MatrixBufferType + { + XMMATRIX world; + XMMATRIX view; + XMMATRIX projection; + }; + + struct TransparentBufferType + { + float blendAmount; + XMFLOAT3 padding; + }; + +public: + TransparentShaderClass(); + TransparentShaderClass(const TransparentShaderClass&); + ~TransparentShaderClass(); + + bool Initialize(ID3D11Device*, HWND); + void Shutdown(); + bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float); + +private: + bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*); + void ShutdownShader(); + void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*); + + bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, float); + void RenderShader(ID3D11DeviceContext*, int); + +private: + ID3D11VertexShader* m_vertexShader; + ID3D11PixelShader* m_pixelShader; + ID3D11InputLayout* m_layout; + ID3D11Buffer* m_matrixBuffer; + ID3D11SamplerState* m_sampleState; + ID3D11Buffer* m_transparentBuffer; +}; + +#endif