XNA或OpenGL球体纹理映射

时间:2022-06-09 06:08:20

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

它直接来自这里: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)

正如你在这个截图中看到的那样:http://img189.imageshack.us/img189/4695/sphereproblem.png,它显示了球体中的一个裂缝......这就是我想要解决的问题。 (使用的纹理: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;

XNA网站上的第一条评论确实解决了以下问题: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.

但是因为我对XNA内部结构不够了解,所以我无法理解这个特定问题所解决的问题。

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).

在网络上,一些人经历过相同的,并报告是关于插值错误,但因为我直接将其实现为片段着色器(每像素/碎片),我不应该有这个问题(没有插值在纹理uv)。

Any info/solution on this?

有关此的任何信息/解决方案?

2 个解决方案

#1


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();
}

#2


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

WrapCoordinates.Zero有效地表示纹理从纹理的角度水平包裹而不是垂直包裹。

#1


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();
}

#2


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

WrapCoordinates.Zero有效地表示纹理从纹理的角度水平包裹而不是垂直包裹。