android opengl es--立方体

android opengl es--立方体_第1张图片

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

 

 

 

public class TwoRend implements Renderer {

 private float oneF = 1.0f;
 private float rato = 0;

 private FloatBuffer triggerBuffer1 = FloatBuffer.wrap(new float[] { 0,
   oneF, 0, -oneF, -oneF, oneF, oneF, -oneF, oneF, 0, oneF, 0, oneF,
   -oneF, oneF, oneF, -oneF, -oneF, 0, oneF, 0, oneF, -oneF, -oneF,
   -oneF, -oneF, oneF, });

 private FloatBuffer triggerBuffer2 = FloatBuffer.wrap(new float[] {
   // top
   oneF, oneF, -oneF, -oneF, oneF, -oneF, oneF,
   oneF,
   oneF,
   -oneF,
   oneF,
   oneF,

   // bottom
   oneF, -oneF, oneF, -oneF, -oneF, oneF, oneF, -oneF,
   -oneF,
   -oneF,
   -oneF,
   -oneF,

   // front
   oneF, oneF, oneF, -oneF, oneF, oneF, oneF, -oneF, oneF,
   -oneF,
   -oneF,
   oneF,

   // behind
   oneF, -oneF, -oneF, -oneF, -oneF, -oneF, oneF, oneF, -oneF, -oneF,
   oneF,
   -oneF,

   // left
   -oneF, oneF, oneF, -oneF, oneF, -oneF, -oneF, -oneF, oneF, -oneF,
   -oneF, -oneF,

   // right
   oneF, oneF, -oneF, oneF, oneF, oneF, oneF, -oneF, -oneF, oneF,
   -oneF, oneF, });

 private FloatBuffer color2 = FloatBuffer.wrap(new float[] { oneF, oneF, 0,
   oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0, oneF, -oneF, -oneF, 0,
   oneF, oneF, oneF, 0, oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0,
   oneF, -oneF, -oneF, 0, oneF, -oneF, -oneF, 0, oneF, oneF, oneF, 0,
   oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0, oneF, -oneF, -oneF, 0,
   oneF, oneF, oneF, 0, oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0,
   oneF, -oneF, -oneF, 0, oneF, -oneF, -oneF, 0, oneF, oneF, oneF, 0,
   oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0, oneF, -oneF, -oneF, 0,
   oneF, oneF, oneF, 0, oneF, -oneF, oneF, 0, oneF });

 private FloatBuffer color = FloatBuffer.wrap(new float[] { oneF, oneF, 0,
   oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0, oneF, -oneF, -oneF, 0,
   oneF, oneF, oneF, 0, oneF, -oneF, oneF, 0, oneF, oneF, -oneF, 0,
   oneF, -oneF, -oneF, 0, oneF, -oneF, -oneF, 0, oneF });

 public void onDrawFrame(GL10 gl) {
  gl.glClear(gl.GL_DEPTH_BUFFER_BIT | gl.GL_COLOR_BUFFER_BIT);
  gl.glMatrixMode(gl.GL_MODELVIEW);
  gl.glLoadIdentity();
  gl.glTranslatef(0, 0, -4.0f);
  gl.glRotatef(rato, 1.0f, 0.0f, 0f);
  gl.glRotatef(rato, 0f, 1.0f, 0f);
  gl.glRotatef(rato, 0f, 0.0f, 1.0f);

  gl.glBindTexture(gl.GL_TEXTURE_2D, 0);

  // gl.glEnableClientState(gl.GL_COLOR_ARRAY);
  gl.glEnableClientState(gl.GL_VERTEX_ARRAY);

  gl.glVertexPointer(3, gl.GL_FLOAT, 0, triggerBuffer2);
  // gl.glColorPointer(4, gl.GL_FLOAT, 0, color);
  gl.glColor4f(1, 1, 0, 0.5f);
  gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 24);

  // gl.glDisableClientState(gl.GL_COLOR_ARRAY);
  gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
  rato += 1.6f;
 }

 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(gl.GL_PROJECTION);
  gl.glLoadIdentity();
  gl.glFrustumf(-ratio, ratio, -1, 1, 1.0f, 10.0f);
  gl.glMatrixMode(gl.GL_MODELVIEW);
  gl.glLoadIdentity();
 }

 protected final void regTexture(GL10 gl, int resId, int key) {
  gl.glBindTexture(GL10.GL_TEXTURE_2D, key);
  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
    GL10.GL_LINEAR);
  gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
    GL10.GL_LINEAR);
 }

 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  gl.glEnable(GL10.GL_DEPTH_TEST);
  gl.glDepthFunc(GL10.GL_LEQUAL);
  gl.glDisable(gl.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(gl.GL_TEXTURE_2D);
  gl.glDepthFunc(gl.GL_LEQUAL);
  gl.glHint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_FASTEST);
 }

}

你可能感兴趣的:(android opengl es--立方体)