const bool Test_Matrix_PerspectiveMatrix() { const TMatrix4d kAd = PerspectiveMatrix(TMatrix4d(), s_kdTau / 2.0, 1.0, 2.0, 4.0); const TMatrix4d kBd = PerspectiveMatrix(TMatrix4d(), -2.0, 2.0, -2.0, 2.0, 4.0, 2.0); const bool kbPass_d = Equal(kAd, kBd, 0.01); const TMatrix4f kAf = PerspectiveMatrix(TMatrix4f(), s_kfTau / 2.0f, 1.0f, 2.0f, 4.0f); const TMatrix4f kBf = PerspectiveMatrix(TMatrix4f(), -2.0f, 2.0f, -2.0f, 2.0f, 4.0f, 2.0f); const bool kbPass_f = Equal(kAf, kBf, 0.01f); return(kbPass_d && kbPass_f); }
void RenderGL(void) { //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== #ifdef __APPLE__ glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer); #endif //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== //========================================================================================================================================================= glClearColor( 0.0f, 0.0f, 0.0f, 1.0 ); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //---------------------------------------------------------------------------------- glViewport(0, 0, (GLsizei)viewWidth, (GLsizei)viewHeight); PerspectiveMatrix(projection, fieldOfView, viewWidth / viewHeight, 0.1, 1000); //========================================================================================================================================================= #ifdef __APPLE__ Rotate(projection, 0.0, 0.0, 1.0, -90.0);//_____FOR_iOS_LANDSCAPE_VIEW #endif //========================================================================================================================================================= //projection[12] = eyePosition[1]; //projection[13] = eyePosition[0]; //projection[15] = eyePosition[2]; //Rotate(projection, 1.0, 0.0, 0.0, look_UP_DOWN); //Rotate(projection, 0.0, 1.0, 0.0, -look_LEFT_RIGHT); rotateModel += 0.1; //glEnable(GL_BLEND); //==================================================================================== #include "_MODEL_FOLDERS_/facing_UP/facing_UP_Render.cpp" //==================================================================================== #include "_MODEL_FOLDERS_/facing_VIEW/facing_VIEW_Render.cpp" //==================================================================================== //glDisable(GL_BLEND); }//_END_RenderGL()
matrix4f VP() { V = PerspectiveMatrix(float(50.0), float((XRES+0.1)/(YRES+0.1)), float(1), float(1000) ); P.set_translate( vec3f( -position.x ,-position.y ,-position.z ) ); rotation.get_value(R); return V*R*P; }
void RenderGL(void) { //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== #ifdef __APPLE__ glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer); #endif //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== //========================================================================================================================================================= glClearColor( 0.0f, 0.0f, 0.0f, 0.5f ); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //---------------------------------------------------------------------------------- glViewport(0, 0, (GLsizei)viewWidth, (GLsizei)viewHeight); PerspectiveMatrix(projection, 35,viewWidth / viewHeight, 0.1, 1000); //========================================================================================================================================================= #ifdef __APPLE__ Rotate(projection, 0.0, 0.0, 1.0, -90.0);//_____FOR_iOS_LANDSCAPE_VIEW #endif //========================================================================================================================================================= rotateModel += 0.1; //==================================================================================== #include "_MODEL_FOLDERS_/facing_UP/facing_UP_Render.cpp" //==================================================================================== #include "_MODEL_FOLDERS_/facing_VIEW/facing_VIEW_Render.cpp" //==================================================================================== }//_END_RenderGL()
void RenderGL(void) { glClearColor(0.0, 0.0, 0.0, 0.5); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, viewWidth, viewHeight); LoadIdentity(projection); PerspectiveMatrix(projection, fieldOfView, (GLfloat)viewWidth / (GLfloat)viewHeight, adjustNearFar[0], adjustNearFar[1]); //Rotate(projection, 0.0, 0.0, 1.0, 90.0);//_____FOR_iOS_LANDSCAPE_VIEW //==================================================================================== shaderNumber = 46; #include "_MODEL_FOLDERS_/sphere/sphere_RENDER.cpp" //==================================================================================== }
void Scene::DrawScene(ParticleSystem* m, double strainSize, bool drawPoints) { int pSize; int cSize; //float* points = m->GetPositions3d(&pSize); //float* colors = m->GetColors(&cSize, strainSize); float *points, *colors; switch(drawMode) { case 0: points = m->GetSurfaceTriangles3d(&pSize); colors = m->GetTriColors(&cSize, strainSize); break; case 1: points = m->GetPositions3d(&pSize, prevMode); colors = m->GetColors(&cSize, strainSize, xpos, ypos, zpos); break; case 2: points = m->GetSurfaceTriangles3d(&pSize); colors = m->GetStrainSurfaceTriColors(&cSize, strainSize); break; case 3: points = m->GetTetCenter(&pSize); colors = m->GetCenterColors(&cSize, strainSize); break; case 4: points = m->GetAllTriangles3d(&pSize); colors = m->GetStrainAllTriColors(&cSize, strainSize); break; } Eigen::Matrix4f rotationMatrix; Eigen::Matrix4f projectionMatrix; rotationMatrix.setZero(); projectionMatrix.setZero(); Eigen::Vector3f pos, target, up; pos << xpos, ypos, zpos; m->GetCameraPosAndSize(&xtarg, &ytarg, &ztarg); target << xtarg, ytarg, ztarg; up << 0, -1, 0; RotationMatrix(pos, target, up, rotationMatrix); PerspectiveMatrix((65*PI)/180.0, ((float)DDWIDTH)/DDHEIGHT, .5, 100, projectionMatrix); g_viewMatrix = projectionMatrix * rotationMatrix; DrawDelegate::BeginFrame(); DrawDelegate::SetViewMatrix(g_viewMatrix.data()); Scene::DrawGrid(1, m->groundLevel); if (drawPoints) { DrawDelegate::SetLineSize(3); switch(drawMode) { case 0: DrawDelegate::DrawTriangles(points, pSize, colors, cSize); break; case 1: DrawDelegate::DrawLines(points, pSize, colors, cSize); break; case 2: DrawDelegate::DrawTriangles(points, pSize, colors, cSize); break; case 3: DrawDelegate::DrawPoints(points, pSize, colors, cSize); break; case 4: DrawDelegate::DrawTriangles(points, pSize, colors, cSize); break; } } }
void RenderGL(void) { //======================================================_SHADOWS_BEGIN_=========================================================================== //======================================================_SHADOWS_BEGIN_=========================================================================== //======================================================_SHADOWS_BEGIN_=========================================================================== //======================================================_SHADOWS_BEGIN_=========================================================================== //============================================================================================================================= glBindFramebuffer(GL_FRAMEBUFFER, fboId_2D);//-----------------------F.B.O. //---------------------------------------------------------------- //glColor4f(color is changed using fragment program) glClearColor(1.0, 1.0, 1.0, 0.0); glClear (GL_COLOR_BUFFER_BIT); glViewport(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT); //---------------------------------------------------------------- glDisable(GL_BLEND); glDisable(GL_DEPTH_TEST); PerspectiveMatrix(ProjectionShadow, 45, 1.0, 0.001, 10000.0); Rotate(ProjectionShadow, 0.0, 1.0, 0.0, 20.0); Rotate(ProjectionShadow, 1.0, 0.0, 0.0, 45.0); glUseProgram(SHADER_shadow);//---------<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< //############################################################################################################################################################|__SHADOW_01 //============================================================================================================================================================|__LAYER__01 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE); //===================================================== darkness_shadow[0] = 0.5; darkness_shadow[1] = 0.5; darkness_shadow[2] = 0.5; //#include "_MODEL_FOLDERS_/flowerShield/flowerShield_Shadow_01.cpp" //==================================================================================== //#include "_MODEL_FOLDERS_/flowerShield/flowerShield_Shadow_00.cpp" //============================================================================================================================================================|__LAYER__02 glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE); //=========================================================== //--------------------------------------------------------- //############################################################################################################################################################|__SHADOW_02 //============================================================================================================================================================|__LAYER__01 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE); //=========================================================== darkness_shadow[0] = 0.3; darkness_shadow[1] = 0.3; darkness_shadow[2] = 0.3; //============================================================================================================================================================|__LAYER__02 glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE); //=========================================================== //------------------------------------------------------------------------------------------------------------------------------------------------------------------- LoadIdentity(textureMatrix); AssignMatrix(textureMatrix, ProjectionShadow); //=========================================================== glEnable(GL_DEPTH_TEST); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glBindFramebuffer(GL_FRAMEBUFFER, 0); //======================================================_SHADOWS_END_============================================================================= //======================================================_SHADOWS_END_============================================================================= //======================================================_SHADOWS_END_============================================================================= //======================================================_SHADOWS_END_============================================================================= //################################################################################################################################################################################################## //################################################################################################################################################################################################## //################################################################################################################################################################################################## //################################################################################################################################################################################################## //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== #ifdef __APPLE__ glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer); #endif //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== //========================================================================================================================================================= glClearColor( 0.0f, 0.0f, 0.0f, 0.5f ); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //---------------------------------------------------------------------------------- glViewport(0, 0, viewWidth, viewHeight); PerspectiveMatrix(projection, 45,(float)viewWidth / (float)viewHeight, 0.1, 10000); //========================================================================================================================================================= #ifdef __APPLE__ Rotate(projection, 0.0, 0.0, 1.0, -90.0);//_____FOR_iOS_LANDSCAPE_VIEW projection[12] = eyePosition[1]; projection[13] = eyePosition[0]; projection[15] = eyePosition[2]; #endif //========================================================================================================================================================= #ifdef WIN32 projection[12] = eyePosition[0]; projection[13] = eyePosition[1]; projection[15] = eyePosition[2]; #endif Rotate(projection, 1.0, 0.0, 0.0, look_UP_DOWN); Rotate(projection, 0.0, 1.0, 0.0, -look_LEFT_RIGHT); //################################################################################################################################################################# //==================================================================================== #include "_MODEL_FOLDERS_/flowerShield/flowerShield_Render.cpp" //==================================================================================== #include "_MODEL_FOLDERS_/sculptris/sculptris_Render.cpp" //==================================================================================== #include "_MODEL_FOLDERS_/sphere/sphere_Render.cpp" #ifdef WIN32 SwapBuffers( hDC ); #endif }
// Draw the scene to the screen void draw() { // Update time time += 2.0f * timeStep; /////////////////////// PART 1: SIMULATION ///////////////////////////////// // Grab buffers for OpenCL acquireGLBuffer(particles.particleBuffer[particles.currentBuffer]); acquireGLBuffer(particles.particleBuffer[1 - particles.currentBuffer]); // Prepare to run some kernels cl_int numParticles = NUM_PARTICLES; cl_int gridElements = GRID_SIZE * GRID_SIZE * GRID_SIZE; cl_uint workSize[3] = {numParticles, 0, 0}; cl_uint gridWorkSize[3] = {gridElements, 0, 0}; cl_uint workgroupSize[3] = {256, 0, 0}; // Clear grid clSetKernelArg(openCLKernels.gridClearKernel, 0, sizeof(cl_mem), &particles.gridSizeBuffer); clSetKernelArg(openCLKernels.gridClearKernel, 1, sizeof(cl_int), &gridElements); clRunKernel(openCLKernels.gridClearKernel, gridWorkSize, workgroupSize); // Compute grid positions clSetKernelArg(openCLKernels.gridKernel, 0, sizeof(cl_mem), &particles.particleBuffer[particles.currentBuffer]); clSetKernelArg(openCLKernels.gridKernel, 1, sizeof(cl_mem), &particles.offsetBuffer); clSetKernelArg(openCLKernels.gridKernel, 2, sizeof(cl_mem), &particles.gridSizeBuffer); clSetKernelArg(openCLKernels.gridKernel, 3, sizeof(cl_int), &numParticles); clRunKernel(openCLKernels.gridKernel, workSize, workgroupSize); // Compute prefix sum for grid clSetKernelArg(openCLKernels.prefixSumKernel, 2, sizeof(cl_uint), (void*)&gridElements); int pingpong = 0; for(cl_int offset = 1; offset <= gridElements; offset *= 2) { if(offset == 1) { clSetKernelArg(openCLKernels.prefixSumKernel, 0, sizeof(cl_mem), (void*)&particles.gridSizeBuffer); } else { clSetKernelArg(openCLKernels.prefixSumKernel, 0, sizeof(cl_mem), (void*)&(pingpong == 0 ? particles.gridBuffer[0] : particles.gridBuffer[1])); } clSetKernelArg(openCLKernels.prefixSumKernel, 1, sizeof(cl_mem), (void*)&(pingpong == 0 ? particles.gridBuffer[1] : particles.gridBuffer[0])); clSetKernelArg(openCLKernels.prefixSumKernel, 3, sizeof(cl_int), (void*)&offset); clRunKernel(openCLKernels.prefixSumKernel, gridWorkSize, workgroupSize); pingpong = 1 - pingpong; } // Reorganize particles clSetKernelArg(openCLKernels.gridReorderKernel, 0, sizeof(cl_mem), &particles.particleBuffer[particles.currentBuffer]); clSetKernelArg(openCLKernels.gridReorderKernel, 1, sizeof(cl_mem), &particles.particleBuffer[1 - particles.currentBuffer]); clSetKernelArg(openCLKernels.gridReorderKernel, 2, sizeof(cl_mem), &particles.velocityBuffer[particles.currentBuffer]); clSetKernelArg(openCLKernels.gridReorderKernel, 3, sizeof(cl_mem), &particles.velocityBuffer[1 - particles.currentBuffer]); clSetKernelArg(openCLKernels.gridReorderKernel, 4, sizeof(cl_mem), &particles.offsetBuffer); clSetKernelArg(openCLKernels.gridReorderKernel, 5, sizeof(cl_mem), (void*)&particles.gridSizeBuffer); clSetKernelArg(openCLKernels.gridReorderKernel, 6, sizeof(cl_mem), (void*)&(pingpong == 0 ? particles.gridBuffer[0] : particles.gridBuffer[1])); clSetKernelArg(openCLKernels.gridReorderKernel, 7, sizeof(cl_int), &numParticles); clRunKernel(openCLKernels.gridReorderKernel, workSize, workgroupSize); particle* testData = (particle*)malloc(sizeof(cl_float) * numParticles * 4); // Swap particle buffers particles.currentBuffer = 1 - particles.currentBuffer; // Send new cell select buffer int cellSelect[27]; for(int i = 0; i < 27; i++) { cellSelect[i] = i; } shuffle(cellSelect, 27); clEnqueueWriteBuffer(clCommandQueue(), particles.cellSelectBuffer, true, 0, 27 * sizeof(cl_int), cellSelect, 0, 0, 0); clFinish(clCommandQueue()); // Recalculate densities and normalized pressure derivatives clSetKernelArg(openCLKernels.dataKernel, 0, sizeof(cl_mem), &particles.particleBuffer[particles.currentBuffer]); clSetKernelArg(openCLKernels.dataKernel, 1, sizeof(cl_mem), &particles.dataBuffer); clSetKernelArg(openCLKernels.dataKernel, 2, sizeof(cl_mem), (void*)&particles.gridSizeBuffer); clSetKernelArg(openCLKernels.dataKernel, 3, sizeof(cl_mem), (void*)&(pingpong == 0 ? particles.gridBuffer[1] : particles.gridBuffer[0])); clSetKernelArg(openCLKernels.dataKernel, 4, sizeof(cl_mem), (void*)&particles.cellSelectBuffer); clSetKernelArg(openCLKernels.dataKernel, 5, sizeof(cl_int), &numParticles); clRunKernel(openCLKernels.dataKernel, workSize, workgroupSize); // Send new cell select buffer cellSelect[27]; for(int i = 0; i < 27; i++) { cellSelect[i] = i; } shuffle(cellSelect, 27); clEnqueueWriteBuffer(clCommandQueue(), particles.cellSelectBuffer, true, 0, 27 * sizeof(cl_int), cellSelect, 0, 0, 0); clFinish(clCommandQueue()); // Integrate position float dT = timeStep; clSetKernelArg(openCLKernels.simulationKernel, 0, sizeof(cl_mem), &particles.particleBuffer[particles.currentBuffer]); clSetKernelArg(openCLKernels.simulationKernel, 1, sizeof(cl_mem), &particles.particleBuffer[1 - particles.currentBuffer]); clSetKernelArg(openCLKernels.simulationKernel, 2, sizeof(cl_mem), &particles.velocityBuffer[particles.currentBuffer]); clSetKernelArg(openCLKernels.simulationKernel, 3, sizeof(cl_mem), &particles.velocityBuffer[1 - particles.currentBuffer]); clSetKernelArg(openCLKernels.simulationKernel, 4, sizeof(cl_mem), &particles.dataBuffer); clSetKernelArg(openCLKernels.simulationKernel, 5, sizeof(cl_mem), (void*)&particles.gridSizeBuffer); clSetKernelArg(openCLKernels.simulationKernel, 6, sizeof(cl_mem), (void*)&(pingpong == 0 ? particles.gridBuffer[1] : particles.gridBuffer[0])); clSetKernelArg(openCLKernels.simulationKernel, 7, sizeof(cl_mem), (void*)&particles.cellSelectBuffer); clSetKernelArg(openCLKernels.simulationKernel, 8, sizeof(cl_float), &dT); clSetKernelArg(openCLKernels.simulationKernel, 9, sizeof(cl_float), &time); clSetKernelArg(openCLKernels.simulationKernel, 10, sizeof(cl_int), &numParticles); clSetKernelArg(openCLKernels.simulationKernel, 11, sizeof(cl_mem), &particles.terrainBuffer); PaddedVector windDir = PadVector( TransformVector(YAxisRotationMatrix(particles.windAngle), MakeVector(1.0f, 0.0f, 0.0f)) ); clSetKernelArg(openCLKernels.simulationKernel, 12, sizeof(cl_float) * 4, &windDir); clSetKernelArg(openCLKernels.simulationKernel, 13, sizeof(cl_float), &particles.windPower); clRunKernel(openCLKernels.simulationKernel, workSize, workgroupSize); // Release buffers back to OpenGL releaseGLBuffer(particles.particleBuffer[particles.currentBuffer]); releaseGLBuffer(particles.particleBuffer[1 - particles.currentBuffer]); // Swap particle buffers particles.currentBuffer = 1 - particles.currentBuffer; //////////////////////// PART 2: RENDERIING //////////////////////////////// // Clear everything first thing. glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glBindFramebuffer(GL_FRAMEBUFFER, framebuffers.backgroundFBO); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Activate shader. glUseProgram(objectShader.shaderProgram); // Set projection Matrix projection = PerspectiveMatrix( 45.0f, (float)WINDOW_WIDTH/(float)WINDOW_HEIGHT, 0.01f, 100.0f ); MatrixAsUniform(objectShader.projectionMatrix, projection); // Vertices glBindBuffer(GL_ARRAY_BUFFER, terrain.vertexBuffer); glVertexAttribPointer( objectShader.vertexPosition, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (void*)0 ); glEnableVertexAttribArray(objectShader.vertexPosition); // Set modelview according to camera Matrix rot = lookatMatrix(camera.pos, VectorAdd(camera.pos, camera.front), camera.up); rot = MatrixMul(RotationMatrix(camera.elevation, MakeVector(1, 0, 0)), rot); Matrix trans = TranslationMatrix(-camera.pos.x, -camera.pos.y, -camera.pos.z); Matrix modelview = MatrixMul(rot, trans); MatrixAsUniform(objectShader.modelviewMatrix, modelview); // Normal view matrix - inverse transpose of modelview. Matrix normalview = MatrixTranspose(FastMatrixInverse(modelview)); MatrixAsUniform(objectShader.normalviewMatrix, normalview); // Set heightmap texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, terrain.heightTexture ); glUniform1i(objectShader.terrainTexture, 0); // Set color textures glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, terrain.lowTexture); glUniform1i(objectShader.lowTexture, 1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, terrain.highTexture); glUniform1i(objectShader.highTexture, 2); // Turn off culling glDisable(GL_CULL_FACE); // Send element buffer to GPU and draw. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrain.elementBuffer); glDrawElements( GL_TRIANGLES, 512 * 512 * 6, GL_UNSIGNED_INT, (void*)0 ); // Turn culling back on glEnable(GL_CULL_FACE); // Switch to low-res viewport glViewport(0, 0, WINDOW_WIDTH / RESOLUTION_DIVIDER, WINDOW_HEIGHT / RESOLUTION_DIVIDER); // Low-res projection matrix Matrix projectionLowres = PerspectiveMatrix( 45.0f, (float)(WINDOW_WIDTH / RESOLUTION_DIVIDER) / (float)(WINDOW_HEIGHT / RESOLUTION_DIVIDER), 0.01f, 100.0f ); // Activate particle depth FBO glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glBindFramebuffer(GL_FRAMEBUFFER, framebuffers.particleFBO[0]); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Activate shader glUseProgram(particleShader.shaderProgram); // Set textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, framebuffers.backgroundTexture); glUniform1i(particleShader.terrainTexture, 0); // Send uniforms MatrixAsUniform(particleShader.modelviewMatrix, modelview); MatrixAsUniform(particleShader.projectionMatrix, projectionLowres); glUniform2f(particleShader.screenSize, WINDOW_WIDTH / RESOLUTION_DIVIDER, WINDOW_HEIGHT / RESOLUTION_DIVIDER); // Bind new buffer and set up arrtibutes glBindBuffer(GL_ARRAY_BUFFER, particles.vertexBuffer[particles.currentBuffer]); glVertexAttribPointer( particleShader.vertexPosition, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (void*)0 ); glEnableVertexAttribArray(particleShader.vertexPosition); // Bind element buffer and draw particles glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, particles.elementBuffer); glDrawElements( GL_POINTS, NUM_PARTICLES, GL_UNSIGNED_INT, (void*)0 ); // Activate particle thickness FBO glBindFramebuffer(GL_FRAMEBUFFER, framebuffers.particleThicknessFBO[0]); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Activate shader glUseProgram(particleThicknessShader.shaderProgram); // Send uniforms MatrixAsUniform(particleThicknessShader.modelviewMatrix, modelview); MatrixAsUniform(particleThicknessShader.projectionMatrix, projectionLowres); glUniform2f(particleThicknessShader.screenSize, WINDOW_WIDTH / RESOLUTION_DIVIDER, WINDOW_HEIGHT / RESOLUTION_DIVIDER); // Set textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, framebuffers.backgroundTexture); glUniform1i(particleThicknessShader.terrainTexture, 0); // Bind new buffer and set up arrtibutes glBindBuffer(GL_ARRAY_BUFFER, particles.vertexBuffer[particles.currentBuffer]); glVertexAttribPointer( particleThicknessShader.vertexPosition, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (void*)0 ); glEnableVertexAttribArray(particleThicknessShader.vertexPosition); // Enable additive blending and disable depth test glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ONE); glDisable(GL_DEPTH_TEST); // Bind element buffer and draw particles, this time rendering thickness map glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, particles.elementBuffer); glDrawElements( GL_POINTS, NUM_PARTICLES, GL_UNSIGNED_INT, (void*)0 ); // Turn blending back off and depth test back on glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); // Activate particle velocity FBO glBindFramebuffer(GL_FRAMEBUFFER, framebuffers.velocityFBO); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Activate shader glUseProgram(particleVelocityShader.shaderProgram); // Send uniforms MatrixAsUniform(particleVelocityShader.modelviewMatrix, modelview); MatrixAsUniform(particleVelocityShader.projectionMatrix, projectionLowres); glUniform2f(particleVelocityShader.screenSize, WINDOW_WIDTH / RESOLUTION_DIVIDER, WINDOW_HEIGHT / RESOLUTION_DIVIDER); // Bind new buffer and set up arrtibutes glBindBuffer(GL_ARRAY_BUFFER, particles.vertexBuffer[particles.currentBuffer]); glVertexAttribPointer( particleVelocityShader.vertexPosition, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (void*)0 ); glEnableVertexAttribArray(particleVelocityShader.vertexPosition); // Bind element buffer and draw particles, this time rendering velocity map glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, particles.elementBuffer); glDrawElements( GL_POINTS, NUM_PARTICLES, GL_UNSIGNED_INT, (void*)0 ); // Curvature flow smoothing begins glUseProgram(curvatureFlowShader.shaderProgram); // Send uniforms glUniform1i(curvatureFlowShader.particleTexture, 0); glUniform2f(curvatureFlowShader.screenSize, WINDOW_WIDTH / RESOLUTION_DIVIDER, WINDOW_HEIGHT / RESOLUTION_DIVIDER); MatrixAsUniform(curvatureFlowShader.projectionMatrix, projectionLowres); // Prepare state glActiveTexture(GL_TEXTURE0); glBindBuffer(GL_ARRAY_BUFFER, screenQuad.vertexBuffer); glVertexAttribPointer( curvatureFlowShader.vertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, (void*)0 ); glEnableVertexAttribArray(curvatureFlowShader.vertexPosition); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screenQuad.elementBuffer); // Smoothing loop glDisable(GL_DEPTH_TEST); pingpong = 0; for(int i = 0; i < smoothingIterations; i++) { // Bind no FBO glBindFramebuffer(GL_FRAMEBUFFER, 0); // Bind texture glBindTexture(GL_TEXTURE_2D, framebuffers.particleTexture[pingpong]); // Activate proper FBO and clear glBindFramebuffer(GL_FRAMEBUFFER, framebuffers.particleFBO[1 - pingpong]); // Draw a quad glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0 ); // Switch buffers pingpong = 1 - pingpong; } glEnable(GL_DEPTH_TEST); // Activate particle color FBO glBindFramebuffer(GL_FRAMEBUFFER, framebuffers.particleColorFBO); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Liquid shading shader glUseProgram(liquidShadeShader.shaderProgram); // Bind and set textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, framebuffers.particleTexture[0]); glUniform1i(liquidShadeShader.particleTexture, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, framebuffers.particleThicknessTexture[0]); glUniform1i(liquidShadeShader.particleThicknessTexture, 1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, terrain.envTexture); glUniform1i(liquidShadeShader.environmentTexture, 2); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, framebuffers.particleVelocityTexture); glUniform1i(liquidShadeShader.velocityTexture, 3); // Send uniforms glUniform2f(liquidShadeShader.screenSize, WINDOW_WIDTH, WINDOW_HEIGHT); MatrixAsUniform(liquidShadeShader.modelviewMatrix, modelview); MatrixAsUniform(liquidShadeShader.projectionMatrix, projection); glUniform1i(liquidShadeShader.useThickness, useThickness); // Draw a quad glDisable(GL_DEPTH_TEST); glBindBuffer(GL_ARRAY_BUFFER, screenQuad.vertexBuffer); glVertexAttribPointer( liquidShadeShader.vertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, (void*)0 ); glEnableVertexAttribArray(liquidShadeShader.vertexPosition); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screenQuad.elementBuffer); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0 ); glEnable(GL_DEPTH_TEST); // Switch back to full-res viewport glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // Deactivate FBOs glBindFramebuffer(GL_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Compose shader glUseProgram(compositionShader.shaderProgram); // Bind and set textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, terrain.envTexture); glUniform1i(compositionShader.backgroundTexture, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, framebuffers.particleColorTexture); glUniform1i(compositionShader.particleTexture, 1); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, framebuffers.backgroundTexture); glUniform1i(compositionShader.terrainTexture, 2); // Send uniforms MatrixAsUniform(compositionShader.modelviewMatrix, modelview); // Draw a quad glDisable(GL_DEPTH_TEST); glBindBuffer(GL_ARRAY_BUFFER, screenQuad.vertexBuffer); glVertexAttribPointer( compositionShader.vertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, (void*)0 ); glEnableVertexAttribArray(compositionShader.vertexPosition); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, screenQuad.elementBuffer); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0 ); glEnable(GL_DEPTH_TEST); // Switch drawing area and displayed area. glutSwapBuffers(); }
void RenderGL(void) { //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //******************************************************************************************************************************************************* //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== #ifdef __APPLE__ glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer); #endif //======================================================================================================================================================== //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //---------------------------------------------------RETURN_TO_MAIN_FRAMEBUFFER____________________!!!!!!!!!!!!!!!!!!!!!!!!!!!! //======================================================================================================================================================== //========================================================================================================================================================= glClearColor( 0.0f, 0.0f, 0.0f, 0.5f ); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //---------------------------------------------------------------------------------- glViewport(0, 0, (GLsizei)viewWidth, (GLsizei)viewHeight); PerspectiveMatrix(projection, 35,viewWidth / viewHeight, 0.1, 300.0); //========================================================================================================================================================= #ifdef __APPLE__ Rotate(projection, 0.0, 0.0, 1.0, -90.0);//_____FOR_iOS_LANDSCAPE_VIEW //projection[12] = eyePosition[1]; //projection[13] = eyePosition[0]; //projection[15] = eyePosition[2]; #endif //========================================================================================================================================================= #ifdef WIN32 //projection[12] = eyePosition[0]; //projection[13] = eyePosition[1]; //projection[15] = eyePosition[2]; #endif Rotate(projection, 1.0, 0.0, 0.0, look_UP_DOWN); Rotate(projection, 0.0, 1.0, 0.0, -look_LEFT_RIGHT); //=========================================================================================================== rotateModel += 0.5; //==================================================================================== #include "_MODEL_FOLDERS_/relief_TristanDean/relief_TristanDean_Render.cpp" //==================================================================================== }//_END_RenderGL()