Need a little help Vertex Arrays

Started by
9 comments, last by ChaosX2 18 years, 3 months ago
I'm trying to understand vertex arrays but I can't seem to find a good tutorial on how to implement them. I have the following structures.

struct MeshVertex
{
	float x , y , z;
};
struct MeshTextureCoord
{
	float u , v;
};
struct MeshFace
{
	unsigned int a , b , c;

};

MeshVertex* m_vertices;
MeshVertex* m_normals;
MeshTextureCoord* m_texturecoords;
MeshFace* m_faces;

Each face is made up of three vertices and each vertex has a texture coord associated with it. However normals are done per face. m_vertices is indexed by faces .a, .b, and .c m_texturecoords is indexed by faces .a, .b, .c m_normals is indexed by faces index. I know to do the following:

glTexCoordPointer ( 2, GL_FLOAT, 0, m_texturecoords);
glVertexPointer   ( 3, GL_FLOAT, 0, m_vertices);
glNormalPointer   ( 3, GL_FLOAT, 0 , m_normals);

So when glDrawElements is called how do I get it to handle the way that the normals are indexed?
<a href="http://ruggles.hopto.org>My Personal Website
Advertisement
I don't know that there's a way to do it like that in OpenGL. You have to either:

a) Assign a normal to each vertex rather than each face, and have OpenGL index them all the same way. This will make it difficult to get 'faceted' shading though, such as you might want for a cube or polyhedron.

b) 'Detach' the faces, by which I mean duplicate each vertex as many times as there are faces incident on it, and assign each copy the normal from the corresponding face.

In short, you have to have one normal per vertex, just like with the texcoord and color array. How you manage it beyond that depends on what sort of shading you want.

[Disclaimer: I'm not an OpenGL expert, so there's at least a chance the above info is partially or wholly wrong.]
You can still achieve flat shading with glShadeModel(GL_FLAT) but jyk is correct, you cannot step the normal array at a different rate from the vertex array. In common usage this is not a problem since people tend to use vertex normals for smooth shading rather than facet normals. But if you are trying to render a simple cube, vertex arrays are not ideal.

I'm actually trying to render a 3ds model.
<a href="http://ruggles.hopto.org>My Personal Website
one more quick question, to calculate each vertex normal I do the following


  • Calculate the face normals for all polygons.
  • For each vertex:

    • Initialise the vertex normal to (0, 0, 0).
    • Loop through every face in the mesh. For each face, check to see if it uses the
      current vertex. If so, add this faces'' normal to the vertex normal.
    • Normalise the vertex normal.



So when you calculate the face normals do you normalize them before using them in the vertex normal calculation? Or do I normalize them after I've calculated the vertex normals?
<a href="http://ruggles.hopto.org>My Personal Website
You can use glEnable(GL_NORMALIZE) to force the implementation to renormalize the normals; this is useful when your modelview matrix scales the normals, too. If you are going to use this mode anyway, you can pass in unnormalized normals. But on some implementations this may have a performance hit, as it adds work to the lighting calculation.
Quote:Original post by ChaosX2
So when you calculate the face normals do you normalize them before using them in the vertex normal calculation? Or do I normalize them after I've calculated the vertex normals?
You can do it either way. (If you use the unnormalized face normals to compute the vertex normals, the vertex normals will be biased towards faces with larger surface area.)
Quote:Original post by jyk
Quote:Original post by ChaosX2
So when you calculate the face normals do you normalize them before using them in the vertex normal calculation? Or do I normalize them after I've calculated the vertex normals?
You can do it either way. (If you use the unnormalized face normals to compute the vertex normals, the vertex normals will be biased towards faces with larger surface area.)


What would be the recommended method for a FPS game?
<a href="http://ruggles.hopto.org>My Personal Website
Quote:Original post by ChaosX2
What would be the recommended method for a FPS game?
I don't do much modeling, so I don't really know. My guess is though that it won't matter that much. Triangles in a typical model will often (but not always) be of roughly uniform area, so the difference between the two methods will probably not be too noticable. And in any case, I doubt one would look more or less 'correct' than the other to the casual observer.

Again though, modeling isn't my area - maybe someone else will have a different answer.
Quote:Original post by ChaosX2

  • Loop through every face in the mesh. For each face, check to see if it uses the
    current vertex. If so, add this faces'' normal to the vertex normal.



Minor improvement for speed is

- Initialize all vertex normals to 0,0,0
- Loop through faces, calculate it's normal and then add it to all of its vertex-normals that it indexes to
- Normalize all vertex-normals

At least that's how I do it.. :)

ch.

This topic is closed to new replies.

Advertisement