代码之家  ›  专栏  ›  技术社区  ›  Resindra Aji

OpenGL纹理投影穿过曲面

  •  0
  • Resindra Aji  · 技术社区  · 7 年前

    我是新来的,我也是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

    所以,问题是,如何使纹理的投影只出现在“投影仪”可见的脸上?非常感谢你。

    1 回复  |  直到 7 年前
        1
  •  1
  •   datenwolf    7 年前

    所以,问题是,如何使纹理的投影只出现在“投影仪”可见的脸上?非常感谢你。

    背面