views:

18

answers:

0

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