应用举例
利用上面讲解的内容给出一个Ophone上绘制一个3D球形的程序。效果图如下:

图2 球形示例
主要的绘制程序:
- static private FloatBuffer vertex;//顶点对应的字节缓冲
- static private FloatBuffer normal;//法向量对应的字节缓冲
- float[] lightPos = new float[] {10.0f, 10.0f, 10.0f, 1.0f };//光源的坐标
- private static final int STEP = 24;//
- private static final float RADIUS = 1.0f;//半径
- protected void init(GL10 gl) {
- gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);//设置背景颜色
- gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);
- gl.glEnable(GL10.GL_LIGHTING);//启用光照
- gl.glEnable(GL10.GL_LIGHT0); //打开光源
- gl.glClearDepthf(1.0f);//设置深度缓存
- gl.glDepthFunc(GL10.GL_LEQUAL);//设置深度缓存比较函数,GL_LEQUAL表示新的像素的深度缓存值小于等于当前像素的深度缓存值时通过深度测试
- gl.glEnable(GL10.GL_DEPTH_TEST);//启用深度缓存
- gl.glEnable(GL10.GL_CULL_FACE);
- gl.glShadeModel(GL10.GL_SMOOTH);//设置阴影模式GL_SMOOTH
- }
- protected void drawFrame(GL10 gl) {
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
- GL10.GL_DEPTH_BUFFER_BIT);
- gl.glMatrixMode(GL10.GL_MODELVIEW);
- gl.glLoadIdentity();
- GLU.gluLookAt(gl, 0, 0, 7f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);//
- drawSphere(gl, RADIUS, STEP, STEP); //绘制球形
- }
public static void gluLookAt (GL10 gl, float eyeX, float eyeY, float eyeZ, float centerX, float centerY, floatcenterZ, float upX, float upY, float upZ)
它共接受三组坐标,分别为eye、 center和up。eye表示我们眼睛在"世界坐标系"中的位置,center表示眼睛"看"的那个点的坐标,up坐标表示观察者本身的方向,如果将观察点比喻成我们的眼睛,那么这个up则表示我们是正立还是倒立异或某一个角度在看,这里是正立方式,所以是{0,1,0}。
- private static void drawSphere(GL10 gl, float radius,
- int stacks, int slices) {
- vertex=allocateFloatBuffer( 4* 6 * stacks * (slices+1) );
- normal=allocateFloatBuffer( 4* 6 * stacks * (slices+1) );
- int i, j, triangles;
- float slicestep, stackstep;
- stackstep = ((float)Math.PI) / stacks;
- slicestep = 2.0f * ((float)Math.PI) / slices;
- for (i = 0; i < stacks; ++i)
- {
- float a = i * stackstep;
- float b = a + stackstep;
- float s0 = (float)Math.sin(a);
- float s1 = (float)Math.sin(b);
- float c0 = (float)Math.cos(a);
- float c1 = (float)Math.cos(b);
- float nv;
- for (j = 0; j <= slices; ++j)
- {
- float c = j * slicestep;
- float x = (float)Math.cos(c);
- float y = (float)Math.sin(c);
- nv=x * s0;
- normal.put(nv);
- vertex.put( nv * radius);
- nv=y * s0;
- normal.put(nv);
- vertex.put( nv * radius);
- nv=c0;
- normal.put(nv);
- vertex.put( nv * radius);
- nv=x * s1;
- normal.put(nv);
- vertex.put( nv * radius);
- nv=y * s1;
- normal.put(nv);
- vertex.put( nv * radius);
- nv=c1;
- normal.put(nv);
- vertex.put( nv * radius);
- }
- }
- normal.position(0);
- vertex.position(0);
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex);
- gl.glNormalPointer(GL10.GL_FLOAT, 0, normal);
- gl.glEnableClientState (GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState (GL10.GL_NORMAL_ARRAY);
- triangles = (slices + 1) * 2;
- for(i = 0; i < stacks; i++)
- gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,
- i * triangles, triangles);
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
- }
- private static FloatBuffer allocateFloatBuffer(int capacity){
- ByteBuffer vbb = ByteBuffer.allocateDirect(capacity);
- vbb.order(ByteOrder.nativeOrder());
- return vbb.asFloatBuffer();
- }
总结
本文介绍了Ophone中利用OpenGL ES绘制图形的基本概念和方法。OpenGL ES中还有很多其他内容,诸如纹理、光照和材质、混合、雾、蒙板、反射、3D模型的加载等。利用OpenGL ES函数可以绘制丰富的图形应用和游戏界面。
