UV coordinates for my sphere

Started by
8 comments, last by tenpoundbear 13 years, 9 months ago
Hey guys,

Gonna see if I can get a response here :)

Trying to work out UV texture coords for my sphere.

Quote:
Source: Paul Rademacher's article Ray Tracing: Graphics for the Masses, "http://www.cs.unc.edu/~rademach/xroads-RT/RTarticle.html"

We then find the longitude as

theta = ( arccos( dot_product( Vp, Ve ) / sin( phi )) ) / ( 2 * PI)
if ( dot_product( cross_product( Vn, Ve ), Vp ) > 0 )
u = theta
else
u = 1 - theta

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

or Sphere Games Site
U = (tan-1(x/z) / 2π) + 1/2
V = (arcsin(y)/π) + 1/2


Now... I am bit confused with these formulas because it looks too simple. What I mean is that previously when I was drawing my sphere geometry... the formula for finding the longitude was kinda of like a two part/ two term thing
x = sin (phi) * cos (theta)
y = sin (phi) * sin (theta)
z = cos (phi)
but here to find the longitude it seems the formula is greatly simplified?

I do realize we are finding angles by the way, and not lengths.

I don't know... could someone clarify this for me?
I hope I made sense.
Advertisement
Could you clarify your question a bit?

The formulae you quoted appear to be determining an angle (longitude) and a tex coords from some information (not clearly identified).

Your code (as you seem to realize) determines positions on a sphere given the latitude and longitude.

Uh.. what's your question? [wink]

EDIT: looking at it all again, it appears the info you quoted from the Sphere Games Site is just determing tex coords from coordinates (x,y,z) on a unit sphere. It appears the sphere axis is oriented along the y axis, so tex coord V is proportional to the distance from the bottom of the sphere to the top (arcsin(y) ~ latitude). Tex coord U is proportional to the angle "around" the sphere (arctan(x/z) ~ longitude).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thanks for reply :)

I think where I am getting confused is when you go from 1 coord system to another.

With generating sphere geometry I went from spherical to 3D cartesian,
but with creating UV coords you go from 3D cartesian coords to UV coords (is that right)?

Is that how they are doing it in the link?

Initially, I just assumed to get UV coords you go from spherical to UV coords but after revising I think that is wrong, hence where I am confused.

Hmmm... how does an angle relate to UV coords... From spherical to 3D points, that was naturally intuitive... it makes sense unlike angles and UV coords.

I am guessing the answer lies in the root of mathematics... anyone able to provide further explanation or give me a link to some resources.

Read heaps of trigonometry stuff... but I really haven't come across this relationship yet.

With Paul's formulas... he doesn't seem to suffer from the problems some people experience at the seams of the texture? Again, answer lies in mathematics I assume?
I haven't read the article you quote so I'm not familiar with the variables used. However, just from inspecting them, you're pretty much correct that the formulae are converting from Cartesian to spherical coords, back and forth.

The angles for spherical coordinates range from -pi to +pi. If I divide the angle for a point of the sphere by 2*pi, I'll get values ranging from -0.5 to +0.5. That happens to be a convenient range for texture coordinates on a sphere also! It's a convenient way to properly spread the texture over the sphere.

If you wanted to space tex coords linearly, you would use a linear conversion of the Cartesian coordinates.

To space the tex coords over a sphere, you convert the linear coordinates to spherical coordinates to spread the tex coords appropriately over the sphere.

Does that help?

EDIT: Your geometry has the Z axis "up." The Z coordinates ( cos(phi) ) run from -1 to +1 (using phi from -pi to +pi). To convert the linear range -1 to +1 to numbers that will run over the surface of the sphere properly, you can use arccos(Z) to get the angle (and divide by pi), and use that as one of the tex coords.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Hi everyone,

Thanks for replies :)
Ok so I've coded some and this is the result.

Don't worry about the texture at the seam (I will fix that last). But you can immediately see that it is wrong.


I am going to concentrate on understanding U first... then V.
With this formula

U = (tan-1(x/z) / 2π) + 1/2

Why inverse tangent? Why not inverse cosine or inverse sine?
And why use (x/z)?

I understand that inverse tangent is is finding angle based on the ratio of two other sides, that is correct yeh?

Thanks for the help all :)
First: the Sphere Games Site has Y as the axis vector for the sphere. It appears you're using Z for your sphere axis. You need to change your code to generate the sphere along the Y axis, or try exchanging Y and Z in the formulae.

You're correct that the arc tangent function returns the angle of a right triangle given the length of opposite and adjacent sides.

Think of a sphere with Y as its primary axis and look "down" along the Y axis. The latitude lines (Y coordinate values) are circles in the X-Z plane. Given a set of X and Z coordinates on that latitude circle, you can draw a right triangle from the origin, along the Z axis the distance of Z coordinate, then perpendicular to the Z axis in the X direction to the circle. The X and Z coordinates are the lengths of the adjacent and opposite sides. The subtended angle (the longitude) is proportional to arctan(x/z).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
First: the Sphere Games Site has Y as the axis vector for the sphere...


This is what you mean right?
In my program, I am using Z as my depth axis.


Quote:
Think of a sphere with Y as its primary axis and look "down" along the Y axis. The latitude lines (Y coordinate values) are circles in the X-Z plane. Given a set of X and Z coordinates on that latitude circle, you can draw a right triangle from the origin, along the Z axis the distance of Z coordinate, then perpendicular to the Z axis in the X direction to the circle. The X and Z coordinates are the lengths of the adjacent and opposite sides. The subtended angle (the longitude) is proportional to arctan(x/z).


So with this, you are nominating the Z axis to be the depth axis, Y axis is up/vertical and X axis is right/horizontal (diagram on the LEFT-HAND side in the picture above)?
That makes sense to me if that is the case.

