最新版本:Android高级进阶四 Android OpenGL开发多边形
有了上一篇Android高级进阶三 Android OpenGL | ES开发框架的介绍,我们现在就来开发一个最简单的静态三角形和静态四边形为例,绝大部分代码我都加上了注释,看官看就可以了~~
先上效果图:
源码如下:
- package org.ourunix.android.opengltest;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.IntBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.app.Activity;
- import android.opengl.GLSurfaceView;
- import android.opengl.GLSurfaceView.Renderer;
- import android.os.Bundle;
- public class OpenGLTestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- GLSurfaceView glView = new GLSurfaceView(this);
- Renderer render = new GLRender();
- glView.setRenderer(render);
- setContentView(glView);
- }
- public class GLRender implements Renderer{
- private int one = 0x10000;
- private int[] trigger = {0, one, 0, -one, -one, 0, one, -one, 0};
- private int[] quarter = {one, one, 0, -one, one, 0, one, -one, 0,
- -one, -one, 0};
- //准备三角各顶点
- private IntBuffer triggerBuffer = BufferUtil.iBuffer(trigger);
- //准备四边顶点
- private IntBuffer quarterBuffer = BufferUtil.iBuffer(quarter);
- //咳,咳现在开始画图了
- @Override
- public void onDrawFrame(GL10 gl) {
- // TODO Auto-generated method stub
- //清楚屏幕和深度缓存
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- gl.glLoadIdentity();
- //现将屏幕向左移动,用来画三角形
- gl.glTranslatef(-1.5f, 0.0f, -6.0f);
- //允许设置顶点
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- //设置三角形
- gl.glVertexPointer(3, GL10.GL_FIXED, 0, triggerBuffer);
- //绘制三角形
- gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
- //重置
- gl.glLoadIdentity();
- //现将屏幕向左移动,用来画四边形
- gl.glTranslatef(1.5f, 0.0f, -6.0f);
- //设置四边形
- gl.glVertexPointer(3, GL10.GL_FIXED, 0, quarterBuffer);
- //绘制四边形
- gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- }
- //当窗口改变时,调用,至少在创建窗口时调用一次,这边设置下场景大小
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- // TODO Auto-generated method stub
- //设置OpenGL场景大小
- float ratio = (float) width / height;
- gl.glViewport(0, 0, width, height);
- gl.glMatrixMode(GL10.GL_PROJECTION);//设置为投影矩阵模式
- gl.glLoadIdentity();//重置
- gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);//设置视角
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- gl.glLoadIdentity();
- }
- //当窗口被创建时我们可以做些初始化工作
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- // TODO Auto-generated method stub
- //告诉系统对透视进行修正
- gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
- //设置清除屏幕时所用的颜色,参数依次为红、绿、蓝、Alpha值
- gl.glClearColorx(0, 0, 0, 0);
- //启用阴影平滑
- gl.glShadeModel(GL10.GL_SMOOTH);
- //以下是关于深度缓存的设置,非常重要
- gl.glClearDepthf(1.0f);//设置深度缓存
- gl.glEnable(GL10.GL_DEPTH_TEST);//启用深度测试
- gl.glDepthFunc(GL10.GL_LEQUAL);//所做深度测试的类型
- }
- }
- public static class BufferUtil {
- public static IntBuffer intBuffer;
- public static IntBuffer iBuffer(int[] a) {
- // 先初始化buffer,数组的长度*4,因为一个float占4个字节
- ByteBuffer mbb = ByteBuffer.allocateDirect(a.length * 4);
- // 数组排列用nativeOrder
- mbb.order(ByteOrder.nativeOrder());
- intBuffer = mbb.asIntBuffer();
- intBuffer.put(a);
- intBuffer.position(0);
- return intBuffer;
- }
- }
- }
最后关于这个BufferUtil 类,为什么需要?查看这个便知道原因: