Spinning globe in Opengl-es

扶醉桌前 提交于 2020-01-20 08:48:08

问题


I found this code to generate a sphere in Opengl es. I am unable to understand the logic, could someone please give me some insights on this.

private void generateData() {

            slicesBuffers = new FloatBuffer[slices];
            normalsBuffers = new FloatBuffer[slices];
            texCoordsBuffers = new FloatBuffer[slices];


            for (int i = 0; i < slices; i++) {

                    float[] vertexCoords = new float[7 * (stacks + 1)];
                    float[] normalCoords = new float[4* (stacks + 1)];
                    float[] textureCoords = new float[10 * (stacks + 1)];



                    double alpha0 = i * (2 * Math.PI) / slices;
                    double alpha1 = (i + 1) * (2 * Math.PI) / slices;

                    float cosAlpha0 = (float) Math.cos(alpha0);
                    float sinAlpha0 = (float) Math.sin(alpha0);
                    float cosAlpha1 = (float) Math.cos(alpha1);
                    float sinAlpha1 = (float) Math.sin(alpha1);

                    for (int j = 0; j <= stacks; j++) {

                            double beta = j * Math.PI / stacks - Math.PI / 2;

                            float cosBeta = (float) Math.cos(beta);
                            float sinBeta = (float) Math.sin(beta);

                            Utils.setXYZ(vertexCoords, 6 * j,
                                            radius * cosBeta * cosAlpha1,
                                            radius * sinBeta,
                                            radius * cosBeta * sinAlpha1);
                            Utils.setXYZ(vertexCoords, 6 * j + 3,
                                            radius * cosBeta * cosAlpha0,
                                            radius * sinBeta,
                                            radius * cosBeta * sinAlpha0);


                                    Utils.setXYZ(normalCoords, 6 * j,
                                                    cosBeta * cosAlpha1,
                                                    sinBeta,
                                                    cosBeta * sinAlpha1);
                                    Utils.setXYZ(normalCoords, 6 * j + 3,
                                                    cosBeta * cosAlpha0,
                                                    sinBeta,
                                                    cosBeta * sinAlpha0);

                                    Utils.setXY(textureCoords, 4 * j,
                                                    ((float) (i + 1)) / slices,
                                                    ((float) j) / stacks);
                                    Utils.setXY(textureCoords, 4 * j + 2,
                                                    ((float) i) / slices,
                                                    ((float) j) / stacks);

                    }

                    slicesBuffers[i] = FloatBuffer.wrap(vertexCoords);

                            normalsBuffers[i] = FloatBuffer.wrap(normalCoords);
                            texCoordsBuffers[i] = FloatBuffer.wrap(textureCoords);

            }
    }

Thankyou


回答1:


For the theory of sphere generation see:

en.wikipedia.org/wiki/Sphere (Vertex)

en.wikipedia.org/wiki/UV_mapping (Texture coordinate) http://groups.google.com/group/android-developers/browse_thread/thread/0030261b82ed71e5/338fc1dcbfe6945f?lnk=raot(Normal surface)

Your code is right, it have a little bit issues, I have make some corrections:

