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 #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 static const char glVertexShader[] =
39  "attribute vec4 vertexPosition;\n"
40  "attribute vec2 vertexTextureCord;\n"
41  "varying vec2 textureCord;\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  " textureCord = vertexTextureCord;\n"
48  "}\n";
49 
50 static const char glFragmentShader[] =
51  "precision mediump float;\n"
52  "uniform sampler2D texture;\n"
53  "varying vec2 textureCord;\n"
54  "void main()\n"
55  "{\n"
56  " gl_FragColor = texture2D(texture, textureCord);\n"
57  "}\n";
58 
59 GLuint loadShader(GLenum shaderType, const char* shaderSource)
60 {
61  GLuint shader = glCreateShader(shaderType);
62  if (shader != 0)
63  {
64  glShaderSource(shader, 1, &shaderSource, NULL);
65  glCompileShader(shader);
66 
67  GLint compiled = 0;
68  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
69 
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 
120  if(linkStatus != GL_TRUE)
121  {
122  GLint bufLength = 0;
123 
124  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
125 
126  if (bufLength > 0)
127  {
128  char* logBuffer = (char*) malloc(bufLength);
129 
130  if (logBuffer != NULL)
131  {
132  glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
133  LOGE("Could not link program:\n%s\n", logBuffer);
134  free(logBuffer);
135  logBuffer = NULL;
136  }
137  }
138  glDeleteProgram(program);
139  program = 0;
140  }
141  }
142  return program;
143 }
144 
152 
154 float modelViewMatrix[16];
155 /* [newGlobals] */
156 float distance = 1;
157 float velocity = 0.1;
159 /* [newGlobals] */
160 
161 bool setupGraphics(int width, int height)
162 {
164 
165  if (!glProgram)
166  {
167  LOGE ("Could not create program");
168  return false;
169  }
170 
171  vertexLocation = glGetAttribLocation(glProgram, "vertexPosition");
172  textureCordLocation = glGetAttribLocation(glProgram, "vertexTextureCord");
173  projectionLocation = glGetUniformLocation(glProgram, "projection");
174  modelViewLocation = glGetUniformLocation(glProgram, "modelView");
175  samplerLocation = glGetUniformLocation(glProgram, "texture");
176 
177  /* Setup the perspective. */
178  /* [matrixPerspective] */
179  matrixPerspective(projectionMatrix, 45, (float)width / (float)height, 0.1f, 170);
180  /* [matrixPerspective] */
181  glEnable(GL_DEPTH_TEST);
182 
183  glViewport(0, 0, width, height);
184 
185  /* Code has been pulled out of the texture function as each of load texture calls for both compressed
186  * and uncompressed textures need to use the same textureId
187  */
188  /* [mipmapRegularTextures] */
189  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
190  /* Generate a texture object. */
191  glGenTextures(2, textureIds);
192  /* Activate a texture. */
193  glActiveTexture(GL_TEXTURE0);
194  /* Bind the texture object. */
195  glBindTexture(GL_TEXTURE_2D, textureIds[0]);
196 
197 
198  /* Load the Texture. */
199  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level0.raw", 0, 512, 512);
200  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level1.raw", 1, 256, 256);
201  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level2.raw", 2, 128, 128);
202  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level3.raw", 3, 64, 64);
203  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level4.raw", 4, 32, 32);
204  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level5.raw", 5, 16, 16);
205  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level6.raw", 6, 8, 8);
206  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level7.raw", 7, 4, 4);
207  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level8.raw", 8, 2, 2);
208  loadTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level9.raw", 9, 1, 1);
209  /* [mipmapRegularTextures] */
210 
211  /* [mipmapCompressedTextures] */
212  /* Activate a texture. */
213  glActiveTexture(GL_TEXTURE1);
214 
215  /* Bind the texture object. */
216  glBindTexture(GL_TEXTURE_2D, textureIds[1]);
217 
218  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level0.pkm", 0);
219  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level1.pkm", 1);
220  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level2.pkm", 2);
221  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level3.pkm", 3);
222  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level4.pkm", 4);
223  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level5.pkm", 5);
224  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level6.pkm", 6);
225  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level7.pkm", 7);
226  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level8.pkm", 8);
227  loadCompressedTexture("/data/data/com.arm.malideveloper.openglessdk.mipmapping/files/level9.pkm", 9);
228  /* [mipmapCompressedTextures] */
229  return true;
230 }
231 
232 
233 /* [vertexIndiceCode] */
234 GLfloat squareVertices[] = { -1.0f, 1.0f, 1.0f,
235  1.0f, 1.0f, 1.0f,
236  -1.0f, -1.0f, 1.0f,
237  1.0f, -1.0f, 1.0f,
238  };
239 
240 GLfloat textureCords[] = { 0.0f, 1.0f,
241  1.0f, 1.0f,
242  0.0f, 0.0f,
243  1.0f, 0.0f,
244 };
245 
246 GLushort indicies[] = {0, 2, 3, 0, 3, 1};
247 /* [vertexIndiceCode] */
248 
250 {
251  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
252  glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
253 
255 
256  /* [matrixTranslate] */
258  /* [matrixTranslate] */
259 
260  glUseProgram(glProgram);
261  glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, squareVertices);
262  glEnableVertexAttribArray(vertexLocation);
263 
264  glVertexAttribPointer(textureCordLocation, 2, GL_FLOAT, GL_FALSE, 0, textureCords);
265  glEnableVertexAttribArray(textureCordLocation);
266  glUniformMatrix4fv(projectionLocation, 1, GL_FALSE,projectionMatrix);
267  glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
268 
269  /* [rangeOfMovement] */
270  glUniform1i(samplerLocation, textureModeToggle);
271  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indicies);
272 
273  distance += velocity;
274  if (distance > 160 || distance < 1)
275  {
276  velocity *= -1;
278  }
279  /* [rangeOfMovement] */
280 }
281 
282 extern "C"
283 {
284  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_mipmapping_NativeLibrary_init (JNIEnv * env, jobject obj, jint width, jint height);
286  JNIEnv * env, jobject obj);
287 };
288 
290  JNIEnv * env, jobject obj, jint width, jint height)
291 {
292  setupGraphics(width, height);
293 }
294 
296  JNIEnv * env, jobject obj)
297 {
298  renderFrame();
299 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
GLuint samplerLocation
Definition: Native.cpp:147
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_mipmapping_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:295
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
static const char glFragmentShader[]
Definition: Native.cpp:50
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 textureIds[2]
Definition: Native.cpp:151
void loadCompressedTexture(const char *texture, unsigned int level)
Loads a compressed texture into memory at an appropriate mipmap level.
Definition: Texture.cpp:62
const float squareVertices[]
Definition: Native.cpp:127
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
GLuint textureCordLocation
Definition: Native.cpp:150
GLuint createProgram(const char *vertexSource, const char *fragmentSource)
Definition: Native.cpp:104
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_mipmapping_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:289
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
float velocity
Definition: Native.cpp:157
GLuint textureModeToggle
Definition: Native.cpp:158
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
GLsizei GLsizei GLfloat distance
Definition: gl2ext.h:2507
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLuint projectionLocation
Definition: Native.cpp:153
GLuint glProgram
Definition: Native.cpp:150
#define LOGE(...)
Definition: Native.cpp:36