Android的OpenGL学习笔记(4)
来源:互联网 发布:域名cname 编辑:程序博客网 时间:2024/04/30 05:54
Android的OpenGL学习笔记(4)
这次主要是给前几次笔记做出的三角形上色呵呵……
其实很简单,只需添加几句话就行了,主要变化在VortexRenderer.java代码中,现把代码贴出来:
- package com.droidnova.android.games.vortex;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.ShortBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLSurfaceView;
- public class VortexRenderer implements GLSurfaceView.Renderer {
- private static final String LOG_TAG = VortexRenderer.class.getSimpleName();
- private float _red = 0f;
- private float _green = 0f;
- private float _blue = 0f;
- // a raw buffer to hold indices allowing a reuse of points.
- private ShortBuffer _indexBuffer;
- // a raw buffer to hold the vertices
- private FloatBuffer _vertexBuffer;
- // a raw buffer to hold the colors
- private FloatBuffer _colorBuffer;
- private short[] _indicesArray = {0, 1, 2};
- private int _nrOfVertices = 3;
- private float _angle;
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- // preparation
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//允许设置顶点
- gl.glEnableClientState(GL10.GL_COLOR_ARRAY);//允许设置颜色
- initTriangle();
- }
- @Override
- public void onSurfaceChanged(GL10 gl, int w, int h) {
- gl.glViewport(0, 0, w, h);
- }
- public void setAngle(float angle) {
- _angle = angle;
- }
- @Override
- public void onDrawFrame(GL10 gl) {
- // define the color we want to be displayed as the "clipping wall"
- gl.glClearColor(_red, _green, _blue, 1.0f);
- // reset the matrix - good to fix the rotation to a static angle
- gl.glLoadIdentity();
- // clear the color buffer to show the ClearColor we called above...
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- // set rotation for the non-static triangle
- gl.glRotatef(_angle, 0f, 1f, 0f);
- // gl.glColor4f(0.5f, 0f, 0f, 0.5f);
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);//设置顶点数组
- gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);//设置颜色数组
- gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
- }
- private void initTriangle() {
- // float has 4 bytes
- ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
- vbb.order(ByteOrder.nativeOrder());
- _vertexBuffer = vbb.asFloatBuffer();
- // short has 4 bytes
- ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);
- ibb.order(ByteOrder.nativeOrder());
- _indexBuffer = ibb.asShortBuffer();
- // float has 4 bytes, 4 colors (RGBA) * number of vertices * 4 bytes
- ByteBuffer cbb = ByteBuffer.allocateDirect(4 * _nrOfVertices * 4);
- cbb.order(ByteOrder.nativeOrder());
- _colorBuffer = cbb.asFloatBuffer();
- float[] coords = {
- -0.5f, -0.5f, 0f, // (x1, y1, z1)
- 0.5f, -0.5f, 0f, // (x2, y2, z2)
- 0.5f, 0.5f, 0f // (x3, y3, z3)
- };
- float[] colors = {
- 1f, 0f, 0f, 1f, // point 1
- 0f, 1f, 0f, 1f, // point 2
- 0f, 0f, 1f, 1f, // point 3
- };
- _vertexBuffer.put(coords);
- _indexBuffer.put(_indicesArray);
- _colorBuffer.put(colors);
- _vertexBuffer.position(0);
- _indexBuffer.position(0);
- _colorBuffer.position(0);
- }
- public void setColor(float r, float g, float b) {
- _red = r;
- _green = g;
- _blue = b;
- }
- }
最终效果图:
- Android的OpenGL学习笔记(4)
- Android的OpenGL学习笔记(1)
- Android的OpenGL学习笔记(2)
- Android的OpenGL学习笔记(3)
- Android的OpenGL学习笔记(5)
- Android的OpenGL学习笔记(6)
- Android OpenGL学习笔记
- Android OpenGL 学习笔记
- Android OpenGL 学习笔记
- 基于Android的OpenGL ES 2.0学习笔记(1)
- 基于Android的OpenGL ES 2.0学习笔记(2)
- android OpenGL学习笔记(一)
- Android OpenGL学习笔记(一)
- Android OpenGL学习笔记(一)
- Android OpenGL 学习笔记 01
- opengl学习笔记(4)
- openGL学习笔记4
- opengl入门教程(精)的学习笔记
- 根基不牢--我的窘迫面试经历
- 编程之美读书笔记_3.10 分层遍历二叉树
- 《我是一只IT小小鸟》书评──将要成为IT小小鸟
- 怀旧的一天
- 【转载】利用smarty生成静态页详解
- Android的OpenGL学习笔记(4)
- 一点感想
- 开心
- 一些话
- 最简单 的 加密算法
- JSP内置对象(9个常用的内置对象)
- 第一次来到这里,感觉真好
- First
- Look and say 源码分析