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();
}
}
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class BaseRend implements Renderer {
public float rato = 0.0f;
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0){
height = 1;
}
float ratio = (float)width / (float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1.0f, 10.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
// gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0, 0, 0, 0.5f);
gl.glClearDepthf(1.0f);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
}
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import zhou.ne.he.BaseRend;
import zhou.ne.he.Cube;
import zhou.ne.he.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.view.KeyEvent;
public class ThriRend extends BaseRend {
public float rato = 0;
private int[] texture = null;// 纹理数组
private Bitmap[] bit = new Bitmap[6];
private boolean key = true;
public ThriRend(Context context) {
bit[0] = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon1);
bit[1] = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon2);
bit[2] = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon3);
bit[3] = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon4);
bit[4] = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon5);
bit[5] = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon6);
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
key = !key;
return false;
}
public void onDrawFrame(GL10 gl) {
super.onDrawFrame(gl);
gl.glTranslatef(0.0f, 0.0f, -5.0f);
// 设置旋转
// gl.glRotatef(rato, 0.0f, 1.0f, 0.0f);
gl.glRotatef(rato, 1.0f, 0.0f, 0.0f);
gl.glRotatef(rato, 0.0f, 1.0f, 0.0f);
gl.glNormalPointer(GL10.GL_FIXED, 0, Cube.normals);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, Cube.vertices);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, Cube.texCoords);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// 绘制四边形
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE,
Cube.indices1);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 8, GL10.GL_UNSIGNED_BYTE,
Cube.indices2);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 12, GL10.GL_UNSIGNED_BYTE,
Cube.indices3);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 16, GL10.GL_UNSIGNED_BYTE,
Cube.indices4);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 20, GL10.GL_UNSIGNED_BYTE,
Cube.indices5);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]);
gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE,
Cube.indices6);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
// rato += 2.5f;
if(key){
gl.glEnable(GL10.GL_BLEND); // 打开混合
gl.glDisable(GL10.GL_DEPTH_TEST); // 关闭深度测试
}else{
gl.glDisable(GL10.GL_BLEND); // 打开混合
gl.glEnable(GL10.GL_DEPTH_TEST); // 关闭深度测试
}
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
super.onSurfaceChanged(gl, width, height);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
super.onSurfaceCreated(gl, config);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
// 打开纹理
gl.glEnable(GL10.GL_TEXTURE_2D);
// 绑定纹理
IntBuffer t_b = IntBuffer.allocate(6);
gl.glGenTextures(6, t_b);// 设置纹理的个数
texture = t_b.array();
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[0], 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[1], 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[2], 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[3]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[3], 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[4]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[4], 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[5]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bit[5], 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glColor4f(1.0f, 1.0f, 1.0f, 0.4f);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
// 设置环境光
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, Cube.lightAmbient);
// 设置漫射光
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, Cube.lightDiffuse);
// 设置光源位置
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, Cube.lightPosition);
// 开启光源
gl.glEnable(GL10.GL_LIGHT1);
// 开启混合
gl.glEnable(GL10.GL_BLEND);
}
}