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 
42 #include <jni.h>
43 #include <android/log.h>
44 
45 #include <GLES3/gl3.h>
46 #include "Common.h"
47 #include "CubeModel.h"
48 #include "Mathematics.h"
49 #include "Matrix.h"
50 #include "PlaneModel.h"
51 #include "ProjectedLights.h"
52 #include "Shader.h"
53 #include "Texture.h"
54 #include "Timer.h"
55 #include <cstring>
56 
57 using namespace MaliSDK;
58 
67 static void drawCubeAndPlane(bool isCameraPointOfView);
68 
75 
82 
91 static void getRenderSceneProgramLocations(GLuint programObjectId,
92  RenderSceneProgramLocations* locationsStoragePtr);
93 
105 static void initializeProgramObject(ProgramAndShaderObjectIds* objectIdsPtr,
106  const char* fragmentShaderFileName,
107  const char* vertexShaderFileName);
108 
112 static void initializeViewMatrices();
113 
117 static void renderFrame();
118 
124 static void setupGeometryData();
125 
134 static void setupGraphics(int width, int height);
135 
139 static void uninit();
140 
145 static void updateSpotLightDirection();
146 
159 GLsizei windowHeight;
160 GLsizei windowWidth;
161 
162 /* Please see the specification above. */
163 static void drawCubeAndPlane(bool isCameraPointOfView)
164 {
165  /* Draw a cube. */
166  {
167  /* [Set colour of a cube for geometry] */
168  /* Set uniform value specific for rendering a cube. */
170  1,
172  /* [Set colour of a cube for geometry] */
173 
174  if (isCameraPointOfView)
175  {
176  /* Use matrices specific for rendering a scene from camera perspective. */
178  1,
179  GL_FALSE,
182  1,
183  GL_FALSE,
186  1,
187  GL_FALSE,
189  }
190  else
191  {
192  /* [Set uniform values for the spot light point of view: cube]*/
193  /* Use matrices specific for rendering a scene from spot light perspective. */
195  1,
196  GL_FALSE,
199  1,
200  GL_FALSE,
203  1,
204  GL_FALSE,
206  /* [Set uniform values for the spot light point of view: cube]*/
207  }
208 
209  /* [Bind VAA for cube] */
210  /* Set cube's coordinates to be used within a program object. */
212  /* [Bind VAA for cube] */
213 
214  /* [Draw a cube] */
215  GL_CHECK(glDrawArrays(GL_TRIANGLES,
216  0,
218  /* [Draw a cube] */
219  }
220 
221  /* Draw a plane. */
222  {
223  /* [Set colour of a plane for geometry] */
224  /* Set uniform value specific for rendering a plane. */
226  1,
228  /* [Set colour of a plane for geometry] */
229 
230  if (isCameraPointOfView)
231  {
232  /* Use matrices specific for rendering a scene from camera perspective. */
234  1,
235  GL_FALSE,
238  1,
239  GL_FALSE,
242  1,
243  GL_FALSE,
245  }
246  else
247  {
248  /* [Set uniform values for the spot light point of view: plane]*/
249  /* Use matrices specific for rendering a scene from spot light perspective. */
251  1,
252  GL_FALSE,
255  1,
256  GL_FALSE,
259  1,
260  GL_FALSE,
262  /* [Set uniform values for the spot light point of view: plane]*/
263  }
264 
265  /* [Bind VAA for plane] */
266  /* Set plane's coordinates to be used within a program object. */
268  /* [Bind VAA for plane] */
269 
270  /* [Draw a plane] */
271  GL_CHECK(glDrawArrays(GL_TRIANGLES,
272  0,
274  /* [Draw a plane] */
275  }
276 }
277 
278 /* Please see the specification above. */
280 {
281  GLsizei imageHeight = 0;
282  GLsizei imageWidth = 0;
283  unsigned char* textureData = NULL;
284 
285  /* [Load BMP image data] */
286  Texture::loadBmpImageData(COLOR_TEXTURE_NAME, &imageWidth, &imageHeight, &textureData);
287  /* [Load BMP image data] */
288 
289  GL_CHECK(glActiveTexture(GL_TEXTURE0 + TEXTURE_UNIT_FOR_COLOR_TEXTURE));
290  /* [Generate and bind colour texture object] */
291  GL_CHECK(glGenTextures (1,
293  GL_CHECK(glBindTexture (GL_TEXTURE_2D,
295  /* [Generate and bind colour texture object] */
296  /* [Set colour texture object data] */
297  GL_CHECK(glTexStorage2D (GL_TEXTURE_2D,
298  1,
299  GL_RGB8,
300  imageWidth,
301  imageHeight));
302  GL_CHECK(glTexSubImage2D(GL_TEXTURE_2D,
303  0,
304  0,
305  0,
306  imageWidth,
307  imageHeight,
308  GL_RGB,
309  GL_UNSIGNED_BYTE,
310  textureData));
311  /* [Set colour texture object data] */
312  /* [Set colour texture object parameters] */
313  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
314  GL_TEXTURE_MIN_FILTER,
315  GL_LINEAR));
316  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
317  GL_TEXTURE_MAG_FILTER,
318  GL_LINEAR));
319  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
320  GL_TEXTURE_WRAP_R,
321  GL_REPEAT));
322  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
323  GL_TEXTURE_WRAP_S,
324  GL_REPEAT));
325  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
326  GL_TEXTURE_WRAP_T,
327  GL_REPEAT));
328  /* [Set colour texture object parameters] */
329 
330  /* Data is already copied to the GPU, we can free the allocated memory now. */
331  if (textureData != NULL)
332  {
333  free(textureData);
334 
335  textureData = NULL;
336  }
337 
338  /* Restore default bindings. */
339  GL_CHECK(glBindTexture(GL_TEXTURE_2D, 0));
340 }
341 
342 /* Please see the specification above. */
344 {
345  /* Generate and configure shadow map texture to hold depth values. */
346  /* [Generate depth texture object] */
347  GL_CHECK(glGenTextures (1,
349  GL_CHECK(glBindTexture (GL_TEXTURE_2D,
351  /* [Generate depth texture object] */
352  /* [Prepare depth texture storage] */
353  GL_CHECK(glTexStorage2D(GL_TEXTURE_2D,
354  1,
355  GL_DEPTH_COMPONENT24,
357  shadowMapHeight));
358  /* [Prepare depth texture storage] */
359  /* [Set depth texture object parameters] */
360  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
361  GL_TEXTURE_MIN_FILTER,
362  GL_LINEAR));
363  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
364  GL_TEXTURE_MAG_FILTER,
365  GL_LINEAR));
366  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
367  GL_TEXTURE_WRAP_S,
368  GL_CLAMP_TO_EDGE));
369  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
370  GL_TEXTURE_WRAP_T,
371  GL_CLAMP_TO_EDGE));
372  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
373  GL_TEXTURE_WRAP_R,
374  GL_CLAMP_TO_EDGE));
375  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
376  GL_TEXTURE_COMPARE_FUNC,
377  GL_LEQUAL));
378  GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
379  GL_TEXTURE_COMPARE_MODE,
380  GL_COMPARE_REF_TO_TEXTURE));
381  /* [Set depth texture object parameters] */
382 
383  /* Attach texture to depth attachment point of a framebuffer object. */
384  /* [Generate and bind framebuffer object] */
385  GL_CHECK(glGenFramebuffers (1,
387  GL_CHECK(glBindFramebuffer (GL_FRAMEBUFFER,
389  /* [Generate and bind framebuffer object] */
390  /* [Bind depth texture to framebuffer] */
391  GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER,
392  GL_DEPTH_ATTACHMENT,
393  GL_TEXTURE_2D,
395  0));
396  /* [Bind depth texture to framebuffer] */
397 }
398 
399 /* Please see the specification above. */
400 static void getRenderSceneProgramLocations(GLuint programObjectId,
401  RenderSceneProgramLocations* locationsStoragePtr)
402 {
403  ASSERT(programObjectId != 0,
404  "Cannot use default program object to retrieve attribute/uniform locations.");
405  ASSERT(locationsStoragePtr != NULL,
406  "Invalid pointer used to store retrieved attribute/uniform locations.");
407 
408  /* Retrieve locations of uniforms and attributes used within the program object. */
409  /* [Get attribute locations] */
410  locationsStoragePtr->attributeVertexCoordinates = GL_CHECK(glGetAttribLocation (programObjectId, "vertexCoordinates"));
411  locationsStoragePtr->attributeVertexNormals = GL_CHECK(glGetAttribLocation (programObjectId, "vertexNormals"));
412  /* [Get attribute locations] */
413  /* [Get colour texture uniform location] */
414  locationsStoragePtr->uniformColorTexture = GL_CHECK(glGetUniformLocation (programObjectId, "colorTexture"));
415  /* [Get colour texture uniform location] */
416  locationsStoragePtr->uniformDirectionalLightAmbient = GL_CHECK(glGetUniformLocation (programObjectId, "directionalLightAmbient"));
417  locationsStoragePtr->uniformDirectionalLightColor = GL_CHECK(glGetUniformLocation (programObjectId, "directionalLightColor"));
418  locationsStoragePtr->uniformDirectionalLightPosition = GL_CHECK(glGetUniformLocation (programObjectId, "directionalLightPosition"));
419  locationsStoragePtr->uniformGeometryColor = GL_CHECK(glGetUniformLocation (programObjectId, "geometryColor"));
420  locationsStoragePtr->uniformModelViewMatrix = GL_CHECK(glGetUniformLocation (programObjectId, "modelViewMatrix"));
421  locationsStoragePtr->uniformModelViewProjectionMatrix = GL_CHECK(glGetUniformLocation (programObjectId, "modelViewProjectionMatrix"));
422  locationsStoragePtr->uniformNormalMatrix = GL_CHECK(glGetUniformLocation (programObjectId, "normalMatrix"));
423  /* [Get depth texture uniform location] */
424  locationsStoragePtr->uniformShadowMap = GL_CHECK(glGetUniformLocation (programObjectId, "shadowMap"));
425  /* [Get depth texture uniform location] */
426  locationsStoragePtr->uniformSpotLightColor = GL_CHECK(glGetUniformLocation (programObjectId, "spotLightColor"));
427  locationsStoragePtr->uniformSpotLightCosAngle = GL_CHECK(glGetUniformLocation (programObjectId, "spotLightCosAngle"));
428  locationsStoragePtr->uniformSpotLightLookAtPointInEyeSpace = GL_CHECK(glGetUniformLocation (programObjectId, "spotLightLookAtPointInEyeSpace"));
429  locationsStoragePtr->uniformSpotLightPositionInEyeSpace = GL_CHECK(glGetUniformLocation (programObjectId, "spotLightPositionInEyeSpace"));
430  locationsStoragePtr->uniformViewToColorTextureMatrix = GL_CHECK(glGetUniformLocation (programObjectId, "viewToColorTextureMatrix"));
431  locationsStoragePtr->uniformViewToDepthTextureMatrix = GL_CHECK(glGetUniformLocation (programObjectId, "viewToDepthTextureMatrix"));
432 
433  /* Make sure that the data retrieved is valid. */
434  ASSERT(locationsStoragePtr->attributeVertexCoordinates != -1 &&
435  locationsStoragePtr->attributeVertexNormals != -1 &&
436  locationsStoragePtr->uniformColorTexture != -1 &&
437  locationsStoragePtr->uniformDirectionalLightAmbient != -1 &&
438  locationsStoragePtr->uniformDirectionalLightColor != -1 &&
439  locationsStoragePtr->uniformDirectionalLightPosition != -1 &&
440  locationsStoragePtr->uniformGeometryColor != -1 &&
441  locationsStoragePtr->uniformModelViewMatrix != -1 &&
442  locationsStoragePtr->uniformModelViewProjectionMatrix != -1 &&
443  locationsStoragePtr->uniformNormalMatrix != -1 &&
444  locationsStoragePtr->uniformShadowMap != -1 &&
445  locationsStoragePtr->uniformSpotLightColor != -1 &&
446  locationsStoragePtr->uniformSpotLightCosAngle != -1 &&
447  locationsStoragePtr->uniformSpotLightLookAtPointInEyeSpace != -1 &&
448  locationsStoragePtr->uniformSpotLightPositionInEyeSpace != -1 &&
449  locationsStoragePtr->uniformViewToColorTextureMatrix != -1 &&
450  locationsStoragePtr->uniformViewToDepthTextureMatrix != -1,
451  "At least one of uniform/attribute locations retrieved is not valid. The uniform/attribute seems to be inactive.");
452 }
453 
454 /* Please see the specification above. */
456  const char* fragmentShaderFileName,
457  const char* vertexShaderFileName)
458 {
459  ASSERT(objectIdsPtr != NULL,
460  "NULL pointer used to store generated object IDs");
461 
462  GLint linkStatus = GL_FALSE;
463 
464  objectIdsPtr->programObjectId = GL_CHECK(glCreateProgram());
465 
467  fragmentShaderFileName,
468  GL_FRAGMENT_SHADER);
470  vertexShaderFileName,
471  GL_VERTEX_SHADER);
472 
473  GL_CHECK(glAttachShader(objectIdsPtr->programObjectId,
474  objectIdsPtr->fragmentShaderObjectId));
475  GL_CHECK(glAttachShader(objectIdsPtr->programObjectId,
476  objectIdsPtr->vertexShaderObjectId));
477  GL_CHECK(glLinkProgram (objectIdsPtr->programObjectId));
478  GL_CHECK(glGetProgramiv(objectIdsPtr->programObjectId,
479  GL_LINK_STATUS,
480  &linkStatus));
481 
482  ASSERT(linkStatus == GL_TRUE,
483  "Linking program object failed.");
484 }
485 
486 /* Please see the specification above. */
488 {
493  Matrix cubeModelMatrix = cubeRotationMatrix * cubeTranslationMatrix;
497 
498  /* Initialize matrices that will be used to render geometry from camera point of view. */
499  {
507  float(windowWidth) / float(windowHeight),
508  NEAR_PLANE,
509  FAR_PLANE);
521 
522  cameraViewProperties.planeViewProperties.modelMatrix = planeTranslationMatrix;
527 
530  Vec4f spotLightPosition = {spotLightProperties.position.x,
533  1.0f};
534 
535  Matrix::matrixTranspose(&cubeNormalMatrix);
536  Matrix::matrixTranspose(&planeNormalMatrix);
537 
540 
543  }
544 
545  /* Initialize matrices that will be used to render geometry from spot light point of view. */
546  {
548  lightViewProperties.planeViewProperties.modelMatrix = planeTranslationMatrix;
553  /* [Set projection matrix from spot light point of view] */
555  1.0f,
556  NEAR_PLANE,
557  FAR_PLANE);
558  /* [Set projection matrix from spot light point of view] */
562  }
563 }
564 
565 /* Please see the specification above. */
566 static void renderFrame()
567 {
568  /* Spot light direction is changing during the rendering process: update it now. */
570 
571  /* Clear the contents of back buffer. */
572  GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
573 
574  /* Set uniform values that are common for all the rendering steps. */
576  1,
577  GL_FALSE,
580  1,
581  GL_FALSE,
584  1,
587  1,
589 
590  /* 1. Draw a scene from spot light point of view (calculate scene depth). */
591  {
592  /* Bind framebuffer object.
593  * There is a texture attached to depth attachment point for this framebuffer object.
594  * By using this framebuffer object, calculated depth values are stored in the texture. */
595  GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, renderSceneObjects.framebufferObjectId));
596 
597  /* Clear the depth and colour buffers. */
598  GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT));
599 
600  /* [Set viewport for light perspective] */
601  /* Set the view port to size of shadow map texture. */
602  GL_CHECK(glViewport(0, 0, shadowMapWidth, shadowMapHeight));
603  /* [Set viewport for light perspective] */
604 
605  /* [Enable shadow map drawing properties] */
606  GL_CHECK(glEnable(GL_POLYGON_OFFSET_FILL));
607  /* [Enable shadow map drawing properties] */
608 
609  /* Enable cull faceing to avoid self shadowing problems: only back faces of the geaometry will be rendered. */
610  GL_CHECK(glEnable(GL_CULL_FACE));
611 
612  /* [Set colour mask for shadow map rendering] */
613  /* Disable writing of each frame buffer colour component. */
614  GL_CHECK(glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
615  /* [Set colour mask for shadow map rendering] */
616 
617  /* [Draw the scene from spot light point of view] */
618  drawCubeAndPlane(false);
619  /* [Draw the scene from spot light point of view] */
620  } /* 1. */
621 
622  /* 2. Draw a scene with lights and shadows from eye point of view. */
623  {
624  /* Use the default framebuffer object: scene will be rendered on a screen. */
625  GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
626 
627  /* Clear the depth and colour buffers. */
628  GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT));
629 
630  /* Set the view port to the size of the window. */
631  GL_CHECK(glViewport(0, 0, windowWidth, windowHeight));
632 
633  GL_CHECK(glDisable(GL_CULL_FACE));
634  GL_CHECK(glDisable(GL_POLYGON_OFFSET_FILL));
635 
636  /* Enable writing of each frame buffer colour component. */
637  GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
638 
639  /* Draw the scene from camera point of view. */
640  drawCubeAndPlane(true);
641  } /* 2. */
642 }
643 
644 /* [Setup geometry data] */
645 /* Please see the specification above. */
646 static void setupGeometryData()
647 {
648  /* Get triangular representation of the scene cube. Store the data in the cubeCoordinates array. */
652 
653  /* Calculate normal vectors for the scene cube created above. */
656 
657  /* Get triangular representation of a square to draw plane in XZ space. Store the data in the planeCoordinates array. */
661 
662  /* Calculate normal vectors for the plane. Store the data in the planeNormals array. */
665 
666  /* Fill buffer objects with data. */
667  /* Buffer holding coordinates of triangles which make up the scene cubes. */
668  GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
670  GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
673  GL_STATIC_DRAW));
674 
675  /* Buffer holding coordinates of normal vectors for each vertex of the scene cubes. */
676  GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
678  GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
681  GL_STATIC_DRAW));
682 
683  /* Buffer holding coordinates of triangles which make up the plane. */
684  GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
686  GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
689  GL_STATIC_DRAW));
690 
691  /* Buffer holding coordinates of the plane's normal vectors. */
692  GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
694  GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
697  GL_STATIC_DRAW));
698 }
699 /* [Setup geometry data] */
700 
701 /* Please see the specification above. */
702 static void setupGraphics(int width, int height)
703 {
704  /* [Declare window resolution] */
705  /* Store window size. */
707  windowWidth = width;
708  /* [Declare window resolution] */
709  /* [Declare shadow map texture resolution] */
710  /* Calculate size of a shadow map texture that will be used. */
713  /* [Declare shadow map texture resolution] */
714 
715  /* Set up a directional light properties. */
723 
724  /* Set up a spot light properties. */
725  spotLightProperties.color.x = 1.0f;
726  spotLightProperties.color.y = 1.0f;
727  spotLightProperties.color.z = 1.0f;
731 
732  /* Set up properties of the geometry that will be rendered. */
747 
748  /* Initialize the matrices that are used to translatevertices into specific space. */
750 
751  /* [Generate objects for rendering the geometry] */
752  /* Generate buffer objects. */
757 
758  /* Generate vertex array objects. */
761  /* [Generate objects for rendering the geometry] */
762 
763  /* Initialize program object responsible for rendering the scene. */
767 
768  /* Initialize OpenGLES objects. */
772 
774 
775  /* Retrieve the program attribute and uniform locations. */
778 
779  /* Set the uniform data which is constant during the rendering process. */
780  /* [Set texture object for colour texture sampler] */
783  /* [Set texture object for colour texture sampler] */
787  1,
790  1,
794  /* [Set texture object for depth texture sampler] */
797  /* [Set texture object for depth texture sampler] */
799  1,
801 
802  /* [Set up Vertex Attrib Arrays] */
803  /* Enable cube VAAs. */
805  GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
809  GL_FLOAT,
810  GL_FALSE,
811  0,
812  NULL));
813  GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
817  GL_FLOAT,
818  GL_FALSE,
819  0,
820  NULL));
822  GL_CHECK(glEnableVertexAttribArray(renderSceneProgramLocations.attributeVertexNormals));
823 
824  /* Enable plane VAAs. */
826  GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
830  GL_FLOAT,
831  GL_FALSE,
832  0,
833  NULL));
834  GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
838  GL_FLOAT,
839  GL_FALSE,
840  0,
841  NULL));
843  GL_CHECK(glEnableVertexAttribArray(renderSceneProgramLocations.attributeVertexNormals));
844  /* [Set up Vertex Attrib Arrays] */
845 
846  /* Bind the depth texture so that it can be used as a fragment shader input data. */
847  /* [Bind depth texture to specific binding point] */
848  GL_CHECK(glActiveTexture(GL_TEXTURE0 + TEXTURE_UNIT_FOR_SHADOW_MAP_TEXTURE));
849  GL_CHECK(glBindTexture (GL_TEXTURE_2D,
851  /* [Bind depth texture to specific binding point] */
852  /* [Set active texture unit for colour texture] */
853  GL_CHECK(glActiveTexture(GL_TEXTURE0 + TEXTURE_UNIT_FOR_COLOR_TEXTURE));
854  /* [Set active texture unit for colour texture] */
855  GL_CHECK(glBindTexture (GL_TEXTURE_2D,
857 
858  /* [Set shadow map drawing properties] */
859  /* Set the Polygon offset, used when rendering the into the shadow map
860  * to eliminate z-fighting in the shadows (if enabled). */
861  GL_CHECK(glPolygonOffset(1.0f, 0.0f));
862  /* Set back faces to be culled (only when GL_CULL_FACE mode is enabled). */
863  GL_CHECK(glCullFace(GL_BACK));
864  /* [Set shadow map drawing properties] */
865  /* [Enable depth test] */
866  /* Enable depth test to do comparison of depth values. */
867  GL_CHECK(glEnable(GL_DEPTH_TEST));
868  /* [Enable depth test] */
869 }
870 
871 /* Please see the specification above. */
872 static void uninit()
873 {
874  /* Use default program object. */
875  GL_CHECK(glUseProgram(0));
876  /* Bind default objects. */
877  GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER, 0));
878  GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
879  GL_CHECK(glBindTexture (GL_TEXTURE_2D, 0));
880 
881  /* Delete buffers. */
886 
887  /* Delete framebuffer object. */
888  GL_CHECK(glDeleteFramebuffers(1, &renderSceneObjects.framebufferObjectId));
889 
890  /* Delete textures. */
891  GL_CHECK(glDeleteTextures(1, &renderSceneObjects.colorTextureObjectId));
892  GL_CHECK(glDeleteTextures(1, &renderSceneObjects.depthTextureObjectId));
893 
894  /* Delete vertex arrays. */
895  GL_CHECK(glDeleteVertexArrays(1, &renderSceneObjects.renderCube.vertexArrayObjectId));
896  GL_CHECK(glDeleteVertexArrays(1, &renderSceneObjects.renderPlane.vertexArrayObjectId));
897 
898  /* Delete program and shader objects. */
902 
903  /* Free the memory allocated. */
905  {
907 
909  }
910 
911  if (cubeGeometryProperties.normals != NULL)
912  {
914 
916  }
917 
919  {
921 
923  }
924 
925  if (planeGeometryProperties.normals != NULL)
926  {
928 
930  }
931 }
932 
933 /* [Update spot light direction] */
934 /* Please see the specification above. */
936 {
937  /* Time used to set light direction and position. */
938  const float currentAngle = timer.getTime() / 4.0f;
939 
940  /* Update the look at point coordinates. */
944 
945  /* Update all the view, projection matrixes athat are connected with updated look at point coordinates. */
949  1.0f};
950 
951  /* Get lookAt matrix from the light's point of view, directed at the center of a plane.
952  * Store result in viewMatrixForShadowMapPass. */
956 
962 
963  Matrix inverseCameraViewMatrix = Matrix::matrixInvert(&cameraViewProperties.viewMatrix);
964  /* [Define colour texture translation matrix] */
965  Matrix colorTextureTranslationMatrix = Matrix::createTranslation(COLOR_TEXTURE_TRANSLATION,
966  0.0f,
968  /* [Define colour texture translation matrix] */
969 
970  /* [Calculate matrix for shadow map sampling: colour texture] */
974  colorTextureTranslationMatrix *
975  inverseCameraViewMatrix;
976  /* [Calculate matrix for shadow map sampling: colour texture] */
977  /* [Calculate matrix for shadow map sampling: depth texture] */
981  inverseCameraViewMatrix;
982  /* [Calculate matrix for shadow map sampling: depth texture] */
983 }
984 /* [Update spot light direction] */
985 
986 extern "C"
987 {
988  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_projectedLights_NativeLibrary_init (JNIEnv * env, jobject obj, jint width, jint height);
989  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_projectedLights_NativeLibrary_step (JNIEnv * env, jobject obj);
990  JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_projectedLights_NativeLibrary_uninit(JNIEnv * env, jobject obj);
991 };
992 
994  JNIEnv * env, jobject obj, jint width, jint height)
995 {
996  setupGraphics(width, height);
997 }
998 
1000  JNIEnv * env, jobject obj)
1001 {
1002  uninit();
1003 }
1004 
1005 
1007  JNIEnv * env, jobject obj)
1008 {
1009  renderFrame();
1010 }
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
GeometryProperties planeGeometryProperties
Definition: Native.cpp:151
static void getTriangleRepresentation(int *numberOfCoordinates, float **coordinates)
Get coordinates of points which make up a plane. The plane is located in XZ space.
Definition: PlaneModel.cpp:117
GLsizei shadowMapHeight
Definition: Native.cpp:155
#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
static void generateAndPrepareColorTextureObject()
Generate a colour texture object and fill it with data.
Definition: Native.cpp:279
int windowHeight
Definition: Native.cpp:574
#define CAMERA_PERSPECTIVE_FOV_IN_DEGREES
static void initializeProgramObject(ProgramAndShaderObjectIds *objectIdsPtr, const char *fragmentShaderFileName, const char *vertexShaderFileName)
Create and compile shader objects. If successful, they are attached to the program object...
Definition: Native.cpp:455
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
RenderSceneObjects renderSceneObjects
Definition: Native.cpp:152
#define SPOT_LIGHT_TRANSLATION_RADIUS
static void generateAndPrepareDepthTextureObject()
Generate a depth texture object, set its properties and bind it to the generated framebuffer object...
Definition: Native.cpp:343
static void getRenderSceneProgramLocations(GLuint programObjectId, RenderSceneProgramLocations *locationsStoragePtr)
Retrieve locations for attributes and uniforms used in a program object responsible for rendering a s...
Definition: Native.cpp:400
Functions for manipulating matrices.
Definition: Matrix.h:31
ProgramAndShaderObjectIds renderSceneProgramAndShadersIds
Definition: Native.cpp:153
static void getNormals(int *numberOfCoordinates, float **normals)
Create normals for a cube which was created with getTriangleRepresentation() function.
Definition: CubeModel.cpp:356
static Matrix matrixInvert(Matrix *matrix)
Get the inverse of a matrix.
Definition: Matrix.cpp:155
#define SPOT_LIGHT_ANGLE_IN_DEGREES
#define TEXTURE_UNIT_FOR_COLOR_TEXTURE
static Matrix createTranslation(float x, float y, float z)
Create and return a translation matrix.
Definition: Matrix.cpp:414
static void setupGeometryData()
Prepare the geometry data that will be used while rendering the scene.
Definition: Native.cpp:646
Provides a platform independent high resolution timer.
Definition: Timer.h:37
#define COLOR_TEXTURE_NAME
static Matrix matrixLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a look at matrix.
Definition: Matrix.cpp:431
Matrix cubeModelMatrix
Definition: Native.cpp:129
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
Definition: Matrix.cpp:425
#define FAR_PLANE
static Matrix biasMatrix
The bias matrix.
Definition: Matrix.h:102
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
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_projectedLights_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:993
Matrix planeNormalMatrix
Definition: Native.cpp:131
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_projectedLights_NativeLibrary_uninit(JNIEnv *env, jobject obj)
Definition: Native.cpp:999
#define CUBE_SCALING_FACTOR
const Vec3f lookAtPoint
Definition: Native.cpp:569
RenderGeometryObjects renderPlane
ModelViewProperties planeViewProperties
Matrix cubeNormalMatrix
Definition: Native.cpp:127
void uninit()
Delete created objects and free allocated memory.
Definition: Native.cpp:1706
static void updateSpotLightDirection()
Calculate the updated direction of the spot light and update the corresponding OpenGLES object settin...
Definition: Native.cpp:935
GLfloat GLfloat f
Definition: gl2ext.h:2707
ModelViewProperties planeViewProperties
CameraViewProperties cameraViewProperties
Definition: Native.cpp:147
static void getTriangleRepresentation(float scalingFactor, int *numberOfCoordinates, float **coordinates)
Compute coordinates of points which make up a cube.
Definition: CubeModel.cpp:29
GLsizei shadowMapWidth
Definition: Native.cpp:156
void renderFrame(void)
Definition: Native.cpp:1536
#define FRAGMENT_SHADER_FILE_NAME
Definition: Boids.h:27
static Vec4f vertexTransform(Vec4f *vector, Matrix *matrix)
Transform a 4D vertex by a matrix.
Definition: Matrix.cpp:559
static void getNormals(int *numberOfCoordinates, float **normals)
Get normals for plane placed in XZ space.
Definition: PlaneModel.cpp:240
static Matrix createRotationY(float angle)
Create and return a rotation matrix around the y-axis matrix.
Definition: Matrix.cpp:511
static void drawCubeAndPlane(bool isCameraPointOfView)
Draw cube and plane model.
Definition: Native.cpp:163
unsigned char * textureData
Definition: ThreadSync.cpp:109
float degreesToRadians(float degrees)
Convert an angle in degrees to radians.
Definition: Mathematics.h:86
#define MODEL_Y_POSITION
static void matrixTranspose(Matrix *matrix)
Transpose a matrix in-place.
Definition: Matrix.cpp:374
Timer timer
Definition: Native.cpp:1059
int windowWidth
Definition: Native.cpp:575
#define LIGHT_PERSPECTIVE_FOV_IN_DEGREES
#define TEXTURE_UNIT_FOR_SHADOW_MAP_TEXTURE
#define ASSERT(x, s)
Definition: common.h:45
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_projectedLights_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:1006
GLint GLsizei width
Definition: gl2ext.h:179
#define NUMBER_OF_POINT_COORDINATES
Number of coordinates for a point in 3D space.
Definition: Common.h:36
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
ModelViewProperties cubeViewProperties
RenderSceneProgramLocations renderSceneProgramLocations
Definition: Native.cpp:154
#define COLOR_TEXTURE_TRANSLATION
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
#define MODEL_Y_ROTATION_ANGLE_IN_DEGREES
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
ModelViewProperties cubeViewProperties
A 4D floating point vector.
Definition: VectorTypes.h:127
SpotLightProperties spotLightProperties
Definition: Native.cpp:157
#define NEAR_PLANE
static void loadBmpImageData(const char *fileName, int *imageWidthPtr, int *imageHeightPtr, unsigned char **textureDataPtrPtr)
Load BMP texture data from a file into memory.
Definition: Texture.cpp:26
RenderGeometryObjects renderCube
GeometryProperties cubeGeometryProperties
Definition: Native.cpp:148
#define PLANE_SCALING_FACTOR
static void initializeViewMatrices()
Initilize data that will be used to translate vertices into eye- and NDC-space.
Definition: Native.cpp:487
SpotLightViewProperites lightViewProperties
Definition: Native.cpp:150
DirectionalLightProperties directionalLightProperties
Definition: Native.cpp:149