Okay, so I’m trying to create a vertex buffer that will allow me to see the normals of my mesh. Problem is, while it does create the lines, they all seem to converge at a single point, and on some meshes they can’t be seen at all (though this is likely because they’re inside the mesh - I’m sure if I turned off z buffering, I’d see them). This is what it looks like:
The yellow lines should be connecting each vertex to it’s normal.
Here’s the part of my code that creates said buffer:
// Create a vert buffer for normal visualization
size = activeHeader.numVerts * sizeof(vert_P) * 2;
vert_P* activeNormVis = (vert_P*)malloc(size);
for (i = 0; i < activeHeader.numVerts; i++)
activeNormVis[i*2].x = activeVertData[i].x;
activeNormVis[i*2].y = activeVertData[i].y;
activeNormVis[i*2].z = activeVertData[i].z;
activeNormVis[(i*2)+1].x = activeVertData[i].nx;
activeNormVis[(i*2)+1].y = activeVertData[i].ny;
activeNormVis[(i*2)+1].z = activeVertData[i].nz;
glBufferData(GL_ARRAY_BUFFER, size, activeNormVis, GL_STATIC_DRAW);
I guess they don’t converge on a single point, but on several points that lie on a unit sphere around the origin…
You ask OpenGL to draw lines from the vertex coordinates to the coordinates of the normal vector, to draw lines in the normal direction you should set the second vertex to position + scale * normal (the scale defines the length of the lines).
They don’t converge at the origin, but rather off to the side (if I had to guess, at about (-0.3, -0.1, -0.2) ), also I don’t use any scaling at all, only rotation and translation, and the point is transformed along with the rest of the mesh.
Progress. I tried “activeNormVis[(i*2)+1].x = activeVertData[i].x + activeVertData[i].nx;” as per ZbuffeR’s suggestion, which seems to kinda fix it except now all my normals point in the same direction:
Unfortunately, because I’m loading from the .3ds format, I need to calculate my own normals, which could be the cause of this. Here’s the code I use to calculate normals:
The “Add the surface normal to every connected vertex” is not good. They can’t be the connected vertices, but are only the 3 vertices of your faces (the ones you used for calculating your cross product). These vertices can’t be the same.
Well, yes, that’s what I meant by it. It adds the value of the surface normal to each vertex that makes up that face, the same vertices which were used in the cross product to get the surface normal, which is then normalized afterward.
c) You calculate the sum of the tempNorm vectors for every surface. The length of this vector is proportional to the size of the surface, so smaller surfaces will have less influence on the resulting normal. This may be what you want, but often the surface normals are normalized before they are added to compute an average normal vector.