【问题标题】:What is the correct way to map a texture on a sphere?在球体上映射纹理的正确方法是什么?
【发布时间】:2012-12-23 20:36:59
【问题描述】:

我在正确映射球体时遇到问题。我用一张世界地图来显示哪里出了问题。北美从上到下出现在最前面,而南美则颠倒过来,像亚洲这样的大陆甚至都没有出现在地图上。


(来源:troll.ws

下面的代码是球体对象

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

我该如何解决?我尝试交换一些坐标和其他东西,但这对我来说更加混乱。

另外,当我将纹理绑定到它时,似乎有一些伪影。可以解决吗?

【问题讨论】:

  • 你的截图在哪里?
  • 托管图像的托管服务不再运行。

标签: opengl geometry textures jogl


【解决方案1】:

您的两个循环都从 0 变为 2*PI。其中一个应该只有半个圆圈。您将球体翻倍,从而产生了不可靠的映射和奇怪的人工制品。

【讨论】:

    【解决方案2】:

    感谢 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);
    

    【讨论】:

      猜你喜欢
      • 2015-05-29
      • 1970-01-01
      • 2018-11-07
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2016-03-14
      相关资源
      最近更新 更多