OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Cube.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
28 
29 #include <string>
30 
31 #include <jni.h>
32 #include <android/log.h>
33 
34 #include "Cube.h"
35 #include "AndroidPlatform.h"
36 #include "Text.h"
37 #include "Shader.h"
38 #include "Texture.h"
39 #include "Matrix.h"
40 #include "Timer.h"
41 
42 using std::string;
43 using namespace MaliSDK;
44 
45 /* Asset directories and filenames. */
46 string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.cube/";
47 string vertexShaderFilename = "Cube_cube.vert";
48 string fragmentShaderFilename = "Cube_cube.frag";
49 
50 /* Shader variables. */
53 GLint iLocColor;
54 GLint iLocMVP;
55 
56 int windowWidth = -1;
57 int windowHeight = -1;
58 
59 /* A text object to draw text on the screen. */
61 
62 bool setupGraphics(int width, int height)
63 {
66 
67  /* Full paths to the shader and texture files */
68  string vertexShaderPath = resourceDirectory + vertexShaderFilename;
69  string fragmentShaderPath = resourceDirectory + fragmentShaderFilename;
70 
71  /* Initialize OpenGL ES. */
72  GL_CHECK(glEnable(GL_BLEND));
73  /* Should do: src * (src alpha) + dest * (1-src alpha). */
74  GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
75 
76  /* Initialize the Text object and add some text. */
78  text->addString(0, 0, "Simple Cube Example", 255, 255, 0, 255);
79 
80  /* Process shaders. */
82  Shader::processShader(&vertexShaderID, vertexShaderPath.c_str(), GL_VERTEX_SHADER);
83  Shader::processShader(&fragmentShaderID, fragmentShaderPath.c_str(), GL_FRAGMENT_SHADER);
84 
85  /* Create programID (ready to attach shaders) */
86  programID = GL_CHECK(glCreateProgram());
87 
88  /* Attach shaders and link programID */
89  GL_CHECK(glAttachShader(programID, vertexShaderID));
90  GL_CHECK(glAttachShader(programID, fragmentShaderID));
91  GL_CHECK(glLinkProgram(programID));
92  GL_CHECK(glUseProgram(programID));
93 
94  /* Get attribute locations of non-fixed attributes like colour and texture coordinates. */
95  iLocPosition = GL_CHECK(glGetAttribLocation(programID, "av4position"));
96  iLocColor = GL_CHECK(glGetAttribLocation(programID, "av3colour"));
97 
98  LOGD("iLocPosition = %i\n", iLocPosition);
99  LOGD("iLocColor = %i\n", iLocColor);
100 
101  /* Get uniform locations */
102  iLocMVP = GL_CHECK(glGetUniformLocation(programID, "mvp"));
103 
104  LOGD("iLocMVP = %i\n", iLocMVP);
105 
106  GL_CHECK(glEnable(GL_CULL_FACE));
107  GL_CHECK(glEnable(GL_DEPTH_TEST));
108 
109  /* Set clear screen color. */
110  GL_CHECK(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));
111 
112  return true;
113 }
114 
115 void renderFrame(void)
116 {
117  GL_CHECK(glUseProgram(programID));
118 
119  /* Enable attributes for position, color and texture coordinates etc. */
120  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
121  GL_CHECK(glEnableVertexAttribArray(iLocColor));
122 
123  /* Populate attributes for position, color and texture coordinates etc. */
124  GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, vertices));
125  GL_CHECK(glVertexAttribPointer(iLocColor, 3, GL_FLOAT, GL_FALSE, 0, colors));
126 
127  static float angleX = 0, angleY = 0, angleZ = 0;
128  /*
129  * Do some rotation with Euler angles. It is not a fixed axis as
130  * quaternions would be, but the effect is nice.
131  */
134 
135  modelView = rotation * modelView;
136 
137  rotation = Matrix::createRotationZ(angleZ);
138 
139  modelView = rotation * modelView;
140 
141  /* Pull the camera back from the cube */
142  modelView[14] -= 2.5;
143 
145  Matrix modelViewPerspective = perspective * modelView;
146 
147  GL_CHECK(glUniformMatrix4fv(iLocMVP, 1, GL_FALSE, modelViewPerspective.getAsArray()));
148 
149  /* Update cube's rotation angles for animating. */
150  angleX += 3;
151  angleY += 2;
152  angleZ += 1;
153 
154  if(angleX >= 360) angleX -= 360;
155  if(angleX < 0) angleX += 360;
156  if(angleY >= 360) angleY -= 360;
157  if(angleY < 0) angleY += 360;
158  if(angleZ >= 360) angleZ -= 360;
159  if(angleZ < 0) angleZ += 360;
160 
161  GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
162  GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36));
163 
164  /* Draw any text. */
165  text->draw();
166 }
167 
168 extern "C"
169 {
171  (JNIEnv *env, jclass jcls, jint width, jint height)
172  {
173  /* Make sure that all resource files are in place. */
176 
177  setupGraphics(width, height);
178  }
179 
181  (JNIEnv *env, jclass jcls)
182  {
183  renderFrame();
184  }
185 
187  (JNIEnv *, jclass)
188  {
189  delete text;
190  }
191 }
int windowWidth
Definition: Cube.cpp:56
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_cube_Cube_init(JNIEnv *env, jclass jcls, jint width, jint height)
Definition: Cube.cpp:171
Functions for drawing text in OpenGL ES.
Definition: Text.h:44
GLint iLocMVP
Definition: Cube.cpp:54
GLuint programID
Definition: Cube.cpp:51
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
GLint iLocPosition
Definition: Cube.cpp:52
const float vertices[]
Definition: Cube.h:30
void renderFrame(void)
Definition: Cube.cpp:115
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_cube_Cube_uninit(JNIEnv *, jclass)
Definition: Cube.cpp:187
Functions for manipulating matrices.
Definition: Matrix.h:31
GLuint fragmentShaderID
static float angleZ
#define LOGD(...)
Definition: AstcTextures.h:28
int windowHeight
Definition: Cube.cpp:57
Matrix modelView
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
Definition: Matrix.cpp:425
string vertexShaderFilename
Definition: Cube.cpp:47
float * getAsArray(void)
Get the matrix elements as a column major order array.
Definition: Matrix.cpp:78
string resourceDirectory
Definition: Cube.cpp:46
static float angleX
Matrix perspective
Definition: EGLPreserve.cpp:81
GLfloat GLfloat f
Definition: gl2ext.h:2707
void addString(int xPosition, int yPosition, const char *string, int red, int green, int blue, int alpha)
Add a std::string to be drawn to the screen.
Definition: Text.cpp:157
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_cube_Cube_step(JNIEnv *env, jclass jcls)
Definition: Cube.cpp:181
#define GL_CHECK(x)
Definition: AstcTextures.h:59
static Matrix createRotationY(float angle)
Create and return a rotation matrix around the y-axis matrix.
Definition: Matrix.cpp:511
static Matrix createRotationZ(float angle)
Create and return a rotation matrix around the z-axis matrix.
Definition: Matrix.cpp:523
static bool getAndroidAsset(JNIEnv *JNIEnvironment, const char destinationDirectory[], const char filename[])
Extract an asset file from the APK.
float angleY
Definition: Native.cpp:118
const float colors[]
Definition: Cube.h:88
bool setupGraphics(int width, int height)
Definition: Cube.cpp:62
GLuint vertexShaderID
GLint GLsizei width
Definition: gl2ext.h:179
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
Definition: Shader.cpp:29
GLint iLocColor
Definition: Cube.cpp:53
void draw(void)
Draw the text to the screen.
Definition: Text.cpp:265
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
Text * text
Definition: Cube.cpp:60
static Matrix createRotationX(float angle)
Create and return a rotation matrix around the x-axis matrix.
Definition: Matrix.cpp:499
string fragmentShaderFilename
Definition: Cube.cpp:48