使用三角形条对球体进行纹理处理

h6my8fg2  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(397)

我正在学习opengl,但是在渲染球体时遇到了问题。我可以正常地绘制和绑定多边形的纹理,但是当我尝试用同样的方法来绘制三角形条绘制的球体时,效果并不理想。部分纹理断裂((下图)
我能知道我做错了什么吗?抱歉,如果这是一个明显的问题。
球体代码如下:(添加纹理部分前,球体可以正常显示)

private void drawSphere(GL gl) {
        Position spherePosition = state.getSpherePosition();
        final float PI = 3.141592f;
        gl.glPushMatrix();
        if(spheretexture == null){
           setSphereTexture();
        }

        gl.glTranslated(spherePosition.getX(), spherePosition.getY(), spherePosition.getZ());
        float[] ambientDiffuse = new float[] {255.0f, 255.0f, 255.0f, 1.0f};
        gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, ambientDiffuse, 0);

        gl.glEnable(GL_BLEND);
        gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        float x, y, z, alpha, beta; // Storage for coordinates and angles
        float radius = 25f;
        int gradation = 10;
        for (alpha = 0.0f; alpha < PI; alpha += PI / gradation) {
        spheretexture.enable();
        spheretexture.bind();
        gl.glBegin(GL_TRIANGLE_STRIP);
        for (beta = 0.0f; beta < 2.01 * PI; beta += PI / gradation) {
            x = (float) (radius * Math.cos(beta) * Math.sin(alpha));
            y = (float) (radius * Math.sin(beta) * Math.sin(alpha));
            z = (float) (radius * Math.cos(alpha));
            gl.glTexCoord2f(beta / (2.0f * PI), alpha / PI);
            gl.glVertex3f(x, y, z);
            x = (float) (radius * Math.cos(beta) * Math.sin(alpha + PI / gradation));
            y = (float) (radius * Math.sin(beta) * Math.sin(alpha + PI / gradation));
            z = (float) (radius * Math.cos(alpha + PI / gradation));
            gl.glTexCoord2f(beta / (2.0f * PI), alpha / PI + 1.0f / gradation);
            gl.glVertex3f(x, y, z);
        }

        gl.glEnd();

        spheretexture.disable();
        gl.glDisable(GL_BLEND);
        gl.glPopMatrix();
    }

    }

输出图片:

qlzsbp2j

qlzsbp2j1#

主要问题是球体的顶帽被画了两次。这会引起z-战斗。注意,首先它是绘制购买之间的地带 PI-PI/gradation 以及 PI 然后它是由 PI 以及 PI+PI/gradation . 这是因为外环做了一次额外的传递。更改为:

for (alpha = 0.0f; alpha < PI-PI/gradation; alpha += PI / gradation) {
    // [...]
}

或者更好

for (int i = 0; i < gradation; ++ i ) {
    float alpha =  PI * (float)i / (float)(gradation);
    // [...]
}

相关问题