三角形为OpenGL ES支持的面,同样创建一个DrawTriangle Activity,定义6个顶点使用三种不同模式来绘制三角形:
float vertexArray[] = {
-0.8f, -0.4f * 1.732f, 0.0f,
0.0f, -0.4f * 1.732f, 0.0f,
-0.4f, 0.4f * 1.732f, 0.0f, 0.0f, -0.0f * 1.732f, 0.0f,
0.8f, -0.0f * 1.732f, 0.0f,
0.4f, 0.4f * 1.732f, 0.0f,
};
本例绘制
public void DrawScene(GL10 gl) {
super.DrawScene(gl); ByteBuffer vbb
= ByteBuffer.allocateDirect(vertexArray.length*);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArray);
vertex.position(); gl.glLoadIdentity();
gl.glTranslatef(, , -); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(, GL10.GL_FLOAT, , vertex);
index++;
index%=;
switch(index){
case :
case :
case :
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLES, , );
break;
case :
case :
case :
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, , );
break;
case :
case :
case :
case :
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, , );
break;
} gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); }
这里index 的目的是为了延迟一下显示(更好的做法是使用固定时间间隔)。前面说过GLSurfaceView 的渲染模式有两种,一种是连续不断的更新屏幕,另一种为on-demand ,只有在调用requestRender() 在更新屏幕。 缺省为RENDERMODE_CONTINUOUSLY 持续刷新屏幕。
OpenGLDemos 使用的是缺省的RENDERMODE_CONTINUOUSLY持续刷新屏幕 ,因此Activity的drawScene 会不断的执行。本例中屏幕上顺序以红,绿,蓝色显示TRIANGLES, TRIANGLE_STRIP,TRIANGLE_FAN。
完整代码:
MainActivity.java
package com.example.gltest; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager; public class MainActivity extends Activity
implements IOpenGLDemo{
float vertexArray[] = {
-0.8f, -0.4f * 1.732f, 0.0f,
-0.4f, 0.4f * 1.732f, 0.0f,
0.0f, -0.4f * 1.732f, 0.0f,
0.4f, 0.4f * 1.732f, 0.0f,
}; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setRenderer(new OpenGLRenderer(this));
setContentView(mGLSurfaceView);
} // public void DrawScene(GL10 gl) {
// gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
// // Clears the screen and depth buffer.
// gl.glClear(GL10.GL_COLOR_BUFFER_BIT
// | GL10.GL_DEPTH_BUFFER_BIT);
//
// }
public void DrawScene(GL10 gl) {
//super.DrawScene(gl); ByteBuffer vbb
= ByteBuffer.allocateDirect(vertexArray.length*);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertex = vbb.asFloatBuffer();
vertex.put(vertexArray);
vertex.position(); gl.glLoadIdentity();
gl.glTranslatef(, , -); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(, GL10.GL_FLOAT, , vertex);
int index = ;
index ++;
index%=;
switch(index){
case :
case :
case :
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLES, , );
break;
case :
case :
case :
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, , );
break;
case :
case :
case :
case :
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, , );
break;
} gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); }
@Override
protected void onResume() {
// Ideally a game should implement
// onResume() and onPause()
// to take appropriate action when the
//activity looses focus
super.onResume();
mGLSurfaceView.onResume();
} @Override
protected void onPause() {
// Ideally a game should implement onResume()
//and onPause()
// to take appropriate action when the
//activity looses focus
super.onPause();
mGLSurfaceView.onPause();
} private GLSurfaceView mGLSurfaceView; }
后面的 OpenGLRenderer.java和 IOpenGLDemo.java一样。