Guest User

GLSurfaceView.Renderer class.

a guest
Mar 27th, 2013
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package dd.ww;
  2.  
  3. import javax.microedition.khronos.egl.EGLConfig;
  4. import javax.microedition.khronos.opengles.GL10;
  5. import android.content.Context;
  6. import android.opengl.GLES20;
  7. import android.opengl.GLSurfaceView.Renderer;
  8. import android.opengl.Matrix;
  9.  
  10. public class Render implements Renderer {
  11.    
  12.     private Context context;
  13.     private Cube cube;
  14.    
  15.     private float[] modelViewProjectionMatrix = new float[16];
  16.     private float[] projectionMatrix = new float[16];
  17.     private float[] viewMatrix = new float[16];
  18.     private float[] rotationMatrix = new float[16];
  19.     private float angle = 0f;
  20.    
  21.     public Render(Context context) {
  22.         this.context = context;
  23.     }
  24.    
  25.     @Override
  26.     public void onSurfaceCreated(GL10 unused, EGLConfig config) {
  27.         GLES20.glClearColor(1f, 1f, 1f, 1f);
  28.         cube = new Cube(context);
  29.     }
  30.    
  31.     @Override
  32.     public void onSurfaceChanged(GL10 unused, int width, int height) {
  33.         GLES20.glViewport(0, 0, width, height);
  34.         float ratio = (float) width / (float) height;
  35.         Matrix.frustumM(projectionMatrix, 0, -3f, 3f, -3f, 3f, 1f, 10f);
  36.     }
  37.    
  38.     @Override
  39.     public void onDrawFrame(GL10 unused) {
  40.         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
  41.         //Camera position
  42.         Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, -4f, 0f, 0f, 0f, 0f, 1f, 0f);
  43.         // projection x view = modelView
  44.         Matrix.multiplyMM(modelViewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
  45.         //Creating rotation matrix
  46.         Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1f);
  47.         //rotation x camera = modelView
  48.         Matrix.multiplyMM(modelViewProjectionMatrix, 0, rotationMatrix, 0, modelViewProjectionMatrix, 0);
  49.         Matrix.setRotateM(rotationMatrix, 0, angle, 0f, -1f, 0f);
  50.         Matrix.multiplyMM(modelViewProjectionMatrix, 0, rotationMatrix, 0, modelViewProjectionMatrix, 0);
  51.         Matrix.setRotateM(rotationMatrix, 0, angle, -1f, 0f, 0f);
  52.         Matrix.multiplyMM(modelViewProjectionMatrix, 0, rotationMatrix, 0, modelViewProjectionMatrix, 0);
  53.        
  54.         cube.draw(modelViewProjectionMatrix);
  55.        
  56.         angle += 0.7f;
  57.         if (angle > 360f)
  58.             angle = 0f;
  59.     }
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment