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 /* [shaders] */
39 static const char glVertexShader[] =
40  "attribute vec4 vertexPosition;\n"
41  "attribute vec2 vertexTextureCord;\n"
42  "varying vec2 textureCord;\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  " textureCord = vertexTextureCord;\n"
49  "}\n";
50 
51 static const char glFragmentShader[] =
52  "precision mediump float;\n"
53  "uniform sampler2D texture;\n"
54  "varying vec2 textureCord;\n"
55  "void main()\n"
56  "{\n"
57  " gl_FragColor = texture2D(texture, textureCord);\n"
58  "}\n";
59 /* [shaders] */
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 
69  GLint compiled = 0;
70  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
71 
72  if (compiled != GL_TRUE)
73  {
74  GLint infoLen = 0;
75  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
76 
77  if (infoLen > 0)
78  {
79  char * logBuffer = (char*) malloc(infoLen);
80 
81  if (logBuffer != NULL)
82  {
83  glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
84  LOGE("Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
85  free(logBuffer);
86  logBuffer = NULL;
87  }
88 
89  glDeleteShader(shader);
90  shader = 0;
91  }
92  }
93  }
94 
95  return shader;
96 }
97 
98 GLuint createProgram(const char* vertexSource, const char * fragmentSource)
99 {
100  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
101  if (vertexShader == 0)
102  {
103  return 0;
104  }
105 
106  GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
107  if (fragmentShader == 0)
108  {
109  return 0;
110  }
111 
112  GLuint program = glCreateProgram();
113 
114  if (program != 0)
115  {
116  glAttachShader(program , vertexShader);
117  glAttachShader(program, fragmentShader);
118  glLinkProgram(program);
119  GLint linkStatus = GL_FALSE;
120  glGetProgramiv(program , GL_LINK_STATUS, &linkStatus);
121 
122  if(linkStatus != GL_TRUE)
123  {
124  GLint bufLength = 0;
125 
126  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
127 
128  if (bufLength > 0)
129  {
130  char* logBuffer = (char*) malloc(bufLength);
131 
132  if (logBuffer != NULL)
133  {
134  glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
135  LOGE("Could not link program:\n%s\n", logBuffer);
136  free(logBuffer);
137  logBuffer = NULL;
138  }
139  }
140  glDeleteProgram(program);
141  program = 0;
142  }
143  }
144  return program;
145 }
146 
154 
156 float modelViewMatrix[16];
157 float angle = 0;
158 
159 /* [setupGraphicsUpdate] */
160 bool setupGraphics(int width, int height)
161 {
163 
164  if (!glProgram)
165  {
166  LOGE ("Could not create program");
167  return false;
168  }
169 
170  vertexLocation = glGetAttribLocation(glProgram, "vertexPosition");
171  textureCordLocation = glGetAttribLocation(glProgram, "vertexTextureCord");
172  projectionLocation = glGetUniformLocation(glProgram, "projection");
173  modelViewLocation = glGetUniformLocation(glProgram, "modelView");
174  samplerLocation = glGetUniformLocation(glProgram, "texture");
175 
176  /* Setup the perspective. */
177  matrixPerspective(projectionMatrix, 45, (float)width / (float)height, 0.1f, 100);
178  glEnable(GL_DEPTH_TEST);
179 
180  glViewport(0, 0, width, height);
181 
182  /* Load the Texture. */
184  if(textureId == 0)
185  {
186  return false;
187  }
188  else
189  {
190  return true;
191  }
192 }
193 /* [setupGraphicsUpdate] */
194 /* [verticesAndTexture] */
195 GLfloat cubeVertices[] = {-1.0f, 1.0f, -1.0f, /* Back. */
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, /* Front. */
200  1.0f, 1.0f, 1.0f,
201  -1.0f, -1.0f, 1.0f,
202  1.0f, -1.0f, 1.0f,
203  -1.0f, 1.0f, -1.0f, /* Left. */
204  -1.0f, -1.0f, -1.0f,
205  -1.0f, -1.0f, 1.0f,
206  -1.0f, 1.0f, 1.0f,
207  1.0f, 1.0f, -1.0f, /* Right. */
208  1.0f, -1.0f, -1.0f,
209  1.0f, -1.0f, 1.0f,
210  1.0f, 1.0f, 1.0f,
211  -1.0f, 1.0f, -1.0f, /* Top. */
212  -1.0f, 1.0f, 1.0f,
213  1.0f, 1.0f, 1.0f,
214  1.0f, 1.0f, -1.0f,
215  -1.0f, - 1.0f, -1.0f, /* Bottom. */
216  -1.0f, -1.0f, 1.0f,
217  1.0f, - 1.0f, 1.0f,
218  1.0f, -1.0f, -1.0f
219  };
220 
221 GLfloat textureCords[] = { 1.0f, 1.0f, /* Back. */
222  0.0f, 1.0f,
223  1.0f, 0.0f,
224  0.0f, 0.0f,
225  0.0f, 1.0f, /* Front. */
226  1.0f, 1.0f,
227  0.0f, 0.0f,
228  1.0f, 0.0f,
229  0.0f, 1.0f, /* Left. */
230  0.0f, 0.0f,
231  1.0f, 0.0f,
232  1.0f, 1.0f,
233  1.0f, 1.0f, /* Right. */
234  1.0f, 0.0f,
235  0.0f, 0.0f,
236  0.0f, 1.0f,
237  0.0f, 1.0f, /* Top. */
238  0.0f, 0.0f,
239  1.0f, 0.0f,
240  1.0f, 1.0f,
241  0.0f, 0.0f, /* Bottom. */
242  0.0f, 1.0f,
243  1.0f, 1.0f,
244  1.0f, 0.0f
245 };
246 /* [verticesAndTexture] */
247 
248 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};
249 
251 {
252  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
253  glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
254 
256 
259 
260  matrixTranslate(modelViewMatrix, 0.0f, 0.0f, -10.0f);
261 
262  glUseProgram(glProgram);
263  glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, cubeVertices);
264  glEnableVertexAttribArray(vertexLocation);
265 
266  /* [enableAttributes] */
267  glVertexAttribPointer(textureCordLocation, 2, GL_FLOAT, GL_FALSE, 0, textureCords);
268  glEnableVertexAttribArray(textureCordLocation);
269  glUniformMatrix4fv(projectionLocation, 1, GL_FALSE,projectionMatrix);
270  glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
271 
272  /* Set the sampler texture unit to 0. */
273  glUniform1i(samplerLocation, 0);
274  /* [enableAttributes] */
275  glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, indicies);
276 
277  angle += 1;
278  if (angle > 360)
279  {
280  angle -= 360;
281  }
282 }
283 
284 extern "C"
285 {
286  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_texturecube_NativeLibrary_init (JNIEnv * env, jobject obj, jint width, jint height);
288  JNIEnv * env, jobject obj);
289 };
290 
292  JNIEnv * env, jobject obj, jint width, jint height)
293 {
294  setupGraphics(width, height);
295 }
296 
298  JNIEnv * env, jobject obj)
299 {
300  renderFrame();
301 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
GLuint samplerLocation
Definition: Native.cpp:147
static const char glFragmentShader[]
Definition: Native.cpp:51
float projectionMatrix[16]
Definition: Native.cpp:156
float modelViewMatrix[16]
Definition: Native.cpp:157
#define LOGE(...)
Definition: Native.cpp:36
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
GLfloat textureCords[]
Definition: Native.cpp:240
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
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_texturecube_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:297
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
GLuint loadSimpleTexture()
Loads a simple 3 x 3 static texture into OpenGL ES.
Definition: Texture.cpp:28
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLushort indicies[]
Definition: Native.cpp:246
GLuint loadShader(GLenum shaderType, const char *shaderSource)
Definition: Native.cpp:69
static const char glVertexShader[]
Definition: Native.cpp:39
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
GLuint vertexLocation
Definition: Native.cpp:151
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_texturecube_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:291
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