Textured Sphrere

Below is my code to texture on a sphere.

I have a problem on tu value(longitude value).
tu value must be in 0.0f ~ 1.0f.
and because it is a sphere(circle on 2d),
0.0f and 1.0f must be mapped to same vertex.
(if mapping start from 0.0f)
but with my code, one vertex can not have two tu, tv values.

How can I texture a sphere ??


LPD3DXMESH pTempEarthMesh = NULL;
D3DXCreateSphere(g_pd3dDevice, 1.0f, 25, 25 , &pTempSunMesh, NULL);
pTempSunMesh->CloneMeshFVF( 0, D3DFVF_MY_VERTEX, g_pd3dDevice, &g_pSunMesh );

if( SUCCEEDED( g_pSunMesh->GetVertexBuffer( &pTempVertexBuffer ) ) )
int nNumVerts = g_pEarthMesh->GetNumVertices();
Vertex *pVertices = NULL;
pTempVertexBuffer->Lock( 0, 0, (void**)&pVertices, 0 );
for( int i = 0; i < nNumVerts; ++i ){
double R = 1.0;
double theta,theta2, r;
double x1, y1;
theta = asin(pVertices[i].y/R);
r = R*cos(theta);
y1 = theta;
theta2 = atan2(pVertices[i].z,pVertices[i].x);
x1 = theta2;
x1 = (x1/PI+1)/2;
y1 = ((y1/PI*2)+1)/2;

pVertices[i].tu= x1;
pVertices[i].tv= 1-y1;

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!