OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Native.cpp File Reference

Demonstration of Occlusion Query functionality in OpenGL ES 3.0. More...

#include <jni.h>
#include <android/log.h>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include "Common.h"
#include "CubeModel.h"
#include "Matrix.h"
#include "Native.h"
#include "PlaneModel.h"
#include "Shader.h"
#include "SuperEllipsoidModel.h"
#include "Text.h"
#include "Texture.h"
#include "Timer.h"

Go to the source code of this file.

Functions

float distanceBetweenPoints (const Vec2f &point1, const Vec2f &point2)
 Compute Euclidean 2-dimensional distance between two points on XY plane. More...
 
bool inNeighbourhood (const Vec2f &point, float minDistance, int j)
 Check if the rounded cubes are a proper distance from each other to prevent cubes overlapping. More...
 
float uniformRandomNumber ()
 Generate random number in the 0.0 to 1.0 range. More...
 
void generateCubesLocations (float planeWidth, float planeHeight, float minDistance)
 Generate random cubes' center locations. More...
 
void sortCubePositions (float *arrayToSort)
 Function that is used to sort cubes' center positions from the nearest to the furthest, relative to the camera position. More...
 
void rewriteVec2fArrayToFloatArray ()
 convert Vec2f array to an array of floats. More...
 
void sendCubeLocationVectorToUniform (int whichCube)
 Sends center position of a cube to the vertex shader's uniform. More...
 
void setupGraphics (int width, int height)
 Function that sets up shaders, programs, uniforms locations, generates buffer objects and query objects. More...
 
void draw (void)
 Draw the plane and cubes. More...
 
void renderFrame ()
 Render one frame. More...
 
void uninit ()
 Releases all OpenGL objects that were created with glGen*() or glCreate*() functions. More...
 
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_init (JNIEnv *, jobject, jint width, jint height)
 
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_step (JNIEnv *, jobject)
 
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_uninit (JNIEnv *, jobject)
 

Variables

const string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.occlusionQueries/files/"
 
int windowWidth = 0
 
int windowHeight = 0
 
Timer timer
 
Timer fpsTimer
 
GLuint programId = 0
 
floatroundedCubeCoordinates = NULL
 
floatroundedCubeNormalVectors = NULL
 
int numberOfRoundedCubesVertices = 0
 
int numberOfRoundedCubeCoordinates = 0
 
int numberOfRoundedCubeNormalVectors = 0
 
Vec2f randomCubesPositions [NUMBER_OF_CUBES]
 
const float minimumDistance = ROUNDED_CUBE_SCALE_FACTOR * 2.0f + 0.1f
 
GLuint cubeQuery [NUMBER_OF_CUBES] = {0}
 
bool occlusionQueriesOn = false
 
float angleY = 0.0f
 
const float cameraRadius = 22.0f
 
const float yCameraTranslation = 1.25f
 
Matrix cubeNormalMatrix
 
Matrix cubeMvpMatrix
 
Matrix cubeModelMatrix
 
Matrix cubeWorldInverseMatrix
 
Matrix planeNormalMatrix
 
Matrix planeMvpMatrix
 
Matrix planeModelMatrix
 
Matrix planeWorldInverseMatrix
 
Matrix projectionMatrix
 
Matrix rotatedViewMatrix
 
Matrix viewMatrix
 
GLint normalMatrixUniformLocation = -1
 
GLint mvpMatrixUniformLocation = -1
 
GLint worldInverseMatrixUniformLocation = -1
 
GLint colorUniformLocation = -1
 
float sortedCubesPositions [2 *NUMBER_OF_CUBES] = {0.0f}
 
const float planeScalingFactor = 40.0f
 
const float planeDividend = 3.0f
 
const float cubeColor [] = {0.0f, 0.75f, 0.0f, 1.0f}
 
const float planeColor [] = {1.0f, 0.8f, 0.0f, 1.0f}
 
const float planeLocation [] = {0.0f, 0.0f, 0.0f}
 
GLuint planeVertexArrayObjectId = 0
 
GLuint normalCubeVertexArrayObjectId = 0
 
GLuint roundedCubeVertexArrayObjectId = 0
 
GLuint planeVerticesBufferId = 0
 
GLuint planeNormalVectorsBufferId = 0
 
GLuint normalCubeBufferId = 0
 
GLuint roundedCubeVerticesBufferId = 0
 
GLuint roundedCubeNormalVectorsBufferId = 0
 
floatnormalCubeVertices = NULL
 
int numberOfCubeCoordinates = 0
 
int numberOfCubeVertices = 0
 
floatplaneVertices = NULL
 
int numberOfPlaneVertices = 0
 
int numberOfPlaneVertexCoordinates = 0
 
floatplaneNormalVectors = NULL
 
int sizeOfPlaneNormalsArray = 0
 
bool modeChanged = false
 
int numberOfRoundedCubesDrawn = 0
 
Texttext
 

