Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

android - Spinning globe in Opengl-es

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...