PyOpenGL: glVertexPointer () offset problem

My vertices alternate in the numpy array (dtype = float32) as follows: ... tu, tv, nx, ny, nz, vx, vy, vz, ...

When rendering, I call gl * Pointer () like this (I included arrays before):

stride = (2 + 3 + 3) * 4 glTexCoordPointer( 2, GL_FLOAT, stride, self.vertArray ) glNormalPointer( GL_FLOAT, stride, self.vertArray + 2 ) glVertexPointer( 3, GL_FLOAT, stride, self.vertArray + 5 ) glDrawElements( GL_TRIANGLES, len( self.indices ), GL_UNSIGNED_SHORT, self.indices ) 

As a result, nothing is displayed. However, if I organize my array so that the vertex position is the first element (... vx, vy, vz, tu, tv, nx, ny, nz, ...) I get the correct positions for the vertices when rendering, but the texture coords and normals are not displayed correctly.

This makes me think that I am not setting the pointer offset correctly. How do I install it? I use almost the same code in another C ++ application and it works.

+4
source share
4 answers

In python, you cannot do pointer arithmetic. What you are trying to do works only for C / C ++.

With a regular Python list:

 >>> array = [1, 2, 3, 4] >>> array [1, 2, 3, 4] >>> array + 2 Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: can only concatenate list (not "int") to list 

With numpy arrays:

 >>> import numpy >>> a = numpy.array([1, 2, 3]) >>> a + 2 array([3, 4, 5]) 

See how and what you want: starting the array at a specific position.

I think you have basically two options:

  • Do not use striped arrays. This has one advantage: when you only need to update the vertices (for example, in the bone animation), you do not need to update the texture coordinates.
  • Using slices may work for you.

Like this:

 >>> a = numpy.array(range(10)) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> a[3:] array([3, 4, 5, 6, 7, 8, 9]) 

Combine this with the right step, you can make it work.

+3
source

I ran into a similar problem and found that dropping the offset in ctypes.c_void_p did the trick.

 from ctypes import c_void_p t_size = self.vertArray.itemsize * 2 n_size = self.vertArray.itemsize * 3 t_offset = c_void_p(0) n_offset = c_void_p(t_size) v_offset = c_void_p(t_size+n_size) glTexCoordPointer( 2, GL_FLOAT, stride, t_offset ) glNormalPointer( GL_FLOAT, stride, n_offset ) glVertexPointer( 3, GL_FLOAT, stride, v_offset ) glDrawElements( GL_TRIANGLES, len( self.indices ), GL_UNSIGNED_SHORT, self.indices ) 
+3
source

I answer my question because I found an alternative way to achieve the correct result. Instead of calling gl * Pointer (), I called:

 glInterleavedArrays( GL_T2F_N3F_V3F, stride, self.vertArray ) 

I'm still interested in finding a solution that works with gl * Pointer ().

0
source

From this link: http://www.opengl.org/sdk/docs/man/xhtml/glTexCoordPointer.xml

step

Specifies the byte offset between consecutive sets of texture coordinates. If stride is 0, the elements of the array are understood as tightly packed. the initial value is 0.

Are you sure that in your case the step is (2 + 3 + 3) * 4?

0
source

Source: https://habr.com/ru/post/1307547/


All Articles