安卓GLSurfaceView使用简单范例

简介: 安卓GLSurfaceView使用简单范例

在AndroidManifest.xml要设置:


<uses-feature    android:glEsVersion="0x00020000" android:required="true"   />

代码:


package net.quantum6.camerafps;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
public class GLSurfaceViewRedSquire extends GLSurfaceView {
    public GLSurfaceViewRedSquire(Context context) {
        this(context, null);
    }
    public GLSurfaceViewRedSquire(Context context, AttributeSet attrs) {
        super(context, attrs);
        setRenderer(new MyRender());
    }
    private static class MyRender implements Renderer {
        private FloatBuffer mVB;
        MyRender() {
            float coords[] = {
                    -0.5f,  0.5f, 0.0f,
                    -0.5f, -0.5f, 0.0f,
                     0.5f, -0.5f, 0.0f,
                     0.5f, -0.5f, 0.0f,
                     0.5f,  0.5f, 0.0f,
                    -0.5f,  0.5f, 0.0f
            };
            ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
            vbb.order(ByteOrder.nativeOrder());
            mVB = vbb.asFloatBuffer();
            mVB.put(coords);
            mVB.position(0);
        }
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        }
        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            gl.glViewport(0, 0, width, height);
        }
        @Override
        public void onDrawFrame(GL10 gl) {
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
            gl.glColor4f   (1.0f, 0.0f, 0.0f, 1.0f);
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVB);
            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
        }
    }
}


目录
相关文章
|
Android开发 UED 数据安全/隐私保护
|
Android开发 Java 数据格式
|
前端开发 Android开发 数据格式