【发布时间】:2017-07-03 08:28:31
【问题描述】:
我是新来的,我也是 openGL 的新手。我想开发某种查看器,它可以加载 .ply 文件、图像作为纹理,并将其投影到网格中。我想问一下纹理投影。加载纹理和设置纹理投影的代码如下:
def loadTexture():
# load the image
imdata = 0
imname = 'Cropped_Image26904_2.jpg'
texture = glGenTextures(1)
im = cv2.imread(imname)
imdata = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
# bind texture
glBindTexture(GL_TEXTURE_2D, texture)
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, imdata.shape[1],
imdata.shape[0], 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, imdata)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_BORDER)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_BORDER)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_BORDER)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_BORDER)
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, imdata.shape[1],
imdata.shape[0], GL_RGB, GL_UNSIGNED_BYTE, imdata)
# set the 'projector' location that project the texture
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluPerspective(fov,
float(glutGet(GLUT_WINDOW_WIDTH))/glutGet(GLUT_WINDOW_HEIGHT), .1,
1e8)
gluLookAt(0,0,1, 0,0,0, 0,1,0)
mat3 = glGetFloatv(GL_PROJECTION_MATRIX)
# mapping the texture
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
glTexGenfv(GL_S, GL_EYE_PLANE, mat3[0])
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
glTexGenfv(GL_T, GL_EYE_PLANE, mat3[1])
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
glTexGenfv(GL_R, GL_EYE_PLANE, mat3[2])
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
glTexGenfv(GL_Q, GL_EYE_PLANE, mat3[3])
# enable the texture
glEnable(GL_TEXTURE_2D)
glEnable(GL_TEXTURE_GEN_S)
glEnable(GL_TEXTURE_GEN_T)
glEnable(GL_TEXTURE_GEN_R)
glEnable(GL_TEXTURE_GEN_Q)
这个在某种程度上可以正常工作。这是它的截图: The viewer form front. The pink ball is the 'projector' position
问题是投影的纹理穿过了盒子并且还在盒子的背面留下了投影: The texture also projected to the backside of the cube
那么,问题来了,如何让纹理的投影只出现在“投影仪”可见的面上?非常感谢。
【问题讨论】:
标签: python opengl textures projection