|
From: <axl...@us...> - 2009-03-15 20:38:42
|
Revision: 178
http://hgengine.svn.sourceforge.net/hgengine/?rev=178&view=rev
Author: axlecrusher
Date: 2009-03-15 20:38:32 +0000 (Sun, 15 Mar 2009)
Log Message:
-----------
lots of updates to math and vertex
Modified Paths:
--------------
Mercury2/src/BoundingBox.cpp
Mercury2/src/BoundingBox.h
Mercury2/src/MercuryMath.h
Mercury2/src/MercuryMatrix.cpp
Mercury2/src/MercuryPlane.cpp
Mercury2/src/MercuryVertex.cpp
Mercury2/src/MercuryVertex.h
Mercury2/src/Viewport.h
Modified: Mercury2/src/BoundingBox.cpp
===================================================================
--- Mercury2/src/BoundingBox.cpp 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/BoundingBox.cpp 2009-03-15 20:38:32 UTC (rev 178)
@@ -32,7 +32,7 @@
m_normals[2] = (m_center - t).Normalize();
}
-BoundingBox BoundingBox::Transform( const MercuryMatrix& m ) const
+void BoundingBox::Transform( const MercuryMatrix& m )
{
BoundingBox bb;
bb.m_extend = m_center;
@@ -40,7 +40,8 @@
bb.m_normals[0] = (m * m_normals[0]).Normalize();
bb.m_normals[1] = (m * m_normals[1]).Normalize();
bb.m_normals[2] = (m * m_normals[2]).Normalize();
- return bb;
+ *this = bb;
+// return bb;
}
RenderableBoundingBox::RenderableBoundingBox(const BoundingBox* bb)
@@ -50,7 +51,8 @@
void RenderableBoundingBox::Render(MercuryNode* node)
{
- BoundingBox gbb = m_bb->Transform( GetGlobalMatrix() );
+ BoundingBox gbb = *m_bb;
+ gbb.Transform( GetGlobalMatrix() );
if ( FRUSTUM->Clip( gbb ) ) return;
const float* center = m_bb->GetCenter();
Modified: Mercury2/src/BoundingBox.h
===================================================================
--- Mercury2/src/BoundingBox.h 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/BoundingBox.h 2009-03-15 20:38:32 UTC (rev 178)
@@ -6,18 +6,25 @@
#include <MercuryMatrix.h>
#include <stdint.h>
-class BoundingBox
+class BoundingVolume
{
public:
+ virtual void LoadFromBinary(char* data) = 0;
+ virtual void Transform( const MercuryMatrix& m ) = 0;
+};
+
+class BoundingBox : public BoundingVolume
+{
+ public:
BoundingBox() {};
BoundingBox(const MercuryVertex& center, const MercuryVertex& extend);
- void LoadFromBinary(char* data);
+ virtual void LoadFromBinary(char* data);
inline const MercuryVertex& GetCenter() const { return m_center; }
inline const MercuryVertex& GetExtend() const { return m_extend; }
- BoundingBox Transform( const MercuryMatrix& m ) const;
+ virtual void Transform( const MercuryMatrix& m );
const MercuryVector& Normal(uint8_t i) const { return m_normals[i]; }
private:
Modified: Mercury2/src/MercuryMath.h
===================================================================
--- Mercury2/src/MercuryMath.h 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/MercuryMath.h 2009-03-15 20:38:32 UTC (rev 178)
@@ -55,9 +55,23 @@
void VectorMultiply4f(const FloatRow* matrix, const FloatRow& p, FloatRow& out );
void TransposeMatrix( FloatRow* m );
-void Float2FloatRow(const float* f, FloatRow& r);
-void FloatRow2Float(const FloatRow& fr, float* f);
+//void Float2FloatRow(const float* f, FloatRow& r);
+//void FloatRow2Float(const FloatRow& fr, float* f);
+inline void MMCrossProduct( const FloatRow& r1, const FloatRow& r2, FloatRow& result)
+{
+ __m128 a,b,c,d,r;//using more registers is faster
+
+ a = _mm_shuffle_ps(r1, r1, 0xc9);
+ b = _mm_shuffle_ps(r2, r2, 0xd2);
+ r = _mm_mul_ps( a, b );
+
+ c = _mm_shuffle_ps(r2, r2, 0xc9);
+ d = _mm_shuffle_ps(r1, r1, 0xd2);
+ r -= _mm_mul_ps( c, d );
+ result = r;
+}
+
const FloatRow gfrZero = { 0.f, 0.f, 0.f, 0.f };
#endif
Modified: Mercury2/src/MercuryMatrix.cpp
===================================================================
--- Mercury2/src/MercuryMatrix.cpp 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/MercuryMatrix.cpp 2009-03-15 20:38:32 UTC (rev 178)
@@ -197,15 +197,9 @@
MercuryVector MercuryMatrix::operator*(const MercuryVector& v) const
{
- float tmp[4];
- FloatRow r, tvo;
-
- v.ConvertToVector4( tmp, 1 );
- Float2FloatRow( tmp, r );
- VectorMultiply4f( m_matrix, r, tvo);
- FloatRow2Float( tvo, tmp );
-// printf("%f %f %f %f\n", tmp[0], tmp[1], tmp[2], tmp[3]);
- return MercuryVertex(tmp);
+ MercuryVector r;
+ VectorMultiply4f( m_matrix, v.ToFloatRow(), r.ToFloatRow() );
+ return r;
}
Modified: Mercury2/src/MercuryPlane.cpp
===================================================================
--- Mercury2/src/MercuryPlane.cpp 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/MercuryPlane.cpp 2009-03-15 20:38:32 UTC (rev 178)
@@ -2,10 +2,14 @@
#include <stdio.h>
#include <MercuryMath.h>
+#define SIGNED_DIST(x) m_normal.DotProduct(x)
+
+// origional algorithim was -x<0
+#define BEHIND_PLANE(x) x>=0
+
bool MercuryPlane::IsBehindPlane(const MercuryVertex& point) const
{
- // origional algorithim was -dotproduct < 0
- return m_normal.DotProduct( point+m_center ) >= 0;
+ return BEHIND_PLANE( SIGNED_DIST( point+m_center ) );
}
bool MercuryPlane::IsBehindPlane(const BoundingBox& bb) const
@@ -14,16 +18,17 @@
const MercuryVertex& extends = bb.GetExtend();
const MercuryVertex &A1(bb.Normal(0)), &A2(bb.Normal(1)), &A3(bb.Normal(2));
-
- float x = ABS( extends.GetX() * m_normal.DotProduct( A1 ) );
- x += ABS( extends.GetY() * m_normal.DotProduct( A2 ) );
- x += ABS( extends.GetZ() * m_normal.DotProduct( A3 ) );
- float d = m_normal.DotProduct( center+m_center );
+ 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 IsBehindPlane( center ); //if we don't intersect, just see what side we are on
+
+ return BEHIND_PLANE(d); //if we don't intersect, just see what side we are on
}
Modified: Mercury2/src/MercuryVertex.cpp
===================================================================
--- Mercury2/src/MercuryVertex.cpp 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/MercuryVertex.cpp 2009-03-15 20:38:32 UTC (rev 178)
@@ -4,33 +4,33 @@
MercuryVertex::MercuryVertex()
{
- m_xyz[0] = m_xyz[1] = m_xyz[2] = 0;
+ (*this)[0] = (*this)[1] = (*this)[2] = 0;
}
MercuryVertex::MercuryVertex( float ix, float iy, float iz )
{
- m_xyz[0] = ix;
- m_xyz[1] = iy;
- m_xyz[2] = iz;
+ (*this)[0] = ix;
+ (*this)[1] = iy;
+ (*this)[2] = iz;
}
MercuryVertex::MercuryVertex( const float * in )
{
for (unsigned int i = 0; i < 3; ++i)
- m_xyz[i] = in[i];
+ (*this)[i] = in[i];
}
MercuryVertex::MercuryVertex( const MercuryVertex& v)
{
for (unsigned int i = 0; i < 3; ++i)
- m_xyz[i] = v.m_xyz[i];
+ (*this)[i] = v[i];
}
void MercuryVertex::NormalizeSelf()
{
float imag = 1.0f/Length();
for (unsigned int i = 0; i < 3; ++i)
- m_xyz[i] *= imag;
+ (*this)[i] *= imag;
}
MercuryVertex MercuryVertex::Normalize() const
@@ -42,64 +42,81 @@
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];
+ float length = (*this)[0]*(*this)[0];
+ length += (*this)[1]*(*this)[1];
+ length += (*this)[2]*(*this)[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]);
+ float tmp = (*this)[0];
+ tmp = max<float>(tmp, (*this)[1]);
+ return max<float>(tmp, (*this)[2]);
}
const MercuryVertex& MercuryVertex::operator *= (const MercuryVertex& p)
{
for (unsigned int i = 0; i < 3; ++i)
- m_xyz[i] *= p.m_xyz[i];
+ (*this)[i] *= p[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];
+ (*this)[i] /= p[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;
+ if ((*this)[i] != p[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;
+ if ((*this)[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];
+ ret[0] = (*this)[1]*p[2] - (*this)[2]*p[1];
+ ret[1] = (*this)[2]*p[0] - (*this)[0]*p[2];
+ ret[2] = (*this)[0]*p[1] - (*this)[1]*p[0];
return ret;
}
+MercuryVertex MercuryVertex::CrossProductSSE(const MercuryVertex& p) const
+{
+ //right now this is a hare slower than the C cross product above
+ MercuryVertex r;
+ MMCrossProduct( m_xyzw, p.m_xyzw, r.m_xyzw );
+ return r;
+}
+
float MercuryVertex::DotProduct(const MercuryVertex& rhs) const
{
- return (m_xyz[0]*rhs.m_xyz[0]+m_xyz[1]*rhs.m_xyz[1]+m_xyz[2]*rhs.m_xyz[2]);
+ return ((*this)[0]*rhs[0]+(*this)[1]*rhs[1]+(*this)[2]*rhs[2]);
}
+MercuryVertex MercuryVertex::DotProduct3(const MercuryVertex& rhs1, const MercuryVertex& rhs2, const MercuryVertex& rhs3) const
+{
+ MercuryVertex dp;
+ dp[0] = DotProduct(rhs1);
+ dp[1] = DotProduct(rhs2);
+ dp[2] = DotProduct(rhs3);
+ return dp;
+}
+
void MercuryVertex::Print() const
{
- printf("%f %f %f\n", m_xyz[0], m_xyz[1], m_xyz[2]);
+ printf("%f %f %f\n", (*this)[0], (*this)[1], (*this)[2]);
}
Modified: Mercury2/src/MercuryVertex.h
===================================================================
--- Mercury2/src/MercuryVertex.h 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/MercuryVertex.h 2009-03-15 20:38:32 UTC (rev 178)
@@ -1,6 +1,10 @@
#ifndef MERCURYVECTOR_H
#define MERCURYVECTOR_H
+#include <MercuryMath.h>
+
+# define __inline__ __inline__ __attribute__((always_inline))
+
class MercuryVertex
{
public:
@@ -10,18 +14,21 @@
MercuryVertex( const MercuryVertex& v);
///Direct conversion to float*
- operator float* () { return m_xyz; }
+ __inline__ operator float* () { return (float*)&m_xyzw; }
///Direct conversion to const float*
- operator const float* () const { return m_xyz; }
+ __inline__ operator const float* () const { return (float*)&m_xyzw; }
- 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 FloatRow& ToFloatRow() { return m_xyzw; }
+ inline const FloatRow& ToFloatRow() const { return m_xyzw; }
+
+ inline const float GetX() const { return (*this)[0]; }
+ inline const float GetY() const { return (*this)[1]; }
+ inline const float GetZ() const { return (*this)[2]; }
+ inline void SetX(const float ix) { (*this)[0] = ix; }
+ inline void SetY(const float iy) { (*this)[1] = iy; }
+ inline void SetZ(const float iz) { (*this)[2] = iz; }
- inline void Zero() { m_xyz[0] = 0; m_xyz[1] = 0; m_xyz[2] = 0; }
+ inline void Zero() { (*this)[0] = 0; (*this)[1] = 0; (*this)[2] = 0; }
///Normalize (make |point| = 1)
void NormalizeSelf();
@@ -33,26 +40,26 @@
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]; }
+ inline void ConvertToVector3( float* out ) const { out[0] = (*this)[0]; out[1] = (*this)[1]; out[2] = (*this)[2]; }
///Write out to be = to this point, however the 4th element will be 0
- inline void ConvertToVector4( float* out, float w = 0 ) const { out[0] = m_xyz[0]; out[1] = m_xyz[1]; out[2] = m_xyz[2]; out[3] = w; }
+ inline void ConvertToVector4( float* out, float w = 0 ) const { out[0] = (*this)[0]; out[1] = (*this)[1]; out[2] = (*this)[2]; out[3] = w; }
///Write out to be = - to this point, however the 4th element will be 0
- inline void ConvertToIVector4( float* out, float w = 0 ) const { out[0] = -m_xyz[0]; out[1] = -m_xyz[1]; out[2] = -m_xyz[2]; out[3] = w; }
+ inline void ConvertToIVector4( float* out, float w = 0 ) const { out[0] = -(*this)[0]; out[1] = -(*this)[1]; out[2] = -(*this)[2]; out[3] = w; }
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 ) { (*this)[0]+=other[0]; (*this)[1]+=other[1]; (*this)[2]+=other[2]; return *this; }
+ inline MercuryVertex& operator -= ( const MercuryVertex& other ) { (*this)[0]-=other[0]; (*this)[1]-=other[1]; (*this)[2]-=other[2]; return *this; }
+ inline MercuryVertex& operator *= ( float f ) { (*this)[0]*=f; (*this)[1]*=f; (*this)[2]*=f; return *this; }
+ inline MercuryVertex& operator /= ( float f ) { (*this)[0]/=f; (*this)[1]/=f; (*this)[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 ); }
+ inline MercuryVertex operator + ( const MercuryVertex& other ) const { return MercuryVertex( (*this)[0]+other[0], (*this)[1]+other[1], (*this)[2]+other[2] ); }
+ inline MercuryVertex operator - ( const MercuryVertex& other ) const { return MercuryVertex( (*this)[0]-other[0], (*this)[1]-other[1], (*this)[2]-other[2] ); }
+ inline MercuryVertex operator * ( float f ) const { return MercuryVertex( (*this)[0]*f, (*this)[1]*f, (*this)[2]*f ); }
+ inline MercuryVertex operator / ( float f ) const { return MercuryVertex( (*this)[0]/f, (*this)[1]/f, (*this)[2]/f ); }
bool operator==(const MercuryVertex& p) const;
inline bool operator!=(const MercuryVertex& p) const { return !(*this == p); }
@@ -62,11 +69,15 @@
///Obtain the cross product (*this) x p
MercuryVertex CrossProduct(const MercuryVertex& p) const;
+ MercuryVertex CrossProductSSE(const MercuryVertex& p) const;
float DotProduct(const MercuryVertex& rhs) const;
+ MercuryVertex DotProduct3(const MercuryVertex& rhs1, const MercuryVertex& rhs2, const MercuryVertex& rhs3) const;
+
void Print() const;
- float m_xyz[3];
+// float (*this)[3];
+ FloatRow m_xyzw;
};
typedef MercuryVertex MercuryVector;
Modified: Mercury2/src/Viewport.h
===================================================================
--- Mercury2/src/Viewport.h 2009-03-08 04:19:56 UTC (rev 177)
+++ Mercury2/src/Viewport.h 2009-03-15 20:38:32 UTC (rev 178)
@@ -9,11 +9,11 @@
enum PlanePos
{
PTOP = 0,
- PBOTTOM,
- PLEFT,
- PRIGHT,
- PNEAR,
- PFAR
+ PBOTTOM,
+ PLEFT,
+ PRIGHT,
+ PNEAR,
+ PFAR
};
class Frustum
@@ -23,13 +23,10 @@
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;
-
- MercuryPlane m_planes[6];
private:
+ MercuryPlane m_planes[6];
MercuryMatrix m_frustum;
float m_aspect, m_fov, m_zNear, m_zFar;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|