From: <axl...@us...> - 2009-03-02 01:59:33
|
Revision: 157 http://hgengine.svn.sourceforge.net/hgengine/?rev=157&view=rev Author: axlecrusher Date: 2009-03-02 01:59:30 +0000 (Mon, 02 Mar 2009) Log Message: ----------- fixing up frustum, preparing for culling Modified Paths: -------------- Mercury2/beerhall.hgmdl Mercury2/docs/MercuryBinaryModelFormat.odt Mercury2/mercury2.kdevelop Mercury2/src/HGMDLModel.cpp Mercury2/src/MercuryUtil.cpp Mercury2/src/MercuryUtil.h Mercury2/src/Viewport.cpp Mercury2/src/Viewport.h Mercury2/tools/obj2hgmdl/obj2hgmdl.cpp Added Paths: ----------- Mercury2/src/MercuryVertex.cpp Mercury2/src/MercuryVertex.h Modified: Mercury2/beerhall.hgmdl =================================================================== (Binary files differ) Modified: Mercury2/docs/MercuryBinaryModelFormat.odt =================================================================== (Binary files differ) Modified: Mercury2/mercury2.kdevelop =================================================================== --- Mercury2/mercury2.kdevelop 2009-03-01 21:10:09 UTC (rev 156) +++ Mercury2/mercury2.kdevelop 2009-03-02 01:59:30 UTC (rev 157) @@ -2,7 +2,7 @@ <kdevelop> <general> <author>Joshua Allen</author> - <email/> + <email></email> <version>2.0</version> <projectmanagement>KDevAutoProject</projectmanagement> <primarylanguage>C++</primarylanguage> @@ -14,8 +14,8 @@ <projectname>Mercury2</projectname> <projectdirectory>.</projectdirectory> <absoluteprojectpath>false</absoluteprojectpath> - <description/> - <defaultencoding/> + <description></description> + <defaultencoding></defaultencoding> <versioncontrol/> </general> <kdevautoproject> @@ -222,7 +222,7 @@ <includePaths>.;</includePaths> </codecompletion> <creategettersetter> - <prefixGet/> + <prefixGet></prefixGet> <prefixSet>set</prefixSet> <prefixVariable>m_,_</prefixVariable> <parameterName>theValue</parameterName> @@ -243,11 +243,11 @@ </cppsupportpart> <kdevdebugger> <general> - <gdbpath/> + <gdbpath></gdbpath> <dbgshell>libtool</dbgshell> - <configGdbScript/> - <runShellScript/> - <runGdbScript/> + <configGdbScript></configGdbScript> + <runShellScript></runShellScript> + <runGdbScript></runGdbScript> <breakonloadinglibs>true</breakonloadinglibs> <separatetty>false</separatetty> <floatingtoolbar>false</floatingtoolbar> Modified: Mercury2/src/HGMDLModel.cpp =================================================================== --- Mercury2/src/HGMDLModel.cpp 2009-03-01 21:10:09 UTC (rev 156) +++ Mercury2/src/HGMDLModel.cpp 2009-03-02 01:59:30 UTC (rev 157) @@ -2,6 +2,9 @@ REGISTER_ASSET_TYPE(HGMDLModel); +const uint16_t EXPCTMJRV = 2; +const uint16_t EXPCTMNRV = 3; + HGMDLModel::HGMDLModel() :MercuryAsset() { @@ -32,13 +35,15 @@ return; } - uint32_t version; - //fread(&version, 4, 1, hgmdl); - hgmdl->Read( &version, 4 ); + uint16_t majorVersion; + uint16_t minorVersion; - if (version != 200) + hgmdl->Read( &majorVersion, 2 ); + hgmdl->Read( &minorVersion, 2 ); + + if ((majorVersion != EXPCTMJRV) || (minorVersion != EXPCTMNRV)) { - printf("Invalid HGMDL version %d\n", version); + printf("Can only read HGMDL version %d.%d, this file is %d.%d\n", EXPCTMJRV,EXPCTMNRV,majorVersion,minorVersion); return; } Modified: Mercury2/src/MercuryUtil.cpp =================================================================== --- Mercury2/src/MercuryUtil.cpp 2009-03-01 21:10:09 UTC (rev 156) +++ Mercury2/src/MercuryUtil.cpp 2009-03-02 01:59:30 UTC (rev 157) @@ -66,7 +66,6 @@ return length; } - /*************************************************************************** * Copyright (C) 2008 by Joshua Allen * * * Modified: Mercury2/src/MercuryUtil.h =================================================================== --- Mercury2/src/MercuryUtil.h 2009-03-01 21:10:09 UTC (rev 156) +++ Mercury2/src/MercuryUtil.h 2009-03-02 01:59:30 UTC (rev 157) @@ -31,6 +31,18 @@ float StrToFloat(const MString & s); +template<typename T> +const T& max(const T& t1, const T& t2) +{ + return t1>t2?t1:t2; +} + +template<typename T> +const T& min(const T& t1, const T& t2) +{ + return t1<t2?t1:t2; +} + //This counter is used with singletons to //ensure proper destruction order of the //singleton Added: Mercury2/src/MercuryVertex.cpp =================================================================== --- Mercury2/src/MercuryVertex.cpp (rev 0) +++ Mercury2/src/MercuryVertex.cpp 2009-03-02 01:59:30 UTC (rev 157) @@ -0,0 +1,120 @@ +#include <MercuryVertex.h> +#include <MercuryUtil.h> +#include <MercuryMath.h> + +MercuryVertex::MercuryVertex() +{ + m_xyz[0] = m_xyz[1] = m_xyz[2] = 0; +} + +MercuryVertex::MercuryVertex( float ix, float iy, float iz ) +{ + m_xyz[0] = ix; + m_xyz[1] = iy; + m_xyz[2] = iz; +} + +MercuryVertex::MercuryVertex( const float * in ) +{ + for (unsigned int i = 0; i < 3; ++i) + m_xyz[i] *= in[i]; +} + +void MercuryVertex::NormalizeSelf() +{ + float imag = 1.0f/Length(); + for (unsigned int i = 0; i < 3; ++i) + m_xyz[i] *= imag; +} + +MercuryVertex MercuryVertex::Normalize() const +{ + MercuryVertex r(*this); + r.NormalizeSelf(); + return r; +} + +float MercuryVertex::Length() const +{ + float length = m_xyz[0]*m_xyz[0]; + length += m_xyz[1]*m_xyz[1]; + length += m_xyz[2]*m_xyz[2]; + return SQRT(length); +} + +float MercuryVertex::GetBiggestElement() const +{ + float tmp = m_xyz[0]; + tmp = max<float>(tmp, m_xyz[1]); + return max<float>(tmp, m_xyz[2]); +} + +const MercuryVertex& MercuryVertex::operator *= (const MercuryVertex& p) +{ + for (unsigned int i = 0; i < 3; ++i) + m_xyz[i] *= p.m_xyz[i]; + return *this; +} + +const MercuryVertex& MercuryVertex::operator /= (const MercuryVertex& p) +{ + for (unsigned int i = 0; i < 3; ++i) + m_xyz[i] /= p.m_xyz[i]; + return *this; +} + +bool MercuryVertex::operator==(const MercuryVertex& p) const +{ + for (unsigned int i = 0; i < 3; ++i) + if (m_xyz[i] != p.m_xyz[i]) return false; + return true; +} + +bool MercuryVertex::operator==(const float f) const +{ + for (unsigned int i = 0; i < 3; ++i) + if (m_xyz[i] != f) return false; + return true; +} + +MercuryVertex MercuryVertex::CrossProduct(const MercuryVertex& p) const +{ + MercuryVertex ret; + ret.m_xyz[0] = m_xyz[1]*p.m_xyz[2] - m_xyz[2]*p.m_xyz[1]; + ret.m_xyz[1] = m_xyz[2]*p.m_xyz[0] - m_xyz[0]*p.m_xyz[2]; + ret.m_xyz[2] = m_xyz[0]*p.m_xyz[1] - m_xyz[1]*p.m_xyz[0]; + return ret; +} + + +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ Added: Mercury2/src/MercuryVertex.h =================================================================== --- Mercury2/src/MercuryVertex.h (rev 0) +++ Mercury2/src/MercuryVertex.h 2009-03-02 01:59:30 UTC (rev 157) @@ -0,0 +1,101 @@ +#ifndef MERCURYVECTOR_H +#define MERCURYVECTOR_H + +class MercuryVertex +{ + public: + MercuryVertex(); + MercuryVertex( float ix, float iy, float iz ); + MercuryVertex( const float * in ); + + ///Direct conversion to float* + operator float* () { return m_xyz; } + ///Direct conversion to const float* + operator const float* () const { return m_xyz; } + + inline const float GetX() const { return m_xyz[0]; } + inline const float GetY() const { return m_xyz[1]; } + inline const float GetZ() const { return m_xyz[2]; } + inline void SetX(const float ix) { m_xyz[0] = ix; } + inline void SetY(const float iy) { m_xyz[1] = iy; } + inline void SetZ(const float iz) { m_xyz[2] = iz; } + + inline void Zero() { m_xyz[0] = 0; m_xyz[1] = 0; m_xyz[2] = 0; } + + ///Normalize (make |point| = 1) + void NormalizeSelf(); + ///Return a normalized point + MercuryVertex Normalize() const; + ///Return the length of |this| + float Length() const; + + float GetBiggestElement() const; + + ///Write out to be = to this point + inline void ConvertToVector3( float* out ) const { out[0] = m_xyz[0]; out[1] = m_xyz[1]; out[2] = m_xyz[2]; } + ///Write out to be = to this point, however the 4th element will be 0 + inline void ConvertToVector4( float* out ) const { out[0] = m_xyz[0]; out[1] = m_xyz[1]; out[2] = m_xyz[2]; out[3] = 0; } + ///Write out to be = - to this point, however the 4th element will be 0 + inline void ConvertToIVector4( float* out ) const { out[0] = -m_xyz[0]; out[1] = -m_xyz[1]; out[2] = -m_xyz[2]; out[3] = 0; } + + const MercuryVertex& operator *= (const MercuryVertex& p); + const MercuryVertex& operator /= (const MercuryVertex& p); + inline MercuryVertex operator * (const MercuryVertex& p) const { MercuryVertex r(*this); r*=p; return r; } + inline MercuryVertex operator / (const MercuryVertex& p) const { MercuryVertex r(*this); r/=p; return r; } + + inline MercuryVertex& operator += ( const MercuryVertex& other ) { m_xyz[0]+=other.m_xyz[0]; m_xyz[1]+=other.m_xyz[1]; m_xyz[2]+=other.m_xyz[2]; return *this; } + inline MercuryVertex& operator -= ( const MercuryVertex& other ) { m_xyz[0]-=other.m_xyz[0]; m_xyz[1]-=other.m_xyz[1]; m_xyz[2]-=other.m_xyz[2]; return *this; } + inline MercuryVertex& operator *= ( float f ) { m_xyz[0]*=f; m_xyz[1]*=f; m_xyz[2]*=f; return *this; } + inline MercuryVertex& operator /= ( float f ) { m_xyz[0]/=f; m_xyz[1]/=f; m_xyz[2]/=f; return *this; } + + inline MercuryVertex operator + ( const MercuryVertex& other ) const { return MercuryVertex( m_xyz[0]+other.m_xyz[0], m_xyz[1]+other.m_xyz[1], m_xyz[2]+other.m_xyz[2] ); } + inline MercuryVertex operator - ( const MercuryVertex& other ) const { return MercuryVertex( m_xyz[0]-other.m_xyz[0], m_xyz[1]-other.m_xyz[1], m_xyz[2]-other.m_xyz[2] ); } + inline MercuryVertex operator * ( float f ) const { return MercuryVertex( m_xyz[0]*f, m_xyz[1]*f, m_xyz[2]*f ); } + inline MercuryVertex operator / ( float f ) const { return MercuryVertex( m_xyz[0]/f, m_xyz[1]/f, m_xyz[2]/f ); } + + bool operator==(const MercuryVertex& p) const; + inline bool operator!=(const MercuryVertex& p) const { return !(*this == p); } + + bool operator==(const float f) const; + inline bool operator!=(const float f) const { return !(*this == f); } + + ///Obtain the cross product (*this) x p + MercuryVertex CrossProduct(const MercuryVertex& p) const; + + float m_xyz[3]; +}; + +typedef MercuryVertex MercuryVector; + +#endif +/**************************************************************************** + * Copyright (C) 2009 by Joshua Allen * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ Modified: Mercury2/src/Viewport.cpp =================================================================== --- Mercury2/src/Viewport.cpp 2009-03-01 21:10:09 UTC (rev 156) +++ Mercury2/src/Viewport.cpp 2009-03-02 01:59:30 UTC (rev 157) @@ -11,27 +11,49 @@ MercuryMatrix m = FindGlobalMatrix(); m.Transpose(); - glLoadMatrixf( (m * m_frustum).Ptr() ); + glLoadMatrixf( (m * m_frustum.GetMatrix()).Ptr() ); //The following 2 are equivelent to above // glLoadMatrixf( m_frustum.Ptr() ); // glMultMatrixf( m.Ptr() ); - + glMatrixMode(GL_MODELVIEW); } -void Viewport::Perspective( float fov, float aspect, float znear, float zfar ) +void Viewport::LoadFromXML(const XMLNode& node) { + m_frustum.SetPerspective( StrToFloat(node.Attribute("fov")), + StrToFloat(node.Attribute("aspect")), + StrToFloat(node.Attribute("near")), + StrToFloat(node.Attribute("far"))); + + m_frustum.LookAt(MercuryVertex(), MercuryVertex(0,0,1), MercuryVertex(0,1,0)); + + RenderableNode::LoadFromXML(node); +} + +void Frustum::SetPerspective( float fov, float aspect, float znear, float zfar ) +{ float xmin, xmax, ymin, ymax; - ymax = znear * TAN(fov * Q_PI / 360.0); + 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 * aspect; - xmax = ymax * aspect; + 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, znear, zfar); + ComputeFrustum(xmin, xmax, ymin, ymax, m_zNear, m_zFar); } -void Viewport::ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar) +void Frustum::ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar) { float near2 = 2*zNear; float rml = right-left; @@ -57,16 +79,59 @@ m_frustum.Transpose(); //XXX fix it to remove this } -void Viewport::LoadFromXML(const XMLNode& node) +void Frustum::LookAt(const MercuryVertex& eye, const MercuryVector& look, const MercuryVector& up) { - Perspective( StrToFloat(node.Attribute("fov")), - StrToFloat(node.Attribute("aspect")), - StrToFloat(node.Attribute("near")), - StrToFloat(node.Attribute("far"))); + //Right now this only builds the frustum planes + MercuryVector X,Y,Z; - RenderableNode::LoadFromXML(node); + Z = (eye - look).Normalize(); //direction behind camera + X = (up * Z).Normalize(); //X axis + Y = Z.CrossProduct( X ); //real up + + m_nc = up - Z * m_zNear; + m_fc = up - Z * m_zFar; + + m_planes[PNEAR].Setup(m_nc, Z*-1); + m_planes[PFAR].Setup(m_fc, Z); + + 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); } +/* +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-03-01 21:10:09 UTC (rev 156) +++ Mercury2/src/Viewport.h 2009-03-02 01:59:30 UTC (rev 157) @@ -3,18 +3,47 @@ #include <RenderableNode.h> #include <MercuryMatrix.h> +#include <MercuryVertex.h> +#include <MercuryPlane.h> + +class Frustum +{ + enum PlanePos + { + PTOP = 0, + PBOTTOM, + PLEFT, + PRIGHT, + PNEAR, + PFAR + }; + + 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); + private: + 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; + MercuryPlane m_planes[6]; +}; + class Viewport : public RenderableNode { public: virtual void Render(); - void Perspective( float fov, float aspect, float znear, float zfar ); virtual void LoadFromXML(const XMLNode& node); GENRTTI(Viewport); private: - void ComputeFrustum(float left, float right, float bottom, float top, float zNear, float zFar); - MercuryMatrix m_frustum; + Frustum m_frustum; }; #endif Modified: Mercury2/tools/obj2hgmdl/obj2hgmdl.cpp =================================================================== --- Mercury2/tools/obj2hgmdl/obj2hgmdl.cpp 2009-03-01 21:10:09 UTC (rev 156) +++ Mercury2/tools/obj2hgmdl/obj2hgmdl.cpp 2009-03-02 01:59:30 UTC (rev 157) @@ -136,7 +136,8 @@ bool b; fwrite("MBMF", 4, 1, mbmf); - tmp32 = 200; fwrite(&tmp32, sizeof(uint32_t), 1, mbmf); + tmp16 = 2; fwrite(&tmp16, sizeof(uint16_t), 1, mbmf); + tmp16 = 3; fwrite(&tmp16, sizeof(uint16_t), 1, mbmf); tmp16 = 1; fwrite(&tmp16, sizeof(uint16_t), 1, mbmf); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |