画一个三角形一个正方形
建立渲染器类
package com.example.demo_opengl; 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.opengl.GLSurfaceView.Renderer; public class MyRenderer implements Renderer { int one = 0x10000; // 一定要16进制,16 int one1 = 0x10000 / 2; // 一定要16进制,16 private IntBuffer triangleBuffer; // 缓冲三角形 private int[] triangleVertices = new int[] { 0, one, 0, -one, -one, 0, one, -one, 0 }; // 三角形的三个顶点,上,左下,右下 // 正方形 private IntBuffer quaterBuffer; private int[] quaterVertices = new int[] { one, one, 0, -one, one, 0, one, -one, 0, -one, -one, 0 }; // 四边形,右上,左上,右下,左下 // 正方形2 private IntBuffer quaterBuffer2; private int[] quaterVertices2 = new int[] { one1, one1, 10, -one1, one1, 10, one1, -one1, 10, -one1, -one1, 10 }; // 四边形,右上,左上,右下,左下 @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // 初始化三角形 // ByteBuffer位于java.nio包中,目前提供了Java基本类型中除Boolean外其他类型的缓冲类型,比如ByteBuffer、DoubleBuffer、FloatBuffer、IntBuffer、LongBuffer和ShortBuffer // 。一个int类占4字节所以要*4 ByteBuffer byteBuffer = ByteBuffer .allocateDirect(triangleVertices.length * 4); // 为缓冲区分配空间 byteBuffer.order(ByteOrder.nativeOrder()); triangleBuffer = byteBuffer.asIntBuffer(); triangleBuffer.put(triangleVertices); // 顶点坐标放入intbuffer triangleBuffer.position(0); // 缓冲区指针指向第一个字节位置 // 画正方形---------------------------------------------- byteBuffer = ByteBuffer.allocateDirect(quaterVertices.length * 4); byteBuffer.order(ByteOrder.nativeOrder()); quaterBuffer = byteBuffer.asIntBuffer(); quaterBuffer.put(quaterVertices); quaterBuffer.position(0); // 画正方形2---------------------------------------------- byteBuffer = ByteBuffer.allocateDirect(quaterVertices2.length * 4); byteBuffer.order(ByteOrder.nativeOrder()); quaterBuffer2 = byteBuffer.asIntBuffer(); quaterBuffer2.put(quaterVertices2); quaterBuffer2.position(0); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // 改变时 float ratio = (float) width / height; // OpenGL场景大小 gl.glViewport(0, 0, width, height); // 投影矩阵 gl.glMatrixMode(GL10.GL_PROJECTION); // 重置投影矩阵 gl.glLoadIdentity(); // x,y,z轴可以设置的最远距离 gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); // 选择模型观察矩阵 gl.glMatrixMode(GL10.GL_MODELVIEW); // 重置模型观察矩阵 gl.glLoadIdentity(); } @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.glLoadIdentity(); gl.glTranslatef(1.5f, 0.0f, -6.0f); // 右移1.5,移入屏幕6 gl.glVertexPointer(3, GL10.GL_FIXED, 0, triangleBuffer); // 设置三角形顶点坐标(维度,顶点类型,步长,缓存) gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); // 绘制三角形 // 画正方形1******************************** // gl.glLoadIdentity(); // gl.glTranslatef(-1.0f, 0.0f, -6.0f); // 左移2,移入屏幕6 // // gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer); // // gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); // GL_TRIANGLE_STRIP规则绘制四边形 // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4); //GL_TRIANGLE_FAN规则绘制四边形 // 画正方形2******************************** gl.glLoadIdentity();//刷新,重置观察矩阵 gl.glTranslatef(-4.0f, 0.0f, -6.0f); // 左移4.0,移入屏幕6 gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer2); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); // GL_TRIANGLE_STRIP规则绘制四边形 // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4); //GL_TRIANGLE_FAN规则绘制四边形 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // 关闭顶点设置 } }
在主函数中调用
1.新建GLSurfaceView
2.新建渲染器类MyRenderer()
3.view加载渲染器并显示
package com.example.demo_opengl; import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView.Renderer; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 新建GLSurfaceView类,渲染器类,载入GLSurfaceView类 GLSurfaceView glSurfaceView = new GLSurfaceView(this); Renderer myRenderer = new MyRenderer(); glSurfaceView.setRenderer(myRenderer); setContentView(glSurfaceView); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }