PDA

View Full Version : sideways textures



pmetcher
18-12-2003, 02:41 AM
Why it is that I see all my textures coming out sideways on faces in my model. I am using OpenGL with the following code;

glBindTexture(GL_TEXTURE_2D, Texture[Idx]);
glBegin(GL_QUADS);

glTexCoord2f(0,0);
glVertex3f(X, Y, Z);
glTexCoord2f(U, 0);
glVertex3f(X, Y, Z);
glTexCoord2f(U, V);
glVertex3f(X, Y, Z);

glEnd();

What is going on here?

Alimonster
18-12-2003, 08:57 AM
(Sorry about not responding to your email yesterday dude, but I fancied an early night.)

The answer involves the texture coordinates (glTexCoord...). Each texture coordinate pair comes before an actual vertex, and specifies how that vertex relates to the texture map.

The texture coordinate (0, 0) is the bottom-left of the image, with the y value increasing up-the-way as with the main coordinate system. The only pitfall here is that most bitmaps store their scanline images upside-down in memory (don't ask me why), which means that the texture coordinates may seem "flipped" from their usual values (as in (0,0) being top-left and y going down, as standard in graphics). However, this is not right and is only due to a (strange) quirk of bitmaps.

Uh, if my memory is correct, which I hope it is!

So, the simplest way to associate a texture coordinate with a vertex is, for quads, to think in terms of "top-left", "bottom-left", "bottom-right" and "top-right" vertices. If you get the coordinates in the wrong order, the texture mapping will be flipped (you may start the coordinates pairs at bottom-left, but your first vertex may be bottom-right, or whatever). This can lead to your textures being flipped (e.g. by 90 degrees).

The solution is pretty straightforward: check that your texture coordinates match the correct corners of your texture map. The simplest way is to create a basic "this way up" texture. Make sure that your texture coordinates are correct, and the order they appear before vertices is correct (top-left coordinates against top-left vertex, etc).

Note that I changed your code to use a tri-strip rather than a quad. This was only to better see the tesselation of the walls (because they were two triangles, but a quad doesn't show this well). Feel free to change it back to GL_QUADs again to make like simpler. :)

If you want a more in-depth take on texture mapping then let me know and I'll throw that onto the "to-do" list.

In case any of you are wondering, this is referring to an OpenGL walk-about at Sulaco. I added sphere-poly collision detection from a gametutorials tutorial to it, but had to hack up the map quickly (and it has sideways wall textures, but I didn't mind that ;)).

pmetcher
18-12-2003, 11:57 PM
Thanks mate. I swapped the order of the calls to glTexCoord2f() and my buildings now rise up from the ground.

Only hassle is that the roads and footpaths are now very wide, but very short. If I add a field to the map file to indicate a vertical or horizontal plane, it will fix that issue.

The map file that came with the wlak through demo included entries for the faces of the model. I am having trouble relating the face entries to the vertex entries in the map file. From what I have read,

17 faces
1 2 27 26 1 1 wall // starting wall

means
1 = s
2 = t
27 = r
26 = 6
1 = u
1 = v
wall = the texture to use
:D
All very well and good, :?: but what are s, t, r and q?

Hope you enjoyed your early night.

Peter :salute: