Optimized triangle normal handling

This commit is contained in:
Matthias Schiffer 2010-01-06 06:12:58 +01:00
parent 2aa2097b6c
commit a30ae7d0fb
5 changed files with 32 additions and 25 deletions

View file

@ -136,7 +136,7 @@ bool Collision::test(const Triangle &t, const vmml::vec3f &m, float r, const vmm
float d; float d;
vmml::vec3f triangleNormal = t.computeNormal(); vmml::vec3f triangleNormal = t.getNormal();
if(test(t, MathUtil::Ray(m - r*triangleNormal, move), &d) && d > -MathUtil::EPSILON) { if(test(t, MathUtil::Ray(m - r*triangleNormal, move), &d) && d > -MathUtil::EPSILON) {
if(d < 1) { if(d < 1) {
if(distance) if(distance)

View file

@ -172,13 +172,13 @@ TriangleRecord Level::loadWall(xmlNodePtr wallNode) {
if(++vertexNum > 2) if(++vertexNum > 2)
break; break;
wall.getTriangle().setVertex(vertexNum, loadVector(node)); wall.getTriangle().setVertex(vertexNum, loadVector(node), false);
} }
else if(!xmlStrcmp(node->name, (xmlChar*)"normal")) { else if(!xmlStrcmp(node->name, (xmlChar*)"normal")) {
if(vertexNum < 0) if(vertexNum < 0)
continue; continue;
wall.getTriangle().setNormal(vertexNum, loadVector(node)); wall.getTriangle().setVertexNormal(vertexNum, loadVector(node));
} }
else if(!xmlStrcmp(node->name, (xmlChar*)"texcoords")) { else if(!xmlStrcmp(node->name, (xmlChar*)"texcoords")) {
if(vertexNum < 0) continue; if(vertexNum < 0) continue;
@ -197,18 +197,14 @@ TriangleRecord Level::loadWall(xmlNodePtr wallNode) {
} }
} }
vmml::vec3f normal = wall.getTriangle().computeNormal(); wall.getTriangle().updateNormal();
vmml::vec3f normal = wall.getTriangle().getNormal();
if(normal.squared_length() > 0) { for(int i = 0; i < 3; ++i) {
normal.normalize(); if(wall.getTriangle().getVertexNormal(i).squared_length() == 0)
wall.getTriangle().setVertexNormal(i, normal);
for(int i = 0; i < 3; ++i) {
if(wall.getTriangle().getNormal(i).squared_length() == 0)
wall.getTriangle().setNormal(i, normal);
}
} }
return wall; return wall;
} }

View file

@ -51,7 +51,7 @@ class MathUtil {
class Plane { class Plane {
public: public:
Plane(const vmml::vec3f &n = vmml::vec3f::ZERO, float d0 = 0) : normal(n), d(d0) {} Plane(const vmml::vec3f &n = vmml::vec3f::ZERO, float d0 = 0) : normal(n), d(d0) {}
Plane(const Triangle &t) : normal(t.computeNormal()), d(t.getVertex(0).dot(normal)) {} Plane(const Triangle &t) : normal(t.getNormal()), d(t.getVertex(0).dot(normal)) {}
bool contains(const vmml::vec3f &v) const { bool contains(const vmml::vec3f &v) const {
return (fabsf(normal.dot(v) - d) < EPSILON); return (fabsf(normal.dot(v) - d) < EPSILON);

View file

@ -53,7 +53,7 @@ void Renderer::renderTriangle(const Triangle &t) {
for(int i = 0; i < 3; ++i) { for(int i = 0; i < 3; ++i) {
glTexCoord2fv(t.getTexCoords(i).array); glTexCoord2fv(t.getTexCoords(i).array);
glNormal3fv(t.getNormal(i).array); glNormal3fv(t.getVertexNormal(i).array);
glVertex3fv(t.getVertex(i).array); glVertex3fv(t.getVertex(i).array);
} }
} }

View file

@ -30,30 +30,44 @@ class Triangle {
Triangle() : color(vmml::vec4f::ONE), texture(0) { Triangle() : color(vmml::vec4f::ONE), texture(0) {
vertices[0] = vertices[1] = vertices[2] = vmml::vec3f::ZERO; vertices[0] = vertices[1] = vertices[2] = vmml::vec3f::ZERO;
normals[0] = normals[1] = normals[2] = vmml::vec3f::ZERO; normals[0] = normals[1] = normals[2] = vmml::vec3f::ZERO;
normal = vmml::vec3f::ZERO;
texcoords[0] = texcoords[1] = texcoords[2] = vmml::vec2f::ZERO; texcoords[0] = texcoords[1] = texcoords[2] = vmml::vec2f::ZERO;
} }
Triangle(const vmml::vec3f &v1, const vmml::vec3f &v2, const vmml::vec3f &v3, const vmml::vec4f &color0) : color(color0), texture(0) { Triangle(const vmml::vec3f &v1, const vmml::vec3f &v2, const vmml::vec3f &v3, const vmml::vec4f &color0, bool update = true) : color(color0), texture(0) {
vertices[0] = v1; vertices[0] = v1;
vertices[1] = v2; vertices[1] = v2;
vertices[2] = v3; vertices[2] = v3;
normals[0] = normals[1] = normals[2] = vmml::vec3f::ZERO; normals[0] = normals[1] = normals[2] = vmml::vec3f::ZERO;
texcoords[0] = texcoords[1] = texcoords[2] = vmml::vec2f::ZERO; texcoords[0] = texcoords[1] = texcoords[2] = vmml::vec2f::ZERO;
if(update)
updateNormal();
else
normal = vmml::vec3f::ZERO;
} }
const vmml::vec3f& getVertex(int i) const {return vertices[i];} const vmml::vec3f& getVertex(int i) const {return vertices[i];}
vmml::vec3f& getVertex(int i) {return vertices[i];} void setVertex(int i, vmml::vec3f v, bool update = true) {
void setVertex(int i, vmml::vec3f v) {
vertices[i] = v; vertices[i] = v;
if(update)
updateNormal();
} }
const vmml::vec3f& getNormal(int i) const {return normals[i];} void updateNormal() {
vmml::vec3f& getNormal(int i) {return normals[i];} normal = vertices[0].compute_normal(vertices[1], vertices[2]);
void setNormal(int i, vmml::vec3f n) { }
const vmml::vec3f& getVertexNormal(int i) const {return normals[i];}
vmml::vec3f& getVertexNormal(int i) {return normals[i];}
void setVertexNormal(int i, vmml::vec3f n) {
normals[i] = n; normals[i] = n;
} }
const vmml::vec3f& getNormal() const {return normal;}
const vmml::vec2f& getTexCoords(int i) const {return texcoords[i];} const vmml::vec2f& getTexCoords(int i) const {return texcoords[i];}
vmml::vec2f& getTexCoords(int i) {return texcoords[i];} vmml::vec2f& getTexCoords(int i) {return texcoords[i];}
void setTexCoords(int i, vmml::vec2f t) { void setTexCoords(int i, vmml::vec2f t) {
@ -73,12 +87,8 @@ class Triangle {
color = c; color = c;
} }
vmml::vec3f computeNormal() const {
return vertices[0].compute_normal(vertices[1], vertices[2]);
}
bool isDegenerate() const { bool isDegenerate() const {
return (computeNormal().squared_length() == 0); return (normal.squared_length() == 0);
} }
void transform(const vmml::mat4f &matrix) { void transform(const vmml::mat4f &matrix) {
@ -94,6 +104,7 @@ class Triangle {
private: private:
vmml::vec3f vertices[3]; vmml::vec3f vertices[3];
vmml::vec3f normals[3]; vmml::vec3f normals[3];
vmml::vec3f normal;
vmml::vec2f texcoords[3]; vmml::vec2f texcoords[3];
vmml::vec4f color; vmml::vec4f color;
unsigned texture; unsigned texture;