public Sphere(int slices,int stacks, float radius, float H,float K,float Z, Bitmap image,Bitmap first,Bitmap second){


    FloatBuffer[] slicesBuffers = new FloatBuffer[slices];
    FloatBuffer[] normalsBuffers = new FloatBuffer[slices];
    FloatBuffer[] texCoordsBuffers = new FloatBuffer[slices];

    float[] total_vertexBuff;
    float[]  total_normalsBuff;
    float[]  total_textCoordsBuff;
    int vertex_counter = 0;
    int normals_counter = 0;
    int texCoords_counter = 0;
    int position_dst;
    float tmp[];

    for (int i = 0; i < slices; i++) {
      float[] vertexCoords = new float[  2 * 3 * (stacks + 1)];
            float[] normalCoords = new float[  2 * 3 *(stacks + 1)];
            float[] textureCoords = new float[ 4 * (stacks + 1) ];
            double alpha0 = i * (2 * Math.PI) / slices;
            double alpha1 = (i + 1) * (2 * Math.PI) / slices;
            float cosAlpha0 = (float) Math.cos(alpha0);
            float sinAlpha0 = (float) Math.sin(alpha0);
            float cosAlpha1 = (float) Math.cos(alpha1);
            float sinAlpha1 = (float) Math.sin(alpha1);

            for (int j = 0; j <= stacks; j++) {

                    double beta = j * Math.PI / stacks - Math.PI / 2;
                    float cosBeta = (float) Math.cos(beta);
                    float sinBeta = (float) Math.sin(beta);
                    setXYZ(vertexCoords, 6 * j,    radius * cosBeta * cosAlpha1,   radius * sinBeta,   radius * cosBeta * sinAlpha1   );
                    setXYZ(vertexCoords, 6 * j + 3,radius * cosBeta * cosAlpha0,radius * sinBeta,radius * cosBeta * sinAlpha0);
                    vertex_counter +=  2;

                    Log.d(TAG, "j:"+j);

                    setXYZ(normalCoords, 6 * j,cosBeta * cosAlpha1,sinBeta,cosBeta * sinAlpha1);
                    setXYZ(normalCoords, 6 * j + 3,cosBeta * cosAlpha0,sinBeta,cosBeta * sinAlpha0);
                    normals_counter += 2;
                    setXY(textureCoords, 4 * j,((float) (i + 1)) / slices,((float) j) / stacks);
                    setXY(textureCoords, 4 * j + 2,((float) i) / slices,((float) j) / stacks);
                    texCoords_counter += 2;
            }

            slicesBuffers[i] = FloatBuffer.wrap(vertexCoords);
            normalsBuffers[i] = FloatBuffer.wrap(normalCoords);
            texCoordsBuffers[i] = FloatBuffer.wrap(textureCoords);

    }



    total_vertexBuff = new float[vertex_counter * 3];
    total_normalsBuff = new float[normals_counter * 3];
    total_textCoordsBuff = new float[texCoords_counter * 2];
    position_dst = 0;
    // ricopio vertici
    for (int i = 0; i < slicesBuffers.length; i++) {
     for(int j = 0; j < slicesBuffers[i].capacity();j++,position_dst++)
      total_vertexBuff[position_dst] = slicesBuffers[i].get(j);

 }
    position_dst = 0;
 // ricopio normali
    for (int i = 0; i < normalsBuffers.length; i++) {
     for(int j = 0; j < normalsBuffers[i].capacity();j++,position_dst++)
      total_normalsBuff[position_dst] = normalsBuffers[i].get(j);

 }


    position_dst = 0;
    // ricopio coordinate texture
    for (int i = 0; i < texCoordsBuffers.length; i++) {
     for(int j = 0; j < texCoordsBuffers[i].capacity();j++,position_dst++)
      total_textCoordsBuff[position_dst] = texCoordsBuffers[i].get(j);

 }




    this.image = image;
    this.half_first = first;
    this.half_second = second;
    this.vertexBuffer = FloatBuffer.wrap(total_vertexBuff);
    this.normalsBuffer = FloatBuffer.wrap(total_normalsBuff);
    this.texCoordsBuffer = FloatBuffer.wrap(total_textCoordsBuff);
    Log.d(TAG, "vertex_counter:"+vertex_counter);
    Log.d(TAG, "texCoords_counter:"+texCoords_counter);
    Log.d(TAG, "vertexBuffer:"+this.vertexBuffer.capacity());
    Log.d(TAG, "texCoordsBuffer:"+this.texCoordsBuffer.capacity());

    this.textures_ids = IntBuffer.allocate(2);
    this.totalVertexCount = vertex_counter;

    this.setPlaneBuffer();
    return;

}

I really hope I help you.

Bye

pedr0



来源:https://stackoverflow.com/questions/3811082/spinning-globe-in-opengl-es

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!