package com.example.cubedemo; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer; import android.opengl.GLU; public abstract class AbstractLightCubeRenderer implements Renderer { private float mCubeRotation=45; public float getmCubeRotation() { return mCubeRotation; } public void setmCubeRotation(float mCubeRotation) { this.mCubeRotation = mCubeRotation; } @Override public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) { gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //couleur de fond gl.glClearDepthf(1.0f); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glDepthFunc(GL10.GL_LEQUAL); gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); } @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, 3f); gl.glRotatef(mCubeRotation, 1.0f, 1.0f, 1.0f); draw(gl); gl.glLoadIdentity(); mCubeRotation += 0.2f; } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { // definition de la taille de la zone de projevtion 2D gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); // modele de projection (mode GL_PROJECTION) GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 1f, 30f); //LookAt : positionnement camera (mode GL_PROJECTION) GLU.gluLookAt(gl, 0f, 0f, 10f, //Position camera 0.0f, 0.0f, 0.0f, // vecteur pointage camera 0f,1f, 0.0f);// orientation -rotation camera (up vector) gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); // light gl.glEnable(GL10.GL_LIGHTING); // the first light gl.glEnable(GL10.GL_LIGHT0); // ambient values gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, new float[] { 0.1f, 0.1f, 0.1f, 1f }, 0); // light that reflects in all directions gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, new float[] { 1f, 1f, 1f, 1f }, 0); // place it in projection space gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, new float[] { 10f, 0f, 10f, 1 }, 0); // allow our object colors to create the diffuse/ambient material // setting gl.glEnable(GL10.GL_COLOR_MATERIAL); // some rendering options gl.glShadeModel(GL10.GL_SMOOTH); } protected abstract void draw(GL10 gl); }