package
com.terry;
import
java.nio.IntBuffer;
import
javax.microedition.khronos.egl.EGLConfig;
import
javax.microedition.khronos.opengles.GL10;
import
android.opengl.GLSurfaceView.Renderer;
public
class
GLRender
implements
Renderer{
float
rotateTri,rotateQuad;
int
one
=
0x10000
;
//
三角形的一個頂點
private
IntBuffer triggerBuffer
=
IntBuffer.wrap(
new
int
[]{
0
,one,
0
,
//
上頂點
-
one,
-
one,
0
,
//
左頂點
one,
-
one,
0
//
右下點
});
//
正方形的四個頂點
private
IntBuffer quateBuffer
=
IntBuffer.wrap(
new
int
[]{
one,one,
0
,
-
one,
-
one,
0
,
one,
-
one,
0
,
-
one,
-
one,
0
});
private
IntBuffer colorBuffer
=
IntBuffer.wrap(
new
int
[]{
one,
0
,
0
,one,
0
,one,
0
,one,
0
,
0
,one,one
});
@Override
public
void
onDrawFrame(GL10 gl) {
//
TODO Auto-generated method stub
//
清除屏幕和深度緩存
gl.glClear(GL10.GL_COLOR_BUFFER_BIT
|
GL10.GL_DEPTH_BUFFER_BIT);
//
重置當前的模型觀察矩陣
gl.glLoadIdentity();
//
左移 1.5 單位,並移入屏幕 6.0
gl.glTranslatef(
-
1.5f
,
0.0f
,
-
6.0f
);
//
設置旋轉
gl.glRotatef(rotateTri,
0.0f
,
1.0f
,
0.0f
);
//
設置定點數組
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//
設置顏色數組
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glColorPointer(
4
, GL10.GL_FIXED,
0
, colorBuffer);
//
設置三角形頂點
gl.glVertexPointer(
3
, GL10.GL_FIXED,
0
, triggerBuffer);
//
繪製三角形
gl.glDrawArrays(GL10.GL_TRIANGLES,
0
,
3
);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
//
繪製三角形結束
gl.glFinish();
/**
********************
*/
/*
渲染正方形
*/
//
重置當前的模型觀察矩陣
gl.glLoadIdentity();
//
左移 1.5 單位,並移入屏幕 6.0
gl.glTranslatef(
1.5f
,
0.0f
,
-
6.0f
);
//
設置當前色爲藍色
gl.glColor4f(
0.5f
,
0.5f
,
1.0f
,
1.0f
);
//
設置旋轉
gl.glRotatef(rotateQuad,
1.0f
,
0.0f
,
0.0f
);
//
設置和繪製正方形
gl.glVertexPointer(
3
, GL10.GL_FIXED,
0
, quateBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,
0
,
4
);
//
繪製正方形結束
gl.glFinish();
//
取消頂點數組
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
//
改變旋轉的角度
rotateTri
+=
0.5f
;
rotateQuad
-=
0.5f
;
}
@Override
public
void
onSurfaceChanged(GL10 gl,
int
width,
int
height) {
//
TODO Auto-generated method stub
float
ratio
=
(
float
) width
/
height;
//
設置OpenGL場景的大小
gl.glViewport(
0
,
0
, width, height);
//
設置投影矩陣
gl.glMatrixMode(GL10.GL_PROJECTION);
//
重置投影矩陣
gl.glLoadIdentity();
//
設置視口的大小
gl.glFrustumf(
-
ratio, ratio,
-
1
,
1
,
1
,
10
);
//
選擇模型觀察矩陣
gl.glMatrixMode(GL10.GL_MODELVIEW);
//
重置模型觀察矩陣
gl.glLoadIdentity();
}
@Override
public
void
onSurfaceCreated(GL10 gl, EGLConfig config) {
//
TODO Auto-generated method stub
//
啓用陰影平滑
gl.glShadeModel(GL10.GL_SMOOTH);
//
黑色背景
gl.glClearColor(
0
,
0
,
0
,
0
);
//
設置深度緩存
gl.glClearDepthf(
1.0f
);
//
啓用深度測試
gl.glEnable(GL10.GL_DEPTH_TEST);
//
所做深度測試的類型
gl.glDepthFunc(GL10.GL_LEQUAL);
//
告訴系統對透視進行修正
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
}
}