Skip to content

Commit

Permalink
UPBGE: Implement Moto function to generate frustum box.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
panzergame committed Mar 29, 2017
1 parent db5bc1b commit 4216f5c
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 26 deletions.
3 changes: 3 additions & 0 deletions intern/moto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(INC_SYS

set(SRC
intern/MT_CmMatrix4x4.cpp
intern/MT_Frustum.cpp
intern/MT_Matrix3x3.cpp
intern/MT_Matrix4x4.cpp
intern/MT_Quaternion.cpp
Expand All @@ -44,6 +45,7 @@ set(SRC
intern/MT_random.cpp

include/MT_CmMatrix4x4.h
include/MT_Frustum.h
include/MT_Matrix3x3.h
include/MT_Matrix4x4.h
include/MT_MinMax.h
Expand All @@ -57,6 +59,7 @@ set(SRC
include/MT_Vector4.h
include/MT_random.h

include/MT_Frustum.inl
include/MT_Matrix3x3.inl
include/MT_Matrix4x4.inl
include/MT_Quaternion.inl
Expand Down
35 changes: 35 additions & 0 deletions intern/moto/include/MT_Frustum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Tristan Porteries.
*
* ***** END GPL LICENSE BLOCK *****
*/

/** \file moto/include/MT_Frustum.h
* \ingroup moto
*/

#include "MT_Matrix4x4.h"

#include <array>

void MT_FrustumBox(const MT_Matrix4x4& mat, std::array<MT_Vector3, 8>& box);

#ifdef GEN_INLINED
# include "MT_Frustum.inl"
#endif
48 changes: 48 additions & 0 deletions intern/moto/include/MT_Frustum.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Tristan Porteries.
*
* ***** END GPL LICENSE BLOCK *****
*/

/** \file moto/include/MT_Frustum.inl
* \ingroup moto
*/

#include "MT_Optimize.h"

GEN_INLINE void MT_FrustumBox(const MT_Matrix4x4& mat, std::array<MT_Vector3, 8>& box)
{
static const MT_Vector3 normalizedBox[8] = {
MT_Vector3(-1.0f, -1.0f, -1.0f),
MT_Vector3(-1.0f, 1.0f, -1.0f),
MT_Vector3(1.0f, 1.0f, -1.0f),
MT_Vector3(1.0f, -1.0f, -1.0f),
MT_Vector3(-1.0f, -1.0f, 1.0f),
MT_Vector3(-1.0f, 1.0f, 1.0f),
MT_Vector3(1.0f, 1.0f, 1.0f),
MT_Vector3(1.0f, -1.0f, 1.0f)
};

for (unsigned short i = 0; i < 8; ++i) {
const MT_Vector3& p3 = normalizedBox[i];
const MT_Vector4 p4 = mat * MT_Vector4(p3.x(), p3.y(), p3.z(), 1.0f);

box[i] = p4.to3d() / p4.w();
}
}
32 changes: 32 additions & 0 deletions intern/moto/intern/MT_Frustum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Tristan Porteries.
*
* ***** END GPL LICENSE BLOCK *****
*/

/** \file moto/include/MT_Frustum.cpp
* \ingroup moto
*/


#include "MT_Frustum.h"

#ifndef GEN_INLINED
# include "MT_Frustum.inl"
#endif
29 changes: 3 additions & 26 deletions source/gameengine/Rasterizer/RAS_DebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "RAS_DebugDraw.h"
#include "RAS_OpenGLDebugDraw.h"

#include "MT_Frustum.h"

RAS_DebugDraw::Shape::Shape(const MT_Vector4& color)
:m_color(color)
{
Expand Down Expand Up @@ -118,37 +120,12 @@ void RAS_DebugDraw::DrawSolidBox(const std::array<MT_Vector3, 8>& vertices, cons
void RAS_DebugDraw::DrawCameraFrustum(const MT_Matrix4x4& projmat, const MT_Matrix4x4& viewmat)
{
std::array<MT_Vector3, 8> box;

box[0][0] = box[1][0] = box[4][0] = box[5][0] = -1.0f;
box[2][0] = box[3][0] = box[6][0] = box[7][0] = 1.0f;
box[0][1] = box[3][1] = box[4][1] = box[7][1] = -1.0f;
box[1][1] = box[2][1] = box[5][1] = box[6][1] = 1.0f;
box[0][2] = box[1][2] = box[2][2] = box[3][2] = -1.0f;
box[4][2] = box[5][2] = box[6][2] = box[7][2] = 1.0f;

const MT_Matrix4x4 mv = (projmat * viewmat).inverse();

for (MT_Vector3& p3 : box) {
const MT_Vector4 p4 = mv * MT_Vector4(p3.x(), p3.y(), p3.z(), 1.0f);
p3 = MT_Vector3(p4.x() / p4.w(), p4.y() / p4.w(), p4.z() / p4.w());
}
MT_FrustumBox((projmat * viewmat).inverse(), box);

DrawSolidBox(box, MT_Vector4(0.4f, 0.4f, 0.4f, 0.4f), MT_Vector4(0.0f, 0.0f, 0.0f, 0.4f),
MT_Vector4(0.8f, 0.5f, 0.0f, 1.0f));
}

/*void RAS_DebugDraw::DisableForText()
{
SetAlphaBlend(GPU_BLEND_ALPHA);
SetLines(false); // needed for texture fonts otherwise they render as wireframe
Enable(RAS_CULL_FACE);
ProcessLighting(false, MT_Transform::Identity());
m_impl->DisableForText();
}*/

void RAS_DebugDraw::RenderBox2D(const MT_Vector2& pos, const MT_Vector2& size, const MT_Vector4& color)
{
m_boxes2D.emplace_back(pos, size, color);
Expand Down

0 comments on commit 4216f5c

Please sign in to comment.