实现GLSurfaceView.Renderer:
public class MyRenderer implements GLSurfaceView.Renderer {
//GLSurfaceView的被创建时回调该方法
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//GL10 is a Interface, GL10 gl???
gl.glDisable(GL10.GL_DITHER);//关闭抗抖动,
}
//GLSurfaceView的大小改变后回调该方法
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
}
//Renderer对象调用该方法绘制GLSurfaceView的当前帧
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
}
}
具体代码如下:::
public class MyRenderer implements Renderer {
float[] triangleData = new float[] { 0.1f, 0.6f, 0.0f, -0.3f, 0.0f, 0.0f,0.3f, 0.1f, 0.0f };
int[] triangleColor = new int[] { 65535, 0, 0, 0, 0, 65535, 0, 0, 0, 0,
65535, 0 };
float[] rectData = new float[] { 0.4f, 0.4f, 0.0f, 0.4f, -0.4f, 0.0f,
-0.4f, 0.4f, 0.0f, -0.4f, -0.4f, 0.0f };
int[] rectColor = new int[] { 0, 65535, 0, 0, 0, 0, 65535, 0, 65535, 0, 0,
0, 65535, 65535, 0, 0 };
float[] rectData2 = new float[] { -0.4f, 0.4f, 0.0f, 0.4f, 0.4f, 0.0f,
0.4f, -0.4f, 0.0f, -0.4f, -0.4f, 0.0f };
float[] pentacle = new float[] { 0.4f, 0.4f, 0.0f, -0.2f, 0.3f, 0.0f, 0.5f,
0.0f, 0f, -0.4f, 0.0f, 0f, -0.1f, -0.3f, 0f };
FloatBuffer triangleDataBuffer;
IntBuffer triangleColorBuffer;
FloatBuffer rectDataBuffer;
IntBuffer rectColorBuffer;
FloatBuffer rectDataBuffer2;
FloatBuffer pentacleBuffer;
private float rotate;
public MyRenderer() {
triangleDataBuffer = FloatBuffer.wrap(triangleData);
rectDataBuffer = FloatBuffer.wrap(rectData);
rectDataBuffer2 = FloatBuffer.wrap(rectData2);
pentacleBuffer = FloatBuffer.wrap(pentacle);
triangleColorBuffer = IntBuffer.wrap(triangleColor);
rectColorBuffer = IntBuffer.wrap(rectColor);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0, 0, 0, 0);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// 设置3d视图的大小以及位置
gl.glViewport(0, 0, width, height);
// 将当前矩阵模型设置为投影矩阵
gl.glMatrixMode(GL10.GL_PROJECTION);
// 初始化单位矩阵
gl.glLoadIdentity();
// 计算透视高度/高度比
float ratio = (float) width / height;
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
}
@Override
public void onDrawFrame(GL10 gl) {
// 清除屏幕缓存和深度缓存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// 启用顶点坐标数据
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// 启用顶点颜色数据
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// 设置当前矩阵堆栈为模型堆栈
gl.glMatrixMode(GL10.GL_MODELVIEW);
// 重置当前模型视图矩阵
gl.glLoadIdentity();
gl.glTranslatef(-0.32f, 0.35f, -1f);
// 设置顶点的位置
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleDataBuffer);
// 设置顶点颜色数据
gl.glColorPointer(4, GL10.GL_FIXED, 0, triangleColorBuffer);
// 根据顶点数画图
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
// 重置当前模型视图矩阵
gl.glLoadIdentity();
gl.glTranslatef(0.6f, 0.8f, -1.5f);
gl.glRotatef(rotate, 0f, 0f, 0.1f);
// 设置顶点的位置
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, rectDataBuffer);
// 设置顶点颜色数据
gl.glColorPointer(4, GL10.GL_FIXED, 0, rectColorBuffer);
// 根据顶点数画图
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
// 重置当前模型视图矩阵
gl.glLoadIdentity();
gl.glTranslatef(-0.4f, -0.5f, -1.5f);
gl.glRotatef(rotate, 0f, 0.2f, 0f);
// 设置顶点的位置
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, rectDataBuffer2);
// 根据顶点数画图
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
// 重置当前模型视图矩阵
gl.glLoadIdentity();
gl.glTranslatef(0.4f, -0.5f, -1.5f);
// 使用纯色填充
gl.glColor4f(1.0f, 0.2f, 0.2f, 0.0f);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
// 设置顶点的位置
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, pentacleBuffer);
// 根据顶点数画图
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 5);
// 绘制结束
gl.glFinish();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
// 旋转度加1
rotate += 1;
}
}
public class RotateRPolygon extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView glView = new GLSurfaceView(this);
MyRenderer myRender = new MyRenderer();
glView.setRenderer(myRender);
setContentView(glView);
}
}