début frustrum

This commit is contained in:
Mamitiana RASOLOJAONA 2024-04-02 12:30:17 +02:00
parent 563fea3d75
commit 41fdb55b04
11 changed files with 625 additions and 0 deletions

View File

@ -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;
}

View File

@ -0,0 +1,32 @@
#ifndef _FRUSTUMCLASS_H_
#define _FRUSTUMCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <directxmath.h>
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

View File

@ -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;
}

View File

@ -0,0 +1,40 @@
#ifndef _MODELLISTCLASS_H_
#define _MODELLISTCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <stdlib.h>
#include <time.h>
///////////////////////////////////////////////////////////////////////////////
// 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

View File

@ -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;
}

View File

@ -0,0 +1,33 @@
#ifndef _POSITIONCLASS_H_
#define _POSITIONCLASS_H_
//////////////
// INCLUDES //
//////////////
#include <math.h>
////////////////////////////////////////////////////////////////////////////////
// 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

View File

@ -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

View File

@ -29,14 +29,17 @@
<ClCompile Include="fontclass.cpp" />
<ClCompile Include="fontshaderclass.cpp" />
<ClCompile Include="fpsclass.cpp" />
<ClCompile Include="Frustrumclass.cpp" />
<ClCompile Include="inputclass.cpp" />
<ClCompile Include="Lightclass.cpp" />
<ClCompile Include="lightmapshaderclass.cpp" />
<ClCompile Include="Lightshaderclass.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="modelclass.cpp" />
<ClCompile Include="Modellistclass.cpp" />
<ClCompile Include="Multitextureshaderclass.cpp" />
<ClCompile Include="normalmapshaderclass.cpp" />
<ClCompile Include="Positionclass.cpp" />
<ClCompile Include="specmapshaderclass.cpp" />
<ClCompile Include="Spriteclass.cpp" />
<ClCompile Include="Systemclass.cpp" />
@ -55,13 +58,16 @@
<ClInclude Include="fontclass.h" />
<ClInclude Include="fontshaderclass.h" />
<ClInclude Include="fpsclass.h" />
<ClInclude Include="Frustrumclass.h" />
<ClInclude Include="inputclass.h" />
<ClInclude Include="lightclass.h" />
<ClInclude Include="lightmapshaderclass.h" />
<ClInclude Include="lightshaderclass.h" />
<ClInclude Include="modelclass.h" />
<ClInclude Include="Modellistclass.h" />
<ClInclude Include="Multitextureshaderclass.h" />
<ClInclude Include="normalmapshaderclass.h" />
<ClInclude Include="Positionclass.h" />
<ClInclude Include="specmapshaderclass.h" />
<ClInclude Include="Spriteclass.h" />
<ClInclude Include="systemclass.h" />

View File

@ -99,6 +99,15 @@
<ClCompile Include="specmapshaderclass.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="Frustrumclass.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="Modellistclass.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="Positionclass.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="applicationclass.h">
@ -170,6 +179,15 @@
<ClInclude Include="specmapshaderclass.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="Frustrumclass.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="Modellistclass.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="Positionclass.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="font01.tga">

View File

@ -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;

View File

@ -36,6 +36,8 @@ public:
bool IsMousePressed();
void KeyDown(unsigned int);
void KeyUp(unsigned int);
bool IsLeftArrowPressed();
bool IsRightArrowPressed();
bool IsKeyDown(unsigned int);