OpenGL 纹理投影穿过表面
OpenGL Texture Projection Go Through Surface
我是新来的,也是 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
那么,问题来了,如何让纹理的投影只出现在'projector'可见的面上呢?非常感谢。
So, the question is, how to make the projection of the texture only appears on the face that visible to the 'projector'? Thank you very much.
绘制盒子的背面时禁用纹理。
或者你希望盒子上出现阴影? OpenGL 不是在整个场景上运行的全局照明渲染器。事实上,OpenGL 只是一支美化的铅笔,一次绘制一个点、线或三角形,它们之间没有任何交互,除了它们对帧缓冲区中像素值的影响。
我是新来的,也是 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
那么,问题来了,如何让纹理的投影只出现在'projector'可见的面上呢?非常感谢。
So, the question is, how to make the projection of the texture only appears on the face that visible to the 'projector'? Thank you very much.
绘制盒子的背面时禁用纹理。
或者你希望盒子上出现阴影? OpenGL 不是在整个场景上运行的全局照明渲染器。事实上,OpenGL 只是一支美化的铅笔,一次绘制一个点、线或三角形,它们之间没有任何交互,除了它们对帧缓冲区中像素值的影响。