33 #include <android/log.h>
35 #include <GLES3/gl3.h>
47 using namespace MaliSDK;
154 const float cubesYPosition = -3.0f;
161 cube.numberOfElementsInPositionArray = 2 * 4;
162 cube.scalingFactor = 2.0f;
165 cube.
position = (
float*) malloc (
cube.numberOfElementsInPositionArray *
sizeof(
float));
186 plane.numberOfElementsInPositionArray = 3;
187 plane.scalingFactor = 15.0;
188 plane.
position = (
float*) malloc (
plane.numberOfElementsInPositionArray *
sizeof(
float));
229 GLuint vertexArrayObjectsNames[3] = {0};
233 GL_CHECK(glGenBuffers(6, bufferObjectIds));
245 GL_CHECK(glGenVertexArrays(3, vertexArrayObjectsNames));
258 GL_CHECK(glBindTexture (GL_TEXTURE_2D,
262 GL_CHECK(glTexStorage2D(GL_TEXTURE_2D,
264 GL_DEPTH_COMPONENT24,
269 GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
270 GL_TEXTURE_MIN_FILTER,
272 GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
273 GL_TEXTURE_MAG_FILTER,
275 GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
278 GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
281 GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
282 GL_TEXTURE_COMPARE_FUNC,
284 GL_CHECK(glTexParameteri(GL_TEXTURE_2D,
285 GL_TEXTURE_COMPARE_MODE,
286 GL_COMPARE_REF_TO_TEXTURE));
293 GL_CHECK(glBindFramebuffer (GL_FRAMEBUFFER,
297 GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER,
405 Vec3f cameraTranslation = {0.0f, 0.0f, -20.0f};
413 cameraTranslation.
z) *
428 &
cube.numberOfPoints,
436 &
plane.numberOfPoints,
437 plane.scalingFactor);
469 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
471 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
477 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
479 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
485 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
487 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
493 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
495 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
501 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
503 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
511 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER,
513 GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
514 cube.numberOfElementsInPositionArray *
sizeof(
float),
535 GL_CHECK(glAttachShader(programId, vertexShaderId));
536 GL_CHECK(glAttachShader(programId, fragmentShaderId));
597 ASSERT(uniformBlockIndex != GL_INVALID_INDEX,
"Could not retrieve uniform block index: uniformBlockIndex");
598 ASSERT(planePositionLocation != -1,
"Could not retrieve uniform location: planePositionLocation");
599 ASSERT(cameraPositionLocation != -1,
"Could not retrieve uniform location: cameraPositionLocation");
600 ASSERT(cameraProjectionMatrixLocation != -1,
"Could not retrieve uniform location: cameraProjectionMatrixLocation");
601 ASSERT(lightProjectionMatrixLocation != -1,
"Could not retrieve uniform location: lightProjectionMatrixLocation");
610 GL_CHECK(glUniform3fv (planePositionLocation,
613 GL_CHECK(glUniform3fv (cameraPositionLocation,
616 GL_CHECK(glUniformMatrix4fv (cameraProjectionMatrixLocation,
620 GL_CHECK(glUniformMatrix4fv (lightProjectionMatrixLocation,
652 ASSERT(positionLocation != -1,
"Could not retrieve attribute location: positionLocation");
653 ASSERT(projectionMatrixLocation != -1,
"Could not retrieve uniform location: projectionMatrixLocation");
654 ASSERT(cameraPositionLocation != -1,
"Could not retrieve uniform location: cameraPositionLocation");
661 GL_CHECK(glBindBuffer (GL_ARRAY_BUFFER,
663 GL_CHECK(glEnableVertexAttribArray(positionLocation));
664 GL_CHECK(glVertexAttribPointer (positionLocation,
677 void draw(
bool hasShadowMapBeenCalculated)
680 GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT));
686 if (!hasShadowMapBeenCalculated)
702 if (!hasShadowMapBeenCalculated)
729 GL_CHECK(glDrawArraysInstanced(GL_TRIANGLES, 0,
cube.numberOfPoints, 2));
751 GL_CHECK(glDrawArrays(GL_TRIANGLES, 0,
plane.numberOfPoints));
754 if (hasShadowMapBeenCalculated)
794 GL_CHECK(glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE));
806 GL_CHECK(glEnable(GL_POLYGON_OFFSET_FILL));
811 GL_CHECK(glDisable(GL_POLYGON_OFFSET_FILL));
820 GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
829 GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE));
847 const float radius = 5.0f;
864 GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
890 GL_CHECK(glPolygonOffset(1.0, 0.0));
896 GL_CHECK(glActiveTexture(GL_TEXTURE0));
944 JNIEnv * env, jobject obj, jint
width, jint
height)
950 JNIEnv * env, jobject obj)
957 JNIEnv * env, jobject obj)
struct LightProperties light
struct LightRepresentationProgramProperties lightRepresentationProgram
GLuint lightRepresentationCoordinatesVertexArrayObjectId
static void getTriangleRepresentation(int *numberOfCoordinates, float **coordinates)
Get coordinates of points which make up a plane. The plane is located in XZ space.
void deleteObjects()
Deletes all created GL objects.
#define SPOT_LIGHT_CUBE_VERTEX_SHADER_FILE_NAME
GLuint planeVertexArrayObjectId
GLint cameraPositionLocation
int numberOfElementsInCoordinatesArray
struct CubesAndPlaneProgramProperties cubesAndPlaneProgram
GLuint shouldRenderPlaneLocation
float getTime()
Returns the time passed since object creation or since reset() was last called.
#define SPOT_LIGHT_CUBE_FRAGMENT_SHADER_FILE_NAME
GLint GLsizei GLsizei height
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_shadowMapping_NativeLibrary_uninit(JNIEnv *env, jobject obj)
GLuint lightRepresentationCoordinatesBufferObjectId
void setupLightRepresentationProgram()
Create a program that will be used to rasterize the geometry of light cube.
struct WindowProperties window
GLuint uniformBlockDataBufferObjectId
GLuint lightViewMatrixLocation
Functions for manipulating matrices.
static void getNormals(int *numberOfCoordinates, float **normals)
Create normals for a cube which was created with getTriangleRepresentation() function.
void setupCubesAndPlaneProgram()
Create a program that will be used to convert vertices into eye-space and then rasterize cubes and pl...
static Matrix createTranslation(float x, float y, float z)
Create and return a translation matrix.
Provides a platform independent high resolution timer.
static Matrix matrixLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a look at matrix.
void createDataForObjectsToBeDrawn()
Initialize data used for drawing the scene.
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
GLuint planeCoordinatesBufferObjectId
void createObjects()
Creates GL objects.
Matrix viewMatrixForShadowMapPass
void drawScene()
Draw the lit shadow-mapped scene from the camera's point of view.
GLint lightDirectionLocation
float * getAsArray(void)
Get the matrix elements as a column major order array.
GLuint cubeCoordinatesBufferObjectId
#define VERTEX_SHADER_FILE_NAME
GLint lightPositionLocation
A 3D floating point vector.
void setUpAndUseProgramObject(GLint programId, const char *fragmentShaderFileName, const char *vertexShaderFileName)
Create, compile and attach vertex and fragment shaders to previously created program object...
void reset()
Resets the timer to 0.0f.
int numberOfElementsInNormalsArray
void uninit()
Delete created objects and free allocated memory.
struct GeometryProperties lightRepresentation
int numberOfElementsInPositionArray
static void getTriangleRepresentation(float scalingFactor, int *numberOfCoordinates, float **coordinates)
Compute coordinates of points which make up a cube.
GLuint cubeNormalsBufferObjectId
void initializeStructureData()
Initialize structure data.
GLuint isCameraPointOfViewLocation
#define FRAGMENT_SHADER_FILE_NAME
GLuint positionAttributeLocation
static void getNormals(int *numberOfCoordinates, float **normals)
Get normals for plane placed in XZ space.
void initializeData()
Initializes data used for rendering.
GLuint planeNormalsBufferObjectId
int numberOfElementsInCoordinatesArray
GLuint framebufferObjectName
Matrix lightProjectionMatrix
float degreesToRadians(float degrees)
Convert an angle in degrees to radians.
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_shadowMapping_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
GLuint bufferObjectIds[numberOfBufferObjectIds]
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_shadowMapping_NativeLibrary_step(JNIEnv *env, jobject obj)
int numberOfElementsInNormalsArray
void calculateLookAtMatrix()
Calculates depth values written to shadow map texture.
GLuint normalsAttributeLocation
struct GeometryProperties cube
GLuint cubesVertexArrayObjectId
struct GeometryProperties plane
GLuint colorOfGeometryLocation
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
Matrix cameraProjectionMatrix
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
void createShadowMap()
Draw the scene from the light's point of view to calculate depth values (calculated values are held i...
void normalize(void)
Normalize 3D floating point vector.
struct ShadowMapTextureProperties shadowMap