1.建立坐标着色点数组
// 定义渐变的顶点颜色 float[] colors = { 1f, 0f, 0f, 1f, // vertex 0 red 0f, 1f, 0f, 1f, // vertex 1 green 0f, 0f, 1f, 1f, // vertex 2 blue 1f, 0f, 1f, 1f, // vertex 3 magenta }; // Our color buffer.颜色类的缓冲变量 private FloatBuffer colorBuffer;
2.建立颜色缓冲区
// float has 4 bytes, colors (RGBA) * 4 bytes,颜色放入字节缓存 ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); cbb.order(ByteOrder.nativeOrder()); colorBuffer = cbb.asFloatBuffer(); colorBuffer.put(colors); colorBuffer.position(0);
3.调用着色方法
gl.glEnableClientState(GL10.GL_COLOR_ARRAY); // NEW LINE ADDED.开启渲染功能 // Point out the where the color buffer is.指定颜色渲染缓存 gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); // NEW LINE ADDED. gl.glVertexPointer(3, GL10.GL_FIXED, 0, triangleBuffer); // 设置三角形顶点坐标(维度,顶点类型,步长,缓存) gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); // 绘制三角形 gl.glDisableClientState(GL10.GL_COLOR_ARRAY); // 关闭颜色渲染
package com.example.demo_opengl; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer; public class Renderer2 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 }; // 四边形,右上,左上,右下,左下 // 定义渐变的顶点颜色 float[] colors = { 1f, 0f, 0f, 1f, // vertex 0 red 0f, 1f, 0f, 1f, // vertex 1 green 0f, 0f, 1f, 1f, // vertex 2 blue 1f, 0f, 1f, 1f, // vertex 3 magenta }; // Our color buffer.颜色类的缓冲变量 private FloatBuffer colorBuffer; @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); // float has 4 bytes, colors (RGBA) * 4 bytes,颜色放入字节缓存 ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); cbb.order(ByteOrder.nativeOrder()); colorBuffer = cbb.asFloatBuffer(); colorBuffer.put(colors); colorBuffer.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.glColor4f(0.0f, 1.0f, 0.0f, 1.0f); // 設置當前色為绿色 gl.glEnableClientState(GL10.GL_COLOR_ARRAY); // NEW LINE ADDED.开启渲染功能 // Point out the where the color buffer is.指定颜色渲染缓存 gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); // NEW LINE ADDED. gl.glVertexPointer(3, GL10.GL_FIXED, 0, triangleBuffer); // 设置三角形顶点坐标(维度,顶点类型,步长,缓存) gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); // 绘制三角形 gl.glDisableClientState(GL10.GL_COLOR_ARRAY); // 关闭颜色渲染 // 画正方形1******************************** gl.glLoadIdentity(); gl.glTranslatef(-1.0f, 0.0f, -6.0f); // 左移2,移入屏幕6 // gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // 設置當前色為蓝 gl.glEnableClientState(GL10.GL_COLOR_ARRAY); // NEW LINE ADDED.开启渲染功能 gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); gl.glVertexPointer(3, GL10.GL_FIXED, 0, quaterBuffer); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); // 关闭颜色渲染 // GL_TRIANGLE_STRIP规则绘制四边形 // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4); //GL_TRIANGLE_FAN规则绘制四边形 // 画正方形2******************************** gl.glLoadIdentity();// 刷新,重置观察矩阵 gl.glTranslatef(-3.0f, 0.0f, -6.0f); // 左移4.0,移入屏幕6 gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // 設置當前色為红色 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); // 关闭顶点设置 } }