Detailed Description

Demonstration of Occlusion Query functionality in OpenGL ES 3.0.

The main purpose of the application is to show the difference in performance when the occlusion query mode is on or off. If the occlusion query mode is on, then only the cubes that are visible to the viewer are rendered. In the other case, when the occlusion query mode is off, then all of the cubes are rendered, which leads to a massive decrease in performance.

  • In the case where occlusion query mode in on: if there is a small number of objects visible for a viewer, the application runs very smooth; the larger the number of the visible objects, the slower the animation is, but still the performance is better than in the following case.
  • In the case where occlusion query mode in off: the performance is constant (very low), regardless of the number of visible cubes (all of them are always rendered).

We are rendering rounded cubes - the objects are more complicated than the normal cubes, which means the time needed for rendering this kind of objects is longer. We are using this fact to demonstrate the occlusion query mode. When we want to verify whether the object is visible for a viewer, we can draw a simpler object (located in the same position as the requested one and being almost of the same size and shape), and once we get the results, we are able to render only those rounded cubes which passed the test.

There is also text displayed (at the bottom left corner of the screen) showing whether the occlusion query mode is currently on or off. The mode changes every 10 seconds.

Definition in file Native.cpp.

Function Documentation

float distanceBetweenPoints ( const Vec2f point1,
const Vec2f point2 
)
inline

Compute Euclidean 2-dimensional distance between two points on XY plane.

Parameters
[in]point1First point.
[in]point2Second point.
Returns
Distance between points on XY plane.

Definition at line 215 of file Native.cpp.

void draw ( void  )

Draw the plane and cubes.

Renders all rounded cubes if occlusion queries turned on, otherwise just draws those visible.

Definition at line 722 of file Native.cpp.

void generateCubesLocations ( float  planeWidth,
float  planeHeight,
float  minDistance 
)

Generate random cubes' center locations.

This algorithm ensures that cube will be the required distance apart.

Parameters
[in]planeWidthWidth of the plane.
[in]planeHeightHeight of the plane.
[in]minDistanceDetermines minimum distance between cubes.

Definition at line 260 of file Native.cpp.

bool inNeighbourhood ( const Vec2f point,
float  minDistance,
int  j 
)
inline

Check if the rounded cubes are a proper distance from each other to prevent cubes overlapping.

Parameters
[in]pointThe point for which we check distance.
[in]minDistanceDetermines minimum distance between points.
[in]jTells how many points are already saved in the randomCubesPositions array.
Returns
True if the point is in neighbourhood of any other point that is already saved in randomCubesPotisitions array and false otherwise.

Definition at line 229 of file Native.cpp.

JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_init ( JNIEnv *  ,
jobject  ,
jint  width,
jint  height 
)

Definition at line 955 of file Native.cpp.

JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_step ( JNIEnv *  ,
jobject   
)

Definition at line 962 of file Native.cpp.

JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_uninit ( JNIEnv *  ,
jobject   
)

Definition at line 967 of file Native.cpp.

void renderFrame ( void  )

Render one frame.

Definition at line 847 of file Native.cpp.

void rewriteVec2fArrayToFloatArray ( )

convert Vec2f array to an array of floats.

Rewrite coordinates from randomCubesPositions array which type is Vec2f to a simple array of floats (sortedCubesPositions) that we can easily pass to the uniform.

Definition at line 374 of file Native.cpp.

void sendCubeLocationVectorToUniform ( int  whichCube)
inline

Sends center position of a cube to the vertex shader's uniform.

Parameters
[in]whichCubeDetermines which cube's center position we should send to the vertex shader.

Definition at line 388 of file Native.cpp.

void setupGraphics ( int  width,
int  height 
)

Function that sets up shaders, programs, uniforms locations, generates buffer objects and query objects.

Parameters
widthWindow width.
heightWindow height.

Definition at line 428 of file Native.cpp.

void sortCubePositions ( float arrayToSort)

Function that is used to sort cubes' center positions from the nearest to the furthest, relative to the camera position.

We use bubble sort algorithm that checks whether an array is sorted or not.

Parameters
[in,out]arrayToSortAn array to be sorted.

Definition at line 325 of file Native.cpp.

float uniformRandomNumber ( )
inline

Generate random number in the 0.0 to 1.0 range.

Returns
Random number in the range 0.0 to 1.0.

Definition at line 246 of file Native.cpp.

void uninit ( )

Releases all OpenGL objects that were created with glGen*() or glCreate*() functions.

Definition at line 925 of file Native.cpp.

Variable Documentation

float angleY = 0.0f

Definition at line 118 of file Native.cpp.

const float cameraRadius = 22.0f

Definition at line 121 of file Native.cpp.

GLint colorUniformLocation = -1

Definition at line 143 of file Native.cpp.

const float cubeColor[] = {0.0f, 0.75f, 0.0f, 1.0f}

Definition at line 154 of file Native.cpp.

