Home | History | Annotate | Download | only in opengl
      1 /*
      2  * Copyright (C) 2011 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 package com.example.android.opengl;
     17 
     18 import javax.microedition.khronos.egl.EGLConfig;
     19 import javax.microedition.khronos.opengles.GL10;
     20 
     21 import android.opengl.GLSurfaceView;
     22 import android.opengl.GLU;
     23 
     24 /**
     25  * Provides drawing instructions for a GLSurfaceView object. This class
     26  * must override the OpenGL ES drawing lifecycle methods:
     27  * <ul>
     28  *   <li>{@link android.opengl.GLSurfaceView.Renderer#onSurfaceCreated}</li>
     29  *   <li>{@link android.opengl.GLSurfaceView.Renderer#onDrawFrame}</li>
     30  *   <li>{@link android.opengl.GLSurfaceView.Renderer#onSurfaceChanged}</li>
     31  * </ul>
     32  */
     33 public class MyGLRenderer implements GLSurfaceView.Renderer {
     34 
     35     private Triangle mTriangle;
     36     private Square mSquare;
     37     private float mAngle;
     38 
     39     @Override
     40     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
     41         // Set the background frame color
     42         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
     43 
     44         mTriangle = new Triangle();
     45         mSquare = new Square();
     46     }
     47 
     48     @Override
     49     public void onDrawFrame(GL10 gl) {
     50 
     51         // Draw background color
     52         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
     53 
     54         // Set GL_MODELVIEW transformation mode
     55         gl.glMatrixMode(GL10.GL_MODELVIEW);
     56         gl.glLoadIdentity();   // reset the matrix to its default state
     57 
     58         // When using GL_MODELVIEW, you must set the view point
     59         GLU.gluLookAt(gl, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
     60 
     61         // Draw square
     62         mSquare.draw(gl);
     63 
     64         // Create a rotation for the triangle
     65 
     66         // Use the following code to generate constant rotation.
     67         // Leave this code out when using TouchEvents.
     68         // long time = SystemClock.uptimeMillis() % 4000L;
     69         // float angle = 0.090f * ((int) time);
     70 
     71         gl.glRotatef(mAngle, 0.0f, 0.0f, 1.0f);
     72 
     73         // Draw triangle
     74         mTriangle.draw(gl);
     75     }
     76 
     77     @Override
     78     public void onSurfaceChanged(GL10 gl, int width, int height) {
     79         // Adjust the viewport based on geometry changes
     80         // such as screen rotations
     81         gl.glViewport(0, 0, width, height);
     82 
     83         // make adjustments for screen ratio
     84         float ratio = (float) width / height;
     85         gl.glMatrixMode(GL10.GL_PROJECTION);        // set matrix to projection mode
     86         gl.glLoadIdentity();                        // reset the matrix to its default state
     87         gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);  // apply the projection matrix
     88     }
     89 
     90     /**
     91      * Returns the rotation angle of the triangle shape (mTriangle).
     92      *
     93      * @return - A float representing the rotation angle.
     94      */
     95     public float getAngle() {
     96         return mAngle;
     97     }
     98 
     99     /**
    100      * Sets the rotation angle of the triangle shape (mTriangle).
    101      */
    102     public void setAngle(float angle) {
    103         mAngle = angle;
    104     }
    105 }