|
Texture mapping a trapezoid |
HoHo
Member #4,534
April 2004
|
I've been struggling with it for a few hours today. I have very little OpenGL experience and I wouldn't be surprised to know there is something really basic I'm missing. Problem is simple: For the reference, here are the original quad, trapezoid I get and trapezoid-like thing I would like to get: Is something like this at all possible with two triangles? If not then how can I achieve such look. In the end the program will look 2D top-down. Performance isn't probably a problem but ease of implementation would be a major plus Code to draw the trapezoid is this. To get a quad just replace 0.2 with 1.0:
Initialization code: glEnable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); glViewport((width - side) / 2, (height - side) / 2, side, side); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.5, +1.5, +1.5, -1.5, 4.0, 15.0); glMatrixMode(GL_MODELVIEW); The code is a part of a QT OpenGL example (opengl/textures). I can post the entire project if you like, just let me know. __________ |
Tobias Dammers
Member #2,604
August 2002
|
You need to specify z coordinates. With perspective-corrected texture mapping, the two triangles should look OK. --- |
HoHo
Member #4,534
April 2004
|
You mean I should simply use a quad with one edge having Z coordinates that are way back to make it look like a trapezoid? Are there any other ways? I would like is the polygon to be on one plane on the screen, it would make things a bit easier for me. __________ |
Bob
Free Market Evangelist
September 2000
|
Break up your quad manually into 4 triangles by picking the center (or center of mass) of the trapezoid. The GPU will subdivide quads into 2 triangles, but you may not like the axis of separation. Using perspective will not help you here. The problem is really that the 2 triangles generated for your quad don't have the same area, and thus will distort the texture differently. -- |
HoHo
Member #4,534
April 2004
|
Now that I think of it, dividing to four triangles should work well. Though I didn't go that way since calculating texture coordinates might be a bit tricky (read: I'm too lazy to work out the math). In the end I did what Tobias said and made a cilinder I view top-down. It works really well for me. End result is here: Thanks for all your help! Changed code is like this:
It is rather inefficient and looks ugly but it will get some serious refactoring later. __________ |
Tobias Dammers
Member #2,604
August 2002
|
Quote: Using perspective will not help you here. The problem is really that the 2 triangles generated for your quad don't have the same area, and thus will distort the texture differently.
Provided the unprojected shape (in 3-space) is a rectangle, it does help, as you see. The triangles do have equal area in 3-space, so the texcoords align nicely. After proper perspective correction, this is still the case. --- |
Bob
Free Market Evangelist
September 2000
|
Quote: Provided the unprojected shape (in 3-space) is a rectangle, it does help, as you see. But it's not. The question was: How to texture a trapezoid. It was not "how to texture a tilted rectangle". The math for the two is different and the resulting images will also be different. -- |
Tobias Dammers
Member #2,604
August 2002
|
From the screenshots, I was assuming that a tilted square were the right solution, since that is what we see in mockshot 3. --- |
|