Begin Function CalculateSurfaceNormal (Input Triangle) Returns Vector

Set Vector U to (Triangle.p2 minus Triangle.p1)

Set Vector V to (Triangle.p3 minus Triangle.p1)

Set Normal.x to (multiply U.y by V.z) minus (multiply U.z by V.y)

Set Normal.y to (multiply U.z by V.x) minus (multiply U.x by V.z)

Set Normal.z to (multiply U.x by V.y) minus (multiply U.y by V.x)

Returning Normal

End Function

And the formula for finding the normal of a vertex is:

n= (n1+n2+n3+n4) / |n1 + n2 + n3 + n4| where there would be 4 adjoining polygons(triangles).

So I've coded it up but am getting strange normals on the interior vertices... here's the relevant code... sorry for the lengthy preamble but I just didn't want to have to reexplain what I mean

std::vector<GLushort> indices; for(int i = 0; i < m-1; i++) { for(int j = 0; j < n-1; j++) { indices.push_back((j*m)+i); indices.push_back((j*m)+i+1); indices.push_back((j*m)+i+m); Polygon p; p.P1 = vertices[(j*m)+i].vertex; p.P2 = vertices[(j*m)+i+1].vertex; p.P3 = vertices[(j*m)+i+m].vertex; p.calculateNormal(); vertices[(j*m)+i].polygons.push_back(p); vertices[(j*m)+i+1].polygons.push_back(p); vertices[(j*m)+i+m].polygons.push_back(p); } } for(int i = 1; i < m; i++) { for(int j = 0; j < n-1; j++) { indices.push_back((j*m)+i); indices.push_back((j*m)+i+m-1); indices.push_back((j*m)+i+m); Polygon p; p.P1 = vertices[(j*m)+i].vertex; p.P2 = vertices[(j*m)+i+m-1].vertex; p.P3 = vertices[(j*m)+i+m].vertex; p.calculateNormal(); vertices[(j*m)+i].polygons.push_back(p); vertices[(j*m)+i+m-1].polygons.push_back(p); vertices[(j*m)+i+m].polygons.push_back(p); } }

And...

struct Polygon { vec3 P1; vec3 P2; vec3 P3; vec3 normal; void calculateNormal() { vec3 U = P2 - P3; vec3 V = P3 - P1; normal = vec3((U[1]*V[2])-(U[2]*V[1]), (U[2]*V[0])-(U[0]*V[2]), (U[0]*V[1])-(U[1]*V[0])); } }; struct Vertex { Vertex(vec3 v, vec3 n) { vertex = v; normal = n; } Vertex(vec3 v) { vertex = v; } void setNormal(std::vector<Polygon> p) { vec3 vecTot; for(int i = 0; i < p.size(); i++) { vecTot = vecTot + p[i].normal; //std::cout << p[i].normal; } GLfloat normalized = sqrt((vecTot[0]*vecTot[0])+(vecTot[1]*vecTot[1])+(vecTot[2]*vecTot[2])); //std::cout << vecTot << " " << normalized << "\n"; normal = vecTot/normalized; } vec3 vertex; vec3 normal; //Material material; std::vector<Polygon> polygons; };

Thanks for anyone who has read this far! And thanks for any help I can get Oh and here is a screenshot of the output (white lines are the normals)...