I am using interleaved VBO
to display geometry using Python and PyOpenGL. The VBO consists of vertices, normals and colors:
[vx1, vy1, vz1, nx1, ny1, nz1, R1, G1, B1, vx2...]
Drawing is made with the code:
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.vbo_id)
v_pointer = None
n_pointer = ctypes.c_void_p(12)
c_pointer = ctypes.c_void_p(24)
v_stride = 24
n_stride = 24
c_stride = 24
glVertexPointer(3,
GL_FLOAT,
v_stride,
v_pointer)
glNormalPointer(GL_FLOAT,
n_stride,
n_pointer)
glColorPointer(3,
GL_FLOAT,
c_stride,
c_pointer)
glDrawArrays(GL_TRIANGLES, #POINTS
0,
self.len_)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) # reset
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glDisableClientState(GL_VERTEX_ARRAY)
The displayed geometry should be a simple cube but all I get is randomly oriented triangles (see figure). I was reading about the glVertexPointer()
problems in PyOpenGL here and found the solution with ctypes.c_void_p()
but I am not really sure how to implement it. Can someone point out what am I doing wrong or missing here? Or how can I fix this?
Based on the documentation the stride
parameter is offset between 2 consecutive arrays of the same type (vertices, normals, colors...) (in bytes) in VBO
array. Or not?
I have solved the problem about normals. The code is:
v_pointer = ctypes.c_void_p(0) # or None
n_pointer = ctypes.c_void_p(12)
c_pointer = ctypes.c_void_p(24)
v_stride = 36
n_stride = 36
c_stride = 36
But I still have problems with colors as the model is not colorful? :) Any ideas are much appreciated.