Jump to content
  • Advertisement
Sign in to follow this  
Exide

OpenGL Using gl*Pointer with an interleaved array

This topic is 2890 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 am trying to draw a simple scene, a rotating textured sphere, using a VBO and glDrawArrays. It is written in Python using PyOpenGL.


glBindTexture(GL_TEXTURE_2D, vbo.texture.id)
glBindBuffer(GL_ARRAY_BUFFER, vbo.id)

glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)

glVertexPointer(3, GL_FLOAT, 4*(2+3+3), None)
glNormalPointer(GL_FLOAT, 4*(2+3+3), None)
glTexCoordPointer(2, GL_FLOAT, 4*(2+3+3), None)
#glInterleavedArrays(GL_T2F_N3F_V3F, 0, None)
glDrawArrays(GL_TRIANGLES, 0, vbo.data.size/(2+3+3))

glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glDisableClientState(GL_VERTEX_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, 0)

When running the code above I get this when I am trying to produce this. If I comment out the [font="Courier New"]glVertexPointer[/font], [font="Courier New"]glNormalPointer[/font] and [font="Courier New"]glTexCoordPointer[/font] and uncomment the [font="Courier New"]glInterleavedArrays[/font] everything renders as desired. I can also reduce the VBO down to just vertices and use [font="Courier New"]glVertexPointer(3, GL_FLOAT, 4*3, None)[/font] by itself and at least get the sphere to render. This leads me to believe the issue lies with the [font="Courier New"]pointer[/font] argument being passed to the gl*Pointer functions.

The PyOpenGL documentation on glVertexPointer says "[font="Arial, Helvetica, sans-serif"]If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target while a vertex array is specified, pointer is treated as a byte offset into the buffer object's data store.[/font][font="Arial, Helvetica, sans-serif"]"[/font] I have tried various values that I thought may be correct but they all produce varying undesired results.

Thoughts?

Share this post


Link to post
Share on other sites
Advertisement
Just get rid of that glInterleavedArrays, you don't need it and I believe it is old and deprecated, you can achieve what you want with just the pointers.

The problem I see with your code is this:

glVertexPointer(3, GL_FLOAT, 4*(2+3+3), None)
glNormalPointer(GL_FLOAT, 4*(2+3+3), None)
glTexCoordPointer(2, GL_FLOAT, 4*(2+3+3), None)
Assuming you have interleaved vertices with each vertex equal to 8 floats, then you need to specify where in the array the vertex/normal/texcoord is, via the last argument.

If your vertex array looks like this:

V = vertex
N = normal
T = texcoord

VVVNNNTT VVVNNNTT VVVNNNTT
(it doesn't have to be this way, but just as an example)

Then your pointers should be as follows (note the last argument)

glVertexPointer(3, GL_FLOAT, 4*(2+3+3), 0) //first vertex is offset 0 bytes from start of array
glNormalPointer(GL_FLOAT, 4*(2+3+3), sizeof(float)*3) //first normal is offset 12 bytes from start of array
glTexCoordPointer(2, GL_FLOAT, 4*(2+3+3), sizeof(float)*6) //first texcoord is offset 24 bytes from start of array

Share this post


Link to post
Share on other sites
[font="Courier New"]vbo.data[/font][font="Arial"] is an array of 32-bit floats with texture coordinates, normals and vertices (in that order) interleaved. With that in mind I tried the following:[/font]


glVertexPointer(3, GL_FLOAT, 4*(2+3+3), 4*(2+3))
glNormalPointer(GL_FLOAT, 4*(2+3+3), 4*2)
glTexCoordPointer(2, GL_FLOAT, 4*(2+3+3), 0)

[font="Arial"]However the result is just a black screen.[/font]


Just get rid of that glInterleavedArrays, you don't need it and I believe it is old and deprecated, you can achieve what you want with just the pointers.

Agreed. I was just using it to ensure the problem didn't lie in how I built the array.

Share this post


Link to post
Share on other sites
Hmm I'm not sure then, what you have looks correct for the pointers. Does it do anything if you use the same data, but only enable GL_VERTEX_ARRAY?

Also check glGetError if you havent.

Share this post


Link to post
Share on other sites

Hmm I'm not sure then, what you have looks correct for the pointers. Does it do anything if you use the same data, but only enable GL_VERTEX_ARRAY?

It still just produces a black scene.


Also check glGetError if you havent.

Absolutely. [font="Courier New"]glGetError[/font] is checked every frame however no errors are being returned.

Share this post


Link to post
Share on other sites
I believe I have resolved this problem.


glVertexPointer(3, GL_FLOAT, 4*(2+3+3), c_void_p(4*(2+3)))
glNormalPointer(GL_FLOAT, 4*(2+3+3), c_void_p(4*2))
glTexCoordPointer(2, GL_FLOAT, 4*(2+3+3), c_void_p(0))

As you can see casting the offset as a [font="Courier New"]ctypes.c_void_p[/font] explicitly does the trick.

When reading the PyOpenGL documentation it seemed to indicate that this casting would be done by the wrapper.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!