Matrix cubeModelMatrix

Definition at line 129 of file Native.cpp.

Matrix cubeMvpMatrix

Definition at line 128 of file Native.cpp.

Matrix cubeNormalMatrix

Definition at line 127 of file Native.cpp.

GLuint cubeQuery[NUMBER_OF_CUBES] = {0}

Definition at line 112 of file Native.cpp.

Matrix cubeWorldInverseMatrix

Definition at line 130 of file Native.cpp.

Timer fpsTimer

Definition at line 85 of file Native.cpp.

const float minimumDistance = ROUNDED_CUBE_SCALE_FACTOR * 2.0f + 0.1f

Definition at line 109 of file Native.cpp.

bool modeChanged = false

Definition at line 199 of file Native.cpp.

GLint mvpMatrixUniformLocation = -1

Definition at line 141 of file Native.cpp.

GLuint normalCubeBufferId = 0

Definition at line 170 of file Native.cpp.

GLuint normalCubeVertexArrayObjectId = 0

Definition at line 160 of file Native.cpp.

float* normalCubeVertices = NULL

Definition at line 179 of file Native.cpp.

GLint normalMatrixUniformLocation = -1

Definition at line 140 of file Native.cpp.

int numberOfCubeCoordinates = 0

Definition at line 182 of file Native.cpp.

int numberOfCubeVertices = 0

Definition at line 183 of file Native.cpp.

int numberOfPlaneVertexCoordinates = 0

Definition at line 190 of file Native.cpp.

int numberOfPlaneVertices = 0

Definition at line 189 of file Native.cpp.

int numberOfRoundedCubeCoordinates = 0

Definition at line 100 of file Native.cpp.

int numberOfRoundedCubeNormalVectors = 0

Definition at line 103 of file Native.cpp.

int numberOfRoundedCubesDrawn = 0

Definition at line 202 of file Native.cpp.

int numberOfRoundedCubesVertices = 0

Definition at line 97 of file Native.cpp.

bool occlusionQueriesOn = false

Definition at line 115 of file Native.cpp.

const float planeColor[] = {1.0f, 0.8f, 0.0f, 1.0f}

Definition at line 155 of file Native.cpp.

const float planeDividend = 3.0f

Definition at line 152 of file Native.cpp.

const float planeLocation[] = {0.0f, 0.0f, 0.0f}

Definition at line 156 of file Native.cpp.

Matrix planeModelMatrix

Definition at line 133 of file Native.cpp.

Matrix planeMvpMatrix

Definition at line 132 of file Native.cpp.

Matrix planeNormalMatrix

Definition at line 131 of file Native.cpp.

float* planeNormalVectors = NULL

Definition at line 193 of file Native.cpp.

GLuint planeNormalVectorsBufferId = 0

Definition at line 167 of file Native.cpp.

const float planeScalingFactor = 40.0f

Definition at line 149 of file Native.cpp.

GLuint planeVertexArrayObjectId = 0

Definition at line 159 of file Native.cpp.

float* planeVertices = NULL

Definition at line 186 of file Native.cpp.

GLuint planeVerticesBufferId = 0

Definition at line 164 of file Native.cpp.

Matrix planeWorldInverseMatrix

Definition at line 134 of file Native.cpp.

GLuint programId = 0

Definition at line 88 of file Native.cpp.

Matrix projectionMatrix

Definition at line 135 of file Native.cpp.

Vec2f randomCubesPositions[NUMBER_OF_CUBES]

Definition at line 106 of file Native.cpp.

const string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.occlusionQueries/files/"

Definition at line 75 of file Native.cpp.

Matrix rotatedViewMatrix

Definition at line 136 of file Native.cpp.

float* roundedCubeCoordinates = NULL

Definition at line 91 of file Native.cpp.

float* roundedCubeNormalVectors = NULL

Definition at line 94 of file Native.cpp.

GLuint roundedCubeNormalVectorsBufferId = 0

Definition at line 176 of file Native.cpp.

GLuint roundedCubeVertexArrayObjectId = 0

Definition at line 161 of file Native.cpp.

GLuint roundedCubeVerticesBufferId = 0

Definition at line 173 of file Native.cpp.

int sizeOfPlaneNormalsArray = 0

Definition at line 196 of file Native.cpp.

float sortedCubesPositions[2 *NUMBER_OF_CUBES] = {0.0f}

Definition at line 146 of file Native.cpp.

Text* text

Definition at line 205 of file Native.cpp.

Timer timer

Definition at line 82 of file Native.cpp.

Matrix viewMatrix

Definition at line 137 of file Native.cpp.

int windowHeight = 0

Definition at line 79 of file Native.cpp.

int windowWidth = 0

Definition at line 78 of file Native.cpp.

GLint worldInverseMatrixUniformLocation = -1

Definition at line 142 of file Native.cpp.

const float yCameraTranslation = 1.25f

Definition at line 124 of file Native.cpp.