summaryrefslogtreecommitdiffstats
path: root/src/Triangle.h
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-01-06 06:12:58 +0100
committerMatthias Schiffer <matthias@gamezock.de>2010-01-06 06:12:58 +0100
commita30ae7d0fb13b9a00a17e621463c186fb6ac5ada (patch)
tree883cf24043ea124de8f6162ba106dce8866d63e1 /src/Triangle.h
parent2aa2097b6cffd6ed58e127b0ed4f76d6255ac495 (diff)
downloadzoom++-a30ae7d0fb13b9a00a17e621463c186fb6ac5ada.tar
zoom++-a30ae7d0fb13b9a00a17e621463c186fb6ac5ada.zip
Optimized triangle normal handling
Diffstat (limited to 'src/Triangle.h')
-rw-r--r--src/Triangle.h33
1 files changed, 22 insertions, 11 deletions
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;