问题描述
def drawCicrcle(x,y,z,radius,numSides):
numVertices = numSides + 2
doublePi = 2.0 * math.pi
circLeverticesX = np.array([],dtype='f')
circLeverticesY = np.array([],dtype='f')
circLeverticesZ = np.array([],dtype='f')
circLeverticesX = np.insert(circLeverticesX,x)
circLeverticesY = np.insert(circLeverticesX,y)
circLeverticesZ = np.insert(circLeverticesX,z)
for i in range(1,numVertices):
circLeverticesX = np.append(
circLeverticesX,x + (radius * math.cos(i * doublePi / numSides)))
circLeverticesY = np.append(
circLeverticesY,y + (radius * math.sin(i * doublePi / numSides)))
circLeverticesZ = np.append(circLeverticesZ,z)
allCircLevertices = np.array([],dtype='f')
for i in range(0,numVertices):
allCircLevertices = np.insert(
allCircLevertices,i*3,circLeverticesX[i])
allCircLevertices = np.insert(
allCircLevertices,(i*3) + 1,circLeverticesY[i])
allCircLevertices = np.insert(
allCircLevertices,(i*3) + 2,circLeverticesZ[i])
vboc = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,vboc)
glBufferData(GL_ARRAY_BUFFER,allCircLevertices,GL_STATIC_DRAW)
glVertexAttribPointer(0,3,GL_FLOAT,False,sizeof(ctypes.c_float)*9,ctypes.c_void_p(36))
glDrawArrays(GL_TRIANGLE_FAN,numVertices)
然后我拨打我的主drawCicrcle(-0.5,0.5,0.0,0.18,360)
我想念什么?
解决方法
circleVerticesX = np.array([numVertices],dtype='f')
并没有达到您的期望。它会创建一个 numpy 数组,其中的单个元素的值为numVertices
(请参见numpy.array
)。
使用顶点坐标创建一个列表,并从该列表创建一个numpy数组:
vertex_list = [...]
# [...]
allCircleVertices = np.array([vertex_list],dtype='f')
功能drawCicrcle
:
def drawCicrcle(x,y,z,radius,numSides):
numVertices = numSides + 2
doublePi = 2.0 * math.pi
vertex_list = [x,z]
for i in range(1,numVertices):
vertex_list.append(x + (radius * math.cos(i * doublePi / numSides)))
vertex_list.append(y + (radius * math.sin(i * doublePi / numSides)))
vertex_list.append(z)
allCircleVertices = np.array([vertex_list],dtype='f')
vboc = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,vboc)
glBufferData(GL_ARRAY_BUFFER,allCircleVertices,GL_STATIC_DRAW)
glVertexAttribPointer(0,3,GL_FLOAT,False,3*sizeof(ctypes.c_float),ctypes.c_void_p(0))
glDrawArrays(GL_TRIANGLE_FAN,numVertices)
或者使用numVertices*3
个元素(参见numpy.empty
)创建一个空的 numpy 数组,并将顶点坐标分配给该数组的字段:
allCircleVertices = np.array([vertex_list],dtype='f')
allCircleVertices[0:3] = [x,z]
# [...]
功能drawCicrcle
:
def drawCicrcle(x,numSides):
numVertices = numSides + 2
doublePi = 2.0 * math.pi
allCircleVertices = np.empty((numVertices*3),dtype='f')
allCircleVertices[0:3] = [x,numVertices):
vx = x + (radius * math.cos(i * doublePi / numSides))
vy = y + (radius * math.sin(i * doublePi / numSides))
allCircleVertices[i*3:i*3+3] = [vx,vy,z]
vboc = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER,numVertices)