So if Sphere Site Game is using the Z axis as the up/vertical and Y axis to be the depth axis (diagram on the RIGHT-HAND side in the picture above)... isn't the formula
Quote:U = (tan-1(x/z) / 2π) + 1/2

wrong in their situation then? Because it doesn't make sense to me unless it was a typo or something...

Last question, for formula
Quote:V = (arcsin(y) / π) + 1/2

Why inverse sine (arcsin = math library function for inverse sine I assume)?
Why (y/π)?
I assumed "(arcsin(y) / π)" = "(arcsine (y/π))"... that is ok yeh?

I am trying to apply the same principles for finding U to V, but I can't work it out :(
Maybe we're just having a semantics problem. As mentioned before, the Sphere Games Site appears to be using the Y axis as the sphere axis which goes through the north and south poles. I had assumed (or intended) that "up" would be a north-south direction for that description.

In your picture, I think Sphere Games is using the orientation on the left, where the circle you've drawn is the equator (assuming you've drawn the circle to be in the X-Z plane).

Assuming the Sphere Games variable y runs from -1 to +1, arcsin(y) ranges from -pi/2 to +pi/2. Then arcsin(y)/pi ranges from -0.5 to +0.5. arcsin(y)/pi+1/2 ranges from 0 to 1, convenient for a texcoord.
Quote:I assumed "(arcsin(y) / π)" = "(arcsine (y/π))"... that is ok yeh?

No. "y" is a Cartesian coordinate. You don't want to divide a coordinate by an angle (pi).

Are we heading in the right direction? [wink]

EDIT:
Quote:Why (y/π)?
As described above, it's not (y/π), it's arcsin(y)/π.

EDIT2: I'm not "nominating" any particular axis for depth. You can use any of the axes (X,Y,Z) for the north-south axis of your sphere. Just adjust the formulae you use appropriately (once you have an understanding of where the tex coords come from).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Maybe a total comparison sheds some light into the dark:

When using the conversion formula's from the OP
x = sin( phi ) * cos( theta )
y = sin( phi ) * sin( theta )
z = cos( phi )
then the poles are on the z axis. One can see this because theta has no effect if phi=0° or phi=180° (since sin(phi) enforces x and y to be zero). So the poles are
phi=0° => [x,y,z] = [0,0,+1]
phi=180° => [x,y,z] = [0,0,-1]

In the above formulas, phi ranges from 0° to 180°, and theta ranges from 0° to 360°. x, y, and z are clamped to the range [-1,+1]. If you have a sphere with a radius different from 1 (i.e. the unit sphere) you have to incorporate the radius into the formula by scaling:
x = r * sin( phi ) * cos( theta )
y = r * sin( phi ) * sin( theta )
z = r * cos( phi )
Now you have the conversion between spherical co-orinates [r,phi,theta] and Cartesian co-ordinates [x,y,z].

Computing the reverse, it seems best to use z for phi
phi = acos( z / r )
what returns a result in [0,pi]. And using x,y for theta
y / x = ( r * sin( phi ) * sin( theta ) ) / ( r * sin( phi ) * cos( theta ) ) = tan( theta )
theta = atan( y / x )
what can be computed if and only if x differs from zero, i.e. apart from the poles! To make things easier, switch to atan2 so that the quadrant is considered correctly
theta = atan2( y , x )
what results in [-pi,+pi] IIRC.

Hence mapping [theta,phi] to [u,v] (i.e. both in the range [0,1]) needs a transformation
u = 0.5 * theta / pi + 0.5
v = phi / pi
so that the vertical texture co-ordinate v runs coarsly in direction of the local z axis from pole to pole.

Now looking at Paul's formula
phi = arccos( -dot_product( Vn, Vp )) , v = phi / pi
and considering that Paul uses a unit sphere only, we can see from comparing terms that -dot_product( Vn, Vp ) must be equivalent to the Cartesian z co-ordinate of the point of interest to match. In other words, -Vn (i.e. the "south") must be along the local Cartesian z axis.

Similarly,
theta = ( arccos( dot_product( Vp, Ve ) / sin( phi )) ) / ( 2 * PI)
shows us that his Ve (pointing to the equator) must be part of the formula in which cos( theta ) and sin( phi ) occurs, which is those of the local x, isn't it?

Now looking at the Sphere Games site, there is a
u = (tan-1(x/z) / 2π) + 1/2
= 0.5 * atan( x / z ) / pi + 0.5
what is similar in the structure of what we have derived above. However, where we used y and x, the Sphere Games site uses x and z, resp.
Looking at
v = (arcsin(y)/π) + 1/2
we only see a similarity with our solution. To clear this up, consider that the phi doesn't run from 0° at the "north" pole to 180° to the "south" pole but from +90° at the "north" pole to 0° at the "equator" to -90° at the "south" pole and you're fine (Buckeye has already stated this).

In summary:
OP's x direction is Paul's Ve direction is Sphere Games' z direction
OP's y direction is Sphere Games' x direction
OP's z direction is Paul's -Vn direction is Sphere Games' y direction
(if I made no error).
Thanks for replies so far guys :)

I am working my way through and trying to understand the maths...

Have a few questions but will only ask one for now, how do you visualize -PI to PI?

I know that it forms a complete 2*PI thingy (360 degrees... a full circle). But if I may use another example like -3 to 3... this is easy to visualize on a number line -3 on the left and 3 on the right with numbers in between -2 -1 0 1 2 etc...

but with -PI to PI... looking at the unit circle... do I just start at -PI rotate 2*PI to arrive at PI?

Thank guys

This topic is closed to new replies.

Advertisement