Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.sampleplugin.graphics;
     18 
     19 import javax.microedition.khronos.egl.EGL10;
     20 import javax.microedition.khronos.egl.EGLConfig;
     21 import javax.microedition.khronos.opengles.GL10;
     22 
     23 import android.opengl.GLSurfaceView;
     24 
     25 /**
     26  * Render a pair of tumbling cubes.
     27  */
     28 
     29 public class CubeRenderer implements GLSurfaceView.Renderer {
     30     public CubeRenderer(boolean useTranslucentBackground) {
     31         mTranslucentBackground = useTranslucentBackground;
     32         mCube = new Cube();
     33     }
     34 
     35     public void onDrawFrame(GL10 gl) {
     36         /*
     37          * Usually, the first thing one might want to do is to clear
     38          * the screen. The most efficient way of doing this is to use
     39          * glClear().
     40          */
     41 
     42         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
     43 
     44         /*
     45          * Now we're ready to draw some 3D objects
     46          */
     47 
     48         gl.glMatrixMode(GL10.GL_MODELVIEW);
     49         gl.glLoadIdentity();
     50         gl.glTranslatef(0, 0, -3.0f);
     51         gl.glRotatef(mAngle,        0, 1, 0);
     52         gl.glRotatef(mAngle*0.25f,  1, 0, 0);
     53 
     54         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
     55         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
     56 
     57         mCube.draw(gl);
     58 
     59         gl.glRotatef(mAngle*2.0f, 0, 1, 1);
     60         gl.glTranslatef(0.5f, 0.5f, 0.5f);
     61 
     62         mCube.draw(gl);
     63 
     64         mAngle += 1.2f;
     65     }
     66 
     67     public void onSurfaceChanged(GL10 gl, int width, int height) {
     68          gl.glViewport(0, 0, width, height);
     69 
     70          /*
     71           * Set our projection matrix. This doesn't have to be done
     72           * each time we draw, but usually a new projection needs to
     73           * be set when the viewport is resized.
     74           */
     75 
     76          float ratio = (float) width / height;
     77          gl.glMatrixMode(GL10.GL_PROJECTION);
     78          gl.glLoadIdentity();
     79          gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
     80     }
     81 
     82     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
     83         /*
     84          * By default, OpenGL enables features that improve quality
     85          * but reduce performance. One might want to tweak that
     86          * especially on software renderer.
     87          */
     88         gl.glDisable(GL10.GL_DITHER);
     89 
     90         /*
     91          * Some one-time OpenGL initialization can be made here
     92          * probably based on features of this particular context
     93          */
     94          gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
     95                  GL10.GL_FASTEST);
     96 
     97          if (mTranslucentBackground) {
     98              gl.glClearColor(0,0,0,0);
     99          } else {
    100              gl.glClearColor(1,1,1,1);
    101          }
    102          gl.glEnable(GL10.GL_CULL_FACE);
    103          gl.glShadeModel(GL10.GL_SMOOTH);
    104          gl.glEnable(GL10.GL_DEPTH_TEST);
    105     }
    106     private boolean mTranslucentBackground;
    107     private Cube mCube;
    108     private float mAngle;
    109 }
    110