【问题标题】:How do I wrap a texture around a sphere?如何在球体周围包裹纹理?
【发布时间】:2012-12-23 00:55:30
【问题描述】:

我用 drawSphere 方法创建了一个类来替换 glutDrawSolidSphere。请参阅下面的代码。

但我想知道,如何在不平铺的情况下将纹理包裹起来?例如,如果我想在上面画一张嘴、眼睛和鼻子,那么我希望它只有一张嘴、两只眼睛和一个鼻子,而不是在整个球体上平铺 100 个。

我正在将 Jogl 与一些库一起使用。

class Shape {

    public void drawSphere(double radius, int slices, int stacks) {
        gl.glEnable(GL_TEXTURE_2D);
        head.bind(gl); //This method is a shorthand equivalent of gl.glBindTexture(texture.getTarget(), texture.getTextureObject());
        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);
                gl.glTexCoord2d(0, 0);
                gl.glVertex3d(p1.x(), p1.y(), p1.z());
                gl.glTexCoord2d(1, 0);
                gl.glVertex3d(p2.x(), p2.y(), p2.z());
                gl.glTexCoord2d(1, 1);
                gl.glVertex3d(p3.x(), p3.y(), p3.z());
                gl.glTexCoord2d(0, 1);
                gl.glVertex3d(p4.x(), p4.y(), p4.z());
            }
        }
        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);
    }
}

【问题讨论】:

    标签: java opengl geometry textures jogl


    【解决方案1】:

    您可以直接将纬度和经度映射到纹理坐标。

    for (double theta = 0; theta < 2 * PI; theta += stack) {
        for (double phi = 0; phi < 2 * PI; phi += slice) {
    

    只需将 thetaphi 缩放到 0 和 1 之间。

    double s0 = theta / (2 * PI);
    double s1 = (theta + stack) / (2 * PI);
    double t0 = phi / (2 * PI);
    double t1 = (phi + slice) / (2 * PI);
    

    并在 texCoord() 调用中使用 s0,s1,t0,t1 代替 0 和 1。

    【讨论】:

    • 谢谢!这真的很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2014-05-23
    • 2015-04-07
    • 2014-12-16
    • 2013-01-07
    • 2020-10-18
    相关资源
    最近更新 更多