52 #include <android/log.h>
72 using namespace MaliSDK;
75 const string resourceDirectory =
"/data/data/com.arm.malideveloper.openglessdk.occlusionQueries/files/";
217 return sqrtf((point2.
x - point1.
x) * (point2.
x - point1.
x) + (point2.
y - point1.
y) * (point2.
y - point1.
y));
231 for (
int i = 0; i < j; i++)
248 return rand() /
float(RAND_MAX);
276 float loopsCounter = 0;
292 if (loopsCounter > NUMBER_OF_CUBES * NUMBER_OF_CUBES)
337 for (
int i = 0; i <=
max; i += 2)
340 Vec2f firstCubeLocation, secondCubeLocation;
342 firstCubeLocation.
x = arrayToSort[i];
343 firstCubeLocation.
y = arrayToSort[i + 1];
345 secondCubeLocation.
x = arrayToSort[i + 2];
346 secondCubeLocation.
y = arrayToSort[i + 3];
348 Vec3f first_cube_location = {firstCubeLocation.
x, 1, firstCubeLocation.
y};
349 Vec3f second_cube_location = {secondCubeLocation.
x, 1, secondCubeLocation.
y};
350 Vec3f transformed_first_cube_location = Matrix::vertexTransform(&first_cube_location, &
rotatedViewMatrix);
351 Vec3f transformed_second_cube_location = Matrix::vertexTransform(&second_cube_location, &
rotatedViewMatrix);
353 if (transformed_first_cube_location.
z < transformed_second_cube_location.
z)
356 arrayToSort[i] = secondCubeLocation.
x;
357 arrayToSort[i + 1] = secondCubeLocation.
y;
359 arrayToSort[i + 2] = firstCubeLocation.
x;
360 arrayToSort[i + 3] = firstCubeLocation.
y;
431 srand((
unsigned int)
time(NULL));
452 viewMatrix = Matrix::matrixCameraLookAt(eyeVector,
464 text->
addString(0, 0,
"Occlusion query OFF", 255, 0, 0, 255);
479 Shader::processShader(&vertexShaderId,
480 vertexShaderPath.c_str(),
482 Shader::processShader(&fragmentShaderId,
483 fragmentShaderPath.c_str(),
491 GL_CHECK(glAttachShader(programId, vertexShaderId));
492 GL_CHECK(glAttachShader(programId, fragmentShaderId));
504 GLuint normalAttributeLocation =
GL_CHECK(glGetAttribLocation (programId,
"normal"));
505 GLuint verticesAttributeLocation =
GL_CHECK(glGetAttribLocation (programId,
"vertex"));
514 ASSERT(verticesAttributeLocation != -1,
"Could not retrieve attribute location: vertex");
515 ASSERT(normalAttributeLocation != -1,
"Could not retrieve attribute location: normal");
583 GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
585 GL_CHECK(glBufferData (GL_ARRAY_BUFFER,
591 GL_CHECK(glVertexAttribPointer(verticesAttributeLocation,
600 GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
602 GL_CHECK(glBufferData (GL_ARRAY_BUFFER,
608 GL_CHECK(glVertexAttribPointer(normalAttributeLocation,
617 GL_CHECK(glEnableVertexAttribArray(verticesAttributeLocation));
618 GL_CHECK(glEnableVertexAttribArray(normalAttributeLocation));
625 GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
627 GL_CHECK(glBufferData (GL_ARRAY_BUFFER,
631 GL_CHECK(glVertexAttribPointer(verticesAttributeLocation,
638 GL_CHECK(glEnableVertexAttribArray(verticesAttributeLocation));
644 GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
646 GL_CHECK(glBufferData (GL_ARRAY_BUFFER,
650 GL_CHECK(glVertexAttribPointer(verticesAttributeLocation,
657 GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
659 GL_CHECK(glBufferData (GL_ARRAY_BUFFER,
663 GL_CHECK(glVertexAttribPointer(normalAttributeLocation,
670 GL_CHECK(glEnableVertexAttribArray(verticesAttributeLocation));
671 GL_CHECK(glEnableVertexAttribArray(normalAttributeLocation));
679 GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
741 GL_CHECK(glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
753 GL_CHECK(glEndQuery(GL_ANY_SAMPLES_PASSED));
760 GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
765 GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT));
769 GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
778 GLuint queryResult = GL_FALSE;
785 if (queryResult == GL_TRUE)
837 GL_CHECK(glDrawArrays (GL_TRIANGLES,
854 LOGI(
"FPS:\t%.1f", FPS);
859 GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
870 Matrix yRotationMatrix = Matrix::createRotationY(-
angleY);
897 LOGI(
"\nOcclusion query ON\n");
903 LOGI(
"\nOcclusion query OFF\n");
905 text->
addString(0, 0,
"Occlusion query OFF", 255, 0, 0, 255);
GLuint planeNormalVectorsBufferId
A 2D floating point vector.
void generateCubesLocations(float planeWidth, float planeHeight, float minDistance)
Generate random cubes' center locations.
GLuint planeVertexArrayObjectId
float getTime()
Returns the time passed since object creation or since reset() was last called.
GLuint planeVerticesBufferId
float projectionMatrix[16]
Functions for drawing text in OpenGL ES.
const string resourceDirectory
float getFPS()
Returns the FPS (Frames Per Second).
int numberOfPlaneVertexCoordinates
GLint GLsizei GLsizei height
float * planeNormalVectors
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_init(JNIEnv *, jobject, jint width, jint height)
Matrix cubeWorldInverseMatrix
Functions for manipulating matrices.
GLint colorUniformLocation
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...
void sortCubePositions(float *arrayToSort)
Function that is used to sort cubes' center positions from the nearest to the furthest, relative to the camera position.
GLuint normalCubeVertexArrayObjectId
const float planeScalingFactor
GLint worldInverseMatrixUniformLocation
GLuint cubeQuery[NUMBER_OF_CUBES]
const float planeLocation[]
Provides a platform independent high resolution timer.
void clear(void)
Removes the current string from the class.
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.
#define NORMAL_CUBE_SCALE_FACTOR
float * getAsArray(void)
Get the matrix elements as a column major order array.
int numberOfCubeCoordinates
GLuint normalCubeBufferId
A 3D floating point vector.
const float yCameraTranslation
void reset()
Resets the timer to 0.0f.
void uninit()
Delete created objects and free allocated memory.
int numberOfPlaneVertices
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.
GLint mvpMatrixUniformLocation
float distanceBetweenPoints(const Vec2f &point1, const Vec2f &point2)
Compute Euclidean 2-dimensional distance between two points on XY plane.
Matrix planeWorldInverseMatrix
GLuint roundedCubeVerticesBufferId
float uniformRandomNumber()
Generate random number in the 0.0 to 1.0 range.
const float minimumDistance
float max(float x, float y)
const float planeDividend
float degreesToRadians(float degrees)
Convert an angle in degrees to radians.
float * roundedCubeNormalVectors
int numberOfRoundedCubesDrawn
float sortedCubesPositions[2 *NUMBER_OF_CUBES]
void sendCubeLocationVectorToUniform(int whichCube)
Sends center position of a cube to the vertex shader's uniform.
float * roundedCubeCoordinates
int numberOfRoundedCubeCoordinates
int numberOfRoundedCubesVertices
GLuint roundedCubeNormalVectorsBufferId
Vec2f randomCubesPositions[NUMBER_OF_CUBES]
int numberOfRoundedCubeNormalVectors
bool isTimePassed(float seconds=1.0f)
Tests if 'seconds' seconds have passed since reset() or this method was called.
void draw(void)
Draw the text to the screen.
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLuint roundedCubeVertexArrayObjectId
#define NUMBER_OF_SAMPLES
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_uninit(JNIEnv *, jobject)
#define ROUNDED_CUBE_SCALE_FACTOR
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_occlusionQueries_NativeLibrary_step(JNIEnv *, jobject)
float * normalCubeVertices
int sizeOfPlaneNormalsArray
GLint normalMatrixUniformLocation
void rewriteVec2fArrayToFloatArray()
convert Vec2f array to an array of floats.