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