GLUSboolean updateWavefront(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; glusMatrix4x4Identityf(modelViewMatrix); glusMatrix4x4Translatef(modelViewMatrix, 0.0f, -1.5f, 0.0f); glusMatrix4x4RotateRyf(modelViewMatrix, angle); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUseProgram(g_program.program); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glBindVertexArray(g_vao); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); angle += 30.0f * time; return GLUS_TRUE; }
GLUSvoid reshape(GLUSint width, GLUSint height) { GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; GLfloat viewMatrix[16]; GLfloat projectionMatrix[16]; GLfloat viewProjectionMatrix[16]; GLfloat modelViewProjectionMatrix[16]; glUseProgram(g_program.program); glViewport(0, 0, width, height); // For now, the grid is just on the "ground". glusMatrix4x4Identityf(modelMatrix); // This model matrix is a rigid body transform. So no need for the inverse, transposed matrix. glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix); glusMatrix4x4Perspectivef(projectionMatrix, 40.0f, (GLfloat) width / (GLfloat) height, 1.0f, 100.0f); glusMatrix4x4LookAtf(viewMatrix, 0.0f, 4.0f, 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusMatrix4x4Multiplyf(viewProjectionMatrix, projectionMatrix, viewMatrix); glusMatrix4x4Multiplyf(modelViewProjectionMatrix, viewProjectionMatrix, modelMatrix); glUniformMatrix4fv(g_modelViewProjectionMatrixLocation, 1, GL_FALSE, modelViewProjectionMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUseProgram(0); reshapeSphere(viewProjectionMatrix); }
GLUSboolean update(GLUSfloat time) { static GLfloat passedTime = 0.0f; static GLUSfloat angle = 0.0f; GLfloat inverseViewMatrix[16]; glusLookAtf(g_viewMatrix, 0.0f, 1.0f, 0.0f, (GLfloat) 0.5f * sinf(angle), 1.0f, -(GLfloat) 0.5f * cosf(angle), 0.0f, 1.0f, 0.0f); glusMatrix4x4Copyf(inverseViewMatrix, g_viewMatrix, GLUS_TRUE); glusMatrix4x4InverseRigidBodyf(inverseViewMatrix); glusMatrix4x4ExtractMatrix3x3f(g_inverseViewNormalMatrix, inverseViewMatrix); // Render the background renderBackground(g_viewMatrix); // Render the water texture renderWaterTexture(passedTime); // Render the water scene renderWater(passedTime); passedTime += time; angle += 2.0f * PIf / 120.0f * time; return GLUS_TRUE; }
GLUSvoid reshape(GLUSint width, GLUSint height) { GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; GLfloat viewMatrix[16]; GLfloat modelViewProjectionMatrix[16]; glViewport(0, 0, width, height); // Initialize with the identity matrix ... glusMatrix4x4Identityf(modelMatrix); // ... and rotate the cube at two axes that we do see some sides. glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 45.0f, 45.0f); // This model matrix is a rigid body transform. So no need for the inverse, transposed matrix. glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix); glusMatrix4x4Perspectivef(modelViewProjectionMatrix, 40.0f, (GLfloat) width / (GLfloat) height, 1.0f, 100.0f); glusMatrix4x4LookAtf(viewMatrix, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // Here we create the view projection matrix ... glusMatrix4x4Multiplyf(modelViewProjectionMatrix, modelViewProjectionMatrix, viewMatrix); // ... and now the final model view projection matrix. glusMatrix4x4Multiplyf(modelViewProjectionMatrix, modelViewProjectionMatrix, modelMatrix); glUniformMatrix4fv(g_modelViewProjectionMatrixLocation, 1, GL_FALSE, modelViewProjectionMatrix); // Set the normal matrix. glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; glusMatrix4x4Identityf(modelViewMatrix); glusMatrix4x4RotateRyf(modelViewMatrix, angle); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); angle += 30.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; // Calculate the model matrix ... glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRyRxf(modelMatrix, 0.0f, angle, -90.0f); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); // Calculate the normal matrix ... glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawElements(GL_PATCHES, g_numberIndicesPlane, GL_UNSIGNED_INT, 0); // angle += time * 45.0f; return GLUS_TRUE; }
GLUSboolean updateWavefront(GLUSfloat time, GLUSfloat modelMatrix[16]) { GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat lightDirection[3]; // Note that the scale matrix is for flipping the model upside down. glusMatrix4x4Identityf(modelViewMatrix); glusMatrix4x4Multiplyf(modelViewMatrix, modelViewMatrix, modelMatrix); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); // Transform light to camera space, as it is currently in world space. glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, g_light->direction); glUseProgram(g_program.program); glUniform3fv(g_lightLocations.directionLocation, 1, lightDirection); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glBindVertexArray(g_vao); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); return GLUS_TRUE; }
Matrix3x3 Matrix4x4::extractMatrix3x3() const { Matrix3x3 result; glusMatrix4x4ExtractMatrix3x3f(result.m, m); return result; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; static GLuint zero = 0; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; glusMatrix4x4Identityf(modelViewMatrix); glusMatrix4x4RotateRyf(modelViewMatrix, angle); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUseProgram(g_program.program); glBindVertexArray(g_vao); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // // Linked list rendering pass. // // Reset the atomic counter. glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(GLuint), &zero); // Reset the head pointers by copying the clear buffer into the texture. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, g_clearBuffer); glBindTexture(GL_TEXTURE_2D, g_headIndexTexture); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RED_INTEGER, GL_UNSIGNED_INT, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); // glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); // // Fullscreen quad rendering. // glUseProgram(g_blendFullscreenProgram.program); glBindVertexArray(g_blendFullscreenVAO); // Resolving and blending is happening in the shader. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); angle += 30.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { // Angle for rotation static GLfloat angle = 0.0f; // Matrix for the model GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat viewMatrix[9]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Calculate the model matrix ... glusMatrix4x4Identityf(modelViewMatrix); // ... by finally rotating the cube. glusMatrix4x4RotateRzRxRyf(modelViewMatrix, 0.0f, 15.0f, angle); // Create the model view matrix. glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); // Again, extract the normal matrix. Remember, so far the model view matrix (rotation part) is orthogonal. glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // // Extract the rotation part of the view matrix. glusMatrix4x4ExtractMatrix3x3f(viewMatrix, g_viewMatrix); // Pass this matrix to the shader with the transpose flag. As the view matrix is orthogonal, the transposed is the inverse view matrix. glUniformMatrix3fv(g_inverseViewMatrixLocation, 1, GL_TRUE, viewMatrix); // glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0); angle += 20.0f * time; return GLUS_TRUE; }
GLUSvoid reshape(GLUSint width, GLUSint height) { GLfloat modelMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat lightDirection[3]; reshapeWavefront(width, height); // glViewport(0, 0, width, height); glusPerspectivef(g_projectionMatrix, 40.0f, (GLfloat)width / (GLfloat)height, 1.0f, 100.0f); // Calculate the inverse. Needed for the SSAO shader to get from projection to view space. glusMatrix4x4Copyf(g_inverseProjectionMatrix, g_projectionMatrix, GLUS_FALSE); glusMatrix4x4Inversef(g_inverseProjectionMatrix); glUseProgram(g_ssaoProgram.program); glUniformMatrix4fv(g_ssaoInverseProjectionMatrixLocation, 1, GL_FALSE, g_inverseProjectionMatrix); glUniformMatrix4fv(g_ssaoProjectionMatrixLocation, 1, GL_FALSE, g_projectionMatrix); // glusMatrix4x4Multiplyf(g_viewProjectionMatrix, g_projectionMatrix, g_viewMatrix); glUseProgram(g_program.program); glUniformMatrix4fv(g_viewProjectionMatrixLocation, 1, GL_FALSE, g_viewProjectionMatrix); glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRxf(modelMatrix, -90.0f); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); // Calculation is in camera space glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, g_light.direction); // Direction already normalized glUniform3fv(g_lightDirectionLocation, 1, lightDirection); }
GLUSboolean update(GLUSfloat time) { GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; glusMatrix4x4Identityf(modelViewMatrix); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Normal rendering glUseProgram(g_program.program); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glBindVertexArray(g_vao); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); // Fur rendering // Do not write to depth, so no fur layer will be discarded. glDepthMask(GL_FALSE); glEnable(GL_BLEND); glUseProgram(g_programFur.program); glUniformMatrix4fv(g_modelViewMatrixFurLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixFurLocation, 1, GL_FALSE, normalMatrix); glBindVertexArray(g_vaoFur); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); glDisable(GL_BLEND); glDepthMask(GL_TRUE); return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; static GLfloat erode = ERODE_START; GLfloat modelMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRyf(modelMatrix, angle); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); // The calculations are done in camera / view space. So pass the view matrix, which is a rigid body transform. glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // glUniform1f(g_erodeLocation, erode); // Loop eroding if (erode < ERODE_END) { erode = ERODE_START; } // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0); // angle += time * 45.0f; erode -= time * 0.1f; return GLUS_TRUE; }
GLUSvoid reshape(GLUSint width, GLUSint height) { GLfloat modelMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat lightDirection[3]; g_width = width; g_height = height; reshapeWavefront(width, height); // glViewport(0, 0, width, height); glusPerspectivef(g_viewProjectionMatrix, 40.0f, (GLfloat) width / (GLfloat) height, 1.0f, 100.0f); glusMatrix4x4Multiplyf(g_viewProjectionMatrix, g_viewProjectionMatrix, g_viewMatrix); glUseProgram(g_program.program); glUniformMatrix4fv(g_viewProjectionMatrixLocation, 1, GL_FALSE, g_viewProjectionMatrix); glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, 3.0f); glusMatrix4x4RotateRxf(modelMatrix, -90.0f); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); // Calculation is in camera space glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glusMatrix4x4MultiplyVector3f(lightDirection, g_viewMatrix, g_light.direction); // Direction already normalized glUniform3fv(g_lightDirectionLocation, 1, lightDirection); }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; // 90 degrees per second. Rotate the texture cube along the y axis (yaw). angle += 90.0f * time; glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 45.0f, angle); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); // Model matrix is a rigid body matrix. glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0); return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; // // Render to FBO. // glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, g_texture[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_CUBE_MAP, g_texture[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, g_texture[2]); glActiveTexture(GL_TEXTURE0); glBindFramebuffer(GL_FRAMEBUFFER, g_fullscreenFBO); glEnable(GL_MULTISAMPLE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Render the background. // Rendering the sphere from inside, so change front facing. glFrontFace(GL_CW); glUseProgram(g_backgroundProgram.program); glUniformMatrix4fv(g_viewProjectionMatrixBackgroundLocation, 1, GL_FALSE, g_viewProjectionMatrix); glBindVertexArray(g_backgroundVAO); glDrawElements(GL_TRIANGLES, g_numberIndicesBackground, GL_UNSIGNED_INT, 0); glFrontFace(GL_CCW); // Render model using BRDF and IBL. glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Scalef(modelMatrix, 0.001f, 0.001f, 0.001f); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix); glUseProgram(g_modelProgram.program); glBindVertexArray(g_modelVAO); // Roughness of material. glUniform1f(g_roughnessMaterialModelLocation, g_roughness); glUniform1f(g_R0MaterialModelLocation, g_R0); glUniform3fv(g_colorMaterialModelLocation, 1, g_colorMaterial); glUniformMatrix4fv(g_viewProjectionMatrixModelLocation, 1, GL_FALSE, g_viewProjectionMatrix); glUniformMatrix4fv(g_modelMatrixModelLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixModelLocation, 1, GL_FALSE, normalMatrix); glDrawArrays(GL_TRIANGLES, 0, g_numberVerticesModel); // // Render full screen to resolve the buffer: MSAA, tone mapping and gamma correction. // glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, g_fullscreenTexture); glBindFramebuffer(GL_FRAMEBUFFER, 0); glDisable(GL_MULTISAMPLE); // No clear needed, as we just draw over the last content. glDisable(GL_DEPTH_TEST); glUseProgram(g_fullscreenProgram.program); glUniform1f(g_exposureFullscreenLocation, g_exposure); glUniform1f(g_gammaFullscreenLocation, g_gamma); glBindVertexArray(g_fullscreenVAO); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glEnable(GL_DEPTH_TEST); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLUSshape sphere; GLUStextfile vertexSource; GLUStextfile geometrySource; GLUStextfile fragmentSource; glusFileLoadText("../Example10/shader/dublicate.vert.glsl", &vertexSource); glusFileLoadText("../Example10/shader/dublicate.geom.glsl", &geometrySource); glusFileLoadText("../Example10/shader/dublicate.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, (const GLUSchar**) &geometrySource.text, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&geometrySource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_viewMatrixLocation = glGetUniformLocation(g_program.program, "u_viewMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_lightPositionLocation = glGetUniformLocation(g_program.program, "u_lightPosition"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); // glusShapeCreateSpheref(&sphere, 1.0f, 32); g_numberIndicesSphere = sphere.numberIndices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) sphere.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, sphere.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) sphere.normals, GL_STATIC_DRAW); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphere.numberIndices * sizeof(GLuint), (GLuint*) sphere.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusShapeDestroyf(&sphere); // glUseProgram(g_program.program); glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.0f, 6.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, g_viewMatrix); glusMatrix4x4ExtractMatrix3x3f(g_normalMatrix, g_viewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, g_normalMatrix); glusMatrix4x4MultiplyPoint4f(g_lightPosition, g_viewMatrix, g_lightPosition); glUniform4fv(g_lightPositionLocation, 1, g_lightPosition); // glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); return GLUS_TRUE; }
GLUSboolean init(GLUSvoid) { GLUSshape plane; GLUStextfile vertexSource; GLUStextfile fragmentSource; GLUStgaimage image; GLfloat normalMatrix[9]; glusLoadTextFile("../Example07_ES/shader/normmap.vert.glsl", &vertexSource); glusLoadTextFile("../Example07_ES/shader/normmap.frag.glsl", &fragmentSource); glusBuildProgramFromSource(&g_program, (const GLUSchar**) &vertexSource.text, (const GLUSchar**) &fragmentSource.text); glusDestroyTextFile(&vertexSource); glusDestroyTextFile(&fragmentSource); // g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix"); g_modelViewMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewMatrix"); g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix"); g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection"); // One texture for the color and one for the normals. g_textureLocation = glGetUniformLocation(g_program.program, "u_texture"); g_normalMapLocation = glGetUniformLocation(g_program.program, "u_normalMap"); g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex"); // The tangent, bitangent and normal do define the tangent space. They are defined in object space, the inverse brings coordinates back to this tangent space. g_tangentLocation = glGetAttribLocation(g_program.program, "a_tangent"); g_bitangentLocation = glGetAttribLocation(g_program.program, "a_bitangent"); g_normalLocation = glGetAttribLocation(g_program.program, "a_normal"); g_texCoordLocation = glGetAttribLocation(g_program.program, "a_texCoord"); // glusLoadTgaImage("rock_color.tga", &image); glGenTextures(1, &g_texture); glBindTexture(GL_TEXTURE_2D, g_texture); glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); glusLoadTgaImage("rock_normal.tga", &image); glGenTextures(1, &g_normalMap); glBindTexture(GL_TEXTURE_2D, g_normalMap); glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data); glusDestroyTgaImage(&image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); // glusCreatePlanef(&plane, 1.5f); g_numberIndicesPlane = plane.numberIndices; glGenBuffers(1, &g_verticesVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) plane.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_tangentsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_tangentsVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) plane.tangents, GL_STATIC_DRAW); glGenBuffers(1, &g_bitangentsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_bitangentsVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) plane.bitangents, GL_STATIC_DRAW); glGenBuffers(1, &g_normalsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) plane.normals, GL_STATIC_DRAW); glGenBuffers(1, &g_texCoordsVBO); glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 2 * sizeof(GLfloat), (GLfloat*) plane.texCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, g_numberIndicesPlane * sizeof(GLuint), (GLuint*) plane.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusDestroyShapef(&plane); // glUseProgram(g_program.program); glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO); glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexLocation); glBindBuffer(GL_ARRAY_BUFFER, g_tangentsVBO); glVertexAttribPointer(g_tangentLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_tangentLocation); glBindBuffer(GL_ARRAY_BUFFER, g_bitangentsVBO); glVertexAttribPointer(g_bitangentLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_bitangentLocation); glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO); glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_normalLocation); glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsVBO); glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_texCoordLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO); // glusLookAtf(g_viewMatrix, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, g_viewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, g_viewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // // Activate and bind first ... glUniform1i(g_textureLocation, 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, g_texture); // .. and second texture. glUniform1i(g_normalMapLocation, 1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, g_normalMap); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLint peelLayer, defaultDepth, peelDepth; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; glusMatrix4x4Identityf(modelViewMatrix); glusMatrix4x4RotateRyf(modelViewMatrix, angle); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelViewMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUseProgram(g_program.program); glBindVertexArray(g_vao); glUniformMatrix4fv(g_modelViewMatrixLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // Depth peeling passes. // see http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.18.9286&rep=rep1&type=pdf // Disable color texture ... glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, 0); for (peelLayer = 0; peelLayer < LAYERS; peelLayer++) { glUniform1i(g_layerLocation, peelLayer); peelDepth = peelLayer % 2; defaultDepth = (peelLayer + 1) % 2; // ... and activate the peeling depth texture. glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, g_depthTexture[peelDepth]); glBindFramebuffer(GL_FRAMEBUFFER, g_blendFullscreenFBO); // Now color buffer ... glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, g_colorTexture, 0, peelLayer); // ... and default depth buffer can be used as the frame buffer. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_depthTexture[defaultDepth], 0); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); return GLUS_FALSE; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); } // Fullscreen quad rendering. glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, g_colorTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); glUseProgram(g_blendFullscreenProgram.program); glBindVertexArray(g_blendFullscreenVAO); glDisable(GL_DEPTH_TEST); // Blending is happening in the shader. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glEnable(GL_DEPTH_TEST); angle += 30.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat angleRadians; GLfloat viewProjectionMatrix[16]; GLfloat viewMatrix[16]; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; GLfloat camera[4] = {0.0, 0.0, 0.0, 1.0}; angleRadians = glusDegToRadf(angle); camera[0] = g_circleRadius * -sinf(angleRadians); camera[2] = g_circleRadius * cosf(angleRadians); // Circle with the camera around the origin by looking at it. glusLookAtf(viewMatrix, camera[0], 0.0f, camera[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusMatrix4x4Multiplyf(viewProjectionMatrix, g_projectionMatrix, viewMatrix); glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, 0.0f, -0.5f, 0.0f); glusMatrix4x4RotateRxf(modelMatrix, 45.0f); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // // First render the background. glUseProgram(g_programBackground.program); glUniformMatrix4fv(g_viewProjectionMatrixBackgroundLocation, 1, GL_FALSE, viewProjectionMatrix); glUniformMatrix4fv(g_modelMatrixBackgroundLocation, 1, GL_FALSE, modelMatrix); glBindVertexArray(g_vaoBackground); glFrontFace(GL_CW); glDrawElements(GL_TRIANGLES, g_numberIndicesBackground, GL_UNSIGNED_INT, 0); // Now render the sphere. glUseProgram(g_program.program); glUniformMatrix4fv(g_viewProjectionMatrixLocation, 1, GL_FALSE, viewProjectionMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4fv(g_cameraLocation, 1, camera); glBindVertexArray(g_vao); glFrontFace(GL_CCW); glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0); // Increase the angle 30 degree per second. angle += 30.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat depthPassMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat viewMatrix[16]; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; // Rendering into the depth pass texture. glBindTexture(GL_TEXTURE_2D, 0); // Setup for the framebuffer. glBindFramebuffer(GL_FRAMEBUFFER, g_fbo); glViewport(0, 0, g_depthPassTextureSize, g_depthPassTextureSize); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glusMatrix4x4LookAtf(viewMatrix, g_lightPosition[0], g_lightPosition[1], g_lightPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusMatrix4x4Multiplyf(depthPassMatrix, g_depthPassMatrix, viewMatrix); glClear(GL_DEPTH_BUFFER_BIT); glUseProgram(g_programDepthPass.program); // Render the Dragon. glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle); // Upscaling a little bit avoids artifacts. glusMatrix4x4Scalef(modelMatrix, 1.05f, 1.05f, 1.05f); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glUniformMatrix4fv(g_modelViewMatrixDepthPassLocation, 1, GL_FALSE, modelViewMatrix); glBindVertexArray(g_vaoDepthPass); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); // Revert for the scene. glBindFramebuffer(GL_FRAMEBUFFER, 0); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glViewport(0, 0, g_width, g_height); glBindTexture(GL_TEXTURE_2D, g_depthPassTexture); // // Render the scene. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(g_program.program); glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix); glUniformMatrix4fv(g_depthPassMatrixLocation, 1, GL_FALSE, depthPassMatrix); // Dragon glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4f(g_diffuseColorLocation, 0.8f, 0.0f, 0.0f, 1.0f); glUniform4f(g_scatterColorLocation, 0.8f, 0.8f, 0.0f, 1.0f); glUniform2f(g_nearFarLocation, g_near, g_far); glUniform1f(g_wrapLocation, g_wrap); glUniform1f(g_scatterWidthLocation, g_scatterWidth); glUniform1f(g_scatterFalloffLocation, g_scatterFalloff); glBindVertexArray(g_vao); glDrawArrays(GL_TRIANGLES, 0, g_numberVertices); // angle += 20.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat modelViewMatrix[16]; GLfloat viewMatrix[16]; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; // Render the scene. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glUseProgram(g_program.program); glusLookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix); // Draw Color glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); // Plane glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, -5.0f); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4f(g_colorLocation, 0.0f, 0.5f, 0.0f, 1.0f); glBindVertexArray(g_vaoPlane); glDrawElements(GL_TRIANGLES, g_numberIndicesPlane, GL_UNSIGNED_INT, 0); // Torus glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4f(g_colorLocation, 0.33f, 0.0f, 0.5f, 1.0f); glBindVertexArray(g_vao); glDrawElements(GL_TRIANGLES_ADJACENCY, g_numberIndices, GL_UNSIGNED_INT, 0); // Draw Shadow Volume // Using zfail see http://joshbeam.com/articles/stenciled_shadow_volumes_in_opengl/ glEnable(GL_STENCIL_TEST); glUseProgram(g_programShadowVolume.program); glUniformMatrix4fv(g_viewMatrixShadowVolumeLocation, 1, GL_FALSE, viewMatrix); glUniformMatrix4fv(g_modelMatrixShadowVolumeLocation, 1, GL_FALSE, modelMatrix); // Only render to the stencil buffer glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthMask(GL_FALSE); // Avoid ugly artifacts glEnable(GL_POLYGON_OFFSET_FILL); // Needed, as vertices in the back are extruded to infinity glEnable(GL_DEPTH_CLAMP); glBindVertexArray(g_vaoShadowVolume); // Render the back faces ... glCullFace(GL_FRONT); glStencilFunc(GL_ALWAYS, 0x0, 0xff); glStencilOp(GL_KEEP, GL_INCR, GL_KEEP); glDrawElements(GL_TRIANGLES_ADJACENCY, g_numberIndices, GL_UNSIGNED_INT, 0); // ... and then the front faces glCullFace(GL_BACK); glStencilFunc(GL_ALWAYS, 0x0, 0xff); glStencilOp(GL_KEEP, GL_DECR, GL_KEEP); glDrawElements(GL_TRIANGLES_ADJACENCY, g_numberIndices, GL_UNSIGNED_INT, 0); // Reset glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); glDisable(GL_POLYGON_OFFSET_FILL); glDisable(GL_DEPTH_CLAMP); // Draw shadow by blending a black, half transparent plane glUseProgram(g_programShadowPlane.program); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); // Only render, where the stencil buffer is not 0 glStencilFunc(GL_NOTEQUAL, 0x0, 0xff); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); glBindVertexArray(g_vaoShadowPlane); glDrawElements(GL_TRIANGLES, g_numberIndicesShadowPlane, GL_UNSIGNED_INT, 0); glDisable(GL_BLEND); glDisable(GL_STENCIL_TEST); // angle += 20.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat biasMatrix[] = { 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f }; static GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 }; static GLfloat angle = 0.0f; GLfloat viewMatrix[16]; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; GLUSgroupList* groupWalker; // // Do the deferred shading by rendering into the geometry buffers. // glBindFramebuffer(GL_FRAMEBUFFER, g_dsFBO); glDrawBuffers(4, drawBuffers); reshape(TEXTURE_WIDTH, TEXTURE_HEIGHT); glUseProgram(g_programDeferredShading.program); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // // Just pass the projection matrix. The final matrix is calculated in the shader. glUniformMatrix4fv(g_projectionMatrixLocation, 1, GL_FALSE, g_projectionMatrix); // Orbit camera around models glusLookAtf(viewMatrix, sinf(angle) * 10.0f, 4.0f, cosf(angle) * 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix); // glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, -(float)OBJECTS_COLUMNS / 2.0f + 0.5f, 0.0f, (float)OBJECTS_COLUMNS / 2.0f - 0.5f); // Scale the model up glusMatrix4x4Scalef(modelMatrix, 10.0f, 10.0f, 10.0f); // Uniform scale, so extracting is sufficient glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); // glActiveTexture(GL_TEXTURE0); groupWalker = g_wavefront.groups; while (groupWalker) { // Set up material values. glUniform4fv(g_material.diffuseColorLocation, 1, groupWalker->group.material->diffuse); glUniform4fv(g_material.specularColorLocation, 1, groupWalker->group.material->specular); glUniform1f(g_material.specularExponentLocation, groupWalker->group.material->shininess); // Enable only texturing, if the material has a texture if (groupWalker->group.material->diffuseTextureName) { glUniform1i(g_useTextureLocation, 1); glUniform1i(g_material.diffuseTextureLocation, 0); glBindTexture(GL_TEXTURE_2D, groupWalker->group.material->diffuseTextureName); } else { glUniform1i(g_useTextureLocation, 0); glUniform1i(g_material.diffuseTextureLocation, 0); glBindTexture(GL_TEXTURE_2D, 0); } glBindVertexArray(groupWalker->group.vao); glDrawElementsInstanced(GL_TRIANGLES, groupWalker->group.numberIndices, GL_UNSIGNED_INT, 0, OBJECTS_COLUMNS * OBJECTS_COLUMNS); groupWalker = groupWalker->next; } // // Render now to display framebuffer. // glBindFramebuffer(GL_FRAMEBUFFER, 0); glDrawBuffer(GL_COLOR_ATTACHMENT0); reshape(TEXTURE_WIDTH, TEXTURE_HEIGHT); glUseProgram(g_programPointLight.program); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUniformMatrix4fv(g_projectionMatrixPointLightLocation, 1, GL_FALSE, g_projectionMatrix); glUniformMatrix4fv(g_viewMatrixPointLightLocation, 1, GL_FALSE, viewMatrix); // glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, -7.0f, 0.0f, 7.0f); glUniformMatrix4fv(g_modelMatrixPointLightLocation, 1, GL_FALSE, modelMatrix); // // Update position matrix of the point lights. Each matrix filed is used to calculate the final position. for (GLint i = 0; i < POINT_LIGHT_COUNT; i++) { g_positionMatrix[i] += g_directionMatrix[i] * time * 5.0f; if (g_positionMatrix[i] > (float)POINT_LIGHT_COUNT - 2.0f) { g_positionMatrix[i] = ((float)POINT_LIGHT_COUNT - 2.0f) - (g_positionMatrix[i] - ((float)POINT_LIGHT_COUNT - 2.0f)); g_directionMatrix[i] *= -1.0f; } else if (g_positionMatrix[i] < 0.0f) { g_positionMatrix[i] = -g_positionMatrix[i]; g_directionMatrix[i] *= -1.0f; } } glUniformMatrix4fv(g_positionMatrixPointLightLocation, 1, GL_FALSE, g_positionMatrix); glUniformMatrix4fv(g_biasMatrixPointLightLocation, 1, GL_FALSE, biasMatrix); glUniform1f(g_radiusPointLightLocation, POINT_LIGHT_RADIUS); // Blend, as color is accumulated for each point light. glEnable(GL_BLEND); // No depth test, as the complete sphere of the point light should be processed. glDisable(GL_DEPTH_TEST); // Enable all the geometry buffer textures. glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, g_dsDiffuseTexture); glUniform1i(g_diffusePointLightLocation, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, g_dsSpecularTexture); glUniform1i(g_specularPointLightLocation, 1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, g_dsPositionTexture); glUniform1i(g_positionPointLightLocation, 2); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, g_dsNormalTexture); glUniform1i(g_normalPointLightLocation, 3); glBindVertexArray(g_vaoPointLight); glDrawElementsInstanced(GL_TRIANGLES, g_numberIndicesPointLight, GL_UNSIGNED_INT, 0, POINT_LIGHT_COUNT); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, 0); // // 20 seconds for one turn angle += 2.0f * GLUS_PI * time / 20.0f; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat modelMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat normalMatrix[9]; GLfloat planeMatrix[16]; GLfloat clippingPlane[4] = {0.0f, 0.0f, -1.0f, 0.0f}; GLfloat planeBecauseOfBug[4] = {0.0f, -1.0f, 0.0f, 100.0f}; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clipping plane glusMatrix4x4Identityf(planeMatrix); glusMatrix4x4RotateRyf(planeMatrix, angle); glusMatrix4x4Translatef(planeMatrix, 0.0f, 0.25, 0.25f); glusMatrix4x4RotateRxf(planeMatrix, -30.0f); // For planes, we do need the inverse, transposed matrix. glusMatrix4x4InverseRigidBodyf(planeMatrix); glusMatrix4x4Transposef(planeMatrix); glusMatrix4x4MultiplyPlanef(clippingPlane, planeMatrix, clippingPlane); glUniform4fv(g_planeLocation, 1, clippingPlane); // Sphere glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRyf(modelMatrix, angle); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glBindVertexArray(g_vao); // Always pass. The 0s are not used. glStencilFunc(GL_ALWAYS, 0, 0); // Visible elements gets twice inverted, because of the front and back face. So the value is 0. // The clipped area only gets inverted by the back face. The front face is clipped, so no second invert is done. The final value is 255 (assume an 8bit stencil buffer). glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT); glEnable(GL_CLIP_DISTANCE0); glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0); // Plane // Use the model matrix from the sphere and add the plane transforms. glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.25, 0.25f); glusMatrix4x4RotateRxf(modelMatrix, -30.0f); glusMatrix4x4Multiplyf(modelViewMatrix, g_viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glBindVertexArray(g_planeVAO); // Render only, where 255 & 1 = 1 is set. glStencilFunc(GL_EQUAL, 1, 1); glDisable(GL_CLIP_DISTANCE0); // Note: Bug in the AMD driver. The above disable is not working: "Values written into gl_ClipDistance for planes that are not enabled have no effect." // For a workaround, I am setting a far plane, which does not clip anything. glUniform4fv(g_planeLocation, 1, planeBecauseOfBug); glDrawElements(GL_TRIANGLES, g_numberIndicesPlane, GL_UNSIGNED_INT, 0); angle += 45.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat modelViewMatrix[16]; GLfloat viewMatrix[16]; GLfloat shadowProjectionMatrix[16]; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; // This shadow plane represents mathematically the background plane GLfloat shadowPlane[4] = {0.0f, 0.0f, 1.0f, 5.0f}; glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // // Render the scene. // glUseProgram(g_program.program); glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix); // Background Plane glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, -5.0f); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4f(g_colorLocation, 0.0f, 0.5f, 0.0f, 1.0f); glBindVertexArray(g_vaoBackground); glDrawElements(GL_TRIANGLES, g_numberIndicesBackground, GL_UNSIGNED_INT, 0); // // Render the planar shadow // glUseProgram(g_programShadow.program); glUniformMatrix4fv(g_viewMatrixShadowLocation, 1, GL_FALSE, viewMatrix); // Torus projected as a shadow glusMatrix4x4PlanarShadowDirectionalLightf(shadowProjectionMatrix, shadowPlane, g_lightDirection); glUniformMatrix4fv(g_shadowProjectionMatrixShadowLocation, 1, GL_FALSE, shadowProjectionMatrix); glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle); glUniformMatrix4fv(g_modelMatrixShadowLocation, 1, GL_FALSE, modelMatrix); glBindVertexArray(g_vaoShadow); // Overwrite the background plane glDisable(GL_DEPTH_TEST); glDrawElements(GL_TRIANGLES, g_numberIndicesTorus, GL_UNSIGNED_INT, 0); glEnable(GL_DEPTH_TEST); // Torus with color glUseProgram(g_program.program); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4f(g_colorLocation, 0.33f, 0.0f, 0.5f, 1.0f); glBindVertexArray(g_vao); glDrawElements(GL_TRIANGLES, g_numberIndicesTorus, GL_UNSIGNED_INT, 0); // angle += 20.0f * time; return GLUS_TRUE; }
GLUSboolean update(GLUSfloat time) { static GLfloat angle = 0.0f; GLfloat shadowMatrix[16]; GLfloat modelViewMatrix[16]; GLfloat viewMatrix[16]; GLfloat modelMatrix[16]; GLfloat normalMatrix[9]; // Rendering into the shadow texture. glBindTexture(GL_TEXTURE_2D, 0); // Setup for the framebuffer. glBindFramebuffer(GL_FRAMEBUFFER, g_fbo); glViewport(0, 0, g_shadowTextureSize, g_shadowTextureSize); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glusMatrix4x4LookAtf(viewMatrix, g_lightPosition[0], g_lightPosition[1], g_lightPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glusMatrix4x4Multiplyf(shadowMatrix, g_shadowMatrix, viewMatrix); glClear(GL_DEPTH_BUFFER_BIT); glUseProgram(g_programShadow.program); // Render the torus. glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glUniformMatrix4fv(g_modelViewMatrixShadowLocation, 1, GL_FALSE, modelViewMatrix); glBindVertexArray(g_vaoShadow); glEnable(GL_POLYGON_OFFSET_FILL); glFrontFace(GL_CW); glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0); glDisable(GL_POLYGON_OFFSET_FILL); glFrontFace(GL_CCW); // Revert for the scene. glBindFramebuffer(GL_FRAMEBUFFER, 0); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glViewport(0, 0, g_width, g_height); glBindTexture(GL_TEXTURE_2D, g_shadowTexture); // // Render the scene. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(g_program.program); glusMatrix4x4LookAtf(viewMatrix, g_cameraPosition[0], g_cameraPosition[1], g_cameraPosition[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_viewMatrixLocation, 1, GL_FALSE, viewMatrix); glUniformMatrix4fv(g_shadowMatrixLocation, 1, GL_FALSE, shadowMatrix); // Plane glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4Translatef(modelMatrix, 0.0f, 0.0f, -5.0f); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4f(g_colorLocation, 0.0f, 0.5f, 0.0f, 1.0f); glBindVertexArray(g_vaoBackground); glDrawElements(GL_TRIANGLES, g_numberIndicesBackground, GL_UNSIGNED_INT, 0); // Torus glusMatrix4x4Identityf(modelMatrix); glusMatrix4x4RotateRzRxRyf(modelMatrix, 0.0f, 0.0f, angle); glusMatrix4x4Multiplyf(modelViewMatrix, viewMatrix, modelMatrix); glusMatrix4x4ExtractMatrix3x3f(normalMatrix, modelViewMatrix); glUniformMatrix4fv(g_modelMatrixLocation, 1, GL_FALSE, modelMatrix); glUniformMatrix3fv(g_normalMatrixLocation, 1, GL_FALSE, normalMatrix); glUniform4f(g_colorLocation, 0.33f, 0.0f, 0.5f, 1.0f); glBindVertexArray(g_vao); glDrawElements(GL_TRIANGLES, g_numberIndicesSphere, GL_UNSIGNED_INT, 0); // angle += 20.0f * time; return GLUS_TRUE; }