Jump to content
• Advertisement

# Need a little help Vertex Arrays

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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?

#### Share this post

##### Share on other sites
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.]

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
I'm actually trying to render a 3ds model.

#### Share this post

##### Share on other sites
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?

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
Quote:
 Original post by ChaosX2So 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.)

#### Share this post

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by ChaosX2So 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?

#### Share this post

##### Share on other sites
Quote:
 Original post by ChaosX2What 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.

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5
• Advertisement

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011153
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!