|
From: <axl...@us...> - 2009-04-12 14:18:35
|
Revision: 204
http://hgengine.svn.sourceforge.net/hgengine/?rev=204&view=rev
Author: axlecrusher
Date: 2009-04-12 14:18:33 +0000 (Sun, 12 Apr 2009)
Log Message:
-----------
Move around the bounding box test logic. This avoids having to use Cast
to figure out which cull test to run
Modified Paths:
--------------
Mercury2/src/BoundingBox.cpp
Mercury2/src/BoundingBox.h
Mercury2/src/Mercury2.cpp
Mercury2/src/MercuryPlane.cpp
Mercury2/src/MercuryPlane.h
Mercury2/src/RenderGraph.cpp
Mercury2/src/RenderableNode.cpp
Mercury2/src/RenderableNode.h
Mercury2/src/Viewport.cpp
Mercury2/src/Viewport.h
Modified: Mercury2/src/BoundingBox.cpp
===================================================================
--- Mercury2/src/BoundingBox.cpp 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/BoundingBox.cpp 2009-04-12 14:18:33 UTC (rev 204)
@@ -4,6 +4,11 @@
#include <string.h>
#include <Viewport.h>
+#define SIGNED_DIST(x) m_normal.DotProduct(x)
+
+// origional algorithim was -x<0
+#define BEHIND_PLANE(x) x>=0
+
BoundingBox::BoundingBox(const MercuryVertex& center, const MercuryVertex& extend)
:m_center(center), m_extend(extend)
{
@@ -55,6 +60,32 @@
*this = bb;
}
+bool BoundingBox::Clip( const MercuryPlane& p )
+{
+ MercuryVertex dp = p.GetNormal().DotProduct3(m_normals[0], m_normals[1], m_normals[2]);
+ float x = ABS( m_extend.GetX() * dp[0] );
+ x += ABS( m_extend.GetY() * dp[1] );
+ x += ABS( m_extend.GetZ() * dp[2] );
+
+ float d = p.GetNormal().DotProduct( m_center+p.GetCenter() ); //signed distance
+ if ( ABS(d) <= x ) //intersection
+ return false;
+
+ return BEHIND_PLANE(d); //if we don't intersect, just see what side we are on
+}
+
+bool BoundingBox::Clip( const Frustum& f )
+{
+ bool inView = true;
+ for (uint8_t i = 0; (i < 6) && inView; ++i)
+ {
+ inView = Clip( f.GetPlane(i) )?false:inView;
+ }
+
+ return !inView;
+}
+
+
void BoundingBox::Render()
{
// BoundingBox gbb = *this;
Modified: Mercury2/src/BoundingBox.h
===================================================================
--- Mercury2/src/BoundingBox.h 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/BoundingBox.h 2009-04-12 14:18:33 UTC (rev 204)
@@ -4,7 +4,9 @@
#include <MercuryNode.h>
#include <MercuryVertex.h>
#include <MercuryMatrix.h>
+#include <Frustum.h>
#include <stdint.h>
+#include <MercuryPlane.h>
class BoundingVolume
{
@@ -15,6 +17,9 @@
virtual void Transform( const MercuryMatrix& m ) = 0;
virtual void Render() {};
virtual BoundingVolume* SpawnClone() const = 0;
+
+ virtual bool Clip( const MercuryPlane& p ) = 0;
+ virtual bool Clip( const Frustum& f ) = 0;
};
class BoundingBox : public BoundingVolume
@@ -37,6 +42,9 @@
virtual void Render();
virtual BoundingVolume* SpawnClone() const;
+ virtual bool Clip( const MercuryPlane& p );
+ virtual bool Clip( const Frustum& f );
+
private:
void ComputeNormals();
@@ -45,17 +53,7 @@
MercuryVector m_normals[3];
};
-/*
-class RenderableBoundingBox : public MercuryAsset
-{
- public:
- RenderableBoundingBox(const BoundingBox* bb);
- virtual void Render(MercuryNode* node);
- private:
- const BoundingBox* m_bb;
-};
-*/
#endif
/****************************************************************************
Modified: Mercury2/src/Mercury2.cpp
===================================================================
--- Mercury2/src/Mercury2.cpp 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/Mercury2.cpp 2009-04-12 14:18:33 UTC (rev 204)
@@ -70,6 +70,7 @@
renderGraph.Build(root);
}
+
renderGraph.Render();
// RenderableNode::RecursiveRender(root);
w->SwapBuffers();
Modified: Mercury2/src/MercuryPlane.cpp
===================================================================
--- Mercury2/src/MercuryPlane.cpp 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/MercuryPlane.cpp 2009-04-12 14:18:33 UTC (rev 204)
@@ -12,27 +12,6 @@
return BEHIND_PLANE( SIGNED_DIST( point+m_center ) );
}
-bool MercuryPlane::IsBehindPlane(const BoundingBox& bb) const
-{
- const MercuryVertex& center = bb.GetCenter();
- const MercuryVertex& extends = bb.GetExtend();
-
- const MercuryVertex &A1(bb.Normal(0)), &A2(bb.Normal(1)), &A3(bb.Normal(2));
-
- MercuryVertex dp = m_normal.DotProduct3(A1, A2, A3);
- float x = ABS( extends.GetX() * dp[0] );
- x += ABS( extends.GetY() * dp[1] );
- x += ABS( extends.GetZ() * dp[2] );
-
- float d = SIGNED_DIST( center+m_center );
- if ( ABS(d) <= x ) //intersection
- return false;
-
- return BEHIND_PLANE(d); //if we don't intersect, just see what side we are on
-}
-
-
-
/****************************************************************************
* Copyright (C) 2009 by Joshua Allen *
* *
Modified: Mercury2/src/MercuryPlane.h
===================================================================
--- Mercury2/src/MercuryPlane.h 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/MercuryPlane.h 2009-04-12 14:18:33 UTC (rev 204)
@@ -2,7 +2,7 @@
#define MERCURYPLANE_H
#include <MercuryVertex.h>
-#include <BoundingBox.h>
+//#include <BoundingBox.h>
class MercuryPlane
{
@@ -16,8 +16,10 @@
inline void SetCenter(const MercuryVertex& center) { m_center = center; }
inline void SetNormal(const MercuryVertex& normal) { m_normal = normal.Normalize(); }
+ inline const MercuryVector& GetNormal() const { return m_normal; }
+ inline const MercuryVector& GetCenter() const { return m_center; }
+
bool IsBehindPlane(const MercuryVertex& point) const;
- bool IsBehindPlane(const BoundingBox& bb) const;
private:
MercuryVertex m_center;
MercuryVector m_normal;
Modified: Mercury2/src/RenderGraph.cpp
===================================================================
--- Mercury2/src/RenderGraph.cpp 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/RenderGraph.cpp 2009-04-12 14:18:33 UTC (rev 204)
@@ -6,17 +6,15 @@
void RenderGraphEntry::Render()
{
MercuryMatrix m;
-
+
if (m_node)
{
- if ( !(m_node->IsHidden() || m_node->IsCulled()) )
- {
- m = *m_matrix;
- m.Transpose();
- glLoadMatrixf( m.Ptr() );
- m_node->PreRender( m );
- m_node->Render( m );
- }
+ if ( m_node->IsHidden() || m_node->IsCulled(*m_matrix) ) return;
+ m = *m_matrix;
+ m.Transpose();
+ glLoadMatrixf( m.Ptr() );
+ m_node->PreRender( m );
+ m_node->Render( m );
}
std::list< RenderGraphEntry >::iterator i;
@@ -25,17 +23,14 @@
if (m_node)
{
- if ( !(m_node->IsHidden() || m_node->IsCulled()) )
- {
- glLoadMatrixf( m.Ptr() );
- m_node->PostRender( m );
- }
+ glLoadMatrixf( m.Ptr() );
+ m_node->PostRender( m );
}
}
void RenderGraph::Build( MercuryNode* node )
{
- printf("rebuilding render graph\n");
+// printf("rebuilding render graph\n");
m_root = RenderGraphEntry();
Build(node, m_root);
}
@@ -48,7 +43,6 @@
if ( rn )
{
//found a new renderable
- printf("Found renderable %p\n", rn);
entry.m_children.push_back( RenderGraphEntry(&rn->FindGlobalMatrix(), rn) );
lastEntry = &(entry.m_children.back());
}
Modified: Mercury2/src/RenderableNode.cpp
===================================================================
--- Mercury2/src/RenderableNode.cpp 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/RenderableNode.cpp 2009-04-12 14:18:33 UTC (rev 204)
@@ -3,6 +3,7 @@
#include <GL/gl.h>
#include <TransformNode.h>
#include <unistd.h>
+#include <Viewport.h>
using namespace std;
@@ -22,6 +23,7 @@
void RenderableNode::Update(float dTime)
{
+ /*
MercuryMatrix m = FindGlobalMatrix();
std::list< MAutoPtr< MercuryAsset > >::iterator i;
@@ -36,6 +38,7 @@
//do some stuff to figure out if this node is culled
}
}
+ */
}
void RenderableNode::PreRender(const MercuryMatrix& matrix)
@@ -130,6 +133,28 @@
MercuryNode::LoadFromXML( node );
}
+bool RenderableNode::IsCulled(const MercuryMatrix& matrix)
+{
+ bool clip = false;
+
+ std::list< MAutoPtr< MercuryAsset > >::iterator i;
+ for (i = m_assets.begin(); i != m_assets.end(); ++i )
+ {
+ const BoundingVolume* bv = (*i)->GetBoundingVolume();
+ if (bv)
+ {
+ BoundingVolume* v = bv->SpawnClone();
+ v->Transform( matrix );
+ clip = v->Clip( *FRUSTUM );
+ delete v;
+ if ( clip == false ) return false;
+ }
+ else
+ return false;
+ }
+ return clip;
+}
+
/***************************************************************************
* Copyright (C) 2008 by Joshua Allen *
* *
Modified: Mercury2/src/RenderableNode.h
===================================================================
--- Mercury2/src/RenderableNode.h 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/RenderableNode.h 2009-04-12 14:18:33 UTC (rev 204)
@@ -32,7 +32,7 @@
const MercuryMatrix& FindGlobalMatrix() const;
- virtual bool IsCulled() { return false; }
+ virtual bool IsCulled(const MercuryMatrix& matrix);
bool IsHidden() { return m_hidden; }
GENRTTI(RenderableNode);
Modified: Mercury2/src/Viewport.cpp
===================================================================
--- Mercury2/src/Viewport.cpp 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/Viewport.cpp 2009-04-12 14:18:33 UTC (rev 204)
@@ -2,9 +2,12 @@
#include <GL/gl.h>
REGISTER_NODE_TYPE(Viewport);
+const Frustum* FRUSTUM;
void Viewport::Render(const MercuryMatrix& matrix)
{
+ FRUSTUM = &m_frustum;
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
@@ -31,119 +34,7 @@
RenderableNode::LoadFromXML(node);
}
-void Frustum::SetPerspective( float fov, float aspect, float znear, float zfar )
-{
- float xmin, xmax, ymin, ymax;
-
- m_fov = fov;
- m_aspect = aspect;
- m_zNear = znear;
- m_zFar = zfar;
- float tang = TAN(m_fov * Q_PI / 360.0);
-
- m_nh = ymax = m_zNear * tang; //nh
- ymin = -ymax;
- xmin = ymin * m_aspect;
- m_nw = xmax = ymax * m_aspect; //nw
-
- m_fh = m_zFar*tang;
- m_fw = m_fh*aspect;
-
- ComputeFrustum(xmin, xmax, ymin, ymax, m_zNear, m_zFar);
-}
-
-void Frustum::ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar)
-{
- float near2 = 2*zNear;
- float rml = right-left;
- float tmb = top - bottom;
- float fmn = zFar - zNear;
-
- float A = (right+left)/rml;
- float B = (top+bottom)/tmb;
- float C = -(zFar+zNear)/fmn;
- float D = -(near2*zFar)/fmn;
-
- m_frustum.Zero();
-
- //row major
- m_frustum[0][0] = near2/rml;
- m_frustum[0][2] = A;
- m_frustum[1][1] = near2/tmb;
- m_frustum[1][2] = B;
- m_frustum[2][2] = C;
- m_frustum[2][3] = D;
- m_frustum[3][2] = -1;
-
-// m_frustum.Transpose(); //XXX fix it to remove this
-}
-
-void Frustum::LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up)
-{
- //Right now this only builds the frustum planes
- MercuryVector X,Y,Z;
-
- Z = (eye - look).Normalize(); //direction behind camera
- X = (up.CrossProduct(Z)).Normalize(); //X axis
- Y = Z.CrossProduct( X ); //real up
-
- m_nc = (eye - Z) * m_zNear;
- m_fc = (eye - Z) * m_zFar;
-
- m_planes[PNEAR].Setup(m_nc, Z*(-1));
- m_planes[PFAR].Setup(m_fc, Z);
-// m_fc.Print();
-// Z.Print();
- MercuryVector aux,normal;
-
- aux = (m_nc + Y*m_nh) - eye;
- aux.NormalizeSelf();
- normal = aux * X;
- m_planes[PTOP].Setup(m_nc+Y*m_nh,normal);
-
- aux = (m_nc - Y*m_nh) - eye;
- aux.NormalizeSelf();
- normal = X * aux;
- m_planes[PBOTTOM].Setup(m_nc-Y*m_nh,normal);
-
- aux = (m_nc - X*m_nw) - eye;
- aux.NormalizeSelf();
- normal = aux * Y;
- m_planes[PLEFT].Setup(m_nc-X*m_nw,normal);
-
- aux = (m_nc + X*m_nw) - eye;
- aux.NormalizeSelf();
- normal = Y * aux;
- m_planes[PRIGHT].Setup(m_nc+X*m_nw,normal);
-}
-
-bool Frustum::Clip(const BoundingBox& bb) const
-{
- bool inView = true;
- for (uint8_t i = 0; (i < 6) && inView; ++i)
- {
- inView = m_planes[i].IsBehindPlane( bb )?false:inView;
- }
-
- return !inView;
-};
-
-/*
-void Frustum::LookAt()
-{
-
-}
-*/
-/*
-bool Frustum::IsPointInFrustum( float x, float y, float z )
-{
- for( uint16_t i = 0; i < 6; ++i )
- if( frustum[i][0] * x + frustum[i][1] * y + frustum[i][2] * z + frustum[i][3] <= 0 )
- return false;
- return true;
-}*/
-
/***************************************************************************
* Copyright (C) 2008 by Joshua Allen *
* *
Modified: Mercury2/src/Viewport.h
===================================================================
--- Mercury2/src/Viewport.h 2009-04-05 23:21:37 UTC (rev 203)
+++ Mercury2/src/Viewport.h 2009-04-12 14:18:33 UTC (rev 204)
@@ -5,36 +5,8 @@
#include <MercuryMatrix.h>
#include <MercuryVertex.h>
#include <MercuryPlane.h>
+#include <Frustum.h>
-enum PlanePos
-{
- PTOP = 0,
- PBOTTOM,
- PLEFT,
- PRIGHT,
- PNEAR,
- PFAR
-};
-
-class Frustum
-{
-
- public:
- void SetPerspective( float fov, float aspect, float znear, float zfar );
- const MercuryMatrix& GetMatrix() const { return m_frustum; }
- void ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar);
- void LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up);
- bool Clip(const BoundingBox& bb) const;
- private:
- MercuryPlane m_planes[6];
- MercuryMatrix m_frustum;
-
- float m_aspect, m_fov, m_zNear, m_zFar;
- float m_nh, m_nw, m_fh, m_fw;
-
- MercuryVector m_nc, m_fc;
-};
-
extern const Frustum* FRUSTUM;
class Viewport : public RenderableNode
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|