android OpenGLES开发第二课 为Polygon添加颜色
16lz
2021-01-26
第一课里是一个简单的正方形的绘制,现在我们要为这个正方形添加颜色。
唯一的不同(和上一课比较,以后同意)就是在Polygon类的draw方法中添加了如下内容:
public void draw(GL10 gl) {// Set the face rotationgl.glFrontFace(GL10.GL_CW);// Point to our vertex buffergl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);// Enable vertex buffergl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// Set The Color To Bluegl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f); // the new code// Draw the vertices as triangle stripgl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);// Disable the client state before leavinggl.glDisableClientState(GL10.GL_VERTEX_ARRAY);}
当然也可以通过下面的方法为Polygon添加颜色,和使用顶点数组一样,我们可以建立一个颜色的数组,具体实现如下:
public class Polygon {/** * The buffer holding the Polygon‘s vertices * * 保存Polygon对象顶点坐标的FloatBuffer */private FloatBuffer vertexBuffer;/** * The initial vertex definition * * 保存Polygon对象顶点坐标的的float数组 */private float vertices[] = { 0.0f, 1.0f, 0.0f, // Top-1.0f, -1.0f, 0.0f, // Bottom Left1.0f, -1.0f, 0.0f // Bottom Right};/** The buffer holding the colors */private FloatBuffer colorBuffer;/** The initial color definition */private float colors[] = { 1.0f, 0.0f, 0.0f, 1.0f, // Set The Color To Red,last value 100% luminance0.0f, 1.0f, 0.0f, 1.0f, // Set The Color To Green, last value 100% luminance0.0f, 0.0f, 1.0f, 1.0f // Set The Color To Blue, last value 100% luminance};/** * The Triangle constructor. * * Initiate the buffers. *///在我们的Polygon类构造方法中为colorBuffer初始化public Polygon() {// this is the common method to initiate the FloatBuffer// 下面是一种常用的初始化FloatBuffer的方法,本人还见到过一种方法,如下:// vertexBuffer = FloatBuffer.wrap(vertices)// 但是如果用这种方法在运行的时候会报错,指出你的FloatBuffer没有序列化,// 不明白原因,如有明白的高手帮解释一下,不胜感激ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);byteBuf.order(ByteOrder.nativeOrder());vertexBuffer = byteBuf.asFloatBuffer();vertexBuffer.put(vertices);vertexBuffer.position(0);byteBuf = ByteBuffer.allocateDirect(colors.length * 4);byteBuf.order(ByteOrder.nativeOrder());colorBuffer = byteBuf.asFloatBuffer();colorBuffer.put(colors);colorBuffer.position(0);}/** * The object own drawing function. Called from the renderer to redraw this * instance with possible changes in values. * * @param gl * - The GL context */// 在draw方法中添加对color的权限public void draw(GL10 gl) {// Set the face rotationgl.glFrontFace(GL10.GL_CW);// Point to our buffersgl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer); //the new code// Enable the vertex and color stategl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_COLOR_ARRAY); //the new code// Draw the vertices as trianglesgl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);// Disable the client state before leavinggl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisableClientState(GL10.GL_COLOR_ARRAY); //the new code}}
这样我们就绘制了一个带有颜色的Polygon。
更多相关文章
- android handler机制源码解析【异步回调】
- Android连续获取当前所连接WiFi及周围热点列表信息的解决方案
- Android(安卓)studio(Windows)快捷键
- Android之dialog的四种形式
- [Android]Activity生命周期之三大循环|五种状态|七种方法
- Android(安卓)对返回按键点击次数的监听
- 一个常见Android崩溃问题的分析
- 全面了解Activity
- Android事件分发机制(一)