View Full Version : Calc Texture Coordinate

I am stack with the little math calculation.

Please somebody tell me the algorithm to solve the question below.

Say,

I have an image with the coordinates

(0,1000) (1000, 1000)

(0,0) (1000, 0)

and a square object with the coordintes of

(0,500) (500, 500)

(0,0) (500, 0)

To render this rectangle with the texture, I have to specify.

(0.0, 0.25) (0.25,0.25)

(0.0, 0.0) (0.25, 0.0)

The problem is I don't know how I can get these number.

Firestar

11-26-2001, 12:22 AM

Hi!

If I got you right, what you're trying to do is to render the first quater of your texture. The texture coordinates in OpenGL are always in the range from 0 to 1. So the texture coordinate (0,0) is the upper left corner of your texture, (1,0) is the upper right corner, (0,1) is the bottom left corner and (1,1) is the bottom right corner of your texture.

Now your texture has a width and height. What you have to do is normalize your texture coordinates to fit in the range 0-1. You do this by dividing the pixel coordinate on your texture with the texture width and the texture height. So if you want to render the first quater for your texture (with width = 4000 and height = 4000), you get your coordinates (0,1000), (1000,1000), (0,0) and (1000,0). So the first coordinate to put in the OpenGL is (0,0.25) because 0/(width=1000) = 0 and 1000/(height=1000) = 0.25. The same goes for the other coordinates. The vertex coordinates for the rectangle have nothing to do with the texture coordinates. You just tell OpenGL, on which vertec to put which texture coordinate. So if you have a rectangle (0,500), (500,500), (0,0) and (500,0) or a rectangle (0,200), (200,200), (0,0), (200,0) with the same texture coordinates OpenGL will always render the same portion of your texture to your rectangle, the only difference is the size of your rendered rectangle.

Hope it helps

Firestar

Sorry, I posted a bad example.

My problem is if texture coordinates are

not rectangle like

(10, 1000) (1000, 1020)

(0, 50 ) (1040, 20)

and I want to calc the u & v value for a point (50, 100).

How do I get the answer?

Firestar

11-26-2001, 02:11 AM

Hi!

You can calculate your coordinate as I already said:

you just normalize you pixel values into the range 0-1. So what you need is your pixel position on the texture map and the texture map's width and height.

For example your width = 2000 and height = 1500, you calculate your uv's like this:

(10,1000) ---> (10/2000,1000/1500) = (0.005, 0.6667)

(1000,1020) ---> (1000/2000,1020/1500) = (0.5,0.68)

and so on...

Hope it helps ;-)

Cheers

Firestar

Problem is not that simple.

Let me describe my situation.

I have an air photo image which is far larger than the area needed. Also the area covered is not rectangle.

What I want to do is to apply this image to my terrain.

Then, I have to calculate uv pairs for each point. Propotions do not work, so I need some normalize method.

Hope you understand !!

Ryo,

Please note that a texture, regardless of it's original size in pixels will occupy an area in 'u,v' space from [0,0] to [1,1].

In order to map a part of this texture onto a shape, you have to map each vertex of the shape to the appropriate (u,v) coordinate.

If you do not want distortions in this mapping you have to create a linear transformation between (x, y) and (u, v) (assuming we're working in a plane of equal z).

As the other posters also pointed out to you, one such transformation is:

min(x) --> u = 0.0

max(x) --> u = 1.0

min(y) --> v = 0.0

max(y) --> v = 1.0

Now, to calculate (u,v) for any (x, y) between these min and max values you use:

u = x / (max(x) - min(x))

v = y / (max(y) - min(y))

Of course there are lots of other transformations possible as well (they are used to stretch/rotate/shift the texture relative to the shape it is used on).

HTH

Jean-Marc

No,No,No...

I guess everybody misunderstood what my problem is.

My air photo image has geographic coordinates and it is not rectangle.

I get image like this.

xy(10, 1000) (1000, 1020)

uv(0,1) (1,1)

-------------

| |

| +P1 +P2 |

| |

| +P3 +P4|

| |

---------------

xy (0, 50 ) (1040, 20)

uv(0,0) (1,0)

I know the xy value for P1 to P4.

Then, how do I get uv for these points?

P1 u -> 0.??

v -> 0.??

[This message has been edited by Ryo (edited 11-27-2001).]

Originally posted by Ryo:

xy(10, 1000) (1000, 1020)

uv(0,1) (1,1)

-------------

| |

| +P1 +P2 |

| |

| +P3 +P4|

| |

---------------

xy (0, 50 ) (1040, 20)

uv(0,0) (1,0)

Ahh, I've overlooked your second sample.

OK, so the xy coordinates are not an exact rectangle...

you have to come up with the solution of the following equations:

u = A * x + B * y + C

v = D * x + E * y + F

(solve for A through F). You know the four corner points which give eight equations with six

unknowns ==> solvable http://www.opengl.org/discussion_boards/ubb/smile.gif:

0 = A * 10 + B * 1000 + C

1 = D * 10 + E * 1000 + F

1 = A * 1000 + B * 1020 + C

1 = D * 1000 + E * 1020 + F

0 = A * 0 + B * 50 + C

0 = D * 0 + E * 50 + F

1 = A * 1040 + B * 20 + C

0 = D * 1040 + E * 20 + F

Perhaps there is a simpler way, but this should work for you.

Good luck

Jean-Marc.

I feel so stupid. I cannot solve the equation. I keep getting wrong answer.

Could you show me

A =

B =

C =

format.

Ryo, you shouldn't feel stupid. MATLAB says there's no solution to that equation system.

I don't find it surprising at all. Eight equations and six unknowns has a one unique solution only if two of the equations is a multiple of another equation.

Ahem, right. A thousand apologies for my previous post.

I'm the one that should feel stupid (and I do http://www.opengl.org/discussion_boards/ubb/frown.gif).

The equations do not have a solution as Bob pointed out. I wrongly assumed that there would be a linear solution to map (x, y) to (u,v) in Ryo's case. My suggestion only works on parallellograms I guess, not on arbitrary convex quads as I assumed.

I do not know of a working method. You will somehow have to come up with a way to create a grid across the area between the four world-corner vertices.

Somehow this sounds similar to the way bezier surfaces and nurbs are tesselated (of which I have no knowlegde, except for some brief texts I read about it -- enough to ring a bell, but that's it).

Perhaps investigating in that direction will help you.

Again, good luck,

Jean-Marc.

[This message has been edited by JML (edited 11-29-2001).]

Thanks everyone.

I will keep searching books and articles.

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.