# OpenGL Vertices, Normals and Indices

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

## Recommended Posts

I'm trying to write a basic python script to take collada geometry data and put it in buffer format for openGL. I've ran into a problem that doesnt make sense to me.
I have
7858 vertices
9519 normals

It doesn't make sense to have more normals than vertices right? In order to specify indexes into the buffers for openGL both the vertex buffer and normal buffer must be the same size right?
Is there simply repeated data in the normals of the collada file that need to be removed?
Is it more complicated then just shifting data around? (i.e. do normals have to be recalculated?)

Any insight would help. Thanks in advance.

##### Share on other sites
If the format contains 1 normal per vertex it's possible, the number of indices should equal the number of normals then. For example, you have two triangles connected to each other trough a shared vertex; you'd have 5 unique vertices, 6 vertex indices and 6 normals.

##### Share on other sites

A cube has 8 vertices, but there needs to be 3 normals at each of those vertices (for a total of 24 normals) if you want to get accurate shading results. Have a look at this image for an example of that. Cube with proper normals. These additional normals are typically created when the angle between two normals on the same vertex break a certain threshold.

If you had just one averaged normal at each vertex then you get this and that's going to give you some funky shading.

##### Share on other sites
Ahhhh. Thanks guys that helps out alot. I didn't think about having multiple normals per vertex for edges, but that makes sense now for lighting calculations. I had been using Mesh Labs to convert my content and I think they do average the normals in order to make #vertices = #normals. This will give bad lighting you say? The way I have been rendering models in openGL requires all of the buffers to be the same size because each buffer is indexed the same. What is the proper way to have multiple normals per vertex using this format?

I suppose there are two options
1)average them in some way (may give bad lighting)
2)repeat vertices for each unique normal (will be slower at runtime). not 100% sure this method would work, the newest vertex may overwrite the previous one?

Thanks for the help!

##### Share on other sites
A better way to think of it is you have more normals than *positions*, and positions != vertices. A vertex is a unique combination of a postiion, normal, and whatever other attributes belong to the vertex (uv, color, etc)

Typically data formats (like collada) have a set of indices per vertex - so for a single vertex, it will index separately into the position stream, normal stream, etc. This is different than what GPUs support, which is a single index per vertex that indexes into every attribute stream (typically interleaved) with the same index.

When building an interleaved vertex buffer for a GPU, you'll end up duplicating attributes across vertices. My typical approach is to create an 'index tuple' per vertex when reading the collada data, the index tuple contains the index for each attribute into the source data for that vertex - from there it's easy to take each index tuple and create a unique vertex for the GPU.

##### Share on other sites
Okay, let me try to make sure I understand what you're saying.

For an index tuple you will have a format similar to collada, maybe something like
v = vertex
t = textureCoord
n = normal

(v1, v2, v3, n1, n2, n3, t1, t2)

would represent one unique vertex

Then one could use those tuples to generate their index buffer. Is it possible to save memory by taking advantage of repeated data via proper indexing? If the tuples are unique I would think the answer is no. Thanks for all of the help. I just want to avoid taking up extra memory by having a brute force script!

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 30
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631408
• Total Posts
2999922
×