Home | History | Annotate | Download | only in helper
      1 /*
      2  * Copyright (C) 2015 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.performanceLaunch.helper;
     18 
     19 import javax.microedition.khronos.egl.EGLConfig;
     20 import javax.microedition.khronos.opengles.GL10;
     21 
     22 import android.opengl.GLES20;
     23 import android.opengl.GLSurfaceView;
     24 import android.opengl.Matrix;
     25 import android.util.Log;
     26 
     27 /**
     28  * Provides drawing instructions for a GLSurfaceView object. This class must override the OpenGL ES
     29  * drawing lifecycle methods:
     30  * <ul>
     31  * <li>{@link android.opengl.GLSurfaceView.Renderer#onSurfaceCreated}</li>
     32  * <li>{@link android.opengl.GLSurfaceView.Renderer#onDrawFrame}</li>
     33  * <li>{@link android.opengl.GLSurfaceView.Renderer#onSurfaceChanged}</li>
     34  * </ul>
     35  * Source : development/samples/OpenGL/HelloOpenGLES20
     36  */
     37 public class SimpleGLRenderer implements GLSurfaceView.Renderer {
     38 
     39     private static final String TAG = "MyGLRenderer";
     40     private Triangle mTriangle;
     41     private Square mSquare;
     42 
     43     // mMVPMatrix is an abbreviation for "Model View Projection Matrix"
     44     private final float[] mMVPMatrix = new float[16];
     45     private final float[] mProjectionMatrix = new float[16];
     46     private final float[] mViewMatrix = new float[16];
     47     private final float[] mRotationMatrix = new float[16];
     48 
     49     private float mAngle;
     50 
     51     @Override
     52     public void onSurfaceCreated(GL10 unused, EGLConfig config) {
     53 
     54         // Set the background frame color
     55         GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
     56 
     57         mTriangle = new Triangle();
     58         mSquare = new Square();
     59     }
     60 
     61     @Override
     62     public void onDrawFrame(GL10 unused) {
     63         float[] scratch = new float[16];
     64 
     65         // Draw background color
     66         GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
     67 
     68         // Set the camera position (View matrix)
     69         Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
     70 
     71         // Calculate the projection and view transformation
     72         Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
     73 
     74         // Draw square
     75         mSquare.draw(mMVPMatrix);
     76 
     77         // Create a rotation for the triangle
     78 
     79         // Use the following code to generate constant rotation.
     80         // Leave this code out when using TouchEvents.
     81         // long time = SystemClock.uptimeMillis() % 4000L;
     82         // float angle = 0.090f * ((int) time);
     83 
     84         Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, 1.0f);
     85 
     86         // Combine the rotation matrix with the projection and camera view
     87         // Note that the mMVPMatrix factor *must be first* in order
     88         // for the matrix multiplication product to be correct.
     89         Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);
     90 
     91         // Draw triangle
     92         mTriangle.draw(scratch);
     93     }
     94 
     95     @Override
     96     public void onSurfaceChanged(GL10 unused, int width, int height) {
     97         // Adjust the viewport based on geometry changes,
     98         // such as screen rotation
     99         GLES20.glViewport(0, 0, width, height);
    100 
    101         float ratio = (float) width / height;
    102 
    103         // this projection matrix is applied to object coordinates
    104         // in the onDrawFrame() method
    105         Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
    106 
    107     }
    108 
    109     /**
    110      * Utility method for compiling a OpenGL shader.
    111      * <p>
    112      * <strong>Note:</strong> When developing shaders, use the checkGlError() method to debug shader
    113      * coding errors.
    114      * </p>
    115      *
    116      * @param type - Vertex or fragment shader type.
    117      * @param shaderCode - String containing the shader code.
    118      * @return - Returns an id for the shader.
    119      */
    120     public static int loadShader(int type, String shaderCode) {
    121 
    122         // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    123         // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    124         int shader = GLES20.glCreateShader(type);
    125 
    126         // add the source code to the shader and compile it
    127         GLES20.glShaderSource(shader, shaderCode);
    128         GLES20.glCompileShader(shader);
    129 
    130         return shader;
    131     }
    132 
    133     /**
    134      * Utility method for debugging OpenGL calls. Provide the name of the call just after making it:
    135      *
    136      * <pre>
    137      * mColorHandle = GLES20.glGetUniformLocation(mProgram, &quot;vColor&quot;);
    138      * MyGLRenderer.checkGlError(&quot;glGetUniformLocation&quot;);
    139      * </pre>
    140      *
    141      * If the operation is not successful, the check throws an error.
    142      *
    143      * @param glOperation - Name of the OpenGL call to check.
    144      */
    145     public static void checkGlError(String glOperation) {
    146         int error;
    147         while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
    148             Log.e(TAG, glOperation + ": glError " + error);
    149             throw new RuntimeException(glOperation + ": glError " + error);
    150         }
    151     }
    152 
    153     /**
    154      * Returns the rotation angle of the triangle shape (mTriangle).
    155      *
    156      * @return - A float representing the rotation angle.
    157      */
    158     public float getAngle() {
    159         return mAngle;
    160     }
    161 
    162     /**
    163      * Sets the rotation angle of the triangle shape (mTriangle).
    164      */
    165     public void setAngle(float angle) {
    166         mAngle = angle;
    167     }
    168 
    169 }
    170