Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r1473 - trunk/Model/Groups


Chronological Thread 
  • From: thiago@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r1473 - trunk/Model/Groups
  • Date: Sun, 15 Jul 2007 23:19:10 -0600 (MDT)

Author: thiago
Date: Sun Jul 15 23:19:07 2007
New Revision: 1473

Modified:
   trunk/Model/Groups/Mesh.cc
   trunk/Model/Groups/Mesh.h
Log:
Mesh class can now derive vertex normals from shared vertices. This will of 
course not create smooth normals if triangles are sharing vertices. Likewise, 
if triangles are sharing vertices, but the triangle edges are supposed to be 
sharp, this will make it look smooth. So it's better to just give the mesh 
class your own vertex normals which are guarenteed correct. Likewise, 
something like a normal map would give even better results...

Modified: trunk/Model/Groups/Mesh.cc
==============================================================================
--- trunk/Model/Groups/Mesh.cc  (original)
+++ trunk/Model/Groups/Mesh.cc  Sun Jul 15 23:19:07 2007
@@ -202,3 +202,42 @@
     materials[i]->preprocess(context);
   Group::preprocess(context);
 }
+
+void Mesh::interpolateNormals()
+{
+  //I think this should do the correct thing, but I might have messed
+  //something with where and when I normalize vectors...
+
+  //set all vertex normals to 0 without doing too much extra work.
+  size_t oldSize = vertexNormals.size();
+  vertexNormals.resize(vertices.size(), Vector(0,0,0));
+  oldSize = min(oldSize, vertexNormals.size());
+  for (size_t i = 0; i < oldSize; ++i) {
+    vertexNormals[i] = Vector(0,0,0);
+  }
+
+  normal_indices.resize(vertex_indices.size());
+
+  for (size_t i = 0; i < vertex_indices.size(); i+=3) {
+    const int index0 = vertex_indices[i+0];
+    const int index1 = vertex_indices[i+1];
+    const int index2 = vertex_indices[i+2];
+
+    normal_indices[i+0] = index0;
+    normal_indices[i+1] = index1;
+    normal_indices[i+2] = index2;
+
+    const Vector& a = vertices[index0];
+    const Vector& b = vertices[index1];
+    const Vector& c = vertices[index2];
+    const Vector n = Cross(b-a, c-a).normal();
+
+    vertexNormals[index0] += n;
+    vertexNormals[index1] += n;
+    vertexNormals[index2] += n;
+  }
+
+  for (size_t i = 0; i < vertexNormals.size(); ++i) {
+    vertexNormals[i] = vertexNormals[i].normal();
+  }    
+}

Modified: trunk/Model/Groups/Mesh.h
==============================================================================
--- trunk/Model/Groups/Mesh.h   (original)
+++ trunk/Model/Groups/Mesh.h   Sun Jul 15 23:19:07 2007
@@ -51,6 +51,7 @@
     virtual bool isParallel() const { return true; }
 
     bool hasVertexNormals() const { return !normal_indices.empty(); }
+    void interpolateNormals();
 
     //should not be allowed to use Group's add and set.
     virtual void add(Object* obj);




  • [MANTA] r1473 - trunk/Model/Groups, thiago, 07/16/2007

Archive powered by MHonArc 2.6.16.

Top of page