OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Native.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2013-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 
21 #include <jni.h>
22 #include <android/log.h>
23 
24 #include <GLES2/gl2.h>
25 #include <GLES2/gl2ext.h>
26 
27 #include <cstdio>
28 #include <cstdlib>
29 #include <cmath>
30 
31 #include "Matrix.h"
32 
33 #define LOG_TAG "libNative"
34 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
35 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
36 
37 /* [vertexShader] */
38 static const char glVertexShader[] =
39  "attribute vec4 vertexPosition;\n"
40  "attribute vec3 vertexColour;\n"
41  "varying vec3 fragColour;\n"
42  "uniform mat4 projection;\n"
43  "uniform mat4 modelView;\n"
44  "void main()\n"
45  "{\n"
46  " gl_Position = projection * modelView * vertexPosition;\n"
47  " fragColour = vertexColour;\n"
48  "}\n";
49 /* [vertexShader] */
50 
51 /* [fragmentShader] */
52 static const char glFragmentShader[] =
53  "precision mediump float;\n"
54  "varying vec3 fragColour;\n"
55  "void main()\n"
56  "{\n"
57  " gl_FragColor = vec4(fragColour, 1.0);\n"
58  "}\n";
59 /* [fragmentShader] */
60 
61 GLuint loadShader(GLenum shaderType, const char* shaderSource)
62 {
63  GLuint shader = glCreateShader(shaderType);
64  if (shader != 0)
65  {
66  glShaderSource(shader, 1, &shaderSource, NULL);
67  glCompileShader(shader);
68  GLint compiled = 0;
69  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
70  if (compiled != GL_TRUE)
71  {
72  GLint infoLen = 0;
73  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
74 
75  if (infoLen > 0)
76  {
77  char * logBuffer = (char*) malloc(infoLen);
78 
79  if (logBuffer != NULL)
80  {
81  glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
82  LOGE("Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
83  free(logBuffer);
84  logBuffer = NULL;
85  }
86 
87  glDeleteShader(shader);
88  shader = 0;
89  }
90  }
91  }
92 
93  return shader;
94 }
95 
96 GLuint createProgram(const char* vertexSource, const char * fragmentSource)
97 {
98  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
99  if (vertexShader == 0)
100  {
101  return 0;
102  }
103 
104  GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
105  if (fragmentShader == 0)
106  {
107  return 0;
108  }
109 
110  GLuint program = glCreateProgram();
111 
112  if (program != 0)
113  {
114  glAttachShader(program, vertexShader);
115  glAttachShader(program, fragmentShader);
116  glLinkProgram(program);
117  GLint linkStatus = GL_FALSE;
118  glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
119  if(linkStatus != GL_TRUE)
120  {
121  GLint bufLength = 0;
122  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
123  if (bufLength > 0)
124  {
125  char* logBuffer = (char*) malloc(bufLength);
126 
127  if (logBuffer != NULL)
128  {
129  glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
130  LOGE("Could not link program:\n%s\n", logBuffer);
131  free(logBuffer);
132  logBuffer = NULL;
133  }
134  }
135  glDeleteProgram(program);
136  program = 0;
137  }
138  }
139  return program;
140 }
141 
147 
149 float modelViewMatrix[16];
150 float angle = 0;
151 
152 /* [setupGraphics] */
153 bool setupGraphics(int width, int height)
154 {
156 
157  if (simpleCubeProgram == 0)
158  {
159  LOGE ("Could not create program");
160  return false;
161  }
162 
163  vertexLocation = glGetAttribLocation(simpleCubeProgram, "vertexPosition");
164  vertexColourLocation = glGetAttribLocation(simpleCubeProgram, "vertexColour");
165  projectionLocation = glGetUniformLocation(simpleCubeProgram, "projection");
166  modelViewLocation = glGetUniformLocation(simpleCubeProgram, "modelView");
167 
168  /* Setup the perspective */
169  matrixPerspective(projectionMatrix, 45, (float)width / (float)height, 0.1f, 100);
170  glEnable(GL_DEPTH_TEST);
171 
172  glViewport(0, 0, width, height);
173 
174  return true;
175 }
176 /* [setupGraphics] */
177 
178 /* [cubeVertices] */
179 GLfloat cubeVertices[] = {-1.0f, 1.0f, -1.0f, /* Back. */
180  1.0f, 1.0f, -1.0f,
181  -1.0f, -1.0f, -1.0f,
182  1.0f, -1.0f, -1.0f,
183  -1.0f, 1.0f, 1.0f, /* Front. */
184  1.0f, 1.0f, 1.0f,
185  -1.0f, -1.0f, 1.0f,
186  1.0f, -1.0f, 1.0f,
187  -1.0f, 1.0f, -1.0f, /* Left. */
188  -1.0f, -1.0f, -1.0f,
189  -1.0f, -1.0f, 1.0f,
190  -1.0f, 1.0f, 1.0f,
191  1.0f, 1.0f, -1.0f, /* Right. */
192  1.0f, -1.0f, -1.0f,
193  1.0f, -1.0f, 1.0f,
194  1.0f, 1.0f, 1.0f,
195  -1.0f, -1.0f, -1.0f, /* Top. */
196  -1.0f, -1.0f, 1.0f,
197  1.0f, -1.0f, 1.0f,
198  1.0f, -1.0f, -1.0f,
199  -1.0f, 1.0f, -1.0f, /* Bottom. */
200  -1.0f, 1.0f, 1.0f,
201  1.0f, 1.0f, 1.0f,
202  1.0f, 1.0f, -1.0f
203  };
204 /* [cubeVertices] */
205 /* [colourComponents] */
206 GLfloat colour[] = {1.0f, 0.0f, 0.0f,
207  1.0f, 0.0f, 0.0f,
208  1.0f, 0.0f, 0.0f,
209  1.0f, 0.0f, 0.0f,
210  0.0f, 1.0f, 0.0f,
211  0.0f, 1.0f, 0.0f,
212  0.0f, 1.0f, 0.0f,
213  0.0f, 1.0f, 0.0f,
214  0.0f, 0.0f, 1.0f,
215  0.0f, 0.0f, 1.0f,
216  0.0f, 0.0f, 1.0f,
217  0.0f, 0.0f, 1.0f,
218  1.0f, 1.0f, 0.0f,
219  1.0f, 1.0f, 0.0f,
220  1.0f, 1.0f, 0.0f,
221  1.0f, 1.0f, 0.0f,
222  0.0f, 1.0f, 1.0f,
223  0.0f, 1.0f, 1.0f,
224  0.0f, 1.0f, 1.0f,
225  0.0f, 1.0f, 1.0f,
226  1.0f, 0.0f, 1.0f,
227  1.0f, 0.0f, 1.0f,
228  1.0f, 0.0f, 1.0f,
229  1.0f, 0.0f, 1.0f
230  };
231 /* [colourComponents] */
232 
233 /* [indices] */
234 GLushort indices[] = {0, 2, 3, 0, 1, 3, 4, 6, 7, 4, 5, 7, 8, 9, 10, 11, 8, 10, 12, 13, 14, 15, 12, 14, 16, 17, 18, 16, 19, 18, 20, 21, 22, 20, 23, 22};
235 /* [indices] */
236 
237 /* [renderFrame] */
239 {
240  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
241  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
242 
244 
247 
248  matrixTranslate(modelViewMatrix, 0.0f, 0.0f, -10.0f);
249 
250  glUseProgram(simpleCubeProgram);
251  glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cubeVertices);
252  glEnableVertexAttribArray(vertexLocation);
253  glVertexAttribPointer(vertexColourLocation, 3, GL_FLOAT, GL_FALSE, 0, colour);
254  glEnableVertexAttribArray(vertexColourLocation);
255 
256  glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix);
257  glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
258 
259  glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, indices);
260 
261  angle += 1;
262  if (angle > 360)
263  {
264  angle -= 360;
265  }
266 }
267 /* [renderFrame] */
268 extern "C"
269 {
271  JNIEnv * env, jobject obj, jint width, jint height);
273  JNIEnv * env, jobject obj);
274 };
275 
277  JNIEnv * env, jobject obj, jint width, jint height)
278 {
279  setupGraphics(width, height);
280 }
281 
283  JNIEnv * env, jobject obj)
284 {
285  renderFrame();
286 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
GLfloat colour[]
Definition: Native.cpp:249
float projectionMatrix[16]
Definition: Native.cpp:156
float modelViewMatrix[16]
Definition: Native.cpp:157
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
static const char glFragmentShader[]
Definition: Native.cpp:52
GLuint simpleCubeProgram
Definition: Native.cpp:142
GLuint modelViewLocation
Definition: Native.cpp:154
void matrixIdentityFunction(float *matrix)
Takes a 4 * 4 and sets the elements to the Identity function.
Definition: Matrix.cpp:23
GLuint vertexColourLocation
Definition: Native.cpp:152
void matrixPerspective(float *matrix, float fieldOfView, float aspectRatio, float zNear, float zFar)
Create a perspective projection matrix and store the results in the first parameter.
Definition: Matrix.cpp:96
static const char glVertexShader[]
Definition: Native.cpp:38
float angle
Definition: Native.cpp:158
void matrixRotateX(float *matrix, float angle)
Rotates a matrix around the x axis by a given angle.
Definition: Matrix.cpp:104
GLuint createProgram(const char *vertexSource, const char *fragmentSource)
Definition: Native.cpp:104
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLuint loadShader(GLenum shaderType, const char *shaderSource)
Definition: Native.cpp:69
void renderFrame(void)
Definition: Native.cpp:1536
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_simplecube_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:282
void matrixTranslate(float *matrix, float x, float y, float z)
Takes in a 4 * 4 matrix and translates it by the vector defined by x y and z.
Definition: Matrix.cpp:48
#define LOGE(...)
Definition: Native.cpp:35
GLuint vertexLocation
Definition: Native.cpp:151
GLfloat cubeVertices[]
Definition: Native.cpp:250
GLint GLsizei width
Definition: gl2ext.h:179
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_simplecube_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:276
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLuint program
Definition: gl2ext.h:1475
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLuint projectionLocation
Definition: Native.cpp:153
void matrixRotateY(float *matrix, float angle)
Rotates a matrix around the y axis by a given angle.
Definition: Matrix.cpp:116