Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Texture Mapping to Sphere is wrong


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Muzzy A   Members   -  Reputation: 647

Like
0Likes
Like

Posted 29 August 2012 - 11:29 PM

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.

Posted Image

//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, 29 August 2012 - 11:34 PM.


Sponsor:

#2 Erik Rufelt   Crossbones+   -  Reputation: 3532

Like
0Likes
Like

Posted 30 August 2012 - 02:23 AM

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.

#3 CryZe   Members   -  Reputation: 768

Like
0Likes
Like

Posted 30 August 2012 - 08:08 AM

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, 30 August 2012 - 08:25 AM.


#4 Muzzy A   Members   -  Reputation: 647

Like
0Likes
Like

Posted 04 September 2012 - 07:20 AM

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?

#5 Erik Rufelt   Crossbones+   -  Reputation: 3532

Like
1Likes
Like

Posted 04 September 2012 - 07:45 AM

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.

#6 Muzzy A   Members   -  Reputation: 647

Like
0Likes
Like

Posted 04 September 2012 - 07:40 PM

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.

#7 Bacterius   Crossbones+   -  Reputation: 9089

Like
0Likes
Like

Posted 04 September 2012 - 08:23 PM

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.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 Erik Rufelt   Crossbones+   -  Reputation: 3532

Like
0Likes
Like

Posted 05 September 2012 - 02:17 AM

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.


Don't use D3DXCreateSphere. Or, just send the normal from the vertex shader, and do your calculations in the pixel shader instead.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS