我正在尝试使用gltexcoordpointer和gldrawarrays构造立方体(我在环境中使用PyGL)。我可以使多维数据集成功显示(尽管阴影为白色),但是我不知道如何将纹理应用于多维数据集。我怀疑问题出在我的“ draw_cube()”函数中。
有没有办法使用openGL 2.x函数解决此问题(我只能使用固定的管道函数)?我的代码如下。
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from PIL import Image
import numpy as np
def run_scene():
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutCreateWindow("textured cube test")
glutDisplayFunc(draw_cube)
glMatrixMode(GL_PROJECTION)
gluPerspective(40, 1, 1, 40)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, 0, 10,
0, 0, 0,
0, 1, 0)
glPushMatrix()
glutMainLoop()
return
def draw_cube():
""" verticie coordinates """
verts = np.array([
[[0, 0, 0],
[1, 0, 0],
[1, 1, 0]],
[[0, 0, 0],
[1, 1, 0],
[0, 1, 0]],
[[0, 0, 1],
[1, 0, 1],
[1, 1, 1]],
[[0, 0, 1],
[1, 1, 1],
[0, 1, 1]],
[[0, 1, 0],
[1, 1, 0],
[0, 1, 1]],
[[0, 1, 1],
[1, 1, 1],
[1, 1, 0]],
[[0, 0, 0],
[1, 0, 0],
[0, 0, 1]],
[[0, 0, 1],
[1, 0, 1],
[1, 0, 0]],
[[1, 0, 0],
[1, 0, 1],
[1, 1, 1]],
[[1, 0, 0],
[1, 1, 0],
[1, 1, 1]],
[[0, 0, 0],
[0, 0, 1],
[0, 1, 1]],
[[0, 0, 0],
[0, 1, 0],
[0, 1, 1]]])
""" texture coordinates """
texCoords = np.array([
[[0,0],
[1,0],
[1,1],
[0,1]],
[[0,0],
[1,0],
[1,1],
[0,1]],
[[0,0],
[1,0],
[1,1],
[0,1]],
[[0,0],
[1,0],
[1,1],
[0,1]],
[[0,0],
[1,0],
[1,1],
[0,1]],
[[0,0],
[1,0],
[1,1],
[0,1]],
])
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
texture_id = load_image('podocytes_and_nuclei.tif')
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointerf(verts)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(2, GL_FLOAT, 0, texCoords[0]);
textureID = glGenTextures(1)
glClientActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, textureID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_id.shape[0], texture_id.shape[1], 0,GL_RGBA, GL_UNSIGNED_BYTE, texture_id)
glDrawArrays(GL_TRIANGLES, 0, np.product(verts.shape[:-1]))
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glPopMatrix()
glutSwapBuffers()
return
""" code for converting an image into a numpy array """
def load_image( imgpath ):
img = Image.open( imgpath )
img.load()
img_data = np.asarray( img, dtype="ubyte" )
img_data_RGBA = np.concatenate((img_data, np.full((512, 512,1), 255, dtype="ubyte")), axis=2)
return img_data_RGBA
if __name__ == '__main__':
run_scene()
答案 0 :(得分:1)
您必须启用2D纹理化,请参见glEnable
:
glEnable(GL_TEXTURE_2D)
设置缩小过滤器(GL_TEXTURE_MIN_FILTER
),请参见glTexParameter
:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
定义纹理坐标属性数组,如下所示:
glTexCoordPointer(2, GL_FLOAT, 0, texCoords)
或这个
glTexCoordPointerf(texCoords)
顶点坐标是三角形,但纹理坐标属性是四边形。
更改纹理坐标。纹理坐标属性应与顶点坐标一样多:
texCoords = np.array([
[[0,0], [1,0], [1,1]], [[0,0], [1,1], [0,1]],
[[0,0], [1,0], [1,1]], [[0,0], [1,1], [0,1]],
[[0,0], [1,0], [1,1]], [[0,0], [1,1], [0,1]],
[[0,0], [1,0], [1,1]], [[0,0], [1,1], [0,1]],
[[0,0], [1,0], [1,1]], [[0,0], [1,1], [0,1]],
[[0,0], [1,0], [1,1]], [[0,0], [1,1], [0,1]]
])
此外,您必须启用depth test:
glEnable(GL_DEPTH_TEST)