# texturing on sphere

This topic is 3418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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)
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 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));
vertex++;
}

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

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632959
• Total Posts
3009458
• ### Who's Online (See full list)

There are no registered users currently online

×