Sign in to follow this  

texturing on sphere

Recommended Posts

hello there, i've been trying to write my first ray tracer and i have come to the point of texture mapping. i have a sphere to cover with a checkerboard texture and im stuck at one point. i know the intersection point between the ray and the sphere and im trying to calculate u and v, the texture coordinates. im using a frame size of 300x300 and 0,0,0 point is the left-most bottom pixel of the frame. im trying to find out an exact formula for u and v but im desperate, i can't find a working one..can anyone help please? thanks..

Share this post

Link to post
Share on other sites
It depends on your texture mapping scheme. I'll describe the usual "latitude-longitude" projection here.

Say your sphere is defined by,

Position: c = (cx, cy, cz)
Radius: r
Orientation: R (rotation matrix)

and you've found a point p on the sphere using a ray-sphere intersection test. Then u,v coordinates for p can be found by
1. Computing d = R-1(p-c).
2. Converting d to spherical coordinates (r, theta, phi).

The u,v coordinates are (theta,phi).

More on step 2: It's done using a two-argument arctangent function. Google "spherical coordinates" and you'll figure it out.

Share this post

Link to post
Share on other sites
did you try with this formula..

int i;
for( i = 0; i < numOfVertices; i++ )
vertex->u = vertex->nx / (sqrt(vertex->nx*vertex->nx + vertex->ny*vertex->ny + vertex->nz*vertex->nz));

vertex->v = vertex->ny / (sqrt(vertex->nx*vertex->nx + vertex->ny*vertex->ny + vertex->nz*vertex->nz));

where u,v are texture coordinates and
nx,ny,nz are normals of each vertex

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this