美文网首页
opengl es2.0绘制多边形

opengl es2.0绘制多边形

作者: Tenny1225 | 来源:发表于2016-08-31 20:16 被阅读415次
    import android.app.Activity;
    import android.graphics.Color;
    import android.opengl.GLES20;
    import android.opengl.GLSurfaceView;
    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 OPENGLTestActivity extends Activity {
        GLSurfaceView glSurfaceView;
       //定点着色器
        public static String VL =  "attribute vec4 vPosition;\n"
                + "void main() {\n"
                + "  gl_Position = 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,
            };
    
            ShortBuffer verticalsIndexBuffer;
           //每个三角形的定点编号,多边形有两个三角形组成
            short[] verticalsIndex = new short[]{
                    0, 1, 2,
                    0, 2, 3
            };
            private int mProgram;
            private int mPositionHandle;
            private int mColorHandle;
            public RenderListener() {
                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");
    
            }
    
            @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.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
                GLES20.glDrawElements(GLES20.GL_TRIANGLES, verticalsIndex.length, GLES20.GL_UNSIGNED_SHORT, verticalsIndexBuffer);
                GLES20.glDisableVertexAttribArray(mPositionHandle);
    
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:opengl es2.0绘制多边形

          本文链接:https://www.haomeiwen.com/subject/hgccettx.html