glDrawElements

Started by
7 comments, last by rick_appleton 18 years, 4 months ago
Hi - I've seen a few issues on this topic but I haven't found a solution to my problem, hopefully someone can shed some light - I'm using glDrawElements to render polygons to the screen. It works well if used just for vertex coordinates, but what about if I want to include normal data, in which each vertex could have 3 normals associated, or other arrays (color, texture mapping, etc). Someone previously suggested putting 3 copies of each vertex's coordinates into the array, but then that defeats the purpose of using shared vertex data and glDrawElements - I could just use glDrawArrays. Is there a way to use glDrawElements to draw shared vertex information for coordinates but also allow to specify 3 values (per face) for color, texture, and normal data? Without making 2 more copies of the coordinate information and defeating having shared information? What is the standard way of rendering a high poly count model to the screen (eg one imported from a 3d package) Thanks!
Advertisement
I'm not an OpenGL expert, but I don't think you can do that; there has to be a one-to-one correspondence between the elements of the different arrays. Obviously your model will be polygonal no matter what, but in some cases you may want to have faces share vertices to get a smooth shading effect (say, for a sphere mesh), and in some cases you'll want to 'detach' the faces to get a faceted shading effect (like for a box). In the latter case, I don't know that there's any way around duplicating the vertex position data.

Again, though, I'm not an expert on rendering, so I may be wrong about the above.
Quote:Original post by Megaboz
Hi - I've seen a few issues on this topic but I haven't found a solution to my problem, hopefully someone can shed some light -

I'm using glDrawElements to render polygons to the screen. It works well if used just for vertex coordinates, but what about if I want to include normal data, in which each vertex could have 3 normals associated, or other arrays (color, texture mapping, etc).

Someone previously suggested putting 3 copies of each vertex's coordinates into the array, but then that defeats the purpose of using shared vertex data and glDrawElements - I could just use glDrawArrays.

Is there a way to use glDrawElements to draw shared vertex information for coordinates but also allow to specify 3 values (per face) for color, texture, and normal data? Without making 2 more copies of the coordinate information and defeating having shared information? What is the standard way of rendering a high poly count model to the screen (eg one imported from a 3d package)

Thanks!


Not to long ago there was a very similiar issue.. The answer is NO.

If u have a cube with 6 faces how many vertex informations do u have? not 8 no, 6*4, and each one needs to be passed in on its own.

But what you could do is, setup your vertex pointer for the 4 vertices required per face and then just setup your colour pointer for the 4 values, then for the next face u will reuse 2 of the previous vertices but make the pointer point to the 4 new colour values.

It get unpractical at a point.
----------------------------

http://djoubert.co.uk
Thanks for the responses, sounds like I'll just have to include coordinate information 3 times for each vertex, I suppose it's not that big of a deal.
Quote:Original post by dawidjoubert
But what you could do is, setup your vertex pointer for the 4 vertices required per face and then just setup your colour pointer for the 4 values, then for the next face u will reuse 2 of the previous vertices but make the pointer point to the 4 new colour values.

It get unpractical at a point.


I'm not sure if you're actually suggesting to do it like this, or if you're merely indicating how it could possibly be done. The problem with doing it like this is that you lose the benefit of glDrawElements over immediate mode, because you are drawing extremely small batches. You'll be better off to do it in immediate mode.
Look up glColorPointer, glNormalPointer and their family of gl*Pointer functions on MSDN or your OpenGL book (or whatever reference you use).

Not to forget the glEnableClientState function.
Well again I suggest using multiple index arrays.

For instance, a cube have 8 vertices and 6 normals:

vertices = [v0x v0y v0z v1x v1y v1z v2x v2y v2z v3x v3y v3z v4x v4y v4z v5x v5y v5z v6x v6y v6z v7x v7y v7z]
normals = [n0x n0y n0z n1x n1y n1z n2x n2y n2z n3x n3y n3z n4x n4y n4z n5x n5y n5z]

then we could specify two index arrays for each array (first triangle):

vertex index = [0 1 2 ... ]
normal index = [0 0 0 ... ]

This way one could save memory on gpu. I can see only one problem, fast memory access...
Quote:Original post by Promag
Well again I suggest using multiple index arrays.

For instance, a cube have 8 vertices and 6 normals:

vertices = [v0x v0y v0z v1x v1y v1z v2x v2y v2z v3x v3y v3z v4x v4y v4z v5x v5y v5z v6x v6y v6z v7x v7y v7z]
normals = [n0x n0y n0z n1x n1y n1z n2x n2y n2z n3x n3y n3z n4x n4y n4z n5x n5y n5z]

then we could specify two index arrays for each array (first triangle):

vertex index = [0 1 2 ... ]
normal index = [0 0 0 ... ]

This way one could save memory on gpu. I can see only one problem, fast memory access...


But the glDrawElements function only takes 1 array of indices, how can I specify both a coordinate index array and a normals index array? I was under the impression that you just gave it a single index array and it retrieved corrresponding values from every enabled list using those index values.

Quote:Original post by Megaboz
Quote:Original post by Promag
Well again I suggest using multiple index arrays.

For instance, a cube have 8 vertices and 6 normals:

vertices = [v0x v0y v0z v1x v1y v1z v2x v2y v2z v3x v3y v3z v4x v4y v4z v5x v5y v5z v6x v6y v6z v7x v7y v7z]
normals = [n0x n0y n0z n1x n1y n1z n2x n2y n2z n3x n3y n3z n4x n4y n4z n5x n5y n5z]

then we could specify two index arrays for each array (first triangle):

vertex index = [0 1 2 ... ]
normal index = [0 0 0 ... ]

This way one could save memory on gpu. I can see only one problem, fast memory access...


But the glDrawElements function only takes 1 array of indices, how can I specify both a coordinate index array and a normals index array? I was under the impression that you just gave it a single index array and it retrieved corrresponding values from every enabled list using those index values.



This is correct. OpenGL uses 1 index to to index all arrays. Hence my previous post.

This topic is closed to new replies.

Advertisement