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 
46 #include <jni.h>
47 #include <android/log.h>
48 
49 #include <GLES3/gl3.h>
50 #include "Common.h"
51 #include "ETCHeader.h"
52 #include "EtcTexture.h"
53 #include "Matrix.h"
54 #include "Shader.h"
55 #include "Text.h"
56 #include "Texture.h"
57 #include "Timer.h"
58 
59 using namespace MaliSDK;
60 
61 /* Structure to hold information about textures:
62  * - internal format of image,
63  * - path to image file,
64  * - texture name,
65  * - texture ID, used by OpenGL ES.
66  */
67 struct Image
68 {
70  char* fileName;
73 };
74 
75 /* Initialization of assets. */
76 Image image0 = {GL_COMPRESSED_R11_EAC,
78  "GL_COMPRESSED_R11_EAC",
79  0};
80 Image image1 = {GL_COMPRESSED_SIGNED_R11_EAC,
82  "GL_COMPRESSED_SIGNED_R11_EAC",
83  0};
84 Image image2 = {GL_COMPRESSED_RG11_EAC,
86  "GL_COMPRESSED_RG11_EAC",
87  0};
88 Image image3 = {GL_COMPRESSED_SIGNED_RG11_EAC,
90  "GL_COMPRESSED_SIGNED_RG11_EAC",
91  0};
92 Image image4 = {GL_COMPRESSED_RGB8_ETC2,
94  "GL_COMPRESSED_RGB8_ETC2",
95  0};
96 Image image5 = {GL_COMPRESSED_SRGB8_ETC2,
98  "GL_COMPRESSED_SRGB8_ETC2",
99  0};
100 Image image6 = {GL_COMPRESSED_RGBA8_ETC2_EAC,
102  "GL_COMPRESSED_RGBA8_ETC2_EAC",
103  0};
104 Image image7 = {GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
106  "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC",
107  0};
108 Image image8 = {GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
110  "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2",
111  0};
112 Image image9 = {GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
114  "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2",
115  0};
116 
117 /* Array of asset objects that will be used for displaying the images and text on screen. */
119  image1,
120  image2,
121  image3,
122  image4,
123  image5,
124  image6,
125  image7,
126  image8,
127  image9};
128 
129 GLuint bufferObjectIds[2] = {0}; /* Array of buffer objects names. Buffer objects hold quad and texture coordinates. */
130 GLuint currentAssetIndex = 0; /* Index of imageArray to currently displayed image. */
131 const float displayInterval = 5.0f; /* Number of seconds to display one image. */
132 GLuint fragmentShaderId = 0; /* Fragment shader name. */
133 Text* internalformatTextDisplayer = NULL; /* Instance of a class that holds text with internalformat of displayed image. */
134 GLint modelViewMatrixLocation = 0; /* Default shader uniform model view location. */
135 const int numberOfTextures = sizeof(imageArray) / sizeof(imageArray[0]);
136 GLint positionLocation = 0; /* Default shader attribute position location. */
137 GLuint programId = 0; /* Program name. */
138 float scalingFactor = 0.75f; /* Scale factor for displaying texture image. */
139 GLint textureCoordinateLocation = 0; /* Default shader attribute texture coordinate location. */
140 GLint textureLocation = 0; /* Default shader uniform sampler2D location.*/
141 Timer timer; /* Instance of a timer that is used to change displayed image every a couple of seconds.*/
142 int windowHeight = 0; /* Height of window */
143 int windowWidth = 0; /* Width of window */
144 GLuint vertexShaderId = 0; /* Vertex shader name. */
146 
147 /* [Array of coordinates describing quad] */
148 float vertexData[] = {-1.0f, -1.0f, 0.0f,
149  1.0f, -1.0f, 0.0f,
150  -1.0f, 1.0f, 0.0f,
151  -1.0f, 1.0f, 0.0f,
152  1.0f, -1.0f, 0.0f,
153  1.0f, 1.0f, 0.0f};
154 /* [Array of coordinates describing quad] */
155 
156 /* [Array of texture coordinates used for mapping texture to a quad] */
157 float textureCoordinatesData[] = {0.0f, 1.0f,
158  1.0f, 1.0f,
159  0.0f, 0.0f,
160  0.0f, 0.0f,
161  1.0f, 1.0f,
162  1.0f, 0.0f};
163 /* [Array of texture coordinates used for mapping texture to a quad] */
164 
169 void initializeTexture(int textureIndex)
170 {
171  ASSERT(textureIndex >= 0 && textureIndex < numberOfTextures,
172  "Incorrect value of index of imageArray.");
173 
174  /* Loads the image data and information about the image. */
175  char* fileName = imageArray[textureIndex].fileName;
176  ETCHeader etcHeader;
177  unsigned char* imageData = NULL;
178 
179  /* [Load compressed image data] */
180  Texture::loadPKMData(fileName, &etcHeader, &imageData);
181  /* [Load compressed image data] */
182 
183  ASSERT(imageData != NULL, "Could not load image data.")
184 
185  /* Get size of compressed image with padding included. */
187  /* [Get image properties] */
188  int imageHeight = etcHeader.getHeight();
189  int imageWidth = etcHeader.getWidth();
190  GLsizei imageSize = etcHeader.getSize(internalformat);
191  /* [Get image properties] */
192 
193  /* Generate and bind texture. Generated texture name is written to imageArray at a given index. */
194  /* [Generate texture object ID] */
195  GL_CHECK(glGenTextures(1, &imageArray[textureIndex].textureId));
196  /* [Generate texture object ID] */
197  /* [Bind texture object] */
198  GL_CHECK(glBindTexture(GL_TEXTURE_2D, imageArray[textureIndex].textureId));
199  /* [Bind texture object] */
200 
201  /* [Fill texture object with data] */
202  /* Call CompressedTexImage2D() function which specifies texture with compressed image. */
203  GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D,
204  0,
205  internalformat,
206  imageWidth,
207  imageHeight,
208  0,
209  imageSize,
210  imageData));
211  /* [Fill texture object with data] */
212 
213  /* [Set texture object parameters] */
214  /* Set parameters for a texture. */
215  GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
216  GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
217  GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
218  GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
219  /* [Set texture object parameters] */
220 }
221 
225 {
226  /* [Set pixel storage mode] */
227  /* Set OpenGL to use right alignment when reading texture images. */
228  GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
229  /* [Set pixel storage mode] */
230 
231  /* Generate textures and fill them with data. */
232  for (int textureIndex = 0; textureIndex < numberOfTextures; textureIndex++)
233  {
234  initializeTexture(textureIndex);
235  }
236 
237  /* Generate and bind vertex array. */
238  GL_CHECK(glGenVertexArrays(1,
239  &vertexArrayId));
240  GL_CHECK(glBindVertexArray(vertexArrayId));
241 
242  /* [Generate and initialize buffer objects] */
243  /* Generate buffers. */
244  GL_CHECK(glGenBuffers(2,
245  bufferObjectIds));
246 
247  /* Fill buffer object with vertex data. */
248  GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
249  bufferObjectIds[0]));
250  GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
251  sizeof(vertexData),
252  vertexData,
253  GL_STATIC_DRAW));
254 
255  /* Fill buffer object with texture coordinates data. */
256  GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
257  bufferObjectIds[1]));
258  GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
259  sizeof(textureCoordinatesData),
261  GL_STATIC_DRAW));
262  /* [Generate and initialize buffer objects] */
263 }
264 
267 void draw()
268 {
269  /* Draw text. */
270  if (internalformatTextDisplayer != NULL)
271  {
274  0,
275  imageArray[currentAssetIndex].nameOfImageIneternalformat,
276  255,
277  255,
278  255,
279  255);
281  }
282 
283  /* [Bind the texture object] */
284  /* Draw texture-mapped quad. */
285  GL_CHECK(glActiveTexture(GL_TEXTURE0));
286  GL_CHECK(glBindTexture (GL_TEXTURE_2D,
288  /* [Bind the texture object] */
289  GL_CHECK(glUseProgram (programId));
290 
291  GL_CHECK(glBindVertexArray(vertexArrayId));
292 
293  /* [Draw quad with texture image] */
294  GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 6));
295  /* [Draw quad with texture image] */
296 }
297 
300 void setupGraphics(int width, int height)
301 {
303  windowWidth = width;
304 
305  setupTextures();
306 
310  windowWidth,
311  windowHeight);
312 
313  glClearColor(0.1f, 0.3f, 0.2f, 1.0f);
314 
315  Matrix scaleMatrix;
316  float scaleMultimplier = 0.0f;
317 
318  /* Create scale matrix and orthographic matrix. */
319  if (height > width)
320  {
321  scaleMultimplier = (float) width / (float) height;
323  scalingFactor * scaleMultimplier * windowHeight,
324  1.0f);
325  }
326  else
327  {
328  scaleMultimplier = (float) height / (float) width;
329  scaleMatrix = Matrix::createScaling(scalingFactor * scaleMultimplier * windowWidth,
331  1.0f);
332  }
333 
334  Matrix ortographicMatrix = Matrix::matrixOrthographic(float(-windowWidth), float(windowWidth), float(-windowHeight),
335  float(windowHeight), -1.0f, 1.0f);
336 
337  /* Enable blending because it is needed for text drawing. */
338  GL_CHECK(glEnable(GL_BLEND));
339  GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
340 
341  /* Do everything to create program. */
344 
345  /*[Create program object] */
346  programId = GL_CHECK(glCreateProgram());
347  /*[Create program object] */
348 
349  /* [Attach shaders to program object] */
350  GL_CHECK(glAttachShader(programId, vertexShaderId));
351  GL_CHECK(glAttachShader(programId, fragmentShaderId));
352  /* [Attach shaders to program object] */
353 
354  /* [Link program object] */
355  GL_CHECK(glLinkProgram(programId));
356  /* [Link program object] */
357  /* [Select active program object] */
358  GL_CHECK(glUseProgram(programId));
359  /* [Select active program object] */
360 
361  /* [Get uniform and attribute locations] */
362  /* Get attributes and uniforms locations from shaders attached to the program. */
363  modelViewMatrixLocation = GL_CHECK(glGetUniformLocation(programId, "modelViewMatrix"));
364  positionLocation = GL_CHECK(glGetAttribLocation (programId, "attributePosition"));
365  textureCoordinateLocation = GL_CHECK(glGetAttribLocation (programId, "attributeTextureCoordinate"));
366  textureLocation = GL_CHECK(glGetUniformLocation(programId, "uniformTexture"));
367  /* [Get uniform and attribute locations] */
368 
369  /* [Check uniform and attribute location values] */
370  ASSERT(modelViewMatrixLocation != -1, "Could not retrieve uniform location: modelViewMatrix.");
371  ASSERT(positionLocation != -1, "Could not retrieve attribute location: attributePosition.");
372  ASSERT(textureCoordinateLocation != -1, "Could not retrieve attribute location: attributeTextureCoordinate.");
373  ASSERT(textureLocation != -1, "Could not retrieve uniform location: uniformTexture.");
374  /* [Check uniform and attribute location values] */
375 
376  /* Set up model-view matrix. */
377  Matrix resultMatrix = ortographicMatrix * scaleMatrix;
378 
379  /* [Enable VAAs] */
380  GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
381  bufferObjectIds[0]));
382  GL_CHECK(glVertexAttribPointer (positionLocation,
383  3,
384  GL_FLOAT,
385  GL_FALSE,
386  0,
387  0));
388  GL_CHECK(glEnableVertexAttribArray(positionLocation));
389 
390  GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
391  bufferObjectIds[1]));
392  GL_CHECK(glVertexAttribPointer (textureCoordinateLocation,
393  2,
394  GL_FLOAT,
395  GL_FALSE,
396  0,
397  0));
398  GL_CHECK(glEnableVertexAttribArray(textureCoordinateLocation));
399  /* [Enable VAAs] */
400 
401  GL_CHECK(glUniformMatrix4fv(modelViewMatrixLocation,
402  1,
403  GL_FALSE,
404  resultMatrix.getAsArray()));
405  /* [Set sample uniform value] */
406  GL_CHECK(glUniform1i (textureLocation,
407  0));
408  /* [Set sample uniform value] */
409 
410  /* Start counting time. */
411  timer.reset();
412 }
413 
416 void renderFrame(void)
417 {
418  /* Clear contents of back buffer. */
419  glClear(GL_COLOR_BUFFER_BIT);
420 
421  /* Check if time for displaying one image has passed. */
423  {
424  /* If last picture available is displayed, move to the first one. */
426  {
428  }
429  else
430  {
431  currentAssetIndex = 0;
432  }
433 
434  /* Reset time counter. */
435  timer.reset();
436  }
437 
438  draw();
439 }
440 
441 void uninit()
442 {
443  /* Delete textures. */
444  for (int i = 0; i < numberOfTextures; i++)
445  {
446  GL_CHECK(glDeleteTextures(1, &imageArray[i].textureId));
447  }
448 
449  /* Delete buffers. */
450  GL_CHECK(glDeleteBuffers(2, bufferObjectIds));
451 
452  /* Delete vertex array object. */
453  GL_CHECK(glDeleteVertexArrays(1, &vertexArrayId));
454 
455  /* Release text object instance. */
456  if (internalformatTextDisplayer != NULL)
457  {
459 
461  }
462 }
463 
464 extern "C"
465 {
466  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcTexture_NativeLibrary_init (JNIEnv * env, jobject obj, jint width, jint height);
467  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcTexture_NativeLibrary_step (JNIEnv * env, jobject obj);
468  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcTexture_NativeLibrary_uninit(JNIEnv * env, jobject obj);
469 };
470 
472  JNIEnv * env, jobject obj, jint width, jint height)
473 {
474  setupGraphics(width, height);
475 }
476 
478  JNIEnv * env, jobject obj)
479 {
480  uninit();
481 }
482 
483 
485  JNIEnv * env, jobject obj)
486 {
487  renderFrame();
488 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
GLuint currentAssetIndex
Definition: Native.cpp:130
static void loadPKMData(const char *filename, ETCHeader *etcHeader, unsigned char **textureData)
Load header and texture data from a pkm file into memory.
Definition: Texture.cpp:199
GLint textureLocation
Definition: Native.cpp:140
#define GL_CHECK(x)
Definition: Native.cpp:64
float getTime()
Returns the time passed since object creation or since reset() was last called.
Definition: Timer.cpp:109
Functions for drawing text in OpenGL ES.
Definition: Text.h:44
Class to extract information from the ETC headers of compressed textures.
Definition: ETCHeader.h:38
int windowHeight
Definition: Native.cpp:574
Definition: Native.cpp:67
#define TEXTURE_GL_COMPRESSED_RGB8_ETC2_FILE_NAME
Definition: EtcTexture.h:43
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
#define TEXTURE_GL_COMPRESSED_RG11_EAC_FILE_NAME
Definition: EtcTexture.h:39
#define TEXTURE_GL_COMPRESSED_RGBA8_ETC2_EAC_FILE_NAME
Definition: EtcTexture.h:47
GLint positionLocation
Definition: Native.cpp:99
GLint textureCoordinateLocation
Definition: Native.cpp:139
#define FONT_VERTEX_SHADER_FILE_NAME
Definition: EtcTexture.h:33
GLuint vertexShaderId
Definition: Native.cpp:57
#define FONT_FRAGMENT_SHADER_FILE_NAME
Definition: EtcTexture.h:29
GLuint programId
Definition: Native.cpp:137
Image image1
Definition: Native.cpp:80
Functions for manipulating matrices.
Definition: Matrix.h:31
#define TEXTURE_GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2_FILE_NAME
Definition: EtcTexture.h:53
void setupTextures()
Definition: Native.cpp:224
void draw()
Definition: Native.cpp:267
unsigned short getHeight(void)
The height of the original texture.
Definition: ETCHeader.cpp:53
void initializeTexture(int textureIndex)
Definition: Native.cpp:169
Provides a platform independent high resolution timer.
Definition: Timer.h:37
void clear(void)
Removes the current string from the class.
Definition: Text.cpp:142
static Matrix matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
Create and return an orthographic projection matrix.
Definition: Matrix.cpp:483
#define FONT_TEXTURE_FILE_NAME
Definition: EtcTexture.h:31
static Matrix createScaling(float x, float y, float z)
Create and return a scaling matrix.
Definition: Matrix.cpp:403
Image image8
Definition: Native.cpp:108
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcTexture_NativeLibrary_uninit(JNIEnv *env, jobject obj)
Definition: Native.cpp:477
float * getAsArray(void)
Get the matrix elements as a column major order array.
Definition: Matrix.cpp:78
#define VERTEX_SHADER_FILE_NAME
Definition: Boids.h:29
#define TEXTURE_GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC_FILE_NAME
Definition: EtcTexture.h:49
GLuint textureId
Definition: Native.cpp:200
float scalingFactor
Definition: Native.cpp:138
Image image2
Definition: Native.cpp:84
const float displayInterval
Definition: Native.cpp:131
#define TEXTURE_GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2_FILE_NAME
Definition: EtcTexture.h:51
void reset()
Resets the timer to 0.0f.
Definition: Timer.cpp:100
Image image6
Definition: Native.cpp:100
char * nameOfImageIneternalformat
Definition: Native.cpp:71
void uninit()
Delete created objects and free allocated memory.
Definition: Native.cpp:1706
GLuint vertexArrayId
Definition: Native.cpp:145
const int numberOfTextures
Definition: Native.cpp:135
Image image4
Definition: Native.cpp:92
GLfloat GLfloat f
Definition: gl2ext.h:2707
void addString(int xPosition, int yPosition, const char *string, int red, int green, int blue, int alpha)
Add a std::string to be drawn to the screen.
Definition: Text.cpp:157
void renderFrame(void)
Definition: Native.cpp:1536
#define FRAGMENT_SHADER_FILE_NAME
Definition: Boids.h:27
GLuint textureId
Definition: Native.cpp:72
Image image3
Definition: Native.cpp:88
GLint GLenum internalformat
Definition: gl2ext.h:572
#define TEXTURE_GL_COMPRESSED_SIGNED_R11_EAC_FILE_NAME
Definition: EtcTexture.h:37
char * fileName
Definition: Native.cpp:70
Image image0
Definition: Native.cpp:76
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcTexture_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:484
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcTexture_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:471
Timer timer
Definition: Native.cpp:1059
Image image5
Definition: Native.cpp:96
#define TEXTURE_GL_COMPRESSED_SIGNED_RG11_EAC_FILE_NAME
Definition: EtcTexture.h:41
int windowWidth
Definition: Native.cpp:575
GLuint bufferObjectIds[numberOfBufferObjectIds]
Definition: Native.cpp:113
Text * internalformatTextDisplayer
Definition: Native.cpp:133
GLint modelViewMatrixLocation
Definition: Native.cpp:134
Image image9
Definition: Native.cpp:112
Image image7
Definition: Native.cpp:104
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei imageSize
Definition: gl2ext.h:575
#define TEXTURE_GL_COMPRESSED_R11_EAC_FILE_NAME
Definition: EtcTexture.h:35
float vertexData[]
Definition: Native.cpp:148
#define TEXTURE_GL_COMPRESSED_SRGB8_ETC2_FILE_NAME
Definition: EtcTexture.h:45
#define ASSERT(x, s)
Definition: common.h:45
precision highp float
Definition: hiz_cull.cs:37
GLuint fragmentShaderId
Definition: Native.cpp:55
GLint GLsizei width
Definition: gl2ext.h:179
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
Image imageArray[]
Definition: Native.cpp:118
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
Definition: Shader.cpp:29
float textureCoordinatesData[]
Definition: Native.cpp:157
unsigned short getWidth(void)
The width of the original texture.
Definition: ETCHeader.cpp:48
void draw(void)
Draw the text to the screen.
Definition: Text.cpp:265
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLenum internalformat
Definition: Native.cpp:69
GLsizei getSize(GLenum internalFormat)
The size of the compressed texture with the padding added.
Definition: ETCHeader.cpp:66