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 #include "Texture.h"
33 
34 #define LOG_TAG "libNative"
35 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
36 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
37 
38 /* [vertexShader] */
39 static const char glVertexShader[] =
40  "attribute vec4 vertexPosition;\n"
41  "attribute vec2 vertexTextureCord;\n"
42  "attribute vec3 vertexNormal;\n"
43  "attribute vec3 vertexColor; \n"
44  "attribute vec3 vertexTangent;\n"
45  "attribute vec3 vertexBiNormal;\n"
46  "varying vec2 textureCord;\n"
47  "varying vec3 varyingColor; \n"
48  "varying vec3 inverseLightDirection;\n"
49  "varying vec3 inverseEyeDirection;\n"
50  "uniform mat4 projection;\n"
51  "uniform mat4 modelView;\n"
52  "void main()\n"
53  "{\n"
54  " vec3 worldSpaceVertex =(modelView * vertexPosition).xyz;"
55  " vec3 transformedVertexNormal = normalize((modelView * vec4(vertexNormal, 0.0)).xyz);"
56 
57  " inverseLightDirection = normalize(vec3(0.0, 0.0, 1.0));\n"
58  " inverseEyeDirection = normalize((vec3(0.0, 0.0, 1.0)- worldSpaceVertex ).xyz);\n"
59 
60  " gl_Position = projection * modelView * vertexPosition;\n"
61  " textureCord = vertexTextureCord;\n"
62  " varyingColor = vertexColor;\n"
63 
64  " vec3 transformedTangent = normalize((modelView * vec4(vertexTangent, 0.0)).xyz);\n"
65  " vec3 transformedBinormal = normalize((modelView * vec4(vertexBiNormal, 0.0)).xyz);\n"
66  " mat3 tangentMatrix = mat3(transformedTangent, transformedBinormal, transformedVertexNormal);\n"
67  " inverseLightDirection =inverseLightDirection * tangentMatrix;\n"
68  " inverseEyeDirection = inverseEyeDirection * tangentMatrix;\n"
69  "}\n";
70 /* [vertexShader] */
71 /* [fragmentShader] */
72 static const char glFragmentShader[] =
73  "precision mediump float;\n"
74  "uniform sampler2D texture;\n"
75  "varying vec2 textureCord;\n"
76  "varying vec3 varyingColor;\n"
77  "varying vec3 inverseLightDirection;\n"
78  "varying vec3 inverseEyeDirection;\n"
79  "varying vec3 transformedVertexNormal;\n"
80  "void main()\n"
81  "{\n"
82  " vec3 fragColor = vec3(0.0,0.0,0.0); \n"
83  " vec3 normal = texture2D(texture, textureCord).xyz;"
84  " normal = normalize(normal * 2.0 -1.0);"
85  /* Calculate the diffuse component. */
86  " vec3 diffuseLightIntensity = vec3(1.0, 1.0, 1.0);\n"
87  " float normalDotLight = max(0.0, dot(normal, inverseLightDirection));\n"
88  " fragColor += normalDotLight * varyingColor *diffuseLightIntensity;\n"
89  /* Calculate the ambient component. */
90  " vec3 ambientLightIntensity = vec3(0.1, 0.1, 0.1);\n"
91  " fragColor += ambientLightIntensity * varyingColor;\n"
92  /* Calculate the specular component. */
93  " vec3 specularLightIntensity = vec3(1.0, 1.0, 1.0);\n"
94  " vec3 vertexSpecularReflectionConstant = vec3(1.0, 1.0, 1.0);\n"
95  " float shininess = 2.0;\n"
96  " vec3 lightReflectionDirection = reflect(vec3(0) - inverseLightDirection, normal);\n"
97  " float normalDotReflection = max(0.0, dot(inverseEyeDirection, lightReflectionDirection));\n"
98  " fragColor += pow(normalDotReflection, shininess) * vertexSpecularReflectionConstant * specularLightIntensity;\n"
99  " /* Make sure the fragment colour is between 0 and 1. */"
100  " clamp(fragColor, 0.0, 1.0);\n"
101  " gl_FragColor = vec4(fragColor,1.0);\n"
102  "}\n";
103 /* [fragmentShader] */
104 
105 GLuint loadShader(GLenum shaderType, const char* shaderSource)
106 {
107  GLuint shader = glCreateShader(shaderType);
108  if (shader != 0)
109  {
110  glShaderSource(shader, 1, &shaderSource, NULL);
111  glCompileShader(shader);
112 
113  GLint compiled = 0;
114  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
115 
116  if (compiled != GL_TRUE)
117  {
118  GLint infoLen = 0;
119  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
120 
121  if (infoLen > 0)
122  {
123  char * logBuffer = (char*) malloc(infoLen);
124 
125  if (logBuffer != NULL)
126  {
127  glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
128  LOGE("Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
129  free(logBuffer);
130  logBuffer = NULL;
131  }
132 
133  glDeleteShader(shader);
134  shader = 0;
135  }
136  }
137  }
138 
139  return shader;
140 }
141 
142 GLuint createProgram(const char* vertexSource, const char * fragmentSource)
143 {
144  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
145  if (vertexShader == 0)
146  {
147  return 0;
148  }
149 
150  GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
151  if (fragmentShader == 0)
152  {
153  return 0;
154  }
155 
156  GLuint program = glCreateProgram();
157 
158  if (program != 0)
159  {
160  glAttachShader(program , vertexShader);
161  glAttachShader(program, fragmentShader);
162  glLinkProgram(program);
163  GLint linkStatus = GL_FALSE;
164  glGetProgramiv(program , GL_LINK_STATUS, &linkStatus);
165 
166  if(linkStatus != GL_TRUE)
167  {
168  GLint bufLength = 0;
169 
170  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
171 
172  if (bufLength > 0)
173  {
174  char* logBuffer = (char*) malloc(bufLength);
175 
176  if (logBuffer != NULL)
177  {
178  glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
179  LOGE("Could not link program:\n%s\n", logBuffer);
180  free(logBuffer);
181  logBuffer = NULL;
182  }
183  }
184  glDeleteProgram(program);
185  program = 0;
186  }
187  }
188  return program;
189 }
190 
192 
193 /* [LocationVariables] */
204 /* [LocationVariables] */
205 
207 float modelViewMatrix[16];
208 float angle = 0;
209 
210 bool setupGraphics(int width, int height)
211 {
213 
214  if (!glProgram)
215  {
216  LOGE ("Could not create program");
217  return false;
218  }
219 
220  /* [setLocation] */
221  vertexLocation = glGetAttribLocation(glProgram, "vertexPosition");
222  textureCordLocation = glGetAttribLocation(glProgram, "vertexTextureCord");
223  projectionLocation = glGetUniformLocation(glProgram, "projection");
224  modelViewLocation = glGetUniformLocation(glProgram, "modelView");
225  samplerLocation = glGetUniformLocation(glProgram, "texture");
226  vertexNormalLocation = glGetAttribLocation(glProgram, "vertexNormal");
227  colorLocation = glGetAttribLocation(glProgram, "vertexColor");
228  tangentLocation = glGetAttribLocation(glProgram, "vertexTangent");
229  biNormalLocation = glGetAttribLocation(glProgram, "vertexBiNormal");
230  /* [setLocation] */
231 
232  /* Setup the perspective. */
233  matrixPerspective(projectionMatrix, 45, (float)width / (float)height, 0.1f, 100);
234  glEnable(GL_DEPTH_TEST);
235 
236  glViewport(0, 0, width, height);
237 
238  /* Load the Texture. */
240  if(textureId == 0)
241  {
242  return false;
243  }
244  else
245  {
246  return true;
247  }
248 }
249 /* [vertexColourTangentNormal] */
250 GLfloat cubeVertices[] = {-1.0f, 1.0f, -1.0f, /* Back. */
251  1.0f, 1.0f, -1.0f,
252  -1.0f, -1.0f, -1.0f,
253  1.0f, -1.0f, -1.0f,
254  -1.0f, 1.0f, 1.0f, /* Front. */
255  1.0f, 1.0f, 1.0f,
256  -1.0f, -1.0f, 1.0f,
257  1.0f, -1.0f, 1.0f,
258  -1.0f, 1.0f, -1.0f, /* Left. */
259  -1.0f, -1.0f, -1.0f,
260  -1.0f, -1.0f, 1.0f,
261  -1.0f, 1.0f, 1.0f,
262  1.0f, 1.0f, -1.0f, /* Right. */
263  1.0f, -1.0f, -1.0f,
264  1.0f, -1.0f, 1.0f,
265  1.0f, 1.0f, 1.0f,
266  -1.0f, 1.0f, -1.0f, /* Top. */
267  -1.0f, 1.0f, 1.0f,
268  1.0f, 1.0f, 1.0f,
269  1.0f, 1.0f, -1.0f,
270  -1.0f, - 1.0f, -1.0f, /* Bottom. */
271  -1.0f, -1.0f, 1.0f,
272  1.0f, - 1.0f, 1.0f,
273  1.0f, -1.0f, -1.0f
274  };
275 
276 GLfloat normals[] = {0.0f, 0.0f, -1.0f, /* Back */
277  0.0f, 0.0f, -1.0f,
278  0.0f, 0.0f, -1.0f,
279  0.0f, 0.0f, -1.0f,
280  0.0f, 0.0f, 1.0f, /* Front */
281  0.0f, 0.0f, 1.0f,
282  0.0f, 0.0f, 1.0f,
283  0.0f, 0.0f, 1.0f,
284  -1.0f, 0.0, 0.0f, /* Left */
285  -1.0f, 0.0f, 0.0f,
286  -1.0f, 0.0f, 0.0f,
287  -1.0f, 0.0f, 0.0f,
288  1.0f, 0.0f, 0.0f, /* Right */
289  1.0f, 0.0f, 0.0f,
290  1.0f, 0.0f, 0.0f,
291  1.0f, 0.0f, 0.0f,
292  0.0f, 1.0f, 0.0f, /* Top */
293  0.0f, 1.0f, 0.0f,
294  0.0f, 1.0f, 0.0f,
295  0.0f, 1.0f, 0.0f,
296  0.0f, -1.0f, 0.0f, /* Bottom */
297  0.0f, -1.0f, 0.0f,
298  0.0f, -1.0f, 0.0f,
299  0.0f, -1.0f, 0.0f
300 };
301 
302 GLfloat colour[] = {1.0f, 0.0f, 0.0f,
303  1.0f, 0.0f, 0.0f,
304  1.0f, 0.0f, 0.0f,
305  1.0f, 0.0f, 0.0f,
306  0.0f, 1.0f, 0.0f,
307  0.0f, 1.0f, 0.0f,
308  0.0f, 1.0f, 0.0f,
309  0.0f, 1.0f, 0.0f,
310  0.0f, 0.0f, 1.0f,
311  0.0f, 0.0f, 1.0f,
312  0.0f, 0.0f, 1.0f,
313  0.0f, 0.0f, 1.0f,
314  1.0f, 1.0f, 0.0f,
315  1.0f, 1.0f, 0.0f,
316  1.0f, 1.0f, 0.0f,
317  1.0f, 1.0f, 0.0f,
318  0.0f, 1.0f, 1.0f,
319  0.0f, 1.0f, 1.0f,
320  0.0f, 1.0f, 1.0f,
321  0.0f, 1.0f, 1.0f,
322  1.0f, 0.0f, 1.0f,
323  1.0f, 0.0f, 1.0f,
324  1.0f, 0.0f, 1.0f,
325  1.0f, 0.0f, 1.0f
326 };
327 
328 GLfloat tangents[] = {-1.0f, 0.0f, 0.0f, /* Back */
329  -1.0f, 0.0f, 0.0f,
330  -1.0f, 0.0f, 0.0f,
331  -1.0f, 0.0f, 0.0f,
332  1.0f, 0.0f, 0.0f, /* Front */
333  1.0f, 0.0f, 0.0f,
334  1.0f, 0.0f, 0.0f,
335  1.0f, 0.0f, 0.0f,
336  0.0f, 0.0f, 1.0f, /* Left */
337  0.0f, 0.0f, 1.0f,
338  0.0f, 0.0f, 1.0f,
339  0.0f, 0.0f, 1.0f,
340  0.0f, 0.0f, -1.0f, /* Right */
341  0.0f, 0.0f, -1.0f,
342  0.0f, 0.0f, -1.0f,
343  0.0f, 0.0f, -1.0f,
344  1.0f, 0.0f, 0.0f, /* Top */
345  1.0f, 0.0f, 0.0f,
346  1.0f, 0.0f, 0.0f,
347  1.0f, 0.0f, 0.0f,
348  1.0f, 0.0f, 0.0f, /* Bottom */
349  1.0f, 0.0f, 0.0f,
350  1.0f, 0.0f, 0.0f,
351  1.0f, 0.0f, 0.0f
352 };
353 
354 GLfloat biNormals[] = { 0.0f, 1.0f, 0.0f, /* Back */
355  0.0f, 1.0f, 0.0f,
356  0.0f, 1.0f, 0.0f,
357  0.0f, 1.0f, 0.0f,
358  0.0f, 1.0f, 0.0f, /* Front */
359  0.0f, 1.0f, 0.0f,
360  0.0f, 1.0f, 0.0f,
361  0.0f, 1.0f, 0.0f,
362  0.0f, 1.0f, 0.0f, /* Left */
363  0.0f, 1.0f, 0.0f,
364  0.0f, 1.0f, 0.0f,
365  0.0f, 1.0f, 0.0f,
366  0.0f, 1.0f, 0.0f, /* Right */
367  0.0f, 1.0f, 0.0f,
368  0.0f, 1.0f, 0.0f,
369  0.0f, 1.0f, 0.0f,
370  0.0f, 0.0f, -1.0f, /* Top */
371  0.0f, 0.0f, -1.0f,
372  0.0f, 0.0f, -1.0f,
373  0.0f, 0.0f, -1.0f,
374  0.0f, 0.0f, 1.0f, /* Bottom */
375  0.0f, 0.0f, 1.0f,
376  0.0f, 0.0f, 1.0f,
377  0.0f, 0.0f, 1.0f
378 
379 };
380 GLfloat textureCords[] = {1.0f, 1.0f, /* Back. */
381  0.0f, 1.0f,
382  1.0f, 0.0f,
383  0.0f, 0.0f,
384  0.0f, 1.0f, /* Front. */
385  1.0f, 1.0f,
386  0.0f, 0.0f,
387  1.0f, 0.0f,
388  0.0f, 1.0f, /* Left. */
389  0.0f, 0.0f,
390  1.0f, 0.0f,
391  1.0f, 1.0f,
392  1.0f, 1.0f, /* Right. */
393  1.0f, 0.0f,
394  0.0f, 0.0f,
395  0.0f, 1.0f,
396  0.0f, 1.0f, /* Top. */
397  0.0f, 0.0f,
398  1.0f, 0.0f,
399  1.0f, 1.0f,
400  0.0f, 0.0f, /* Bottom. */
401  0.0f, 1.0f,
402  1.0f, 1.0f,
403  1.0f, 0.0f
404 };
405 
406 GLushort indicies[] = {0, 3, 2, 0, 1, 3, 4, 6, 7, 4, 7, 5, 8, 9, 10, 8, 11, 10, 12, 13, 14, 15, 12, 14, 16, 17, 18, 16, 19, 18, 20, 21, 22, 20, 23, 22};
407 /* [vertexColourTangentNormal] */
408 
410 {
411  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
412  glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
413 
415 
418 
419  matrixTranslate(modelViewMatrix, 0.0f, 0.0f, -10.0f);
420 
421  glUseProgram(glProgram);
422 
423  /* [supplyData] */
424  glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cubeVertices);
425  glEnableVertexAttribArray(vertexLocation);
426  glVertexAttribPointer(textureCordLocation, 2, GL_FLOAT, GL_FALSE, 0, textureCords);
427  glEnableVertexAttribArray(textureCordLocation);
428  glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, colour);
429  glEnableVertexAttribArray(colorLocation);
430  glVertexAttribPointer(vertexNormalLocation, 3, GL_FLOAT, GL_FALSE, 0, normals);
431  glEnableVertexAttribArray(vertexNormalLocation);
432  glVertexAttribPointer(biNormalLocation, 3, GL_FLOAT, GL_FALSE, 0, biNormals);
433  glEnableVertexAttribArray(biNormalLocation);
434  glVertexAttribPointer(tangentLocation, 3, GL_FLOAT, GL_FALSE, 0, tangents);
435  glEnableVertexAttribArray(tangentLocation);
436  glUniformMatrix4fv(projectionLocation, 1, GL_FALSE,projectionMatrix);
437  glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
438  /* [supplyData] */
439 
440  /* Set the sampler texture unit to 0. */
441  glUniform1i(samplerLocation, 0);
442 
443  glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, indicies);
444 
445  angle += 1;
446  if (angle > 360)
447  {
448  angle -= 360;
449  }
450 }
451 
452 extern "C"
453 {
454  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_normalmapping_NativeLibrary_init (JNIEnv * env, jobject obj, jint width, jint height);
456  JNIEnv * env, jobject obj);
457 };
458 
460  JNIEnv * env, jobject obj, jint width, jint height)
461 {
462  setupGraphics(width, height);
463 }
464 
466  JNIEnv * env, jobject obj)
467 {
468  renderFrame();
469 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
#define LOGE(...)
Definition: Native.cpp:36
GLuint samplerLocation
Definition: Native.cpp:147
GLuint vertexNormalLocation
Definition: Native.cpp:178
GLfloat colour[]
Definition: Native.cpp:249
float projectionMatrix[16]
Definition: Native.cpp:156
GLfloat tangents[]
Definition: Native.cpp:328
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
void loadTexture(const char *texture, unsigned int level, unsigned int width, unsigned int height)
Loads a desired texture into memory at an appropriate mipmap level.
Definition: Texture.cpp:35
GLfloat textureCords[]
Definition: Native.cpp:240
GLuint colorLocation
Definition: Native.cpp:199
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
float angle
Definition: Native.cpp:158
GLuint textureCordLocation
Definition: Native.cpp:150
GLuint textureId
Definition: Native.cpp:200
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
static const char glFragmentShader[]
Definition: Native.cpp:72
GLfloat biNormals[]
Definition: Native.cpp:354
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLushort indicies[]
Definition: Native.cpp:246
GLuint loadShader(GLenum shaderType, const char *shaderSource)
Definition: Native.cpp:69
void renderFrame(void)
Definition: Native.cpp:1536
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
static const char glVertexShader[]
Definition: Native.cpp:39
GLuint biNormalLocation
Definition: Native.cpp:203
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
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLuint program
Definition: gl2ext.h:1475
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_normalmapping_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:465
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_normalmapping_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:459
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
GLuint tangentLocation
Definition: Native.cpp:202
GLuint glProgram
Definition: Native.cpp:150