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 java.nio.ByteBuffer;
     20 import java.nio.ByteOrder;
     21 import java.nio.FloatBuffer;
     22 
     23 import android.opengl.GLES20;
     24 
     25 /**
     26  * A two-dimensional triangle for use as a drawn object in OpenGL ES 2.0.
     27  * Source : development/samples/OpenGL/HelloOpenGLES20
     28  */
     29 public class Triangle {
     30 
     31     private final String vertexShaderCode =
     32             // This matrix member variable provides a hook to manipulate
     33             // the coordinates of the objects that use this vertex shader
     34             "uniform mat4 uMVPMatrix;" +
     35                     "attribute vec4 vPosition;" +
     36                     "void main() {" +
     37                     // the matrix must be included as a modifier of gl_Position
     38                     // Note that the uMVPMatrix factor *must be first* in order
     39                     // for the matrix multiplication product to be correct.
     40                     "  gl_Position = uMVPMatrix * vPosition;" +
     41                     "}";
     42 
     43     private final String fragmentShaderCode =
     44             "precision mediump float;" +
     45                     "uniform vec4 vColor;" +
     46                     "void main() {" +
     47                     "  gl_FragColor = vColor;" +
     48                     "}";
     49 
     50     private final FloatBuffer vertexBuffer;
     51     private final int mProgram;
     52     private int mPositionHandle;
     53     private int mColorHandle;
     54     private int mMVPMatrixHandle;
     55 
     56     // number of coordinates per vertex in this array
     57     static final int COORDS_PER_VERTEX = 3;
     58     static float triangleCoords[] = {
     59             // in counterclockwise order:
     60             0.0f, 0.622008459f, 0.0f, // top
     61             -0.5f, -0.311004243f, 0.0f, // bottom left
     62             0.5f, -0.311004243f, 0.0f
     63             // bottom right
     64     };
     65     private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
     66     private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
     67 
     68     float color[] = {
     69             0.63671875f, 0.76953125f, 0.22265625f, 0.0f
     70     };
     71 
     72     /**
     73      * Sets up the drawing object data for use in an OpenGL ES context.
     74      */
     75     public Triangle() {
     76         // initialize vertex byte buffer for shape coordinates
     77         ByteBuffer bb = ByteBuffer.allocateDirect(
     78                 // (number of coordinate values * 4 bytes per float)
     79                 triangleCoords.length * 4);
     80         // use the device hardware's native byte order
     81         bb.order(ByteOrder.nativeOrder());
     82 
     83         // create a floating point buffer from the ByteBuffer
     84         vertexBuffer = bb.asFloatBuffer();
     85         // add the coordinates to the FloatBuffer
     86         vertexBuffer.put(triangleCoords);
     87         // set the buffer to read the first coordinate
     88         vertexBuffer.position(0);
     89 
     90         // prepare shaders and OpenGL program
     91         int vertexShader = SimpleGLRenderer.loadShader(
     92                 GLES20.GL_VERTEX_SHADER, vertexShaderCode);
     93         int fragmentShader = SimpleGLRenderer.loadShader(
     94                 GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
     95 
     96         mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
     97         GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
     98         GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
     99         GLES20.glLinkProgram(mProgram); // create OpenGL program executables
    100 
    101     }
    102 
    103     /**
    104      * Encapsulates the OpenGL ES instructions for drawing this shape.
    105      *
    106      * @param mvpMatrix - The Model View Project matrix in which to draw this shape.
    107      */
    108     public void draw(float[] mvpMatrix) {
    109         // Add program to OpenGL environment
    110         GLES20.glUseProgram(mProgram);
    111 
    112         // get handle to vertex shader's vPosition member
    113         mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    114 
    115         // Enable a handle to the triangle vertices
    116         GLES20.glEnableVertexAttribArray(mPositionHandle);
    117 
    118         // Prepare the triangle coordinate data
    119         GLES20.glVertexAttribPointer(
    120                 mPositionHandle, COORDS_PER_VERTEX,
    121                 GLES20.GL_FLOAT, false,
    122                 vertexStride, vertexBuffer);
    123 
    124         // get handle to fragment shader's vColor member
    125         mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
    126 
    127         // Set color for drawing the triangle
    128         GLES20.glUniform4fv(mColorHandle, 1, color, 0);
    129 
    130         // get handle to shape's transformation matrix
    131         mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    132         SimpleGLRenderer.checkGlError("glGetUniformLocation");
    133 
    134         // Apply the projection and view transformation
    135         GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    136         SimpleGLRenderer.checkGlError("glUniformMatrix4fv");
    137 
    138         // Draw the triangle
    139         GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
    140 
    141         // Disable vertex array
    142         GLES20.glDisableVertexAttribArray(mPositionHandle);
    143     }
    144 
    145 }
    146