android游戏开发(OpenGL ES绘制矩形平面)

来源:互联网 发布:写简历的软件 编辑:程序博客网 时间:2024/04/30 16:20

接触android将近一年了,以前学的应用开发,现在自学android游戏开发,把自己学到的分享出来一下,(这也是我的第一篇博客),不说废话了,开始正文:

GLRender类用于图形的渲染工作,Util类用于glrender中的数据缓冲。

GLRender类:

package com.example.xu.vertexdraw;public class GLRender implements GLSurfaceView.Renderer {    public float xrot;    public float yrot;    public float zrot;    //定义顶点缓冲    private FloatBuffer _vertexsBuffer;    //定义顶点缓冲    private FloatBuffer _vertexsBUffer;    //定义索引缓冲    private ByteBuffer _indicesBuffer;    public void onSurfaceCreated(GL10 gl,EGLConfig config){        //启用阴影平滑        gl.glShadeModel(GL10.GL_SMOOTH);        //黑色背景        gl.glClearColor(0,0,0,0);        //设置深度缓存        gl.glClearDepthf(1.0f);        //启用深度测试        gl.glEnable(GL10.GL_DEPTH_TEST);        //所做深度测试的内容        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST);        //创建顶点数据        float vertexs[] = new float[12];        vertexs[0] = 1.0f;        vertexs[1] = 1.0f;        vertexs[2] = 0.0f;        vertexs[3] = -1.0f;        vertexs[4] = 1.0f;        vertexs[5] = 0.0f;        vertexs[6] = -1.0f;        vertexs[7] = -1.0f;        vertexs[8] = 0.0f;        vertexs[9] = 1.0f;        vertexs[10] = -1.0f;        vertexs[11] = 0.0f;        //创建索引数据        byte indices[] = new byte[6];        indices[0] = 0;        indices[1] = 1;        indices[2] = 2;        indices[3] = 0;        indices[4] = 2;        indices[5] = 3;        //创建顶点缓冲        this._vertexsBuffer = Util.getFloatBuffer(vertexs);        //创建索引缓冲        this._indicesBuffer = Util.getByteBUffer(indices);    }    public void onDrawFrame(GL10 gl){        //清除屏幕和深度缓存        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);        //重置当前的模型观察矩阵        gl.glLoadIdentity();        //重置当前的模型观察矩阵        gl.glLoadIdentity();       //移入屏幕的一段距离        gl.glTranslatef(0.0f,0.0f,-3.0f);        //设置三个方向的旋转        gl.glRotatef(xrot,1.0f,0.0f,0.0f);        gl.glRotatef(yrot,0.0f,1.0f,0.0f);        gl.glRotatef(zrot,0.0f,0.0f,1.0f);        //允许设置顶点        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);        gl.glVertexPointer(3,GL10.GL_FLOAT,0,this._vertexsBuffer);        //索引绘制法        gl.glDrawElements(GL10.GL_TRIANGLES,6,GL10.GL_UNSIGNED_BYTE,this._indicesBuffer);        //取消顶点设置        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);        //结束绘制        gl.glFinish();    }    public void onSurfaceChanged(GL10 gl,int width,int heigth){        float ratio = (float) width / heigth;        //设置opengl场景的大小        gl.glViewport(0,0,width,heigth);        //设置投影矩阵        gl.glMatrixMode(GL10.GL_PROJECTION);        //重置投影矩阵        gl.glLoadIdentity();        //设置视口大小        gl.glFrustumf(-ratio,ratio,-1,1,1,10);        //选择模型观察矩阵        gl.glMatrixMode(GL10.GL_MODELVIEW);        //重置模型观察矩阵        gl.glLoadIdentity();    }}

Util类:

public class Util {    public static FloatBuffer getFloatBuffer(float[] vertexs) {        FloatBuffer buffer;        ByteBuffer qbb = ByteBuffer.allocateDirect(vertexs.length * 18);        qbb.order(ByteOrder.nativeOrder());        buffer = qbb.asFloatBuffer();        buffer.put(vertexs);        buffer.position(0);        return buffer;    }    public static ByteBuffer getByteBUffer(byte[] indices) {        ByteBuffer buffer = null;        buffer = ByteBuffer.allocateDirect(indices.length);        buffer.put(indices);        buffer.position(0);        return buffer;    }}

还需新建MySurfaceView类用于获取用户输入,实现人机交互。

(求大神勿喷,第一次写)

0 0
原创粉丝点击