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:
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);
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:
m = 2.0;
m = 2.0;
m = 1.0;
// 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.