diff --git a/enginecustom/Frustrumclass.cpp b/enginecustom/Frustrumclass.cpp new file mode 100644 index 0000000..a88ca5d --- /dev/null +++ b/enginecustom/Frustrumclass.cpp @@ -0,0 +1,300 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: frustumclass.cpp +//////////////////////////////////////////////////////////////////////////////// +#include "Frustrumclass.h" + + +FrustumClass::FrustumClass() +{ +} + + +FrustumClass::FrustumClass(const FrustumClass& other) +{ +} + + +FrustumClass::~FrustumClass() +{ +} + + +void FrustumClass::ConstructFrustum(XMMATRIX viewMatrix, XMMATRIX projectionMatrix, float screenDepth) +{ + XMMATRIX finalMatrix; + XMFLOAT4X4 projMatrix, matrix; + float zMinimum, r, t; + + + // Load the projection matrix into a XMFLOAT4X4 structure. + XMStoreFloat4x4(&projMatrix, projectionMatrix); + + // Calculate the minimum Z distance in the frustum. + zMinimum = -projMatrix._43 / projMatrix._33; + r = screenDepth / (screenDepth - zMinimum); + projMatrix._33 = r; + projMatrix._43 = -r * zMinimum; + + // Load the updated XMFLOAT4X4 back into the original projection matrix. + projectionMatrix = XMLoadFloat4x4(&projMatrix); + + // Create the frustum matrix from the view matrix and updated projection matrix. + finalMatrix = XMMatrixMultiply(viewMatrix, projectionMatrix); + + // Load the final matrix into a XMFLOAT4X4 structure. + XMStoreFloat4x4(&matrix, finalMatrix); + + // Get the near plane of the frustum. + m_planes[0].x = matrix._13; + m_planes[0].y = matrix._23; + m_planes[0].z = matrix._33; + m_planes[0].w = matrix._43; + + // Normalize it. + t = (float)sqrt((m_planes[0].x * m_planes[0].x) + (m_planes[0].y * m_planes[0].y) + (m_planes[0].z * m_planes[0].z)); + m_planes[0].x /= t; + m_planes[0].y /= t; + m_planes[0].z /= t; + m_planes[0].w /= t; + + // Calculate the far plane of frustum. + m_planes[1].x = matrix._14 - matrix._13; + m_planes[1].y = matrix._24 - matrix._23; + m_planes[1].z = matrix._34 - matrix._33; + m_planes[1].w = matrix._44 - matrix._43; + + // Normalize it. + t = (float)sqrt((m_planes[1].x * m_planes[1].x) + (m_planes[1].y * m_planes[1].y) + (m_planes[1].z * m_planes[1].z)); + m_planes[1].x /= t; + m_planes[1].y /= t; + m_planes[1].z /= t; + m_planes[1].w /= t; + + // Calculate the left plane of frustum. + m_planes[2].x = matrix._14 + matrix._11; + m_planes[2].y = matrix._24 + matrix._21; + m_planes[2].z = matrix._34 + matrix._31; + m_planes[2].w = matrix._44 + matrix._41; + + // Normalize it. + t = (float)sqrt((m_planes[2].x * m_planes[2].x) + (m_planes[2].y * m_planes[2].y) + (m_planes[2].z * m_planes[2].z)); + m_planes[2].x /= t; + m_planes[2].y /= t; + m_planes[2].z /= t; + m_planes[2].w /= t; + + // Calculate the right plane of frustum. + m_planes[3].x = matrix._14 - matrix._11; + m_planes[3].y = matrix._24 - matrix._21; + m_planes[3].z = matrix._34 - matrix._31; + m_planes[3].w = matrix._44 - matrix._41; + + // Normalize it. + t = (float)sqrt((m_planes[3].x * m_planes[3].x) + (m_planes[3].y * m_planes[3].y) + (m_planes[3].z * m_planes[3].z)); + m_planes[3].x /= t; + m_planes[3].y /= t; + m_planes[3].z /= t; + m_planes[3].w /= t; + + // Calculate the top plane of frustum. + m_planes[4].x = matrix._14 - matrix._12; + m_planes[4].y = matrix._24 - matrix._22; + m_planes[4].z = matrix._34 - matrix._32; + m_planes[4].w = matrix._44 - matrix._42; + + // Normalize it. + t = (float)sqrt((m_planes[4].x * m_planes[4].x) + (m_planes[4].y * m_planes[4].y) + (m_planes[4].z * m_planes[4].z)); + m_planes[4].x /= t; + m_planes[4].y /= t; + m_planes[4].z /= t; + m_planes[4].w /= t; + + // Calculate the bottom plane of frustum. + m_planes[5].x = matrix._14 + matrix._12; + m_planes[5].y = matrix._24 + matrix._22; + m_planes[5].z = matrix._34 + matrix._32; + m_planes[5].w = matrix._44 + matrix._42; + + // Normalize it. + t = (float)sqrt((m_planes[5].x * m_planes[5].x) + (m_planes[5].y * m_planes[5].y) + (m_planes[5].z * m_planes[5].z)); + m_planes[5].x /= t; + m_planes[5].y /= t; + m_planes[5].z /= t; + m_planes[5].w /= t; + + return; +} + +bool FrustumClass::CheckPoint(float x, float y, float z) +{ + int i; + + + // Check if the point is inside all six planes of the view frustum. + for (i = 0; i < 6; i++) + { + if (((m_planes[i].x * x) + (m_planes[i].y * y) + (m_planes[i].z * z) + m_planes[i].w) < 0.0f) + { + return false; + } + } + + return true; +} + +bool FrustumClass::CheckCube(float xCenter, float yCenter, float zCenter, float radius) +{ + int i; + + + // Check if any one point of the cube is in the view frustum. + for (i = 0; i < 6; i++) + { + if (m_planes[i].x * (xCenter - radius) + + m_planes[i].y * (yCenter - radius) + + m_planes[i].z * (zCenter - radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + radius) + + m_planes[i].y * (yCenter - radius) + + m_planes[i].z * (zCenter - radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter - radius) + + m_planes[i].y * (yCenter + radius) + + m_planes[i].z * (zCenter - radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + radius) + + m_planes[i].y * (yCenter + radius) + + m_planes[i].z * (zCenter - radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter - radius) + + m_planes[i].y * (yCenter - radius) + + m_planes[i].z * (zCenter + radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + radius) + + m_planes[i].y * (yCenter - radius) + + m_planes[i].z * (zCenter + radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter - radius) + + m_planes[i].y * (yCenter + radius) + + m_planes[i].z * (zCenter + radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + radius) + + m_planes[i].y * (yCenter + radius) + + m_planes[i].z * (zCenter + radius) + m_planes[i].w >= 0.0f) + { + continue; + } + + return false; + } + + return true; +} + +bool FrustumClass::CheckSphere(float xCenter, float yCenter, float zCenter, float radius) +{ + int i; + + + // Check if the radius of the sphere is inside the view frustum. + for (i = 0; i < 6; i++) + { + if (((m_planes[i].x * xCenter) + (m_planes[i].y * yCenter) + (m_planes[i].z * zCenter) + m_planes[i].w) < -radius) + { + return false; + } + } + + return true; +} + +bool FrustumClass::CheckRectangle(float xCenter, float yCenter, float zCenter, float xSize, float ySize, float zSize) +{ + int i; + + + // Check if any of the 6 planes of the rectangle are inside the view frustum. + for (i = 0; i < 6; i++) + { + if (m_planes[i].x * (xCenter - xSize) + + m_planes[i].y * (yCenter - ySize) + + m_planes[i].z * (zCenter - zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + xSize) + + m_planes[i].y * (yCenter - ySize) + + m_planes[i].z * (zCenter - zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter - xSize) + + m_planes[i].y * (yCenter + ySize) + + m_planes[i].z * (zCenter - zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter - xSize) + + m_planes[i].y * (yCenter - ySize) + + m_planes[i].z * (zCenter + zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + xSize) + + m_planes[i].y * (yCenter + ySize) + + m_planes[i].z * (zCenter - zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + xSize) + + m_planes[i].y * (yCenter - ySize) + + m_planes[i].z * (zCenter + zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter - xSize) + + m_planes[i].y * (yCenter + ySize) + + m_planes[i].z * (zCenter + zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + if (m_planes[i].x * (xCenter + xSize) + + m_planes[i].y * (yCenter + ySize) + + m_planes[i].z * (zCenter + zSize) + m_planes[i].w >= 0.0f) + { + continue; + } + + return false; + } + + return true; +} diff --git a/enginecustom/Frustrumclass.h b/enginecustom/Frustrumclass.h new file mode 100644 index 0000000..593de6d --- /dev/null +++ b/enginecustom/Frustrumclass.h @@ -0,0 +1,32 @@ +#ifndef _FRUSTUMCLASS_H_ +#define _FRUSTUMCLASS_H_ + +////////////// +// INCLUDES // +////////////// +#include +using namespace DirectX; + + +//////////////////////////////////////////////////////////////////////////////// +// Class name: FrustumClass +//////////////////////////////////////////////////////////////////////////////// +class FrustumClass +{ +public: + FrustumClass(); + FrustumClass(const FrustumClass&); + ~FrustumClass(); + + void ConstructFrustum(XMMATRIX, XMMATRIX, float); + + bool CheckPoint(float, float, float); + bool CheckCube(float, float, float, float); + bool CheckSphere(float, float, float, float); + bool CheckRectangle(float, float, float, float, float, float); + +private: + XMFLOAT4 m_planes[6]; +}; + +#endif \ No newline at end of file diff --git a/enginecustom/Modellistclass.cpp b/enginecustom/Modellistclass.cpp new file mode 100644 index 0000000..786d9d5 --- /dev/null +++ b/enginecustom/Modellistclass.cpp @@ -0,0 +1,69 @@ +#include "Modellistclass.h" + +ModelListClass::ModelListClass() +{ + m_ModelInfoList = 0; +} + + +ModelListClass::ModelListClass(const ModelListClass& other) +{ +} + + +ModelListClass::~ModelListClass() +{ +} + + +void ModelListClass::Initialize(int numModels) +{ + int i; + + + // Store the number of models. + m_modelCount = numModels; + + // Create a list array of the model information. + m_ModelInfoList = new ModelInfoType[m_modelCount]; + + + // Seed the random generator with the current time. + srand((unsigned int)time(NULL)); + + // Go through all the models and randomly generate the position. + for (i = 0; i < m_modelCount; i++) + { + // Generate a random position in front of the viewer for the mode. + m_ModelInfoList[i].positionX = (((float)rand() - (float)rand()) / RAND_MAX) * 10.0f; + m_ModelInfoList[i].positionY = (((float)rand() - (float)rand()) / RAND_MAX) * 10.0f; + m_ModelInfoList[i].positionZ = ((((float)rand() - (float)rand()) / RAND_MAX) * 10.0f) + 5.0f; + } + + return; +} + +void ModelListClass::Shutdown() +{ + // Release the model information list. + if (m_ModelInfoList) + { + delete[] m_ModelInfoList; + m_ModelInfoList = 0; + } + + return; +} + +int ModelListClass::GetModelCount() +{ + return m_modelCount; +} + +void ModelListClass::GetData(int index, float& positionX, float& positionY, float& positionZ) +{ + positionX = m_ModelInfoList[index].positionX; + positionY = m_ModelInfoList[index].positionY; + positionZ = m_ModelInfoList[index].positionZ; + return; +} diff --git a/enginecustom/Modellistclass.h b/enginecustom/Modellistclass.h new file mode 100644 index 0000000..0ce3ca4 --- /dev/null +++ b/enginecustom/Modellistclass.h @@ -0,0 +1,40 @@ +#ifndef _MODELLISTCLASS_H_ +#define _MODELLISTCLASS_H_ + + +////////////// +// INCLUDES // +////////////// +#include +#include + + +/////////////////////////////////////////////////////////////////////////////// +// Class name: ModelListClass +/////////////////////////////////////////////////////////////////////////////// +class ModelListClass +{ +private: + struct ModelInfoType + { + float positionX, positionY, positionZ; + }; + +public: + ModelListClass(); + ModelListClass(const ModelListClass&); + ~ModelListClass(); + + void Initialize(int); + void Shutdown(); + + int GetModelCount(); + void GetData(int, float&, float&, float&); + +private: + int m_modelCount; + ModelInfoType* m_ModelInfoList; +}; + +#endif + diff --git a/enginecustom/Positionclass.cpp b/enginecustom/Positionclass.cpp new file mode 100644 index 0000000..246815f --- /dev/null +++ b/enginecustom/Positionclass.cpp @@ -0,0 +1,96 @@ +#include "Positionclass.h" + +PositionClass::PositionClass() +{ + m_frameTime = 0.0f; + m_rotationY = 0.0f; + m_leftTurnSpeed = 0.0f; + m_rightTurnSpeed = 0.0f; +} + + +PositionClass::PositionClass(const PositionClass& other) +{ +} + + +PositionClass::~PositionClass() +{ +} + +void PositionClass::SetFrameTime(float time) +{ + m_frameTime = time; + return; +} + +void PositionClass::GetRotation(float& y) +{ + y = m_rotationY; + return; +} + +void PositionClass::TurnLeft(bool keydown) +{ + // If the key is pressed increase the speed at which the camera turns left. If not slow down the turn speed. + if (keydown) + { + m_leftTurnSpeed += m_frameTime * 1.5f; + + if (m_leftTurnSpeed > (m_frameTime * 200.0f)) + { + m_leftTurnSpeed = m_frameTime * 200.0f; + } + } + else + { + m_leftTurnSpeed -= m_frameTime * 1.0f; + + if (m_leftTurnSpeed < 0.0f) + { + m_leftTurnSpeed = 0.0f; + } + } + + // Update the rotation using the turning speed. + m_rotationY -= m_leftTurnSpeed; + if (m_rotationY < 0.0f) + { + m_rotationY += 360.0f; + } + + return; +} + + +void PositionClass::TurnRight(bool keydown) +{ + // If the key is pressed increase the speed at which the camera turns right. If not slow down the turn speed. + if (keydown) + { + m_rightTurnSpeed += m_frameTime * 1.5f; + + if (m_rightTurnSpeed > (m_frameTime * 200.0f)) + { + m_rightTurnSpeed = m_frameTime * 200.0f; + } + } + else + { + m_rightTurnSpeed -= m_frameTime * 1.0f; + + if (m_rightTurnSpeed < 0.0f) + { + m_rightTurnSpeed = 0.0f; + } + } + + // Update the rotation using the turning speed. + m_rotationY += m_rightTurnSpeed; + if (m_rotationY > 360.0f) + { + m_rotationY -= 360.0f; + } + + return; +} \ No newline at end of file diff --git a/enginecustom/Positionclass.h b/enginecustom/Positionclass.h new file mode 100644 index 0000000..2e5702e --- /dev/null +++ b/enginecustom/Positionclass.h @@ -0,0 +1,33 @@ +#ifndef _POSITIONCLASS_H_ +#define _POSITIONCLASS_H_ + + +////////////// +// INCLUDES // +////////////// +#include + + +//////////////////////////////////////////////////////////////////////////////// +// Class name: PositionClass +//////////////////////////////////////////////////////////////////////////////// +class PositionClass +{ +public: + PositionClass(); + PositionClass(const PositionClass&); + ~PositionClass(); + + void SetFrameTime(float); + void GetRotation(float&); + + void TurnLeft(bool); + void TurnRight(bool); + +private: + float m_frameTime; + float m_rotationY; + float m_leftTurnSpeed, m_rightTurnSpeed; +}; + +#endif diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 1c3bc25..e02b162 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -24,6 +24,9 @@ #include "inputclass.h" #include "normalmapshaderclass.h" #include "specmapshaderclass.h" +#include "Frustrumclass.h" +#include "Positionclass.h" +#include "Modellistclass.h" ///////////// @@ -53,6 +56,7 @@ private: bool Render(float, float, float, float); bool UpdateMouseStrings(int, int, bool); bool UpdateFps(); + bool UpdateRenderCountString(int); private: D3DClass* m_Direct3D; @@ -71,6 +75,7 @@ private: LightClass* m_Lights; int m_numLights; FontShaderClass* m_FontShader; + TextClass* m_RenderCountString; FontClass* m_Font; TextClass *m_TextString1, *m_TextString2, *m_TextString3; FpsClass* m_Fps; @@ -78,6 +83,9 @@ private: int m_previousFps; NormalMapShaderClass* m_NormalMapShader; SpecMapShaderClass* m_SpecMapShader; + ModelListClass* m_ModelList; + PositionClass* m_Position; + FrustumClass* m_Frustum; }; #endif diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj index 2d54fd5..0f26008 100644 --- a/enginecustom/enginecustom.vcxproj +++ b/enginecustom/enginecustom.vcxproj @@ -29,14 +29,17 @@ + + + @@ -55,13 +58,16 @@ + + + diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index 1a53214..de7c42b 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -99,6 +99,15 @@ Fichiers sources + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + @@ -170,6 +179,15 @@ Fichiers d%27en-tête + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + diff --git a/enginecustom/inputclass.cpp b/enginecustom/inputclass.cpp index c50216a..d220be9 100644 --- a/enginecustom/inputclass.cpp +++ b/enginecustom/inputclass.cpp @@ -255,6 +255,27 @@ bool InputClass::IsEscapePressed() return false; } +bool InputClass::IsLeftArrowPressed() +{ + if (m_keyboardState[DIK_LEFT] & 0x80) + { + return true; + } + + return false; +} + + +bool InputClass::IsRightArrowPressed() +{ + if (m_keyboardState[DIK_RIGHT] & 0x80) + { + return true; + } + + return false; +} + void InputClass::GetMouseLocation(int& mouseX, int& mouseY) { mouseX = m_mouseX; diff --git a/enginecustom/inputclass.h b/enginecustom/inputclass.h index 21d3c2b..0e80bb2 100644 --- a/enginecustom/inputclass.h +++ b/enginecustom/inputclass.h @@ -36,6 +36,8 @@ public: bool IsMousePressed(); void KeyDown(unsigned int); void KeyUp(unsigned int); + bool IsLeftArrowPressed(); + bool IsRightArrowPressed(); bool IsKeyDown(unsigned int);