aboutsummaryrefslogtreecommitdiff
path: root/dep/src/g3dlite/Triangle.cpp
diff options
context:
space:
mode:
authorclick <none@none>2010-06-05 00:59:25 +0200
committerclick <none@none>2010-06-05 00:59:25 +0200
commite77716188861d4aa83b227a90e04a66b63baeb1f (patch)
treece72764181a760314ec851f7535052dcf75649db /dep/src/g3dlite/Triangle.cpp
parent1426c2970f42a2d065198806f750bf5dd28d580b (diff)
HIGHLY EXPERIMENTAL - USE AT YOUR OWN RISK
Implement the use of the new vmap3-format by Lynx3d (mad props to you for this, and thanks for the talks earlier) + reduced Vmap size to less than one third, and improve precision + indoor/outdoor check which allows automatic unmounting of players + additional area information from WMOAreaTable.dbc, removed existing "hacks" + WMO liquid information for swimming and fishing correctly in buildings/cities/caves/instances (lava and slime WILL hurt from now on!) - buildfiles for windows are not properly done, and will need to be sorted out NOTE: Do NOT annoy Lynx3d about this, any issues with this "port" is entirely our fault ! THIS REVISION IS CONSIDERED UNSTABLE AND CONTAINS WORK IN PROGRESS - USE AT YOUR OWN RISK! --HG-- branch : trunk
Diffstat (limited to 'dep/src/g3dlite/Triangle.cpp')
-rw-r--r--dep/src/g3dlite/Triangle.cpp110
1 files changed, 96 insertions, 14 deletions
diff --git a/dep/src/g3dlite/Triangle.cpp b/dep/src/g3dlite/Triangle.cpp
index 2e221b12f1d..253438ad5fb 100644
--- a/dep/src/g3dlite/Triangle.cpp
+++ b/dep/src/g3dlite/Triangle.cpp
@@ -1,22 +1,27 @@
/**
@file Triangle.cpp
-
- @maintainer Morgan McGuire, graphics3d.com
-
+
+ @maintainer Morgan McGuire, http://graphics.cs.williams.edu
+
@created 2001-04-06
- @edited 2006-01-20
+ @edited 2008-12-28
- Copyright 2000-2006, Morgan McGuire.
+ Copyright 2000-2009, Morgan McGuire.
All rights reserved.
*/
#include "G3D/platform.h"
#include "G3D/Triangle.h"
#include "G3D/Plane.h"
+#include "G3D/BinaryInput.h"
+#include "G3D/BinaryOutput.h"
+#include "G3D/debugAssert.h"
#include "G3D/AABox.h"
+#include "G3D/Ray.h"
namespace G3D {
+
void Triangle::init(const Vector3& v0, const Vector3& v1, const Vector3& v2) {
_plane = Plane(v0, v1, v2);
@@ -27,8 +32,8 @@ void Triangle::init(const Vector3& v0, const Vector3& v1, const Vector3& v2) {
static int next[] = {1,2,0};
for (int i = 0; i < 3; ++i) {
- const Vector3 e = _vertex[next[i]] - _vertex[i];
- edgeMagnitude[i] = e.magnitude();
+ const Vector3& e = _vertex[next[i]] - _vertex[i];
+ edgeMagnitude[i] = e.magnitude();
if (edgeMagnitude[i] == 0) {
edgeDirection[i] = Vector3::zero();
@@ -37,37 +42,64 @@ void Triangle::init(const Vector3& v0, const Vector3& v1, const Vector3& v2) {
}
}
- edge01 = _vertex[1] - _vertex[0];
- edge02 = _vertex[2] - _vertex[0];
+ _edge01 = _vertex[1] - _vertex[0];
+ _edge02 = _vertex[2] - _vertex[0];
_primaryAxis = _plane.normal().primaryAxis();
- _area = (float)edgeDirection[0].cross(edgeDirection[2]).magnitude() * (edgeMagnitude[0] * edgeMagnitude[2]);
-
+ _area = 0.5f * edgeDirection[0].cross(edgeDirection[2]).magnitude() * (edgeMagnitude[0] * edgeMagnitude[2]);
+ //0.5f * (_vertex[1] - _vertex[0]).cross(_vertex[2] - _vertex[0]).dot(_plane.normal());
}
+
Triangle::Triangle() {
init(Vector3::zero(), Vector3::zero(), Vector3::zero());
}
+
Triangle::Triangle(const Vector3& v0, const Vector3& v1, const Vector3& v2) {
init(v0, v1, v2);
}
+
Triangle::~Triangle() {
}
-double Triangle::area() const {
+
+Triangle::Triangle(class BinaryInput& b) {
+ deserialize(b);
+}
+
+
+void Triangle::serialize(class BinaryOutput& b) {
+ _vertex[0].serialize(b);
+ _vertex[1].serialize(b);
+ _vertex[2].serialize(b);
+}
+
+
+void Triangle::deserialize(class BinaryInput& b) {
+ _vertex[0].deserialize(b);
+ _vertex[1].deserialize(b);
+ _vertex[2].deserialize(b);
+ init(_vertex[0], _vertex[1], _vertex[2]);
+}
+
+
+float Triangle::area() const {
return _area;
}
+
const Vector3& Triangle::normal() const {
return _plane.normal();
}
+
const Plane& Triangle::plane() const {
return _plane;
}
+
Vector3 Triangle::center() const {
return (_vertex[0] + _vertex[1] + _vertex[2]) / 3.0;
}
@@ -85,9 +117,10 @@ Vector3 Triangle::randomPoint() const {
s = 1.0f - s;
}
- return edge01 * s + edge02 * t + _vertex[0];
+ return _edge01 * s + _edge02 * t + _vertex[0];
}
+
void Triangle::getBounds(AABox& out) const {
Vector3 lo = _vertex[0];
Vector3 hi = lo;
@@ -100,5 +133,54 @@ void Triangle::getBounds(AABox& out) const {
out = AABox(lo, hi);
}
-} // G3D
+bool Triangle::intersect(const Ray& ray, float& distance, float baryCoord[3]) const {
+ static const float EPS = 1e-5f;
+
+ // See RTR2 ch. 13.7 for the algorithm.
+
+ const Vector3& e1 = edge01();
+ const Vector3& e2 = edge02();
+ const Vector3 p(ray.direction().cross(e2));
+ const float a = e1.dot(p);
+
+ if (abs(a) < EPS) {
+ // Determinant is ill-conditioned; abort early
+ return false;
+ }
+
+ const float f = 1.0f / a;
+ const Vector3 s(ray.origin() - vertex(0));
+ const float u = f * s.dot(p);
+
+ if ((u < 0.0f) || (u > 1.0f)) {
+ // We hit the plane of the m_geometry, but outside the m_geometry
+ return false;
+ }
+
+ const Vector3 q(s.cross(e1));
+ const float v = f * ray.direction().dot(q);
+
+ if ((v < 0.0f) || ((u + v) > 1.0f)) {
+ // We hit the plane of the triangle, but outside the triangle
+ return false;
+ }
+
+ const float t = f * e2.dot(q);
+
+ if ((t > 0.0f) && (t < distance)) {
+ // This is a new hit, closer than the previous one
+ distance = t;
+
+ baryCoord[0] = 1.0 - u - v;
+ baryCoord[1] = u;
+ baryCoord[2] = v;
+
+ return true;
+ } else {
+ // This hit is after the previous hit, so ignore it
+ return false;
+ }
+}
+
+} // G3D