1
votes

I'm trying to map a completly normal texture into a sphere.

I can't change my texture to a wrapped one, so I need to find some mapping function.

This is my vertex shader code:

vec3 north = vec3(0.0, 0.0, 1.0);
vec3 equator = vec3(0.0, 1.0, 0.0);
vec3 northEquatorCross = cross(equator, north);
vec3 vertexRay = normalize(gl_Vertex.xyz);
float phi = acos(vertexRay.z);
float tv = (phi / (PI*tiling));    
float tu = 0.0;
if (vertexRay.z == 1.0 || vertexRay.z == -1.0) {
    tu = 0.5;
} else {
    float ang_hor = acos(max(min(vertexRay.y / sin(phi), 1.0), -1.0));      
    float temp = ang_hor / ((2.0*tiling) * PI);
    tu = (vertexRay.x >= 0.0) ? temp : 1.0 - temp;
} 


texPosition  = vec2(tu, tv);

its straight from here: http://blogs.msdn.com/coding4fun/archive/2006/10/31/912562.aspx

This is my fragment shader:

color = texture2D(debugTex, texPosition);

As you can see in this screenshot: http://img189.imageshack.us/img189/4695/sphereproblem.png, it shows a crack in the sphere... and this is what I'm trying to fix. (the texture used: http://img197.imageshack.us/img197/56/debug.jpg)

The first comment in the XNA website really fixes the problems using: device.RenderState.Wrap0 = WrapCoordinates.Zero;

But because I don't understand enough about XNA internals, I can't understand what this solves in this particular problem.

Around the web, some have experienced the same, and reported to be about interpolation errors, but because I'm implementing this directly as a fragment shaders (per-pixel/frag), I shouldn't have this problem (no interpolation in the texture uv).

Any info/solution on this?

2
I've just realized I was making a mistake... I was passing the uv values using the varying specifiers, so it really was being interpolated. - fabiopedrosa

2 Answers

1
votes

It's funny ! I've had the same problem, but it got fixed, I suspect there are some floating point issues... here is my shader that works!

uniform float r;
void main(void) {
    float deg1, deg2, rr, sdeg1, sdeg2, cdeg2;
    deg1 = (gl_Vertex.y / " "32" ".0) * 2.0 * 3.1415926;
    deg2 = (gl_Vertex.x / " "32" ".0) * 2.0 * 3.1415926;
    sdeg1 = sin(deg1);
    sdeg2 = sin(deg2);
    cdeg2 = cos(deg2);
    gl_Vertex.y = r*sdeg1;
    rr = r*cos(deg1);
    if(rr < 0.0001) rr = 0.0001;
    gl_Vertex.x = rr*sdeg2;
    gl_Vertex.z = rr*cdeg2;
    vec3 vertexRay = normalize(gl_Vertex.xyz);
    float phi = acos(vertexRay.y);
    float tv = (phi / (3.1415926));
    float sphi = sin(phi);
    float theta = 0.5;
    float temp = vertexRay.z / sphi;
    if(temp > 1.0) temp = 1.0;
    if(temp < -1.0) temp = -1.0;
    theta = acos(temp) / (2.0*3.1415926);
    float tu = 0.0;
    if(deg2 > 3.1415926) tu = theta;
    else tu = 1.0 - theta;
    gl_TexCoord[0].x = tu;
    gl_TexCoord[0].y = tv;
    gl_Position = ftransform();
}
0
votes

WrapCoordinates.Zero effectively signifies that the texture is wrapped horizontally, not vertically, from the perspective of the texture.