# Texture Mapping to Sphere is wrong

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

## Recommended Posts

Hey, trying to wrap a 2D texture around a sphere, but I'm getting this line of jumbled texture coords somehow, I can't quite figure it out.

 //Here is my shader code in HLSL // VERTEX SHADER float p = 3.141592f; float3 x = float3(1, 0, 0); float3 z = float3(0, 0, 1); float3 norm = normalize(input.normal); // find theta using the normal and z float theta = acos(dot(norm,z)); // find the x-y projection of the normal float3 proj = normalize(float3(norm.x, norm.y, 0)); // find phi using the x-y projection and x float phi = acos(dot(proj,x)); // if x-y projection is in quadrant 3 or 4, then phi = 2PI - phi if (proj.y < 0) phi = 2*p - phi; float3 coords = float3(phi / (2*p), theta / p, 0); output.texCoord = coords; // PIXEL SHADER float4 color = float4(tex2D(gSampler,float2(input.texCoord.x,input.texCoord.y)).rgb,1); return color; 

Any help would be appreciated, I'd love it, if this looked good Edited by Muzzy A

##### Share on other sites
Somewhere on your sphere you will have a "start" line where the x texture coord is zero, and then you increase it going around the sphere until it should reach 1.0 on exactly the same line as it was zero before. My guess is that when you reach the vertices right before the other side you set your x texture coord to 0.99 or something like that (correctly), but then when you get back to the starting point you set it to 0.0 again instead of 1.0, so the last strip of triangles gets the whole sphere texture compressed into a thin line.
You could fix this by for example duplicating that line of vertices and use 1.0 instead of 0.0 for the last one. It's easier if you precompute the texcoords and store in the vertex buffer, as it can be difficult to distinguish if you want 0.0 or 1.0 in the shader, unless you add some other type of marker to the vertices.

##### Share on other sites
Calculate phi using atan2:

 float3 norm = normalize(input.normal); float theta = acos(norm.z); float phi = atan2(norm.y, norm.x); float2 texCoord = float2(phi / 2, theta) / 3.14159265; float4 color = 1.0f; color.rgb = tex2D(gSampler, texCoord).rgb; 

The problem lies in the fact, that you're calculating the texture coordinates in the vertex shader. The problem is that one vertex is on the far right of the texture, while the other 2 vertices of a triangle are on the far left of the texture, which results in almost the whole texture being visible on such a triangle. You need to fix your sphere if you want to do the texture coordinate calculation in the vertex shader. If you're going to do that, you should also probably bake the texture coordinates into the vertex buffer. Edited by CryZe

##### Share on other sites
I moved the calculation of the texture coords to when im creating the vertex buffer, i pass it through the vertex buffer. But still i get that same line through the texture. I'm not sure what i could change to make it work right though? Any ideas?

##### Share on other sites
Your sphere likely consists of "stripes" or "columns" going from the north pole to the south pole, consisting of triangles. Each stripe is textured by a rectangle in the texture. Think about this until you see it clearly. You wrap your rectangular texture around the sphere, and each column of triangles get texture coords somewhere in the texture.

Your problem is at the edge where the right side of your texture touches the left side of your texture, when it is completely wrapped around your sphere.

The stripe at exactly the right side of the texture should have texture X coordinates from 1.0 at the right side to 1.0 - x on the left side, where x is whatever width each stripe has in the texture.

In your case, when going completely around the sphere the "last stripe" will in fact not get 1.0 on its right side, but 0.0, as you are back at the beginning, so that stripe will be textured with the whole texture from 0.0 to 1.0 - x, instead of the small part from 1.0 - x to 1.0.

You must duplicate your vertices at that particular line, so when creating triangles, instead of using the last line of vertices together with the first to make the last part of the sphere, duplicate the first vertices and replace the texture-coord values in the copy with 1.0 instead of 0.0. Use those vertices instead when creating your last stripe of triangles.

##### Share on other sites

Your sphere likely consists of "stripes" or "columns" going from the north pole to the south pole, consisting of triangles. Each stripe is textured by a rectangle in the texture. Think about this until you see it clearly. You wrap your rectangular texture around the sphere, and each column of triangles get texture coords somewhere in the texture.

Your problem is at the edge where the right side of your texture touches the left side of your texture, when it is completely wrapped around your sphere.

The stripe at exactly the right side of the texture should have texture X coordinates from 1.0 at the right side to 1.0 - x on the left side, where x is whatever width each stripe has in the texture.

In your case, when going completely around the sphere the "last stripe" will in fact not get 1.0 on its right side, but 0.0, as you are back at the beginning, so that stripe will be textured with the whole texture from 0.0 to 1.0 - x, instead of the small part from 1.0 - x to 1.0.

You must duplicate your vertices at that particular line, so when creating triangles, instead of using the last line of vertices together with the first to make the last part of the sphere, duplicate the first vertices and replace the texture-coord values in the copy with 1.0 instead of 0.0. Use those vertices instead when creating your last stripe of triangles.

Ya i understood what you meant the first time you said it, but i'm using D3DXCreateSphere() to generate one for me, i don't know how they set up the verts in there so i don't quite know which tex coord would be the last. I tried setting the last few texcoords to 1, didn't change a thing.

##### Share on other sites
You must duplicate one meridian (the one corresponding to the jumbled texture) so that one has the x-component of the texcoords to 0, and the other has them to 1. Not sure how you can achieve this through the opaque D3DX sphere mesh creation, though.

##### Share on other sites

Ya i understood what you meant the first time you said it, but i'm using D3DXCreateSphere() to generate one for me, i don't know how they set up the verts in there so i don't quite know which tex coord would be the last. I tried setting the last few texcoords to 1, didn't change a thing.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007530
• ### Who's Online (See full list)

There are no registered users currently online

×