package com.sunny;

import android.app.Activity;
import android.os.Bundle;

public class mainActivity extends Activity {
private static final String LOG_TAG=mainActivity.class.getSimpleName();
private VortexView _vortexView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_vortexView=new VortexView(this);
setContentView(_vortexView);
}
}

VortexView

package com.sunny;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

public class VortexView extends GLSurfaceView {//继承了GLSurfaceView是因为它会帮助我们画3D图像
private static final String LOG_TAG=VortexView.class.getSimpleName();
private VortexRenderer _renderer;//一个Renderer包含画一帧所必需的所有东西
public VortexView(Context context) {
super(context);
// TODO Auto-generated constructor stub
_renderer=new VortexRenderer();
setRenderer(_renderer);
}
@Override
public boolean onTouchEvent(final MotionEvent event) {
// TODO Auto-generated method stub
queueEvent(new Runnable(){

@Override
public void run() {//调用renderer中的setColor方法
// TODO Auto-generated method stub
_renderer.setColor(event.getX()/getWidth(), event.getY()/getHeight(), 1.0f);
_renderer.setAngle(event.getX()/10);
}

});
return true;
}

}

VortexRenderer

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 FloatBuffer _vertexBuffer;//为三角形保存坐标
private short[] _indicesArray={0,1,2};
private int _nrOfVertices=3;//定义需要多少个顶点.对于一个三角形来说,一共需要三个顶点

private float _angle;
public void setAngle(float angle){
_angle=angle;
}
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();

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)
};
_vertexBuffer.put(coords);
_indexBuffer.put(_indicesArray);

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


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

@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) {//当任何时候调用一个画图方法的时候
// TODO Auto-generated method stub
// set rotation
gl.glRotatef(_angle, 0f, 1f, 0f);//绕y轴旋,参数中的值表示一个向量,标志三角形绕着旋转的坐标轴

// define the color we want to be displayed as the "clipping wall"
gl.glClearColor(_red, _green, _blue, 1.0f);
// clear the color buffer to show the ClearColor we called above...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// 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.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);

}

}

wps_clip_image-7498

更多相关文章

  1. android 关于 发送post请求的代码总结(包含加上参数)
  2. Android Activity间用Intent和Bundle传递参数
  3. Android 线程的使用(传递多个参数)
  4. Android 中Activity 之间传递参数
  5. Android camera预览参数以及实际图像大小设置
  6. android中AudioRecord采集音频的参数说明以及audioTrack的播放
  7. android 开发使用 kotlin 进行点击事件监听和界面跳转,直接传也方

随机推荐

  1. c语言函数由哪两部分组成
  2. c语言在gcc中怎么运行程序?
  3. c语言的源程序不必通过编译就可以执行对
  4. fgetc函数的作用详解
  5. C语言中的文件类型只有哪两种
  6. c#语言为什么用的人很少
  7. 三分钟带你了解选择排序和冒泡排序的用法
  8. c语言strcmp函数用法详解
  9. c语言中int范围是多少?
  10. C语言中switch语句的case后能否是一个关