Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Pixel_Outlaw on October 14, 2007
-
question 1:
I've got some nice terrain drawing/creating functions going in my library and I currently have the faces shaded per face with no per vertex normals. I was reading and found out that to enable proper goroud shading you need per vertex normals. I read that each normal may be found by averaging the normals of adjacent edge verticies. Naturally my heightmap is made of triangles and it would be easy if each vertex only had 4 neighbors. However, at the edges there are only three or even two neighbors. Does averaging still apply here? I'm having a heck of a time with Gouraud vectors, I kind of cheated and pasted some code for finding face vectors based on 3 points I'm now paying for the quick and dirty copy-paste.
question 2:
Are there other and better ways to create heightmap terrains using triangles? It seems that it might be better to use triangles that crease in more than one direction. I've just seen quads with two triangles that crease in the same direction. Is this OK?
A little pic of a very small terrian I've mapped.
-
1. It doesn't really matter how many triangles the vertex is shared by, the way I do it is to:
loop through all vertices and set the normals to (0.0,0.0,0.0)
loop through all triangles and add the triangle normal to all 3 vertices of the triangle
loop through all vertices and normalise the normal
Although this works well enough it isn't a perfect solution, considering a cube for example, there may be a vertex that is shared the by 2 triangles that make up one cube face and only 1 triangle that makes up one of the other cube faces which means the normal will be weighted towards the normal of the face that has 2 triangles sharing the vertex
2. I'm not quite sure what you mean by triangles that crease in more than one direction, it shouldn't matter where in 3d space your 3 triangle vertex coordinates are as you can always connect any 3 points to form a flat triangle (unless they form a straight line or 2 or more vertices share the same coordinates)
-
I was wondering if I could use averages from the adjacent face normals for each vertex normal. If so how do I average normals?
How do you average normals? Do you just average the x, y, and z components?
Everything I've read says to find a vertex normal you have to average the normals derived from adjacent faces. ???
-
Add them together and re-normalize at the end.
You can improve on the 'add them all together' algorithm, but it's an excellent start.
Jim
-
Creasing in one diagonal direction is not quite as adaptable as a mesh that can crease in either diagonal direction. In those pictures every quad is made of two tris that share an edge that runs from top right to bottom left. if you flip the verts for every column then for every row you will end up with digonal edges that criss cross. When the criss cross you will find it easier to create peaks, valleys etc.
-
You can always do a check to see which of the triangle pairs gives you least error. Create both pairs of triangles, and that pair which has the greater dot product between the triangle normals is the flattest.
Jim