Skip to content

Commit 4216f5c

Browse files
committed
UPBGE: Implement Moto function to generate frustum box.
The new function MT_FrustumBox in MT_Frustum is used to generate the 8 vertices of a box using the combinaison of the projection and modelview matrix of a camera.
1 parent db5bc1b commit 4216f5c

File tree

5 files changed

+121
-26
lines changed

5 files changed

+121
-26
lines changed

intern/moto/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ set(INC_SYS
3434

3535
set(SRC
3636
intern/MT_CmMatrix4x4.cpp
37+
intern/MT_Frustum.cpp
3738
intern/MT_Matrix3x3.cpp
3839
intern/MT_Matrix4x4.cpp
3940
intern/MT_Quaternion.cpp
@@ -44,6 +45,7 @@ set(SRC
4445
intern/MT_random.cpp
4546

4647
include/MT_CmMatrix4x4.h
48+
include/MT_Frustum.h
4749
include/MT_Matrix3x3.h
4850
include/MT_Matrix4x4.h
4951
include/MT_MinMax.h
@@ -57,6 +59,7 @@ set(SRC
5759
include/MT_Vector4.h
5860
include/MT_random.h
5961

62+
include/MT_Frustum.inl
6063
include/MT_Matrix3x3.inl
6164
include/MT_Matrix4x4.inl
6265
include/MT_Quaternion.inl

intern/moto/include/MT_Frustum.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* ***** BEGIN GPL LICENSE BLOCK *****
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software Foundation,
16+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*
18+
* Contributor(s): Tristan Porteries.
19+
*
20+
* ***** END GPL LICENSE BLOCK *****
21+
*/
22+
23+
/** \file moto/include/MT_Frustum.h
24+
* \ingroup moto
25+
*/
26+
27+
#include "MT_Matrix4x4.h"
28+
29+
#include <array>
30+
31+
void MT_FrustumBox(const MT_Matrix4x4& mat, std::array<MT_Vector3, 8>& box);
32+
33+
#ifdef GEN_INLINED
34+
# include "MT_Frustum.inl"
35+
#endif

intern/moto/include/MT_Frustum.inl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* ***** BEGIN GPL LICENSE BLOCK *****
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software Foundation,
16+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*
18+
* Contributor(s): Tristan Porteries.
19+
*
20+
* ***** END GPL LICENSE BLOCK *****
21+
*/
22+
23+
/** \file moto/include/MT_Frustum.inl
24+
* \ingroup moto
25+
*/
26+
27+
#include "MT_Optimize.h"
28+
29+
GEN_INLINE void MT_FrustumBox(const MT_Matrix4x4& mat, std::array<MT_Vector3, 8>& box)
30+
{
31+
static const MT_Vector3 normalizedBox[8] = {
32+
MT_Vector3(-1.0f, -1.0f, -1.0f),
33+
MT_Vector3(-1.0f, 1.0f, -1.0f),
34+
MT_Vector3(1.0f, 1.0f, -1.0f),
35+
MT_Vector3(1.0f, -1.0f, -1.0f),
36+
MT_Vector3(-1.0f, -1.0f, 1.0f),
37+
MT_Vector3(-1.0f, 1.0f, 1.0f),
38+
MT_Vector3(1.0f, 1.0f, 1.0f),
39+
MT_Vector3(1.0f, -1.0f, 1.0f)
40+
};
41+
42+
for (unsigned short i = 0; i < 8; ++i) {
43+
const MT_Vector3& p3 = normalizedBox[i];
44+
const MT_Vector4 p4 = mat * MT_Vector4(p3.x(), p3.y(), p3.z(), 1.0f);
45+
46+
box[i] = p4.to3d() / p4.w();
47+
}
48+
}

intern/moto/intern/MT_Frustum.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* ***** BEGIN GPL LICENSE BLOCK *****
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software Foundation,
16+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*
18+
* Contributor(s): Tristan Porteries.
19+
*
20+
* ***** END GPL LICENSE BLOCK *****
21+
*/
22+
23+
/** \file moto/include/MT_Frustum.cpp
24+
* \ingroup moto
25+
*/
26+
27+
28+
#include "MT_Frustum.h"
29+
30+
#ifndef GEN_INLINED
31+
# include "MT_Frustum.inl"
32+
#endif

source/gameengine/Rasterizer/RAS_DebugDraw.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "RAS_DebugDraw.h"
2828
#include "RAS_OpenGLDebugDraw.h"
2929

30+
#include "MT_Frustum.h"
31+
3032
RAS_DebugDraw::Shape::Shape(const MT_Vector4& color)
3133
:m_color(color)
3234
{
@@ -118,37 +120,12 @@ void RAS_DebugDraw::DrawSolidBox(const std::array<MT_Vector3, 8>& vertices, cons
118120
void RAS_DebugDraw::DrawCameraFrustum(const MT_Matrix4x4& projmat, const MT_Matrix4x4& viewmat)
119121
{
120122
std::array<MT_Vector3, 8> box;
121-
122-
box[0][0] = box[1][0] = box[4][0] = box[5][0] = -1.0f;
123-
box[2][0] = box[3][0] = box[6][0] = box[7][0] = 1.0f;
124-
box[0][1] = box[3][1] = box[4][1] = box[7][1] = -1.0f;
125-
box[1][1] = box[2][1] = box[5][1] = box[6][1] = 1.0f;
126-
box[0][2] = box[1][2] = box[2][2] = box[3][2] = -1.0f;
127-
box[4][2] = box[5][2] = box[6][2] = box[7][2] = 1.0f;
128-
129-
const MT_Matrix4x4 mv = (projmat * viewmat).inverse();
130-
131-
for (MT_Vector3& p3 : box) {
132-
const MT_Vector4 p4 = mv * MT_Vector4(p3.x(), p3.y(), p3.z(), 1.0f);
133-
p3 = MT_Vector3(p4.x() / p4.w(), p4.y() / p4.w(), p4.z() / p4.w());
134-
}
123+
MT_FrustumBox((projmat * viewmat).inverse(), box);
135124

136125
DrawSolidBox(box, MT_Vector4(0.4f, 0.4f, 0.4f, 0.4f), MT_Vector4(0.0f, 0.0f, 0.0f, 0.4f),
137126
MT_Vector4(0.8f, 0.5f, 0.0f, 1.0f));
138127
}
139128

140-
/*void RAS_DebugDraw::DisableForText()
141-
{
142-
SetAlphaBlend(GPU_BLEND_ALPHA);
143-
SetLines(false); // needed for texture fonts otherwise they render as wireframe
144-
145-
Enable(RAS_CULL_FACE);
146-
147-
ProcessLighting(false, MT_Transform::Identity());
148-
149-
m_impl->DisableForText();
150-
}*/
151-
152129
void RAS_DebugDraw::RenderBox2D(const MT_Vector2& pos, const MT_Vector2& size, const MT_Vector4& color)
153130
{
154131
m_boxes2D.emplace_back(pos, size, color);

0 commit comments

Comments
 (0)