增加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);
}


}

image_thumb

更多相关文章

  1. android LayoutInflater.inflate()的参数及其功能
  2. appium启动APP配置参数:
  3. android中AudioRecord采集音频的参数说明
  4. Android Jetpack应指南学习笔记8——Navigation(二)使用safe arg
  5. android上传图片和参数(属性)到服务器
  6. Android jni中数组参数的传递方式
  7. Android 访问Webservice接口,参数对象不能串行化问题解决(java.lan
  8. Android中main.xml界面参数笔记
  9. android中LayoutParams设置参数的理解

随机推荐

  1. Android开发黄金搭档:Android(安卓)Studio
  2. 一步步探索学习Android(安卓)Touch事件分
  3. android在apk中获取root权限,并执行命令
  4. 【Android】下拉刷新实现
  5. Android启动Application onCreate走了多
  6. Android(安卓)sdk版本以及兼容性问题
  7. Android之3D动画效果
  8. Android(安卓)App 构建流程分析
  9. Android(安卓)自定义Switch开关按钮的样
  10. 使用Ant自动构建Android版本