opengl es2.0绘制球体

import android.app.Activity;
import android.graphics.Color;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.Bundle;
import android.view.ViewGroup;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by Administrator on 2016/8/31 0031.
 */
public class Test2 extends Activity {
    GLSurfaceView glSurfaceView;
    //定点着色器
    public static String VL = "attribute vec4 vPosition;\n" +
            "uniform mat4 u_Matrix;"
            + "void main() {\n"
            + "  gl_Position = u_Matrix*vPosition;\n"
            + "}";
    //片段着色器
    public static String FL = "precision mediump float;\n" +
            "uniform vec4 u_Color;"
            + "void main() {\n"
            + "  gl_FragColor = u_Color;\n"
            + "}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glSurfaceView = new GLSurfaceView(this);
        glSurfaceView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        setContentView(glSurfaceView);
        //设置opengl es版本
        glSurfaceView.setEGLContextClientVersion(2);
        glSurfaceView.setRenderer(new RenderListener());
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

    }

    class RenderListener implements GLSurfaceView.Renderer {

        FloatBuffer verticalsBuffer;
        //多边形定点
       /* float[] verticals = new float[]{
                -1, -1, 0.0f, // top
                -1f, 1, 0.0f, // bottom left
                1f, 1, 0.0f,  // bottom right
               // 1,-0.5f,0.0f,
        };*/
        float[] verticals = new float[20 * 40 * 6 * 3];
        float[] projectMatrix = new float[16];
        ShortBuffer verticalsIndexBuffer;
        //每个三角形的定点编号,多边形有两个三角形组成
        short[] verticalsIndex = new short[]{
                0, 1, 2,
                0, 2, 3
        };
        private int mProgram;
        private int mPositionHandle;
        private int mColorHandle;
        private int mMatricHandle;

        public RenderListener() {
            float x = 0;
            float y = 0;
            float z = -1;
            float r = 1;
            int index=0;
            double d = 9 * Math.PI / 180;
            for (int i = 0; i < 180; i += 9) {
                double d1 = i * Math.PI / 180;
                for (int j = 0; j < 360; j += 9) {
                    double d2 = j * Math.PI / 180;
                    verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2+d));
                    verticals[index++] = (float) (y+r*Math.cos(d1+d));
                    verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2+d));

                    verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2));
                    verticals[index++] = (float) (y+r*Math.cos(d1));
                    verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2));

                    verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2+d));
                    verticals[index++] = (float) (y+r*Math.cos(d1));
                    verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2+d));

                    verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2+d));
                    verticals[index++] = (float) (y+r*Math.cos(d1+d));
                    verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2+d));

                    verticals[index++] = (float) (x+r*Math.sin(d1+d)*Math.cos(d2));
                    verticals[index++] = (float) (y+r*Math.cos(d1+d));
                    verticals[index++] = (float) (z+r*Math.sin(d1+d)*Math.sin(d2));

                    verticals[index++] = (float) (x+r*Math.sin(d1)*Math.cos(d2));
                    verticals[index++] = (float) (y+r*Math.cos(d1));
                    verticals[index++] = (float) (z+r*Math.sin(d1)*Math.sin(d2));

                }
            }

            verticalsBuffer = ByteBuffer.allocateDirect(verticals.length * 4)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer()
                    .put(verticals);
            verticalsBuffer.position(0);

            ByteBuffer dlb = ByteBuffer.allocateDirect(
                    // (对应顺序的坐标数 * 2)short是2字节
                    verticalsIndex.length * 2);
            dlb.order(ByteOrder.nativeOrder());
            verticalsIndexBuffer = dlb.asShortBuffer();
            verticalsIndexBuffer.put(verticalsIndex);
            verticalsIndexBuffer.position(0);
        }

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            mProgram = GLES20.glCreateProgram();

            int vertexShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
            GLES20.glShaderSource(vertexShader, VL);
            GLES20.glCompileShader(vertexShader);

            int fragmentShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
            GLES20.glShaderSource(fragmentShader, FL);
            GLES20.glCompileShader(fragmentShader);

            GLES20.glAttachShader(mProgram, vertexShader);
            GLES20.glAttachShader(mProgram, fragmentShader);

            GLES20.glLinkProgram(mProgram);

            mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
            mColorHandle = GLES20.glGetUniformLocation(mProgram, "u_Color");
            mMatricHandle = GLES20.glGetUniformLocation(mProgram, "u_Matrix");

            final float aspectRatio = width > height ? width * 1f / height : height * 1f / width;
            if (width > height) {
                Matrix.orthoM(projectMatrix, 0, -aspectRatio, aspectRatio, -1f, 1f, -1f, 1f);
            } else {
                Matrix.orthoM(projectMatrix, 0, -1f, 1f, -aspectRatio, aspectRatio, -1f, 1f);
            }
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
            GLES20.glUseProgram(mProgram);
            GLES20.glEnableVertexAttribArray(mPositionHandle);
            GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false,
                    12, verticalsBuffer);
            GLES20.glUniform4fv(mColorHandle, 1, new float[]{0, 1, 1, 5}, 0);
            GLES20.glUniformMatrix4fv(mMatricHandle, 1, false, projectMatrix, 0);
            GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0,20 * 40 * 6 );
            //GLES20.glDrawElements(GLES20.GL_TRIANGLES, verticalsIndex.length, GLES20.GL_UNSIGNED_SHORT, verticalsIndexBuffer);
            GLES20.glDisableVertexAttribArray(mPositionHandle);

        }
    }
}

你可能感兴趣的:(opengl es2.0绘制球体)