🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Interesting texture problem.

Started by
11 comments, last by gato 23 years, 11 months ago
I was doing the first Nehe Tutorials. Texture Mapping is really cool! As an extension of chapter 6, I wanted to put texture to a simple trapeze, to see how a texture is projected when the angles between vertexes are not 90°. So I wrote the following code: glBegin(GL_QUADS); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,3.0f); glTexCoord2f(0.0f,1.0f); glVertex3f(-3.0f,3.0f,3.0f); glTexCoord2f(1.0f,0.0f); glVertex3f(6.0f,-3.0f,3.0f); glTexCoord2f(1.0f,1.0f); glVertex3f(3.0f,3.0f,3.0f); glEnd(); But the texture is not well projected! There is a change of "direction" in the diagonal of the trapeze. I guess that''s ''cose the program interpoles triangle by triangle, and if we have polygons with 90° angles it makes no difference, but with a trapeze the texture is not good projected. How should I map the texture in these cases?
Advertisement
Hey gato,

OpenGL is not using perspective correct texture mapping. I don''t think OpenGL supports this, but I might be wrong...
I know this is not exactly what you''re asking, but it might be a solution? You can always split up your trapeze into multiple polygons - detail will improve but performance will drop...(bet you already thought of it)

- Bas

Thanks, that''s a solution, but are 8 triangles necessary just to normally fit a texture in a trapeze??
Only two should do.

If you have any quad (which a trapeze is), then you can split it into two triangles by one of its diagonals.

EL
----------------------------------------"Inash neteia haeg joa kavari quilm..." SD4
Actually, the splitting of the quad into two triangles is what causes the misalignment of the texture in the center. The more you split it up, the better it will appear. As for perspective correction for textures, I saw something on www.opengl.org once that explained how to fix this, can''t remember when or what it was called though. You might want to check through the tutorials and code snipets though.

Morgan
Yes, but with two I don''t know how to get a projection of the texture.

Maybe I didn''t explain the problem well, I''ll put an example: imagine the texture is an 8*8 table, like a chess table. I want to project it in a trapeze and mantein the lines rected (180 degrees in the trapeze diagonal) and the paralellism.

Do I really need to divide it in 8 triangles? I can''t believe it!
The last message was to answer Demon Lord.
Morgan, thanks, I''ll take a look.
Well, i''ve found a quite good solution to this problem. I''ll explain: by tweaking the texture coordinates, you''ll get correct perspective correction.

Example:
You have a polygon going from (3d space) xyz:
Vertex 1: 10,0,7
Vertex 2: 5,10,7
Vertex 3: -5,10,7
Vertex 4: -10,0,7
(ie a trapeze lying in the xy plane).

Using OpenGL, a such polygon would have the problem you described (change of direction on the diagonal, between vertex 1 and 3).

The solution is to keep the same angles between the edges, in the projection of the polygon on the texture. Thus.. if you use a 256x256 texture and tex. coords like:

Vertex 1'': 1.0 0.0
Vertex 2'': 0.7 1.0
Vertex 3'': 0.3 1.0
Vertex 4'': 0.0 0.0

You''ll get a correct result.

Y.
Ei! I tried it and...much better!! (although not perfect). Thanks, although I''m not sure to understand why 0.3 and 0.7... is it possible to put it in a generic form (equation) so that it works allways?

It''s pure geometry problem!

Tomorrow I''ll take a better look.
It''s not perfect because it''s not exactly 0.3 and 0.7 I just gave this coords quickly because i didn''t have much time. The idea is to keep the same angles between the edges in the original polygon, and in the projected polygon.

In other words.. if you have an angle of 60° between vertex1 and vertex2 in 3d.. you should have an angle of 60° between the same projected edges in texture space. That''s all.

So, if i take back my example.. without scaling.. you could directly use the coords. of the projected polygon on the XY plane.
Thus..

In 3d space:
Vertex 1: 10 0 7
Vertex 2: 5 10 7
Vertex 3: -5 10 7
Vertex 4: -10 0 7

Projected (remove the Z coord to project on the XY plane):
Vertex 1: 10 0
Vertex 2: 5 10
Vertex 3: -5 10
Vertex 4: -10 0

If you take the above values as the tex. coords, you should see tiling and correct perspective. However, in general, the tex. coords are from 0.0 to 1.0 (no tiling), so you should translate them and scale them. I guess it would give a translation of X=+10;Y=0 and a scale of X=1/20;Y=1/10.

The X translation of +10 is found from the vertex4, x=-10, so we need to add 10 to map it to 0. The scale is 1/(max-min).

So the exact values after translation/scale are:

Vertex 1: 1.0 0.0
Vertex 2: 0.75 1.0
Vertex 3: 0.25 1.0
Vertex 4: 0.0 0.0

I wasn''t that far

Anyway, if you want an algorithm, it''s actually quite easy. It''s very similar to texture coords. generation for lightmaps, and there are many good articles on this subject.
The idea is to find the max component value of the normal of the face, and to project the polygon either on the XY XZ or YZ plane depending of the result. Then find the min/max values of the coords in texture space, and deduce the translation and scale values.

Y.

This topic is closed to new replies.

Advertisement