增加color mode

package com.sunny;
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=0.9f;//用浮点数来定义RGB颜色系统中的每一个颜色
private float _green=0.2f;
private float _blue=0.2f;

private ShortBuffer _indexBuffer;//保存索引
private ShortBuffer _indexBufferStatic;

private FloatBuffer _vertexBuffer;//为三角形保存坐标
private FloatBuffer _vertexBufferStatic;

private FloatBuffer _colorBuffer;
private short[] _indicesArray={0,1,2};
private int _nrOfVertices=3;//定义需要多少个顶点.对于一个三角形来说,一共需要三个顶点

private float _angle;
public void setAngle(float angle){
_angle=angle;
}


@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {//surface创建以后调用
// TODO Auto-generated method stub
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//设置OpenGL使用vertex数组来画
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
initTriangle();
//initStaticTriangle();
}

@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {//surface发生改变以后调用,例如从竖屏切换到横屏的时候
// TODO Auto-generated method stub
gl.glViewport(0,0,w,h);
}

@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);

gl.glRotatef(_angle, 0f, 1f, 0f);//绕y轴旋,参数中的值表示一个向量,标志三角形绕着旋转的坐标轴
// set the color of our element 设置三角形为暗红色
//gl.glColor4f(0.5f, 0f, 0f, 0.5f);
// define the vertices we want to draw
/*使用glVertexPointer()初始化Vertex Pointer.
* 第一个参数是大小,也是顶点的维数。我们使用的是x,y,z三维坐标。
* 第二个参数,GL_FLOAT定义buffer中使用的数据类型。
* 第三个变量是0,是因为我们的坐标是在数组中紧凑的排列的,没有使用offset。
* 最后第四个参数顶点缓冲。
*/
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
// finally draw the vertices
/*glDrawElements()将所有这些元素画出来。
* 第一个参数定义了什么样的图元将被画出来。
* 第二个参数定义有多少个元素,
* 第三个是indices使用的数据类型。
* 最后一个是绘制顶点使用的索引缓冲。
*/
gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer);

gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);

}
public void setColor(float r, float g, float b) {
_red = r;
_green = g;
_blue = b;
}
private void initTriangle(){
//为这里两个buffer分配必须的内存
// float has 4 bytes
ByteBuffer vbb=ByteBuffer.allocateDirect(_nrOfVertices*3*4);
vbb.order(ByteOrder.nativeOrder());
_vertexBuffer=vbb.asFloatBuffer();
// short has 2 bytes
ByteBuffer ibb=ByteBuffer.allocateDirect(_nrOfVertices*2);
ibb.order(ByteOrder.nativeOrder());
_indexBuffer=ibb.asShortBuffer();

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)
0f,0.5f,0f// (x3, y3, z3)
};
float[] colors={//RGBA(Red,Green,Blue,alpha)
1f,0f,0f,1f,
0f,1f,0f,1f,
0f,0f,1f,1f
};
_vertexBuffer.put(coords);
_indexBuffer.put(_indicesArray);
_colorBuffer.put(colors);

_vertexBuffer.position(0);
_indexBuffer.position(0);
_colorBuffer.position(0);
}


}

更多相关文章

  1. Android(安卓)Studio 使用技巧(1)
  2. Android(安卓)Studio 使用技巧(3)
  3. swig android的使用
  4. Android:自定义toast
  5. Android(安卓)反射获得控件对象
  6. Android(安卓)weex的使用
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Android(安卓)SDCard Mount 流程分析(一)
  2. Android(安卓)应用程序快速启动的秘诀
  3. 【Android(安卓)开发】: Android(安卓)消
  4. android中的SVG图像的各个属性意义
  5. android局部更新(RecyclerView+ DiffUtil
  6. RelativeLayout 相对布局
  7. Android中View的事件执行机制
  8. android之LinearLayout中android:layout_
  9. Android应用程序消息处理机制(Looper、Han
  10. 系出名门Android(1) - 在 Windows 下搭建