void SetupBlurProg(void) { // Set the blur program as the current one glUseProgram(blurProg); // Set MVP matrix glUniformMatrix4fv(glGetUniformLocation(blurProg, "mvpMatrix"), 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); // Setup the textue units for the blur targets, these rotate every frame glUniform1i(glGetUniformLocation(blurProg, "textureUnit0"), GetBlurTarget0()); glUniform1i(glGetUniformLocation(blurProg, "textureUnit1"), GetBlurTarget1()); glUniform1i(glGetUniformLocation(blurProg, "textureUnit2"), GetBlurTarget2()); glUniform1i(glGetUniformLocation(blurProg, "textureUnit3"), GetBlurTarget3()); glUniform1i(glGetUniformLocation(blurProg, "textureUnit4"), GetBlurTarget4()); glUniform1i(glGetUniformLocation(blurProg, "textureUnit5"), GetBlurTarget5()); }
/////////////////////////////////////////////////////////////////////////////// // Render a frame. The owning framework is responsible for buffer swaps, // flushes, etc. void RenderScene(void) { static CStopWatch animationTimer; static float totalTime = 6; // To go back and forth static float halfTotalTime = totalTime/2; float seconds = animationTimer.GetElapsedSeconds() * speedFactor; float xPos = 0; // Calculate the next postion of the moving object // First perform a mod-like operation on the time as a float while(seconds > totalTime) seconds -= totalTime; // Move object position, if it's gone half way across // start bringing it back if(seconds < halfTotalTime) xPos = seconds -halfTotalTime*0.5f; else xPos = totalTime - seconds -halfTotalTime*0.5f; // First draw world to screen modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); // Marble glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(), vWhite, 0); floorBatch.Draw(); DrawWorld(0.0f, xPos); modelViewMatrix.PopMatrix(); if(bUsePBOPath) { #ifndef OPENGL_ES // First bind the PBO as the pack buffer, then read the pixels directly to the PBO glBindBuffer(GL_PIXEL_PACK_BUFFER, pixBuffObjs[0]); glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); // Next bind the PBO as the unpack buffer, then push the pixels straight into the texture glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixBuffObjs[0]); // Setup texture unit for new blur, this gets imcremented every frame glActiveTexture(GL_TEXTURE0+GetBlurTarget0() ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); #endif } else { // Grab the screen pixels and copy into local memory glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelData); // Push pixels from client memory into texture // Setup texture unit for new blur, this gets imcremented every frame glActiveTexture(GL_TEXTURE0+GetBlurTarget0() ); #ifndef OPENGL_ES glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); #else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); #endif } // Draw full screen quad with blur shader and all blur textures projectionMatrix.PushMatrix(); projectionMatrix.LoadIdentity(); projectionMatrix.LoadMatrix(orthoMatrix); modelViewMatrix.PushMatrix(); modelViewMatrix.LoadIdentity(); glDisable(GL_DEPTH_TEST); SetupBlurProg(); screenQuad.Draw(); glEnable(GL_DEPTH_TEST); modelViewMatrix.PopMatrix(); projectionMatrix.PopMatrix(); // Move to the next blur texture for the next frame AdvanceBlurTaget(); // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); UpdateFrameCount(); }
void render(GLFWwindow* window, double time) { static float tx = 0.0f; static float dx = 2500.0f; if (tx + dx*time > 3.5) { dx = -2500; tx = 3.5 - (tx + dx*time - 3.5); } if (tx + dx*time < -3.5) { dx = 2500; tx = -3.5 + (tx + dx*time + 3.5); } tx += time * dx; glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glVertexAttribPointer( 1, // attribute 1. No particular reason for 1, but must match the layout in the shader. 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); // Draw the triangle ! glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(programID); // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); // Camera matrix glm::mat4 View = glm::lookAt( glm::vec3(4, 3, 3), // Camera is at (4,3,3), in World Space glm::vec3(0, 0, 0), // and looks at the origin glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down) ); // Model matrix : an identity matrix (model will be at the origin) glm::mat4 Model = glm::translate(tx, 0.0f, 0.0f); // Changes for each model ! // Our ModelViewProjection : multiplication of our 3 matrices glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around // Get a handle for our "MVP" uniform. // Only at initialisation time. GLuint MatrixID = glGetUniformLocation(programID, "MVP"); // Send our transformation to the currently bound shader, // in the "MVP" uniform // For each model you render, since the MVP will be different (at least the M part) glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); glDrawArrays(GL_TRIANGLES, 0, 12 * 3); // Starting from vertex 0; 3 vertices total -> 1 triangle int width, height; glfwGetWindowSize(window, &width, &height); glBindBuffer(GL_PIXEL_PACK_BUFFER, pixelbuffer); glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixelbuffer); glActiveTexture(GL_TEXTURE0 + GetBlurTarget0()); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glDisable(GL_DEPTH_TEST); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, texvertexbuffer); glVertexAttribPointer( 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, texcoordbuffer); glVertexAttribPointer( 1, // attribute 1. No particular reason for 1, but must match the layout in the shader. 2, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); glUseProgram(blurprogramID); MVP = glm::mat4(); glUniformMatrix4fv(glGetUniformLocation(blurprogramID, "MVP"), 1, GL_FALSE, &MVP[0][0]); // Setup the textue units for the blur targets, these rotate every frame glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit0"), GetBlurTarget0()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit1"), GetBlurTarget1()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit2"), GetBlurTarget2()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit3"), GetBlurTarget3()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit4"), GetBlurTarget4()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit5"), GetBlurTarget5()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit6"), GetBlurTarget6()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit7"), GetBlurTarget7()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit8"), GetBlurTarget8()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit9"), GetBlurTarget9()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit10"), GetBlurTarget10()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit11"), GetBlurTarget11()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit12"), GetBlurTarget12()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit13"), GetBlurTarget13()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit14"), GetBlurTarget14()); glUniform1i(glGetUniformLocation(blurprogramID, "textureUnit15"), GetBlurTarget15()); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); AdvanceBlurTaget(); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); }