Android(安卓)3D 旋转的三角形(三)
立体
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 ShortBuffer _indexBuffer;//保存索引
private FloatBuffer _vertexBuffer;//保存定点坐标
private FloatBuffer _colorBuffer;
//private short[] _indicesArray={0,1,2};
private int _nrOfVertices=0;//定义需要多少个顶点.对于一个三角形来说,一共需要三个顶点
private float _xAngle;
private float _yAngle;
public float getXAngle() {
return _xAngle;
}public void setXAngle(float angle) {
this._xAngle = angle;
}public float getYAngle() {
return _yAngle;
}public void setYAngle(float angle) {
this._yAngle = angle;
}@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {//surface创建以后调用
// TODO Auto-generated method stub
// enable the differentiation of which side may be visible
gl.glEnable(GL10.GL_CULL_FACE);//enable了culling面,以保证只有一面
// which is the front? the one which is drawn counter clockwise
gl.glFrontFace(GL10.GL_CCW);//GL_CCW表示逆时针
// which one should NOT be drawn
gl.glCullFace(GL10.GL_BACK);//GL_FRONT_AND_BACK
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
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) {//当任何时候调用一个画图方法的时候
// define the color we want to be displayed as the "clipping wall"
gl.glClearColor(0f, 0f, 0f, 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
gl.glRotatef(_xAngle, 1f, 0f, 0f);
gl.glRotatef(_yAngle, 0f, 1f, 0f);
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[] coords={//坐标
-0.5f, -0.5f, 0.5f, // 0
0.5f, -0.5f, 0.5f, // 1
0f, -0.5f, -0.5f, // 2
0f, 0.5f, 0f, // 3
};
_nrOfVertices=coords.length;//更加的动态
float[] colors={//颜色
1f, 0f, 0f, 1f, // point 0 red
0f, 1f, 0f, 1f, // point 1 green
0f, 0f, 1f, 1f, // point 2 blue
1f, 1f, 1f, 1f, // point 3 white
};
short[] indices=new short[]{//定点数
0, 1, 3, // rwg
0, 2, 1, // rbg
0, 3, 2, // rbw
1, 2, 3, // bwg
};
//为这里两个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();
_vertexBuffer.put(coords);
_indexBuffer.put(indices);
_colorBuffer.put(colors);
_vertexBuffer.position(0);
_indexBuffer.position(0);
_colorBuffer.position(0);
}
}
更多相关文章
- Android中Adapter中edittext,checkbox记住状态解决方案(一)
- FFmpeg 调用 Android(安卓)MediaCodec 进行硬解码(附源码)
- 调用Android(安卓)installer 安装和卸载程序
- Android调用系统相机和图库
- AIDL (Android(安卓)Interface Definition Language) Android(安
- Android(安卓)APK应用安装原理(1)-解析AndroidManifest原理-Pack
- Android应用开发提高系列(4)——Android动态加载(上)——加载未安装A
- Android(安卓)中Activity,Window和View之间的关系
- Android应用开发提高系列(4)——Android动态加载(上)——加载未安装A