View Full Version : Ccalculating Tex Co-ords for rectangluar pics

12-12-2002, 10:12 PM
I noticed when using Mipmapped textures,that some bitmaps appeared skewed and distorted.

through experimentation i found that depending on the aspect ratio of the bitmaps, different texture co-ordinates were required to keep the bitmap from skewing

i have set my format so that the verticies are given to openGL in a clockwise manner

sample verticies (z =0)

| |
| |
| |

Tex co-ords
A : 0,1
B : 1,1
C : 1,0
D : 0,0

I used a square bitmap and placed it on the square ABCD and all was good. the texture fit on perfectly. The top left of the bitmap was aligned to A, the top right to B, and so on.

when i used a rectangular bitmap (ratio x:y 2:1), that was NOT the case. The texture was all skewed.

i was able to (by trial and error) make the texture fit on to the quad properly y changing the texture co-ords.

how do i calculate the correct texture co-ords given the X and Y of the bitmap?

Problem 2:
Using the Square bitmap as a texture, i made ABCD into a Trapezoid. The Result (http://www.geocities.com/mycae/trapez.jpg)

you can see a line (Vertex A -> vertex C) where the 2 different methods of fixing the texture are visble. How do i make it so that the texture is streched without the obvious discontiniuty across the whole poly (where it appears to be divided into triangles...), so that there isnt that line.

All suggestions are appreciated.

12-13-2002, 10:23 AM
http://nehe.gamedev.net/tutorials/lesson.asp?l=06 is a good place to start learning texture mapping. You can also read the other tutorials. They are great.
You should replace the one's in the texture coordinates with poly.height / tex.height (the y values) and poly.width / tex.width (the x values). This way your texture will be tiled (I understand that this is what you want).

12-14-2002, 05:32 AM
Thatīs all very easy.
The size and aspect ratio of your image doesnīt matter to OpenGL. It always handles the coordinate (0|0) as the lower left corner and (1|1) as the upper right corner of your image.
So with the coordinates you used, every image, no matter which size it is, will be stretched to fit exactly onto your quad. So if you have an image that is 100 pixels wide and 50 pixels high and you want the ratio to be correct you have to change your coords:

Either you change the X-coordinate to 0.5, which would mean, that only the left half of the image will appear on your quad,
or you change the Y-coordinate to 2.0, which would mean, that the image will appear to times on your quad, once in the upper half, and once in the lower half.

Hope that helps you.

12-14-2002, 05:38 PM
Ok, i dont know what i did, but the picture scales properly to the quad, if the quad is a rectangle.

still having the problem with irregular quadrilaterals.