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) { // 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; }
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) { 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 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; }
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; }