title: OpenGL Texture Projection Go Through Surface
body: <p>I'm new here and I'm also new to openGL. I want to develop some kind of viewer which load .ply files, images as texture, and project it to the mesh. I want to ask about the texture projection. The code of loading texture and set the projection of the texture can be seen bellow:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>This one works fine to some extend. Here are a screenshots of it:
  <a href="https://i.stack.imgur.com/Tnlwi.png" rel="nofollow noreferrer">The viewer form front. The pink ball is the 'projector' position</a></p>
  
  <p>The problem is that the projected texture went through the box and also leave a projection on the back side of the box:
  <a href="https://i.stack.imgur.com/phY73.png" rel="nofollow noreferrer">The texture also projected to the backside of the cube</a></p>
  
  <p>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.</p>