import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
public class OneRend implements GLSurfaceView.Renderer {
float oneF = 1.0f;
float quadRotate = 0f;
private FloatBuffer triggerBuffer2 = FloatBuffer.wrap(new float[] { 0,
oneF, 0, -oneF, -oneF, 0, oneF, -oneF, 0 });
private FloatBuffer triggerBuffer1 = FloatBuffer.wrap(new float[] { oneF,
oneF, 0, -oneF, oneF, 0, oneF, -oneF, 0, -oneF, -oneF, 0 });
private FloatBuffer color = FloatBuffer.wrap(new float[] { oneF, oneF, 0,
oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0, oneF, -oneF, -oneF, 0,
oneF });
public OneRend() {
}
/**
* 画图
*/
public void onDrawFrame(GL10 gl) {
// 清除深度缓存
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
// 重置观察模式
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
// 平移中心(左移1.5个单位,向屏幕内移6.0个单位)
gl.glTranslatef(-1.5f, 0.0f, -4.0f);
gl.glRotatef(quadRotate, 0, .5f, 0f);
// gl.glColor4f(1f, 0f, 0f, 1f);
// 激活设备状态,准备用顶点的方式来绘制
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(gl.GL_COLOR_ARRAY);
gl.glVertexPointer(3, gl.GL_FLOAT, 0, triggerBuffer2);
gl.glColorPointer(4, gl.GL_FLOAT, 0, color);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
gl.glFinish();
gl.glTranslatef(3.0f, 0.0f, 0.0f);
gl.glRotatef(quadRotate, 0.0f, 1f, 0f);
// gl.glColor4f(0f, 1f, 0f, 1f);
gl.glVertexPointer(3, gl.GL_FLOAT, 0, triggerBuffer1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(gl.GL_COLOR_ARRAY);
quadRotate += .5f;
}
/**
* 大小改变时
*/
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0) {
height = 1;
}
float ratio = (float) width / height;
gl.glViewport(0, 0, width, height);
// 选择投影矩阵
gl.glMatrixMode(gl.GL_PROJECTION);
// 重置投影矩阵
gl.glLoadIdentity();
// 设置视口大小
gl.glFrustumf(-ratio, ratio, -1, 1, 1f, 10.0f);
// 选择模型观察矩阵
gl.glMatrixMode(gl.GL_MODELVIEW);
// 重置
gl.glLoadIdentity();
}
/**
* 实使化工作
*/
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
// 取消抖动
gl.glDisable(GL10.GL_DITHER);
// 阴影平滑
gl.glShadeModel(gl.GL_SMOOTH);
// 黑色背景
gl.glClearColor(0, 0, 0, 0);
// 设置深度缓存
gl.glClearDepthf(1.0f);
// 启用深度测试
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glEnable(GL10.GL_BLEND); // 打开混合
// 设置深度测试类型(深度小的时候也渲染)
gl.glDepthFunc(gl.GL_LEQUAL);
gl.glDisable(GL10.GL_DEPTH_TEST); // 关闭深度测试
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE); // 基于源象素alpha通道值的半透明混合函数
// 最好的视角修正
gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_FASTEST);
}
}
import zhou.ne.he.four.FourRend;
import zhou.ne.he.one.OneRend;
import zhou.ne.he.thri.ThriRend;
import zhou.ne.he.two.TwoRend;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
public class Run extends Activity {
private GLSurfaceView gl_view = null;
private GLSurfaceView.Renderer gl_rend = null;
private float mPreviousX;
private float mPreviousY;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gl_view = new GLSurfaceView(this);
gl_rend = new FourRend(this.getApplicationContext());
// gl_rend = new OneRend();
// gl_rend = new TwoRend();
// gl_rend = new ThriRend(this.getApplicationContext());
gl_view.setRenderer(gl_rend);
setContentView(gl_view);
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
((ThriRend) gl_rend).onKeyUp(keyCode, event);
return false;
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
if (event.getAction() == event.ACTION_MOVE) {
((ThriRend) gl_rend).rato += (x - mPreviousX) * 0.5f;
}
mPreviousX = x;
mPreviousY = y;
return super.onTouchEvent(event);
}
protected void onResume() {
super.onResume();
gl_view.onResume();
}
protected void onPause() {
super.onPause();
gl_view.onPause();
}
}