Permalink
Browse files

Merge branch 'master' of github.com:joesfer/RenderLib

Conflicts:
	include/math/algebra/matrix/matrix4.h
	include/math/algebra/matrix/matrix4.inl
	source/dataStructs/kdtree/kdTree.cpp
  • Loading branch information...
joesfer committed Mar 24, 2012
2 parents 417cee8 + 402eaf6 commit 6de6ebb37a5973dee8beddda8d9938efe0d18286
@@ -37,7 +37,10 @@ class BVH {
BVH( const std::vector<RenderLib::Math::Vector3f>& vertices, const std::vector<int>& indices );
bool intersection(const RenderLib::Raytracing::Ray& r,
const std::vector<RenderLib::Math::Vector3f>& vertices, const std::vector<int>& indices,
- RenderLib::Math::Vector3f& isect) const;
+ RenderLib::Math::Vector3f& hitpoint,
+ int* triangleIndex = NULL,
+ float* barycentricU = NULL,
+ float* barycentricV = NULL ) const;
private:
class BVHNode {
@@ -57,12 +60,13 @@ class BVH {
std::vector<int>& primitives,
const RenderLib::Geometry::BoundingBox& bounds );
+ struct hit_t;
RenderLib::Raytracing::Sphere boundingSphere( const std::vector<RenderLib::Math::Vector3f>& vertices, const std::vector<int>& indices ) const;
bool intersection_r(const RenderLib::Raytracing::Ray& r,
const std::vector<RenderLib::Math::Vector3f>& vertices,
const std::vector<int>& indices,
int node,
- RenderLib::Math::Vector3f& isect ) const;
+ hit_t& hit ) const;
std::vector<BVHNode> nodes;
std::vector<RenderLib::Raytracing::Sphere> volumes;
@@ -162,7 +162,7 @@ bool KdTree::traceClosest( const TraceDesc& trace, const RenderLib::DataStructur
const Point3f& p1 = verts[ indices[ triangleOffset + 1] ].position;
const Point3f& p2 = verts[ indices[ triangleOffset + 2] ].position;
- if ( segmentTriangleIntersect_DoubleSided( ray.origin, ray.direction, tMin, tMax, p0, p1, p2, t, v, w ) ) {
+ if ( segmentTriangleIntersect_DoubleSided( ray.origin, ray.direction, tMin, tMax, p0, p1, p2, t, v, w ) ) {
if ( trace.testOnly ) {
return true;
}
@@ -184,7 +184,7 @@ bool KdTree::traceClosest( const TraceDesc& trace, const RenderLib::DataStructur
const Point3f& p1 = verts[ indices[ triangleOffset + 1] ].position;
const Point3f& p2 = verts[ indices[ triangleOffset + 2] ].position;
- if ( segmentTriangleIntersect_SingleSided( trace.startPoint, trace.endPoint, p0, p1, p2, t, v, w ) ) {
+ if ( segmentTriangleIntersect_SingleSided( trace.startPoint, trace.endPoint, p0, p1, p2, t, v, w ) ) {
if ( trace.testOnly ) {
return true;
} else if ( t < isect.t ) {
@@ -67,9 +67,9 @@ namespace DataStructures {
void medianPartition(PhotonMapSample_t** tree, const int begin, const int end, const int median, const int axis);
void nearestSamples_r(PhotonMapKNN* nearestSamples, SampleIndex_t indexArray ) const;
- ::std::vector<PhotonMapSample_t> sampleMap; // Organized as an array first, and then balanced as a binary tree
+ ::std::vector<PhotonMapSample_t> sampleMap; // Organized as an array first, and then balanced as a binary tree
// where the i-th child is located at 2*i and its sibling at 2*i+1
- ::std::vector<size_t> mapping; // Transforms internal indices back to the original order, so that
+ ::std::vector<SampleIndex_t> mapping; // Transforms internal indices back to the original order, so that
// ::nearestSamples index results match the provided samples array.
RenderLib::Geometry::BoundingBox bbox; // tree bounds
};
@@ -74,7 +74,9 @@ namespace Geometry {
template<typename T>
inline bool segmentTriangleIntersect_SingleSided( const RenderLib::Math::Point3<T>& p, const RenderLib::Math::Point3<T>& q,
const RenderLib::Math::Point3<T>& a, const RenderLib::Math::Point3<T>& b, const RenderLib::Math::Point3<T>& c,
- float& t, float& v, float& w ) {
+ T& t, // segment dist
+ T& v, T& w // barycentric
+ ) {
using namespace RenderLib::Math;
Vector3<T> ab = b - a;
@@ -116,7 +118,8 @@ namespace Geometry {
inline bool segmentTriangleIntersect_DoubleSided( const RenderLib::Math::Point3<T>& p, const RenderLib::Math::Vector3<T>& dir,
T minT, T maxT,
const RenderLib::Math::Point3<T>& a, const RenderLib::Math::Point3<T>& b, const RenderLib::Math::Point3<T>& c,
- T& t, T& v, T& w,
+ T& t, // segment dist
+ T& v, T& w, // barycentric
const T epsilon = 1e-5 ) {
using namespace RenderLib::Math;
@@ -159,17 +162,5 @@ namespace Geometry {
return segmentTriangleIntersect_DoubleSided<T>( p, dir, -lengthEpsilon, maxT + lengthEpsilon, a, b, c, t, v, w, epsilon );
}
-
- ////////////////////////////////////////////////////////////////////////////
- // segmentTriIntersection
- //
- // An alternative version of segmentTriangleIntersect_SingleSided which
- // returns the intersection point instead of the barycentric coordinates
- //
- // This function expects triangles in CCW order
- bool segmentTriIntersection( const RenderLib::Math::Point3f& p, const RenderLib::Math::Point3f& q,
- const RenderLib::Math::Point3f& a, const RenderLib::Math::Point3f& b, const RenderLib::Math::Point3f& c,
- RenderLib::Math::Vector3f& isect);
-
} // namespace Geometry
} // namespace RenderLib
@@ -40,15 +40,23 @@ namespace Math {
T m30, T m31, T m32, T m33 );
Matrix4( const T* m );
+ static Matrix4<T> Matrix4<T>::rotationFromAngles(float yaw, float pitch, float roll);
+
// Operators ///
Matrix4<T> transposed() const;
Matrix4<T>& transpose();
+ Matrix4<T> Matrix4<T>::inverse() const;
+ Matrix4<T>& Matrix4<T>::invert();
+
+ void toAngles(float& yaw, float& pitch, float& roll) const;
+
// Methods ///
T determinant() const;
Vector3<T> transform(const Vector3<T>& v) const;
Point3<T> transform(const Point3<T>& p) const;
+ Matrix4<T> Matrix4<T>::operator * (const Matrix4<T>& M ) const;
public:
T m[4][4];
@@ -48,7 +48,7 @@ Matrix4<T> Matrix4<T>::transposed() const {
for( i = 0; i < 4; i++ ) {
for( j = 0; j < 4; j++ ) {
- transpose[ i ][ j ] = m[ j ][ i ];
+ transpose.m[ i ][ j ] = m[ j ][ i ];
}
}
return transpose;
@@ -122,9 +122,139 @@ Matrix4<T>::transform
template< typename T >
Point3<T> Matrix4<T>::transform(const Point3<T>& p) const {
// w = 1
- float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3];
- float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3];
- float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3];
- float w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3];
+ float x = m[0][0] * p.x + m[0][1] * p.y + m[0][2] * p.z + m[0][3];
+ float y = m[1][0] * p.x + m[1][1] * p.y + m[1][2] * p.z + m[1][3];
+ float z = m[2][0] * p.x + m[2][1] * p.y + m[2][2] * p.z + m[2][3];
+ float w = m[3][0] * p.x + m[3][1] * p.y + m[3][2] * p.z + m[3][3];
return Point3<T>( x / w, y / w, z / w );
}
+
+/*
+================
+Matrix4<T>::operator *
+================
+*/
+template< typename T >
+Matrix4<T> Matrix4<T>::operator * (const Matrix4<T>& M ) const {
+ Matrix4<T> dst;
+
+ for ( int i = 0; i < 4; i++ ) {
+ for ( int j = 0; j < 4; j++ ) {
+ dst.m[i][j] = 0;
+ for( int k = 0; k < 4; k++ ) {
+ dst.m[i][j] += m[i][k] * M.m[k][j];
+ }
+ }
+ }
+ return dst;
+}
+
+/*
+================
+Matrix4<T>::rotationFromAngles
+================
+*/
+template< typename T >
+Matrix4<T> Matrix4<T>::rotationFromAngles(float yaw, float pitch, float roll) {
+ const float phi = yaw;
+ const float theta = pitch;
+ const float psi = roll;
+
+ float cosPhi = cosf(phi);
+ float sinPhi = sinf(phi);
+ float cosTheta = cosf(theta);
+ float sinTheta = sinf(theta);
+ float cosPsi = cosf(psi);
+ float sinPsi = sinf(psi);
+
+ return Matrix4( cosTheta*cosPsi, -cosPhi*sinPsi + sinPhi*sinTheta*cosPsi, sinPhi*sinPsi + cosPhi*sinTheta*cosPsi, 0,
+ cosTheta*sinPsi, cosPhi*cosPsi + sinPhi*sinTheta*sinPsi, -sinPhi*cosPsi + cosPhi*sinTheta*sinPsi, 0,
+ -sinTheta, sinPhi*cosTheta, cosPhi*cosTheta, 0,
+ 0, 0, 0, 1);
+}
+
+/*
+================
+Matrix4<T>::toAngles
+================
+*/
+template< typename T >
+void Matrix4<T>::toAngles(float& yaw, float& pitch, float& roll) const {
+ float theta;
+ float cp;
+ float sp;
+
+ sp = m[ 2 ][ 0 ];
+
+ // cap off our sin value so that we don't get any NANs
+ if ( sp > 1.0f ) {
+ sp = 1.0f;
+ } else if ( sp < -1.0f ) {
+ sp = -1.0f;
+ }
+
+ theta = -asinf( sp );
+ cp = cosf( theta );
+
+ if ( cp > 8192.0f * FLT_EPSILON ) {
+ pitch = theta;
+ roll = atan2f( m[ 1 ][ 0 ], m[ 0 ][ 0 ] );
+ yaw = atan2f( m[ 2 ][ 1 ], m[ 2 ][ 2 ] );
+ } else {
+ pitch = theta;
+ roll = -atan2f( m[ 0 ][ 1 ], m[ 1 ][ 1 ] );
+ yaw = 0.0f;
+ }
+}
+
+/*
+================
+Matrix4<T>::inverse
+================
+*/
+template< typename T >
+Matrix4<T> Matrix4<T>::inverse() const {
+ Matrix4<T> inv = *this;
+ return inv.invert();
+}
+
+/*
+================
+Matrix4<T>::invert
+================
+*/
+template< typename T >
+Matrix4<T>& Matrix4<T>::invert() {
+ T inv[4][4], det;
+
+ inv[0][0] = m[1][1]*m[2][2]*m[3][3] - m[1][1]*m[2][3]*m[3][2] - m[2][1]*m[1][2]*m[3][3] + m[2][1]*m[1][3]*m[3][2] + m[3][1]*m[1][2]*m[2][3] - m[3][1]*m[1][3]*m[2][2];
+ inv[1][0] = -m[1][0]*m[2][2]*m[3][3] + m[1][0]*m[2][3]*m[3][2] + m[2][0]*m[1][2]*m[3][3] - m[2][0]*m[1][3]*m[3][2] - m[3][0]*m[1][2]*m[2][3] + m[3][0]*m[1][3]*m[2][2];
+ inv[2][0] = m[1][0]*m[2][1]*m[3][3] - m[1][0]*m[2][3]*m[3][1] - m[2][0]*m[1][1]*m[3][3] + m[2][0]*m[1][3]*m[3][1] + m[3][0]*m[1][1]*m[2][3] - m[3][0]*m[1][3]*m[2][1];
+ inv[3][0] = -m[1][0]*m[2][1]*m[3][2] + m[1][0]*m[2][2]*m[3][1] + m[2][0]*m[1][1]*m[3][2] - m[2][0]*m[1][2]*m[3][1] - m[3][0]*m[1][1]*m[2][2] + m[3][0]*m[1][2]*m[2][1];
+ inv[0][1] = -m[0][1]*m[2][2]*m[3][3] + m[0][1]*m[2][3]*m[3][2] + m[2][1]*m[0][2]*m[3][3] - m[2][1]*m[0][3]*m[3][2] - m[3][1]*m[0][2]*m[2][3] + m[3][1]*m[0][3]*m[2][2];
+ inv[1][1] = m[0][0]*m[2][2]*m[3][3] - m[0][0]*m[2][3]*m[3][2] - m[2][0]*m[0][2]*m[3][3] + m[2][0]*m[0][3]*m[3][2] + m[3][0]*m[0][2]*m[2][3] - m[3][0]*m[0][3]*m[2][2];
+ inv[2][1] = -m[0][0]*m[2][1]*m[3][3] + m[0][0]*m[2][3]*m[3][1] + m[2][0]*m[0][1]*m[3][3] - m[2][0]*m[0][3]*m[3][1] - m[3][0]*m[0][1]*m[2][3] + m[3][0]*m[0][3]*m[2][1];
+ inv[3][1] = m[0][0]*m[2][1]*m[3][2] - m[0][0]*m[2][2]*m[3][1] - m[2][0]*m[0][1]*m[3][2] + m[2][0]*m[0][2]*m[3][1] + m[3][0]*m[0][1]*m[2][2] - m[3][0]*m[0][2]*m[2][1];
+ inv[0][2] = m[0][1]*m[1][2]*m[3][3] - m[0][1]*m[1][3]*m[3][2] - m[1][1]*m[0][2]*m[3][3] + m[1][1]*m[0][3]*m[3][2] + m[3][1]*m[0][2]*m[1][3] - m[3][1]*m[0][3]*m[1][2];
+ inv[1][2] = -m[0][0]*m[1][2]*m[3][3] + m[0][0]*m[1][3]*m[3][2] + m[1][0]*m[0][2]*m[3][3] - m[1][0]*m[0][3]*m[3][2] - m[3][0]*m[0][2]*m[1][3] + m[3][0]*m[0][3]*m[1][2];
+ inv[2][2] = m[0][0]*m[1][1]*m[3][3] - m[0][0]*m[1][3]*m[3][1] - m[1][0]*m[0][1]*m[3][3] + m[1][0]*m[0][3]*m[3][1] + m[3][0]*m[0][1]*m[1][3] - m[3][0]*m[0][3]*m[1][1];
+ inv[3][2] = -m[0][0]*m[1][1]*m[3][2] + m[0][0]*m[1][2]*m[3][1] + m[1][0]*m[0][1]*m[3][2] - m[1][0]*m[0][2]*m[3][1] - m[3][0]*m[0][1]*m[1][2] + m[3][0]*m[0][2]*m[1][1];
+ inv[0][3] = -m[0][1]*m[1][2]*m[2][3] + m[0][1]*m[1][3]*m[2][2] + m[1][1]*m[0][2]*m[2][3] - m[1][1]*m[0][3]*m[2][2] - m[2][1]*m[0][2]*m[1][3] + m[2][1]*m[0][3]*m[1][2];
+ inv[1][3] = m[0][0]*m[1][2]*m[2][3] - m[0][0]*m[1][3]*m[2][2] - m[1][0]*m[0][2]*m[2][3] + m[1][0]*m[0][3]*m[2][2] + m[2][0]*m[0][2]*m[1][3] - m[2][0]*m[0][3]*m[1][2];
+ inv[2][3] = -m[0][0]*m[1][1]*m[2][3] + m[0][0]*m[1][3]*m[2][1] + m[1][0]*m[0][1]*m[2][3] - m[1][0]*m[0][3]*m[2][1] - m[2][0]*m[0][1]*m[1][3] + m[2][0]*m[0][3]*m[1][1];
+ inv[3][3] = m[0][0]*m[1][1]*m[2][2] - m[0][0]*m[1][2]*m[2][1] - m[1][0]*m[0][1]*m[2][2] + m[1][0]*m[0][2]*m[2][1] + m[2][0]*m[0][1]*m[1][2] - m[2][0]*m[0][2]*m[1][1];
+
+ det = m[0][0]*inv[0][0] + m[0][1]*inv[1][0] + m[0][2]*inv[2][0] + m[0][3]*inv[3][0];
+ if (det == 0) {
+ assert(false);
+ } else {
+ det = 1.0f / det;
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ m[i][j] = inv[i][j] * det;
+ }
+ }
+ }
+
+ return *this;
+}
@@ -33,7 +33,7 @@ namespace Math {
inline Normal2<T> operator -() const;
- inline Normal2<T>& Normalize();
+ inline T normalize();
inline static Normal2<T> normalize(const Normal2<T>& n);
inline bool equals( const Normal2<T>& p, T epsilon ) const;
@@ -80,13 +80,13 @@ inline Normal2<T> Normal2<T>::operator -() const {
}
template< typename T >
-inline Normal2<T>& Normal2<T>::normalize() {
+inline T Normal2<T>::normalize() {
T length = sqrt( x * x + y * y);
assert(length != 0);
T invLength = 1.0 / length;
x *= invLength;
y *= invLength;
- return *this;
+ return length;
}
template< typename T >
@@ -112,11 +112,11 @@ inline bool Normal2<float>::equals( const Normal2<float>& p, float epsilon ) con
}
template<>
-inline Normal2<float>& Normal2<float>::normalize() {
+inline float Normal2<float>::normalize() {
float length = sqrtf( x * x + y * y );
assert(length != 0);
float invLength = 1.0f / length;
x *= invLength;
y *= invLength;
- return *this;
+ return length;
}
@@ -4,23 +4,24 @@ inline Normal3<T>::Normal3( const T X, const T Y, const T Z) {
// build Normal3<T> making vector <x,y,z> unitary
T length = sqrt(X*X + Y*Y + Z*Z);
- assert(length > 0);
-
- x = X / length;
- y = Y / length;
- z = Z / length;
+ if ( length > 0 ) {
+ x = X / length;
+ y = Y / length;
+ z = Z / length;
+ }
}
template< typename T >
inline Normal3<T>::Normal3( const Vector3<T>& v) {
// build Normal3<T> making vector <x,y,z> unitary
- T length = v.Length();
+ T length = v.length();
- assert(length > 0);
- x = v.x / length;
- y = v.y / length;
- z = v.z / length;
+ if ( length > 0 ) {
+ x = v.x / length;
+ y = v.y / length;
+ z = v.z / length;
+ }
}
template< typename T >
@@ -94,12 +95,12 @@ inline Normal3<T> Normal3<T>::normalize(const Normal3<T>& n) {
}
template< typename T >
-inline T Normal3<T>::dot(const Normal3<T>& n, const Vector3<T>& v) const {
+inline T Normal3<T>::dot(const Normal3<T>& n, const Vector3<T>& v) {
return n.x*v.x + n.y*v.y + n.z*v.z;
}
template< typename T >
-inline T Normal3<T>::dot(const Normal3<T>& n1, const Normal3<T>& n2) const {
+inline T Normal3<T>::dot(const Normal3<T>& n1, const Normal3<T>& n2) {
return x*v.x + y*v.y + z*v.z;
}
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <math.h>
+#include <math/algebra/matrix/matrix4.h>
+#include <math/algebra/vector/vector3.h>
+
+namespace RenderLib {
+namespace Math {
+
+//////////////////////////////////////////////////////////////////////////////////////////
+// Quaternion
+//////////////////////////////////////////////////////////////////////////////////////////
+
+class Quaternion {
+public:
+ inline Quaternion( float x, float y, float z, float w);
+ static Quaternion fromAxisRotation(const RenderLib::Math::Vector3f& axis, float angle);
+ static Quaternion fromEuler( float yaw, float pitch, float roll );
+
+ RenderLib::Math::Matrix4f toRotation() const;
+ inline Quaternion operator*(const Quaternion& Q) const;
+ inline Quaternion& normalize();
+ inline Quaternion conjugate() const;
+public:
+ float x,y,z,w;
+private:
+ explicit Quaternion() {}
+};
+
+#include "quaternion.inl"
+
+} // namespace Math
+} // namespace RenderLib
Oops, something went wrong.

0 comments on commit 6de6ebb

Please sign in to comment.