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 <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
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  /* [Add a vertex normal attribute.] */
42  "attribute vec3 vertexNormal;\n"
43  /* [Add a vertex normal attribute.] */
44  "varying vec3 fragColour;\n"
45  "uniform mat4 projection;\n"
46  "uniform mat4 modelView;\n"
47  "void main()\n"
48  "{\n"
49  /* [Setup scene vectors.] */
50  " vec3 transformedVertexNormal = normalize((modelView * vec4(vertexNormal, 0.0)).xyz);"
51  " vec3 inverseLightDirection = normalize(vec3(0.0, 1.0, 1.0));\n"
52  " fragColour = vec3(0.0);\n"
53  /* [Setup scene vectors.] */
54  "\n"
55  /* [Calculate the diffuse component.] */
56  " vec3 diffuseLightIntensity = vec3(1.0, 1.0, 1.0);\n"
57  " vec3 vertexDiffuseReflectionConstant = vertexColour;\n"
58  " float normalDotLight = max(0.0, dot(transformedVertexNormal, inverseLightDirection));\n"
59  " fragColour += normalDotLight * vertexDiffuseReflectionConstant * diffuseLightIntensity;\n"
60  /* [Calculate the diffuse component.] */
61  "\n"
62  /* [Calculate the ambient component.] */
63  " vec3 ambientLightIntensity = vec3(0.1, 0.1, 0.1);\n"
64  " vec3 vertexAmbientReflectionConstant = vertexColour;\n"
65  " fragColour += vertexAmbientReflectionConstant * ambientLightIntensity;\n"
66  /* [Calculate the ambient component.] */
67  "\n"
68  /* [Calculate the specular component.] */
69  " vec3 inverseEyeDirection = normalize(vec3(0.0, 0.0, 1.0));\n"
70  " vec3 specularLightIntensity = vec3(1.0, 1.0, 1.0);\n"
71  " vec3 vertexSpecularReflectionConstant = vec3(1.0, 1.0, 1.0);\n"
72  " float shininess = 2.0;\n"
73  " vec3 lightReflectionDirection = reflect(vec3(0) - inverseLightDirection, transformedVertexNormal);\n"
74  " float normalDotReflection = max(0.0, dot(inverseEyeDirection, lightReflectionDirection));\n"
75  " fragColour += pow(normalDotReflection, shininess) * vertexSpecularReflectionConstant * specularLightIntensity;\n"
76  /* [Calculate the specular component.] */
77  "\n"
78  " /* Make sure the fragment colour is between 0 and 1. */"
79  " clamp(fragColour, 0.0, 1.0);\n"
80  "\n"
81  " gl_Position = projection * modelView * vertexPosition;\n"
82  "}\n";
83 /* [vertexShader] */
84 
85 static const char glFragmentShader[] =
86  "precision mediump float;\n"
87  "varying vec3 fragColour;\n"
88  "void main()\n"
89  "{\n"
90  " gl_FragColor = vec4(fragColour, 1.0);\n"
91  "}\n";
92 
93 GLuint loadShader(GLenum shaderType, const char* shaderSource)
94 {
95  GLuint shader = glCreateShader(shaderType);
96  if (shader != 0)
97  {
98  glShaderSource(shader, 1, &shaderSource, NULL);
99  glCompileShader(shader);
100  GLint compiled = 0;
101  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
102  if (compiled != GL_TRUE)
103  {
104  GLint infoLen = 0;
105  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
106 
107  if (infoLen > 0)
108  {
109  char * logBuffer = (char*) malloc(infoLen);
110 
111  if (logBuffer != NULL)
112  {
113  glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
114  LOGE("Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
115  free(logBuffer);
116  logBuffer = NULL;
117  }
118 
119  glDeleteShader(shader);
120  shader = 0;
121  }
122  }
123  }
124 
125  return shader;
126 }
127 
128 GLuint createProgram(const char* vertexSource, const char * fragmentSource)
129 {
130  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
131  if (vertexShader == 0)
132  {
133  return 0;
134  }
135 
136  GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
137  if (fragmentShader == 0)
138  {
139  return 0;
140  }
141 
142  GLuint program = glCreateProgram();
143 
144  if (program != 0)
145  {
146  glAttachShader(program, vertexShader);
147  glAttachShader(program, fragmentShader);
148  glLinkProgram(program);
149  GLint linkStatus = GL_FALSE;
150  glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
151  if(linkStatus != GL_TRUE)
152  {
153  GLint bufLength = 0;
154  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
155  if (bufLength > 0)
156  {
157  char* logBuffer = (char*) malloc(bufLength);
158 
159  if (logBuffer != NULL)
160  {
161  glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
162  LOGE("Could not link program:\n%s\n", logBuffer);
163  free(logBuffer);
164  logBuffer = NULL;
165  }
166  }
167  glDeleteProgram(program);
168  program = 0;
169  }
170  }
171  return program;
172 }
173 
177 /* [Global variable to hold vertex normal attribute location.] */
179 /* [Global variable to hold vertex normal attribute location.] */
182 
184 float modelViewMatrix[16];
185 float angle = 0;
186 
187 bool setupGraphics(int width, int height)
188 {
190 
191  if (lightingProgram == 0)
192  {
193  LOGE ("Could not create program");
194  return false;
195  }
196 
197  vertexLocation = glGetAttribLocation(lightingProgram, "vertexPosition");
198  vertexColourLocation = glGetAttribLocation(lightingProgram, "vertexColour");
199  /* [Get vertex normal attribute location.] */
200  vertexNormalLocation = glGetAttribLocation(lightingProgram, "vertexNormal");
201  /* [Get vertex normal attribute location.] */
202  projectionLocation = glGetUniformLocation(lightingProgram, "projection");
203  modelViewLocation = glGetUniformLocation(lightingProgram, "modelView");
204 
205  /* Setup the perspective */
206  matrixPerspective(projectionMatrix, 45, (float)width / (float)height, 0.1f, 100);
207  glEnable(GL_DEPTH_TEST);
208 
209  glViewport(0, 0, width, height);
210 
211  return true;
212 }
213 
214 /* [Geometry] */
215 GLfloat verticies[] = { 1.0f, 1.0f, -1.0f, /* Back. */
216  -1.0f, 1.0f, -1.0f,
217  1.0f, -1.0f, -1.0f,
218  -1.0f, -1.0f, -1.0f,
219  0.0f, 0.0f, -2.0f,
220  -1.0f, 1.0f, 1.0f, /* Front. */
221  1.0f, 1.0f, 1.0f,
222  -1.0f, -1.0f, 1.0f,
223  1.0f, -1.0f, 1.0f,
224  0.0f, 0.0f, 2.0f,
225  -1.0f, 1.0f, -1.0f, /* Left. */
226  -1.0f, 1.0f, 1.0f,
227  -1.0f, -1.0f, -1.0f,
228  -1.0f, -1.0f, 1.0f,
229  -2.0f, 0.0f, 0.0f,
230  1.0f, 1.0f, 1.0f, /* Right. */
231  1.0f, 1.0f, -1.0f,
232  1.0f, -1.0f, 1.0f,
233  1.0f, -1.0f, -1.0f,
234  2.0f, 0.0f, 0.0f,
235  -1.0f, -1.0f, 1.0f, /* Bottom. */
236  1.0f, -1.0f, 1.0f,
237  -1.0f, -1.0f, -1.0f,
238  1.0f, -1.0f, -1.0f,
239  0.0f, -2.0f, 0.0f,
240  -1.0f, 1.0f, -1.0f, /* Top. */
241  1.0f, 1.0f, -1.0f,
242  -1.0f, 1.0f, 1.0f,
243  1.0f, 1.0f, 1.0f,
244  0.0f, 2.0f, 0.0f
245  };
246 /* [Geometry] */
247 
248 /* [Colours] */
249 GLfloat colour[] = {1.0f, 0.0f, 0.0f, /* Back. */
250  1.0f, 0.0f, 0.0f,
251  1.0f, 0.0f, 0.0f,
252  1.0f, 0.0f, 0.0f,
253  1.0f, 0.0f, 0.0f,
254  0.0f, 1.0f, 0.0f, /* Front. */
255  0.0f, 1.0f, 0.0f,
256  0.0f, 1.0f, 0.0f,
257  0.0f, 1.0f, 0.0f,
258  0.0f, 1.0f, 0.0f,
259  0.0f, 0.0f, 1.0f, /* Left. */
260  0.0f, 0.0f, 1.0f,
261  0.0f, 0.0f, 1.0f,
262  0.0f, 0.0f, 1.0f,
263  0.0f, 0.0f, 1.0f,
264  1.0f, 1.0f, 0.0f, /* Right. */
265  1.0f, 1.0f, 0.0f,
266  1.0f, 1.0f, 0.0f,
267  1.0f, 1.0f, 0.0f,
268  1.0f, 1.0f, 0.0f,
269  0.0f, 1.0f, 1.0f, /* Bottom. */
270  0.0f, 1.0f, 1.0f,
271  0.0f, 1.0f, 1.0f,
272  0.0f, 1.0f, 1.0f,
273  0.0f, 1.0f, 1.0f,
274  1.0f, 0.0f, 1.0f, /* Top. */
275  1.0f, 0.0f, 1.0f,
276  1.0f, 0.0f, 1.0f,
277  1.0f, 0.0f, 1.0f,
278  1.0f, 0.0f, 1.0f
279  };
280 /* [Colours] */
281 
282 /* [Normals] */
283 GLfloat normals[] = { 1.0f, 1.0f, -1.0f, /* Back. */
284  -1.0f, 1.0f, -1.0f,
285  1.0f, -1.0f, -1.0f,
286  -1.0f, -1.0f, -1.0f,
287  0.0f, 0.0f, -1.0f,
288  -1.0f, 1.0f, 1.0f, /* Front. */
289  1.0f, 1.0f, 1.0f,
290  -1.0f, -1.0f, 1.0f,
291  1.0f, -1.0f, 1.0f,
292  0.0f, 0.0f, 1.0f,
293  -1.0f, 1.0f, -1.0f, /* Left. */
294  -1.0f, 1.0f, 1.0f,
295  -1.0f, -1.0f, -1.0f,
296  -1.0f, -1.0f, 1.0f,
297  -1.0f, 0.0f, 0.0f,
298  1.0f, 1.0f, 1.0f, /* Right. */
299  1.0f, 1.0f, -1.0f,
300  1.0f, -1.0f, 1.0f,
301  1.0f, -1.0f, -1.0f,
302  1.0f, 0.0f, 0.0f,
303  -1.0f, -1.0f, 1.0f, /* Bottom. */
304  1.0f, -1.0f, 1.0f,
305  -1.0f, -1.0f, -1.0f,
306  1.0f, -1.0f, -1.0f,
307  0.0f, -1.0f, 0.0f,
308  -1.0f, 1.0f, -1.0f, /* Top. */
309  1.0f, 1.0f, -1.0f,
310  -1.0f, 1.0f, 1.0f,
311  1.0f, 1.0f, 1.0f,
312  0.0f, 1.0f, 0.0f
313  };
314 /* [Normals] */
315 
316 /* [Indices] */
317 GLushort indices[] = {0, 2, 4, 0, 4, 1, 1, 4, 3, 2, 3, 4, /* Back. */
318  5, 7, 9, 5, 9, 6, 6, 9, 8, 7, 8, 9, /* Front. */
319  10, 12, 14, 10, 14, 11, 11, 14, 13, 12, 13, 14, /* Left. */
320  15, 17, 19, 15, 19, 16, 16, 19, 18, 17, 18, 19, /* Right. */
321  20, 22, 24, 20, 24, 21, 21, 24, 23, 22, 23, 24, /* Bottom. */
322  25, 27, 29, 25, 29, 26, 26, 29, 28, 27, 28, 29 /* Top. */
323  };
324 /* [Indices] */
325 
327 {
328  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
329  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
330 
332 
335 
336  matrixTranslate(modelViewMatrix, 0.0f, 0.0f, -10.0f);
337 
338  glUseProgram(lightingProgram);
339  glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, verticies);
340  glEnableVertexAttribArray(vertexLocation);
341  glVertexAttribPointer(vertexColourLocation, 3, GL_FLOAT, GL_FALSE, 0, colour);
342  glEnableVertexAttribArray(vertexColourLocation);
343 
344  /* [Upload vertex normals.] */
345  glVertexAttribPointer(vertexNormalLocation, 3, GL_FLOAT, GL_FALSE, 0, normals);
346  glEnableVertexAttribArray(vertexNormalLocation);
347  /* [Upload vertex normals.] */
348 
349  glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix);
350  glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
351 
352  /* [Draw the object.] */
353  glDrawElements(GL_TRIANGLES, 72, GL_UNSIGNED_SHORT, indices);
354  /* [Draw the object.] */
355 
356  angle += 1;
357  if (angle > 360)
358  {
359  angle -= 360;
360  }
361 }
362 
363 extern "C"
364 {
366  JNIEnv * env, jobject obj, jint width, jint height);
368  JNIEnv * env, jobject obj);
369 };
370 
372  JNIEnv * env, jobject obj, jint width, jint height)
373 {
374  setupGraphics(width, height);
375 }
376 
378  JNIEnv * env, jobject obj)
379 {
380  renderFrame();
381 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
static const char glVertexShader[]
Definition: Native.cpp:38
GLuint vertexNormalLocation
Definition: Native.cpp:178
GLfloat colour[]
Definition: Native.cpp:249
float projectionMatrix[16]
Definition: Native.cpp:156
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_lighting_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:371
float modelViewMatrix[16]
Definition: Native.cpp:157
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
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
static const char glFragmentShader[]
Definition: Native.cpp:85
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
#define LOGE(...)
Definition: Native.cpp:35
float angle
Definition: Native.cpp:158
GLuint lightingProgram
Definition: Native.cpp:174
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
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_lighting_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:377
GLuint loadShader(GLenum shaderType, const char *shaderSource)
Definition: Native.cpp:69
void renderFrame(void)
Definition: Native.cpp:1536
GLfloat verticies[]
Definition: Native.cpp:215
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
GLuint vertexLocation
Definition: Native.cpp:151
GLint GLsizei width
Definition: gl2ext.h:179
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
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
GLfloat normals[]
Definition: Native.cpp:283