Question

I use Managed DirectX with C# to texture a sphere (Mesh.Sphere).

I use the following code to calculate U and V:

CustomVertex.PositionNormalTextured[] vertData = (CustomVertex.PositionNormalTextured[])tempMesh.VertexBuffer.Lock(0, typeof(CustomVertex.PositionNormalTextured), LockFlags.None, tempMesh.NumberVertices);

for (int i = 0; i < vertData.Length; ++i)
{
    vertData[i].Tu = (float)(1.0 - (double)(0.5f + Math.Atan2(vertData[i].Nz, vertData[i].Nx) / (Math.PI * 2)));
    vertData[i].Tv = (float)(0.5 - Math.Asin(vertData[i].Ny) / Math.PI);
}

Now I have the problem, that the poles of the sphere and the poles of my texture (equirectangular projection) does not match.Red points present the poles in my program

The red points in the picture are the place where the poles of the sphere currently match the texture.

Can someone tell me what I can do to fix this problem?

Was it helpful?

Solution

Your above code works perfectly provided the sphere is centered at the origin and y is up. I will demonstrate:

Applying the maths with the assumption that poles exist at 0, 1, 0 and 0, -1, 0 gives the following numbers of the poles.

 u = 1.0 - (0.5 + (atan2( 0, 0 ) / (2 * PI));
 => u = 1.0 - (0.5 + (0 / (2 * PI));
 => u = 1.0 - 0.5;
 => u = 0.5
 v = 0.5 - (asin( 1 ) / PI)
 => v = 0

and

 u = 0.5
 v = 0.5 - (asin( -1 ) / PI)
 => v = 0.5 - -0.5
 => v = 1.0

Which are the correct values, for u and v, ie (0.5, 0) and (0.5, 1).

If you are using z-up then this WILL give incorrect values (as you would need to swap the y and z over in your calculations) but it still does not give the pole values you are suggesting:

 u = 1.0 - (0.5 + (atan2( 1, 0 ) / (2 * PI));
 => u = 1.0 - (0.5 + (PI / (2 * PI)))
 => u = 1.0 - (0.5 + 0.5);
 => u = 0
 v = 0.5 - (asin( 0 ) / PI)
 => v = 0.5 - (0 / PI)
 => v = 0.5

and

 u = 1.0 - (0.5 + (atan2( -1, 0 ) / (2 * PI));
 => u = 1.0 - (0.5 + (-PI / (2 * PI)))
 => u = 1.0 - (0.5 - 0.5);
 => u = 1.0
 v = 0.5 - (asin( 0 ) / PI)
 => v = 0.5 - (0 / PI)
 => v = 0.5

The reason for this is fairly sensible. In the u direction the sphere wraps entirely round. ie a u of 0 is the same as a u of 1. This happens entirely in the x-z plane in the equation you have posted (y is not considered for u). This is why it is divided by 2 * pi or the number of radians in a full circle. The v direction does not wrap around. In fact it only applies to half the range and thus a division pi. You'll note that only y is used in the calculation and, hence, x and z do not affect the v calculation.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top