本文介绍了PyOpenGL:glVertexPointer()偏移量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的顶点交错成一个numpy数组(dtype = float32),如下所示:... tu,tv,nx,ny,nz,vx,vy,vz,...

My vertices are interleaved in a numpy array (dtype = float32) like this: ... tu, tv, nx, ny, nz, vx, vy, vz, ...

渲染时,我这样调用gl * Pointer()(我之前启用了数组):

When rendering, I'm calling gl*Pointer() like this (I have enabled the 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 )

结果是什么也没有渲染.但是,如果我组织数组以使顶点位置是第一个元素(... vx,vy,vz,tu,tv,nx,ny,nz,...),则在渲染但纹理时会得到正确的顶点位置坐标和法线显示不正确.

The result is that nothing renders. 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 correct positions for vertices while rendering but texture coords and normals aren't rendered correctly.

这使我相信我没有正确设置指针偏移量.我应该如何设置?我在C ++中的其他应用程序中使用的代码几乎完全相同,并且可以正常工作.

This leads me to believe that I'm not setting the pointer offset right. How should I set it? I'm using almost the exact same code in my other app in C++ and it works.

推荐答案

在python中,您无法执行指针运算.您尝试执行的操作仅适用于C/C ++.

In python, you can't do pointer arithmetic. What you're trying to do only works for C/C++.

使用普通的Python列表:

With normal 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

使用numpy数组:

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

看看您想要的是什么:在特定位置启动数组.

See how neither does what you want: starting the array at a certain position.

我认为您基本上有两种选择:

I think you have basically two options:

  1. 不要使用交错数组.这确实具有一个优势:当您只需要更新顶点(如骨骼动画中的顶点)时,就不需要更新纹理坐标.
  2. 使用切片可能为您工作.
  1. Don't use interleaved arrays. This does have one advantage: when you only need to update the vertices (like in bone animations), you don't need to update the texture coordinates.
  2. Using slices might work for you.

赞:

>>> 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 a correct stride, you can probably get it to work.

这篇关于PyOpenGL:glVertexPointer()偏移量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:51
查看更多