2012-12-23 43 views
0

我遇到了正确的球体映射问题。我用世界地图来显示它出错的地方。北美从上到下出现在前方,而南美则倒在另一边,像亚洲这样的大陆甚至没有在地图上出现。在球体上映射纹理的正确方法是什么?

Screenshot http://i.troll.ws/3aedbe4b.png

下面的代码是近似球形物体

class Shape { 

    public void drawSphere(double radius, int slices, int stacks) { 
     gl.glEnable(GL_TEXTURE_2D); 
     head.bind(gl); //Method that binds the world-map (for testing) texture. 
     gl.glBegin(GL_QUADS); 
     double stack = (2 * PI)/stacks; 
     double slice = (2 * PI)/slices; 
     for (double theta = 0; theta < 2 * PI; theta += stack) { 
      for (double phi = 0; phi < 2 * PI; phi += slice) { 
       Vector p1 = getPoints(phi, theta, radius); 
       Vector p2 = getPoints(phi + slice, theta, radius); 
       Vector p3 = getPoints(phi + slice, theta + stack, radius); 
       Vector p4 = getPoints(phi, theta + stack, radius); 
       double s0 = theta/(2 * PI); 
       double s1 = (theta + stack)/(2 * PI); 
       double t0 = phi/(2 * PI); 
       double t1 = (phi + slice)/(2 * PI); 

       vectorToNormal(norm(p1)); 
       gl.glTexCoord2d(s0, t0); 
       vectorToVertex(p1); 

       vectorToNormal(norm(p2)); 
       gl.glTexCoord2d(s0, t1); 
       vectorToVertex(p2); 

       vectorToNormal(norm(p3)); 
       gl.glTexCoord2d(s1, t1); 
       vectorToVertex(p3); 

       vectorToNormal(norm(p4)); 
       gl.glTexCoord2d(s1, t0); 
       vectorToVertex(p4); 
      } 
     } 
     gl.glEnd(); 
     gl.glDisable(GL_TEXTURE_2D); 
    } 

    Vector getPoints(double phi, double theta, double radius) { 
     double x = radius * cos(theta) * sin(phi); 
     double y = radius * sin(theta) * sin(phi); 
     double z = radius * cos(phi); 
     return new Vector(x, y, z); 
    } 

我怎样才能解决呢?我尝试交换一些坐标和其他东西,但这使我更加混乱。

此外,当我将纹理绑定到纹理上时,似乎还有一些工件。这是可以修复的吗?

+0

你的屏幕截图在哪里? – genpfault

+0

托管映像的托管服务不再运行。 – Yatoom

回答

2

你的循环都是从0到2 * PI。其中之一应该只是一个半圈。你已经把球体翻了一番,导致了诡计多端的映射和奇怪的人造物。

+0

谢谢你!这是你第二次帮助我:D – Yatoom

0

感谢JasonD,这固定了它。

for (double theta = 0; theta < 2 * PI; theta += stack) { 
    for (double phi = 0; phi < 1 * PI; phi += slice) { 
     Vector p1 = getPoints(phi, theta, radius); 
     Vector p2 = getPoints(phi + slice, theta, radius); 
     Vector p3 = getPoints(phi + slice, theta + stack, radius); 
     Vector p4 = getPoints(phi, theta + stack, radius); 
     double s0 = theta/(2 * PI); 
     double s1 = (theta + stack)/(2 * PI); 
     double t0 = phi/(1 * PI); 
     double t1 = (phi + slice)/(1 * PI);