我想绘制一个球体并对其进行纹理处理,我将其绘制为三角形,而当我尝试对其进行纹理处理时,某些三角形未被覆盖

我正在使用此功能来生成坐标

public void createSolidSphere()
    {
        float R = (float) (1./(float)(rings-1));
        float S = (float) (1./(float)(sectors-1));
        int r, s;
        int texCoordsIndex = -1;
        int verticesIndex = -1;
        int normalsIndex = -1;
        int indicesIndex = -1;
        for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
            float y = (float)Math.sin( -Math.PI/2 + Math.PI * r * R );
            float x = (float)Math.cos(2*Math.PI * s * S) * (float)Math.sin( Math.PI * r * R );
            float z = (float)Math.sin(2*Math.PI * s * S) * (float)Math.sin( Math.PI * r * R );

            texcoords[++texCoordsIndex] = s*S;
            texcoords[++texCoordsIndex] = r*R;

            vertices[++verticesIndex] = x * radius;
            vertices[++verticesIndex] = y * radius;
            vertices[++verticesIndex] = z * radius;

            normals[++normalsIndex] = x;
            normals[++normalsIndex] = y;
            normals[++normalsIndex] = z;
        }
        for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {


            indices[++indicesIndex] = r * sectors + (s+1);
            indices[++indicesIndex] = (r+1) * sectors + (s+1);
            indices[++indicesIndex] = (r+1) * sectors + s;
        }
    }


java - 使用LWJGL opengl对球体进行纹理处理?-LMLPHP

最佳答案

您必须绘制四边形而不是三角形。一个四边形可以由2个三角形组成。
每个四边形由4点组成:

0: r * sectors + (s+1)
1: (r+1) * sectors + (s+1)
2: (r+1) * sectors + s
3: r * sectors + s


这4个点可以排列为2个三角形:

    2           1
     + +-------+
     | \ \     |
     |   \ \   |
     |     \ \ |
     +------+  +
    3           0


您必须为每个四边形添加6个索引,而不是3个:

for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {

    // triangle 1
    indices[++indicesIndex] = r * sectors + (s+1);
    indices[++indicesIndex] = (r+1) * sectors + (s+1);
    indices[++indicesIndex] = (r+1) * sectors + s;

    // triangle 2
    indices[++indicesIndex] = r * sectors + (s+1);
    indices[++indicesIndex] = (r+1) * sectors + s;
    indices[++indicesIndex] = r * sectors + s+;
}

10-06 08:31