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) 2014-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 
38 
39 static const char glVertexShader[] =
40  "attribute vec4 vertexPosition;\n"
41  "attribute vec3 vertexColour;\n"
42  "varying vec3 fragColour;\n"
43  "uniform mat4 projection;\n"
44  "uniform mat4 modelView;\n"
45  "void main()\n"
46  "{\n"
47  " gl_Position = projection * modelView * vertexPosition;\n"
48  " fragColour = vertexColour;\n"
49  "}\n";
50 
51 static const char glFragmentShader[] =
52  "precision mediump float;\n"
53  "varying vec3 fragColour;\n"
54  "void main()\n"
55  "{\n"
56  " gl_FragColor = vec4(fragColour, 1.0);\n"
57  "}\n";
58 
59 
60 static GLuint loadShader(GLenum shaderType, const char* shaderSource)
61 {
62  GLuint shader = glCreateShader(shaderType);
63  if (shader != 0)
64  {
65  glShaderSource(shader, 1, &shaderSource, NULL);
66  glCompileShader(shader);
67  GLint compiled = 0;
68  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
69  if (compiled != GL_TRUE)
70  {
71  GLint infoLen = 0;
72  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
73 
74  if (infoLen > 0)
75  {
76  char * logBuffer = (char*) malloc(infoLen);
77 
78  if (logBuffer != NULL)
79  {
80  glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
81  LOGE("Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
82  free(logBuffer);
83  logBuffer = NULL;
84  }
85 
86  glDeleteShader(shader);
87  shader = 0;
88  }
89  }
90  }
91 
92  return shader;
93 }
94 
95 static GLuint createProgram(const char* vertexSource, const char * fragmentSource)
96 {
97  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
98  if (vertexShader == 0)
99  {
100  return 0;
101  }
102 
103  GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
104  if (fragmentShader == 0)
105  {
106  return 0;
107  }
108 
109  GLuint program = glCreateProgram();
110 
111  if (program != 0)
112  {
113  glAttachShader(program, vertexShader);
114  glAttachShader(program, fragmentShader);
115  glLinkProgram(program);
116  GLint linkStatus = GL_FALSE;
117  glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
118  if(linkStatus != GL_TRUE)
119  {
120  GLint bufLength = 0;
121  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
122  if (bufLength > 0)
123  {
124  char* logBuffer = (char*) malloc(bufLength);
125 
126  if (logBuffer != NULL)
127  {
128  glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
129  LOGE("Could not link program:\n%s\n", logBuffer);
130  free(logBuffer);
131  logBuffer = NULL;
132  }
133  }
134  glDeleteProgram(program);
135  program = 0;
136  }
137  }
138  return program;
139 }
140 
146 /* [vboIDDefinition] */
148 /* [vboIDDefinition] */
149 
150 static float projectionMatrix[16];
151 static float modelViewMatrix[16];
152 static float angle = 0;
153 
154 /* [vboVertexData] */
155 static GLfloat cubeVertices[] = { -1.0f, 1.0f, -1.0f, /* Back Face First Vertex Position */
156  1.0f, 0.0f, 0.0f, /* Back Face First Vertex Colour */
157  1.0f, 1.0f, -1.0f, /* Back Face Second Vertex Position */
158  1.0f, 0.0f, 0.0f, /* Back Face Second Vertex Colour */
159  -1.0f, -1.0f, -1.0f, /* Back Face Third Vertex Position */
160  1.0f, 0.0f, 0.0f, /* Back Face Third Vertex Colour */
161  1.0f, -1.0f, -1.0f, /* Back Face Fourth Vertex Position */
162  1.0f, 0.0f, 0.0f, /* Back Face Fourth Vertex Colour */
163  -1.0f, 1.0f, 1.0f, /* Front. */
164  0.0f, 1.0f, 0.0f,
165  1.0f, 1.0f, 1.0f,
166  0.0f, 1.0f, 0.0f,
167  -1.0f, -1.0f, 1.0f,
168  0.0f, 1.0f, 0.0f,
169  1.0f, -1.0f, 1.0f,
170  0.0f, 1.0f, 0.0f,
171  -1.0f, 1.0f, -1.0f, /* Left. */
172  0.0f, 0.0f, 1.0f,
173  -1.0f, -1.0f, -1.0f,
174  0.0f, 0.0f, 1.0f,
175  -1.0f, -1.0f, 1.0f,
176  0.0f, 0.0f, 1.0f,
177  -1.0f, 1.0f, 1.0f,
178  0.0f, 0.0f, 1.0f,
179  1.0f, 1.0f, -1.0f, /* Right. */
180  1.0f, 1.0f, 0.0f,
181  1.0f, -1.0f, -1.0f,
182  1.0f, 1.0f, 0.0f,
183  1.0f, -1.0f, 1.0f,
184  1.0f, 1.0f, 0.0f,
185  1.0f, 1.0f, 1.0f,
186  1.0f, 1.0f, 0.0f,
187  -1.0f, -1.0f, -1.0f, /* Top. */
188  0.0f, 1.0f, 1.0f,
189  -1.0f, -1.0f, 1.0f,
190  0.0f, 1.0f, 1.0f,
191  1.0f, -1.0f, 1.0f,
192  0.0f, 1.0f, 1.0f,
193  1.0f, -1.0f, -1.0f,
194  0.0f, 1.0f, 1.0f,
195  -1.0f, 1.0f, -1.0f, /* Bottom. */
196  1.0f, 0.0f, 1.0f,
197  -1.0f, 1.0f, 1.0f,
198  1.0f, 0.0f, 1.0f,
199  1.0f, 1.0f, 1.0f,
200  1.0f, 0.0f, 1.0f,
201  1.0f, 1.0f, -1.0f,
202  1.0f, 0.0f, 1.0f,
203 };
204 /* [vboVertexData] */
205 
206 /* [vboStrideSize] */
207 static GLushort strideLength = 6 * sizeof(GLfloat);
208 /* [vboStrideSize] */
209 
210 /* [vboColourOffset] */
211 static GLushort vertexColourOffset = 3 * sizeof (GLfloat);
212 /* [vboColourOffset] */
213 
214 /* [vboBufferSize] */
215 static GLushort vertexBufferSize = 48 * 3 * sizeof (GLfloat);
216 /* [vboBufferSize] */
217 /* [vboElementSize] */
218 static GLushort elementBufferSize = 36 * sizeof(GLushort);
219 /* [vboElementSize] */
220 
221 static 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};
222 
223 static bool setupGraphics(int width, int height)
224 {
226 
227  if (glProgram == 0)
228  {
229  LOGE ("Could not create program");
230  return false;
231  }
232  /* [vboCreation] */
233  glGenBuffers(2, vboBufferIds);
234  glBindBuffer(GL_ARRAY_BUFFER, vboBufferIds[0]);
235  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboBufferIds[1]);
236  /* [vboCreation] */
237 
238  /* [vboAllocateSpace] */
239  glBufferData(GL_ARRAY_BUFFER, vertexBufferSize, cubeVertices, GL_STATIC_DRAW);
240  glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBufferSize, indices, GL_STATIC_DRAW);
241  /* [vboAllocateSpace] */
242 
243  vertexLocation = glGetAttribLocation(glProgram, "vertexPosition");
244  vertexColourLocation = glGetAttribLocation(glProgram, "vertexColour");
245  projectionLocation = glGetUniformLocation(glProgram, "projection");
246  modelViewLocation = glGetUniformLocation(glProgram, "modelView");
247 
248  matrixPerspective(projectionMatrix, 45, (float)width / (float)height, 0.1f, 100);
249  glEnable(GL_DEPTH_TEST);
250 
251  glViewport(0, 0, width, height);
252 
253  return true;
254 }
255 
256 static void renderFrame()
257 {
258  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
259  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
260 
262 
265 
266  matrixTranslate(modelViewMatrix, 0.0f, 0.0f, -10.0f);
267 
268  glUseProgram(glProgram);
269 
270  /* [vboVertexAttribPointer] */
271  glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, strideLength, 0);
272  glEnableVertexAttribArray(vertexLocation);
273  glVertexAttribPointer(vertexColourLocation, 3, GL_FLOAT, GL_FALSE, strideLength, (const void *) vertexColourOffset);
274  glEnableVertexAttribArray(vertexColourLocation);
275  /* [vboVertexAttribPointer] */
276 
277  glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix);
278  glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
279 
280  /* [vboDrawElements] */
281  glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
282  /* [vboDrawElements] */
283 
284  angle += 1;
285  if (angle > 360)
286  {
287  angle -= 360;
288  }
289 }
290 
291 extern "C"
292 {
294  JNIEnv * env, jobject obj, jint width, jint height);
296  JNIEnv * env, jobject obj);
297 };
298 
300  JNIEnv * env, jobject obj, jint width, jint height)
301 {
302  setupGraphics(width, height);
303 }
304 
306  JNIEnv * env, jobject obj)
307 {
308  renderFrame();
309 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
float projectionMatrix[16]
Definition: Native.cpp:156
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
GLuint vertexColourLocation
Definition: Native.cpp:152
static GLuint vboBufferIds[2]
Definition: Native.cpp:147
static GLushort vertexBufferSize
Definition: Native.cpp:215
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_vbo_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:305
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
static GLushort strideLength
Definition: Native.cpp:207
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
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
static GLushort vertexColourOffset
Definition: Native.cpp:211
static const char glFragmentShader[]
Definition: Native.cpp:51
GLuint vertexLocation
Definition: Native.cpp:151
static GLushort elementBufferSize
Definition: Native.cpp:218
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
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
GLuint glProgram
Definition: Native.cpp:150
static const char glVertexShader[]
Definition: Native.cpp:39
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_vbo_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:299