      1: package com.programmez.opengl.exercice1;
      2:
      3: import javax.microedition.khronos.egl.EGLConfig;
      4: import javax.microedition.khronos.opengles.GL10;
      5: import com.programmez.opengl.exercice1.R;
      6:
      7: import android.content.Context;
      8: import android.opengl.GLSurfaceView;
      9:
     10: import static android.opengl.GLES10.*;
     11:
     12: class CubeRenderer implements GLSurfaceView.Renderer {
     13:         private final Context context;
     14:
     15:         private final GLCube cube = new GLCube();
     16:         private long startTime;
     17:         private float mAngleX;
     18:         private float mAngleY;
     19:
     20:         public float getmAngleX() {
     21:                 return mAngleX;
     22:         }
     23:
     24:         public void setmAngleX(float mAngleX) {
     25:                 this.mAngleX = mAngleX;
     26:         }
     27:
     28:         public float getmAngleY() {
     29:                 return mAngleY;
     30:         }
     31:
     32:         public void setmAngleY(float mAngleY) {
     33:                 this.mAngleY = mAngleY;
     34:         }
     35:
     36:         CubeRenderer(Context context) {
     37:                 this.context = context;
     38:         }
     39:
     40:         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
     41:
     42:                 glDisable(GL10.GL_DITHER);
     43:                 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
     44:
     45:                 startTime = System.currentTimeMillis();
     46:
     47:                 // Define the lighting

     48:                 float lightAmbient[] = new float[] { 0.2f, 0.2f, 0.2f, 1 };
     49:                 float lightDiffuse[] = new float[] { 1, 1, 1, 1 };
     50:                 float[] lightPos = new float[] { 1, 1, 1, 1 };
     51:                 glEnable(GL10.GL_LIGHTING);
     52:                 glEnable(GL10.GL_LIGHT0);
     53:                 glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
     54:                 glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
     55:                 glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);
     56:
     57:                 // What is the cube made of?

     58:                 float matAmbient[] = new float[] { 1, 1, 1, 1 };
     59:                 float matDiffuse[] = new float[] { 1, 1, 1, 1 };
     60:                 glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
     61:                 glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);
     62:
     63:                 // Set up any OpenGL options we need

     64:                 glEnable(GL10.GL_DEPTH_TEST);
     65:                 glDepthFunc(GL10.GL_LEQUAL);
     66:                 glEnableClientState(GL10.GL_VERTEX_ARRAY);
     67:
     68:                 // Enable textures

     69:                 glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
     70:                 glEnable(GL10.GL_TEXTURE_2D);
     71:
     72:                 // Load the cube's texture from a bitmap

     73:                 GLCube.loadTexture(gl, context, R.drawable.texture_cube);
     74:         }
     75:
     76:         public void onSurfaceChanged(GL10 gl, int width, int height) {
     77:                 glViewport(0, 0, width, height);
     78:                 glMatrixMode(GL10.GL_PROJECTION);
     79:                 glLoadIdentity();
     80:                 float ratio = (float) width / height;
     81:                 // GLU.gluPerspective(gl, 45.0f, ratio, 1, 100f);

     82:                 glFrustumf(-ratio, ratio, -1, 1, 1, 10);
     83:
     84:                 //la camÃİra par dÃİfaut est en 0,0,0

     85:         }
     86:
     87:         public void onDrawFrame(GL10 gl) {
     88:                 glDisable(GL10.GL_DITHER);
     89:
     90:                 // Clear the screen to black

     91:                 glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
     92:
     93:                 // Position model so we can see it

     94:
     95:                 // de cette maniÃ¨re on va tourner toute la scÃ¨ne

     96:                 //si je voulais seulement dÃİplacer la camÃİra...

     97:                 //il faudrait faire un push/pop, puis dÃİplacer camÃİra??

     98:
     99:
    100:                 glMatrixMode(GL10.GL_MODELVIEW);
    101:                 glLoadIdentity();
    102:                 glTranslatef(0, 0, -2.0f);//bouge le cube uniquement

    103:
    104:                 // Other drawing commands go here...

    105:
    106:                 // Set rotation angle based on the time

    107:                 long now = System.currentTimeMillis();
    108:                 long elapsed = now - startTime;
    109:
    110:                 glRotatef( (mAngleX), 1, 0, 0);
    111:                 glRotatef( (mAngleY), 0, 1, 0);
    112:                 startTime = now;
    113:
    114:                 // Draw the model

    115:                 cube.draw(gl);
    116:         }
    117: }