From a30ae7d0fb13b9a00a17e621463c186fb6ac5ada Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 6 Jan 2010 06:12:58 +0100 Subject: Optimized triangle normal handling --- src/Triangle.h | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/Triangle.h') diff --git a/src/Triangle.h b/src/Triangle.h index 2b34c3f..e13574c 100644 --- a/src/Triangle.h +++ b/src/Triangle.h @@ -30,30 +30,44 @@ class Triangle { Triangle() : color(vmml::vec4f::ONE), texture(0) { vertices[0] = vertices[1] = vertices[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; } - 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[1] = v2; vertices[2] = v3; normals[0] = normals[1] = normals[2] = vmml::vec3f::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];} - vmml::vec3f& getVertex(int i) {return vertices[i];} - void setVertex(int i, vmml::vec3f v) { + void setVertex(int i, vmml::vec3f v, bool update = true) { vertices[i] = v; + + if(update) + updateNormal(); + } + + void updateNormal() { + normal = vertices[0].compute_normal(vertices[1], vertices[2]); } - const vmml::vec3f& getNormal(int i) const {return normals[i];} - vmml::vec3f& getNormal(int i) {return normals[i];} - 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; } + const vmml::vec3f& getNormal() const {return normal;} + const vmml::vec2f& getTexCoords(int i) const {return texcoords[i];} vmml::vec2f& getTexCoords(int i) {return texcoords[i];} void setTexCoords(int i, vmml::vec2f t) { @@ -73,12 +87,8 @@ class Triangle { color = c; } - vmml::vec3f computeNormal() const { - return vertices[0].compute_normal(vertices[1], vertices[2]); - } - bool isDegenerate() const { - return (computeNormal().squared_length() == 0); + return (normal.squared_length() == 0); } void transform(const vmml::mat4f &matrix) { @@ -94,6 +104,7 @@ class Triangle { private: vmml::vec3f vertices[3]; vmml::vec3f normals[3]; + vmml::vec3f normal; vmml::vec2f texcoords[3]; vmml::vec4f color; unsigned texture; -- cgit v1.2.3