Posted 13 October 2006 - 07:21 PM
Since you're doing the actual screenspace projection yourself, you need to manually specify the homogenous texture coordinate yourself so that OpenGL knows you need perspective, which is simple via the API:
Simply call glTexCoord4f(s,t,0,q) instead of glTexCoord2f(s,t) and supply the homogeneous coordinate 'q' for the perspective transformation.
Okay, so how do you calculate the 'q', then? Well it turns out that this is easy if you know some information about your perspective transformation. Unfortunately this will be extremely difficult for your case without more information, but I'll explain the theory anyway:
Given a 3d vertex ('x,'y,'z), OpenGL will extend it to a 4d vector: ('x,'y,'z, 1). Given a 2d vertex ('x, 'y), OpenGL will extend it to a 4d vector ('x,'y,0,1)
The view-space vertex (ie. the above 4d coordinate transformed by the modelview matrix) will be be further transformed by the projection matrix (ie. a matrix setup via gluPerspective) to yield (x,y,z,w). In cases where there is no perspective, 'w' will be 1.
The final coordinate that is passed on to be transmitted into window coordinates is (x,y,z,w) multiplied by '1/w'. ie (x/w, y/w, z/w).
(Remember, in cases where there is no perspective, 'w' will be 1 and will yield the original vector (x,y,z) still)
Additionally, the 1/w term is used to perspective-correct the texture coordinate that you have given OpenGL. If you gave (s,t) to OpenGL, the final coordinate that gets passed down the pipeline is also multiplied by 1/w, ie. it will result in the coordinate: (s/w, t/w, 0, 1/w).
Now, your current situation is as follows: your vertex position is already 'perspective-corrected'. Ie you are passing "post-projection" screen coordinates (x,y) to OpenGL, but you are not passing down perspective correctable texture coordinates (ie. ones with a valid 'q' term)
To get the perspective correct texture coordinate you will need the 'w' term for the projected vertex and pass down the (s/w,t/w,0,1/w) instead of the (s,t) that you normally would have.
This is not an easy task in your case - as calculating 'w' is extremely non-trivial without:
1.) a concept of how far each vertex is from the camera/eyepoint.
2.) a projection transformation that would performs the *EXACT SAME* perspective transformation that you used to obtain your already projected screen coordinates.
Sorry to be the bearer of such horrid news, but, for the most part I think you'll find things easier if you just build your quads in 3d and let OpenGL do the perspective calculations for you. Let the API work for you! :)
What you need is perspective correction - this normally involves giving the rasterization system (in this case OpenGL) some kinda perspective matrix in the 3d case and letting it figure out the perspective correct texture mapping itself.