Skewed/Sheared Texture Mapping in OpenGL

Started by
0 comments, last by FReY 17 years, 6 months ago
Hi, I have recently come upon a certain task I need to do for my game. It seemed easy at first, but it turned out to be a bit trickier than I imagined. What I need to do is very simple: I have to render a trapezoid with 3 of the 4 sides at 90 degree angles, except the last one. It has to be correctly textured/gradient filled. Imagine a flat square facing you in 3D space, rotate it a bit, and that's exactly what I need. However, my game is 2D (for the most part) and all the points (including this trapezoid) lie on the z-plane. So what I need is some sort of skewed/sheared (I'm not sure which term correctly describes this) texture mapping (or shading, if I use basic colors and no textures). The most obvious solution to try would be something like this, of course:
glBegin(GL_QUADS);
	glTexCoord2f(0, 0); glVertex2f(0, 0);
	glTexCoord2f(1, 0); glVertex2f(100, 0);
	glTexCoord2f(1, 1); glVertex2f(100, 100);
	glTexCoord2f(0, 1); glVertex2f(0, 100 / 2);
glEnd();
Unfortunately, the quad gets broken down into 2 triangles and the results are not as desired: To be honest, I'm not exactly sure how to properly accomplish something like this in OpenGL. I've tried searching around but didn't really find anything (maybe it's because I don't know the right term for this kind of transformation). Reading the texture mapping section of the red book didn't help either. The only solution I could think of, was to modify the texture matrix to perform this transformation, so something like this:
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glGetDoublev(GL_TEXTURE_MATRIX, m);
m[0] = 2.0;
m[5] = 2.0;
m[3] = 1.0;
glLoadMatrixd(m);
// Note: glTexCoord2f(0, 1); has to be changed to glTexCoord2f(0, 0.5);
Did the trick. Only thing is, the exact coordinates of my trapezoid will be determined at run time (once per frame), so this matrix would have to be recomputed for every different angle that the top edge makes... This hardly seems like an elegant solution, anyway. I was wondering if anyone could point me towards a better/simpler solution, if there is one. Note that I'm trying to minimize the system requirements, so using shaders for such a simple task, for example, might not be a better solution for me. --- Thanks, shurcooL`
Advertisement
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.

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! :)
do unto others... and then run like hell.

This topic is closed to new replies.

Advertisement