在JOGL中渲染纹理球体

时间:2016-02-16 15:38:47

标签: java textures jogl

我一直在寻找几个小时来找到在JOGL中绘制纹理球体的好方法。我需要的只是正确方向上的一点或某些适用于某人的代码。到目前为止,我所能找到的只有helloTexture(虽然它有效,但显然不是球体),是否有办法将其转换为球体或者我应该在其他地方试试运气?

1 个答案:

答案 0 :(得分:1)

我是为你写的,因为我修改它以使用GL_TRIANGLES,所以未经测试,请尝试让我知道

radius是球体半径,rings是水平切片,sectors是垂直切片

private void createGeometry(float radius, short rings, short sectors)   {

    float R = 1f / (float)(rings - 1);
    float S = 1f / (float)(sectors - 1);
    short r, s;
    float x, y, z;

    points = new float[rings * sectors * 3];
    normals = new float[rings * sectors * 3];
    texcoords = new float[rings * sectors * 2];

    int t = 0, v = 0, n = 0;
    for(r = 0; r < rings; r++) {
        for(s = 0; s < sectors; s++) {
            x = (float) (Math.cos(2 * Math.PI * s * S) * Math.sin(Math.PI * r * R ));
            y = (float) Math.sin(-Math.PI / 2 + Math.PI * r * R );
            z = (float) (Math.sin(2 * Math.PI * s * S) * Math.sin(Math.PI * r * R ));

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

            points[v++] = x * radius;
            points[v++] = y * radius;
            points[v++] = z * radius;

            normals[n++] = x;
            normals[n++] = y;
            normals[n++] = z;
        }


    }
    int counter = 0;
    indices = new short[rings * sectors * 6];
    for(r = 0; r < rings - 1; r++){
        for(s = 0; s < sectors-1; s++) {
            indices[counter++] = (short) (r * sectors + s);      
            indices[counter++] = (short) (r * sectors + (s + 1));   
            indices[counter++] = (short) ((r + 1) * sectors + (s + 1));  
            indices[counter++] = (short) ((r + 1) * sectors + (s + 1)); 
            indices[counter++] = (short) (r * sectors + (s + 1));    
            indices[counter++] = (short) ((r + 1) * sectors + s);    
        }
    }
}