int main() { GLFWwindow * window = initTest(800, 600); // Create a game loop. double lastTime = glfwGetTime(); int frame = 0; while(!glfwWindowShouldClose(window)) { glfwPollEvents(); // Rendering glClearColor(0.0f, 0.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Finish rendering glfwSwapBuffers(window); showFPS(window, lastTime, frame); } glfwTerminate(); return 0; }
void wyDirector::drawFrame() { // process auto release pool wyClearAutoReleasePool(); // check frame rate setting, but neglect it if in screenshot mode #ifndef WY_CFLAG_SCREENSHOT_MODE if(m_maxFrameRate > 0) { int64_t now = wyUtils::currentTimeMillis(); m_savedDelta += now - m_lastFrameTime; m_lastFrameTime = now; if(m_savedDelta < m_minFrameInterval) { // sleep to save power usleep((m_minFrameInterval - m_savedDelta) * 1000); // recalculate saved delta now = wyUtils::currentTimeMillis(); m_savedDelta += now - m_lastFrameTime; m_lastFrameTime = now; m_savedDelta %= m_minFrameInterval; } else { m_savedDelta %= m_minFrameInterval; } } #endif // need check texture? if(m_needCheckTexture) { gTextureManager->invalidateAllTextures(); m_needCheckTexture = false; } // set default state glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); // if we have next scene to set, reset delta time if(!m_UIPaused) { if(m_nextScene != NULL) { m_nextDeltaTimeZero = true; } } // calculate global delta calculateDeltaTime(); // if in screenshot mode, use 60fps always #ifdef WY_CFLAG_SCREENSHOT_MODE m_delta = 1.f / 60.f; #endif if (!m_UIPaused) { // update scheduler and actions if(!m_paused) { gScheduler->tickLocked(m_delta * m_tickFactor); } // to avoid flicker, nextScene MUST be here: after tick and before draw if(m_nextScene != NULL) { gotoNextScene(); } } if(!m_paused) { // draw the scene if(m_runningScene != NULL) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_runningScene->visit(); } /* * can't move action tick before visit running scene. sometimes the internal * state may not consistent because scheduler already updated before visit. * an example is: move a particle system with an action, can see particle system * shaked consistently. */ if(!m_UIPaused) gActionManager->tick(m_delta * m_tickFactor); #ifndef WY_CFLAG_SCREENSHOT_MODE // calcuate fps or not // but don't calculate fps if in screenshot mode if(m_calculateFPS) calculateFPS(); // show fps or not // but don't show fps if in screenshot mode if(m_displayFPS) showFPS(); // make screenshot if(m_makeScreenshot) { m_makeScreenshot = false; // make screenshot wyUtils::makeScreenshot(m_screenshotPath, m_screenshotRect); // notify notifyDirectorScreenCaptured(); // free path wyFree((void*)m_screenshotPath); m_screenshotPath = NULL; } #endif // #ifndef WY_CFLAG_SCREENSHOT_MODE } // make screenshot #ifdef WY_CFLAG_SCREENSHOT_MODE char fsPath[128]; sprintf(fsPath, "/sdcard/WiEngine/%06d.png", sScreenshotFrameIndex++); const char* path = wyUtils::mapLocalPath(fsPath); wyUtils::makeScreenshot(path); wyFree((void*)path); #endif // #ifdef WY_CFLAG_SCREENSHOT_MODE // process events gEventDispatcher->processEventsLocked(); }
void MyGLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The Current Modelview Matrix glTranslatef( 0.0f, 0.0f, z ); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glBindTexture( GL_TEXTURE_2D, texture[filter*2 + 1] ); glPushMatrix(); glRotatef( xrot, 1.0f, 0.0f, 0.0f); glRotatef( yrot, 0.0f, 1.0f, 0.0f); /* Determine what object to draw */ switch( object ) { case 0: /* Draw our cube */ drawCube( ); break; case 1: /* Draw a cylinder */ glTranslatef( 0.0f, 0.0f, -1.5f ); gluCylinder( quadratic, 1.0f, 1.0f, 3.0f, 32, 32 ); break; case 2: /* Draw a sphere */ gluSphere( quadratic, 1.3f, 32, 32 ); break; case 3: /* Draw a cone */ glTranslatef( 0.0f, 0.0f, -1.5f ); gluCylinder( quadratic, 1.0f, 0.0f, 3.0f, 32, 32 ); break; }; glPopMatrix(); /* Disable Texture Coord Generation For S */ glDisable( GL_TEXTURE_GEN_S ); /* Disable Texture Coord Generation For T */ glDisable( GL_TEXTURE_GEN_T ); /* This Will Select The BG Texture */ glBindTexture( GL_TEXTURE_2D, texture[filter*2] ); glPushMatrix(); glTranslatef( 0.0f, 0.0f, -29.0f ); glBegin( GL_QUADS ); glNormal3f( 0.0f, 0.0f, 1.0f); glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -13.3f, -10.0f, 10.0f ); glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 13.3f, -10.0f, 10.0f ); glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 13.3f, 10.0f, 10.0f ); glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -13.3f, 10.0f, 10.0f ); glEnd(); glPopMatrix(); xrot += xspeed; /* Add xspeed To xrot */ yrot += yspeed; /* Add yspeed To yrot */ showFPS(); }
int processSdlEvents(void) { SDL_Event event; resetEvents(); while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: return 1; break; default: keyboardEvent(&event.key,1); break; } break; case SDL_KEYUP: keyboardEvent(&event.key,0); break; case SDL_MOUSEMOTION: mouseMovedEvent(event.motion.x,event.motion.y, event.motion.xrel,event.motion.yrel); break; case SDL_MOUSEBUTTONDOWN: mouseButtonEvent(event.button.button,1); break; case SDL_MOUSEBUTTONUP: mouseButtonEvent(event.button.button,0); break; case SDL_QUIT: closeGdl(); exit(0); break; } /* If the next event to process is of type KEYUP or * MOUSEBUTTONUP we want to stop processing here, so that * a fast up/down event be noticed by Lua. */ if (SDL_PeepEvents(&event,1,SDL_PEEKEVENT,SDL_ALLEVENTS)) { if (event.type == SDL_KEYUP || event.type == SDL_MOUSEBUTTONUP) break; /* Go to lua before processing more. */ } } flipGdl(); /* Call the setup function, only the first time. */ if (l81.epoch == 0) { setup(); if (l81.luaerr) return l81.luaerr; } /* Call the draw function at every iteration. */ draw(); l81.epoch++; /* Refresh the screen */ if (l81.opt_show_fps) showFPS(); SDL_Flip(l81.fb->screen); /* Wait some time if the frame was produced in less than 1/FPS seconds. */ SDL_framerateDelay(&l81.fb->fps_mgr); /* Stop execution on error */ return l81.luaerr; }
int main() { EinApplication::setErrorCallback(onError); EinApplication *app = new EinApplication(); EinWindow* window = new EinWindow(WIN_WIDTH, WIN_HEIGHT, "cgBentRendering", false); Camera* mainCamera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f)); GLfloat quadVertices[] = { // Vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates. // Positions // TexCoords -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f }; // Setup screenQuad VAO GLuint quadVAO, quadVBO; glGenVertexArrays(1, &quadVAO); glGenBuffers(1, &quadVBO); glBindVertexArray(quadVAO); glBindBuffer(GL_ARRAY_BUFFER, quadVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat))); glBindVertexArray(0); Shader modelGbuffer("shaders/modelGbuffer.vert", "shaders/modelGbuffer.frag"); modelGbuffer.Link(); // Shader bentNormalsShader("shaders/quad.vert", "shaders/bent_normals.frag"); // bentNormalsShader.Link(); // // algorithm params // Params params; // params.sampleRadius = 1.0f; // params.maxDistance = params.sampleRadius * 1.6f; // params.numRayMarchingSteps = 3; // params.patternSize = 8; // params.sampleCount = 8; // params.rayMarchingBias = params.sampleRadius / float(params.numRayMarchingSteps) / 1000.0f; // // glUniform1f(glGetUniformLocation(bentNormalsShader.Program, "sampleRadius"), params.sampleRadius); // glUniform1f(glGetUniformLocation(bentNormalsShader.Program, "maxDistance"), params.maxDistance); // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "patternSize"), params.patternSize); // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "sampleCount"), params.sampleCount); // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "numRayMarchingSteps"), params.numRayMarchingSteps); // // int kernelSize = 32; // glm::vec3 *kernel = new glm::vec3[kernelSize]; // srandTimeSeed(); // for (int i = 0; i < kernelSize; i++) { // kernel[i] = glm::vec3( // randMToN(-1.0f, 1.0f), // randMToN(-1.0f, 1.0f), // randMToN(0.0f, 1.0f)); // kernel[i] = glm::normalize(kernel[i]); // // float scale = (float)i / (float)kernelSize; // kernel[i] *= lerp(0.1f, 1.0f, scale * scale); // } // // for (int i = 0; i< kernelSize; i++) { // std::cerr << kernel[i].x << " " << kernel[i].y << " " << kernel[i].z << std::endl; // } // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "uKernelSize"), kernelSize); // glUniform3fv(glGetUniformLocation(bentNormalsShader.Program, "uKernelOffsets"), kernelSize, (const GLfloat*)glm::value_ptr(kernel[0])); // // int ssaoNoiseSize = 8; // int noiseDataSize = ssaoNoiseSize * ssaoNoiseSize; // glm::vec3 *noiseData = new glm::vec3[noiseDataSize]; // srandTimeSeed(); // for (int i = 0; i < noiseDataSize; i++) { // noiseData[i] = glm::vec3( // randMToN(-1.0f, 1.0f), // randMToN(-1.0f, 1.0f), // 0.0f); // noiseData[i] = glm::normalize(noiseData[i]); // } // Texture noiseTex; // // noiseTex.Bind(); // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ssaoNoiseSize, ssaoNoiseSize, 0, GL_RGB, GL_FLOAT, noiseData); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // noiseTex.Unbind(); Shader lightingShader("shaders/quad.vert", "shaders/lighting.frag"); lightingShader.Link(); Shader simpleShader("shaders/simple.vert", "shaders/simple.frag"); simpleShader.Link(); // Load models Model demonHeadModel("models/bake/monkeyright.obj"); Texture bentNormalsTexture("models/bake/nice_bent_normals.png", GL_REPEAT, GL_REPEAT, GL_NEAREST, GL_NEAREST); // Setup gBuffer Framebuffer *gBuffer = new Framebuffer(); gBuffer->BindFb(); gBuffer->addTextureAttachment(TexAttachmentType::DEPTH, window->GetFramebufferSize()); gBuffer->addTextureAttachment(TexAttachmentType::RGBA, window->GetFramebufferSize(), "position"); gBuffer->addTextureAttachment(TexAttachmentType::RGB, window->GetFramebufferSize(), "normal"); gBuffer->addTextureAttachment(TexAttachmentType::RGB, window->GetFramebufferSize(), "bentNormal"); gBuffer->addTextureAttachment(TexAttachmentType::RGB, window->GetFramebufferSize(), "color"); gBuffer->setupMRT(); if(!gBuffer->isReady()) std::cerr << "Gbuffer incomplete" << std::endl; gBuffer->UnbindFb(); // Get textures from gBuffer Texture depthTex = gBuffer->getTextureAttachment(DEPTH); Texture positionTex = gBuffer->getTextureAttachment("position"); Texture normalTex = gBuffer->getTextureAttachment("normal"); Texture bentNormalsTex = gBuffer->getTextureAttachment("bentNormal"); Texture colorTex = gBuffer->getTextureAttachment("color"); // // Setup bent_normalsBuffer // Framebuffer *bentNormalsBuffer = new Framebuffer(); // bentNormalsBuffer->BindFb(); // bentNormalsBuffer->addTextureAttachment(TexAttachmentType::RGBA, window->GetFramebufferSize(), "bent_normals"); // if(!bentNormalsBuffer->isReady()) // std::cerr << "Bent normals buffer incomplete" << std::endl; // bentNormalsBuffer->UnbindFb(); // // Get bent normals texture // Texture bentNormalsTex = bentNormalsBuffer->getTextureAttachment("bent_normals"); bool useBentNormals = false; float bentNormalsInfluence = 0.3f; EinInputManager *inputManager = window->GetInputManager(); while(!window->ShouldClose()) { // Set frame time GLfloat currentFrame = app->GetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; // Pool events inputManager->pollEvents(); if(inputManager->isExit(KeyActionType::KEY_UP)) { window->SetShouldClose(true); } if(inputManager->isKey(GLFW_KEY_T, KeyActionType::KEY_UP)) { if (useBentNormals) useBentNormals = false; else useBentNormals = true; } if(inputManager->isKey(GLFW_KEY_Y, KeyActionType::KEY_DOWN)) { bentNormalsInfluence -= 0.01f; if(bentNormalsInfluence < 0.0f) bentNormalsInfluence = 0.0f; } if(inputManager->isKey(GLFW_KEY_U, KeyActionType::KEY_DOWN)) { bentNormalsInfluence += 0.01f; if(bentNormalsInfluence > 1.0f) bentNormalsInfluence = 1.0f; } // Handle camera movements doMovement(window->GetInputManager(), mainCamera); // Bind gBuffer gBuffer->BindFb(); // Setup OpenGL options // Clear the screen to black glEnable(GL_DEPTH_TEST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Activate shader modelGbuffer.Use(); // Create transformations glm::mat4 view = mainCamera->GetViewMatrix(); glm::mat4 projection = glm::perspective(mainCamera->zoomQuantity, (float)(window->GetWindowSize().width)/(float)(window->GetWindowSize().height), 0.1f, 10.0f); glm::mat4 model; // model = glm::rotate(model, -90.0f, glm::vec3(1.0, 0.0, 0.0)); // Get the uniform locations GLint modelLoc = glGetUniformLocation(modelGbuffer.Program, "model"); GLint viewLoc = glGetUniformLocation(modelGbuffer.Program, "view"); GLint projLoc = glGetUniformLocation(modelGbuffer.Program, "projection"); // Pass the matrices to the shader glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection)); glActiveTexture(GL_TEXTURE0); bentNormalsTexture.Bind(); glUniform1i(glGetUniformLocation(modelGbuffer.Program, "normalMap"), 0); demonHeadModel.Draw(modelGbuffer); gBuffer->UnbindFb(); // bentNormalsBuffer->BindFb(); // glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // glClear(GL_COLOR_BUFFER_BIT); // glDisable(GL_DEPTH_TEST); // // bentNormalsShader.Use(); // glBindVertexArray(quadVAO); // GLint viewMatrixLoc = glGetUniformLocation(bentNormalsShader.Program, "viewMatrix"); // GLint viewProjectionMatrixLoc = glGetUniformLocation(bentNormalsShader.Program, "projectionMatrix"); // // Pass the matrices to the shader // glUniformMatrix4fv(viewMatrixLoc, 1, GL_FALSE, glm::value_ptr(view)); // glUniformMatrix4fv(viewProjectionMatrixLoc, 1, GL_FALSE, glm::value_ptr(projection)); // // glActiveTexture(GL_TEXTURE0); // positionTex.Bind(); // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "positionTexture"), 0); // // glActiveTexture(GL_TEXTURE1); // normalTex.Bind(); // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "normalTexture"), 1); // // glActiveTexture(GL_TEXTURE2); // depthTex.Bind(); // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "depthTexture"), 2); // // glActiveTexture(GL_TEXTURE3); // noiseTex.Bind(); // glUniform1i(glGetUniformLocation(bentNormalsShader.Program, "uNoiseTex"), 3); // glDrawArrays(GL_TRIANGLES, 0, 6); // glBindVertexArray(0); // bentNormalsBuffer->UnbindFb(); // Activate shader glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_DEPTH_TEST); lightingShader.Use(); glBindVertexArray(quadVAO); glActiveTexture(GL_TEXTURE0); positionTex.Bind(); glUniform1i(glGetUniformLocation(lightingShader.Program, "tPosition"), 0); glActiveTexture(GL_TEXTURE1); normalTex.Bind(); glUniform1i(glGetUniformLocation(lightingShader.Program, "tNormals"), 1); glActiveTexture(GL_TEXTURE2); colorTex.Bind(); glUniform1i(glGetUniformLocation(lightingShader.Program, "tDiffuse"), 2); glActiveTexture(GL_TEXTURE3); bentNormalsTex.Bind(); glUniform1i(glGetUniformLocation(lightingShader.Program, "tBentNormals"), 3); GLint lightPosLoc = glGetUniformLocation(lightingShader.Program, "lightPos"); GLint viewerPosLoc = glGetUniformLocation(lightingShader.Program, "viewerPos"); glUniform3f(viewerPosLoc, mainCamera->position.x, mainCamera->position.y, mainCamera->position.z); glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z); glUniform1i(glGetUniformLocation(lightingShader.Program, "useBentNormals"), useBentNormals); glUniform1f(glGetUniformLocation(lightingShader.Program, "bentNormalsInfluence"), bentNormalsInfluence); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0); // Swap screen buffer window->SwapBuffers(); showFPS(window); } // Clean up resources lightingShader.Delete(); modelGbuffer.Delete(); glDeleteVertexArrays(1, &quadVAO); delete window; delete mainCamera; delete app; }
int main(int argc, char *argv[]) { // UI-related variables int running = 0; int mousebtn1, lastmousebtn1; int mousex, mousey, lastmousex, lastmousey; float posx, posy, zoom, rotx, rotz; // Shader-related variables GLuint textureID; int texw = 0; int texh = 0; GLhandleARB programObj; // Initialise GLFW glfwInit(); // Open OpenGL window if( !glfwOpenWindow( 512, 512, 0,0,0,0, 0,0, GLFW_WINDOW ) ) { glfwTerminate(); return 0; } // Init user interface (mouse drag for pan/zoom/tilt/rotation) posx = 0.0f; posy = 0.0f; zoom = 1.0f; rotx = 0.0f; rotz = 0.0f; glfwGetMousePos(&mousex, &mousey); // Requires an open window lastmousex = mousex; lastmousey = mousey; mousebtn1 = lastmousebtn1 = GLFW_RELEASE; // Load OpenGL extensions (requires an open window) loadExtensions(); // Load textures glEnable(GL_TEXTURE_2D); glGenTextures( 1, &textureID ); loadDistTexture(PATH "disttex.tga", textureID, &texw, &texh); // Create, load and compile the shader programs programObj = createShader(PATH "vertex.glsl", PATH "fragment1.glsl"); // Disable vertical sync (on cards that support // it, and if current driver settings so permit) glfwSwapInterval( 1 ); // Main loop running = GL_TRUE; while( running ) { showFPS(texw, texh, zoom); // Set the uniform shader variables setUniformVariables(programObj, 0, (float)texw, (float)texh); renderScene(programObj, posx, posy, zoom, rotx, rotz); glfwSwapBuffers(); // Handle mouse pan (button 1 drag), zoom (shift-btn 1 drag up/down), // tilt (ctrl-btn 1 drag up/down) and rotation (ctrl-btn 1 drag left/right) lastmousebtn1 = mousebtn1; mousebtn1 = glfwGetMouseButton(GLFW_MOUSE_BUTTON_1); lastmousex = mousex; lastmousey = mousey; glfwGetMousePos(&mousex, &mousey); if((mousebtn1 == GLFW_PRESS) && (lastmousebtn1 == GLFW_PRESS)) { if(glfwGetKey(GLFW_KEY_LSHIFT)) { zoom *= pow(1.01, (lastmousey - mousey)); if(zoom < 0.26f) zoom = 0.26f; // Do not go beyond 180 degrees FOV } else if (glfwGetKey(GLFW_KEY_LCTRL)) { rotz -= (lastmousex - mousex) * 0.5; rotx += (lastmousey - mousey) * 0.5; if(rotx > 89.5f) rotx = 89.5f; if(rotx < 0.0f) rotx = 0.0f; } else { posx += (lastmousex - mousex) / zoom; posy += (lastmousey - mousey) / zoom; } } if(glfwGetKey('1')) { programObj = createShader(PATH "vertex.glsl", PATH "fragment1.glsl"); } if(glfwGetKey('2')) { programObj = createShader(PATH "vertex.glsl", PATH "fragment2.glsl"); } if(glfwGetKey('3')) { programObj = createShader(PATH "vertex.glsl", PATH "fragment3.glsl"); } if(glfwGetKey('4')) { programObj = createShader(PATH "vertex.glsl", PATH "fragment4.glsl"); } // Check if the ESC key is pressed or the window has been closed running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED ); } // Close the window (if still open) and terminate GLFW glfwTerminate(); return 0; }
void DarkSDK ( void ) { // entry point for the application //dbSetDir ( "media\\" ); // initial application set up dbSyncOn ( ); // turn sync on dbSyncRate ( 0 ); // set sync rate to 60 dbBackdropOn ( ); // switch backdrop off - no need to clear screen dbSetCameraRange ( 0.5f, 30000 ); // set the camera range PhysStart ( ); // floor dbMakeObjectBox ( 1, 100, 1, 100 ); dbPositionObject ( 1, 0, 0, 0 ); dbColorObject ( 1, dbRgb ( 100, 0, 0 ) ); PhysCreateStaticRigidBodyMesh ( 1 ); /* dbLoadObject ( "bin1.x", 2 ); //dbPositionObject ( 2, 0, 1 + dbObjectCollisionCenterY ( 2 ), 0 ); dbPositionObject ( 2, 0, 0, 0 ); //dbOffsetLimb ( 2, 0, dbObjectCollisionCenterX ( 2 ) * -1, dbObjectCollisionCenterY ( 2 ) * -1, dbObjectCollisionCenterZ ( 2 ) * -1 ); PhysCreateRigidBodyBox ( 2 ); dbMakeObjectCube ( 3, 5 ); dbPositionObject ( 3, 20, 10, 0 ); PhysCreateRigidBodyBox ( 3 ); */ const NxReal height = 0.3; const NxReal width = 2; const NxReal length = 4; //dbMakeObjectBox ( 5, length*0.65, height*0.85, width*0.65 ); dbMakeObjectBox ( 5, 3.5,3,4); dbPositionObject ( 5, 3.5,4,0 ); dbMakeObjectSphere ( 10, 0.8 ); dbMakeObjectSphere ( 11, 0.8 ); dbMakeObjectSphere ( 12, 0.8 ); dbMakeObjectSphere ( 13, 0.8 ); CreateTruck ( ); //dbLoadObject ( "corsa.x", 5 ); //dbPositionObject ( 5, 0, 1, 0 ); /* InitCar ( ); car = CreateCar(NxVec3(0,5,0)); */ dbPositionCamera ( 0, 20, -30 ); // PhysCreateRigidBodyBox ( 10 ); // loop round until escape key is pressed while ( LoopSDK ( ) ) { if ( dbEscapeKey ( ) ) return; if ( dbSpaceKey ( ) ) PhysSetVelocity ( 10, 0, 20, 0 ); UpdateTruck ( ); //if ( dbSpaceKey ( ) ) // PhysSetVelocity ( 3, -20, 0, 0 ); /* { bool bKey = false; if ( dbKeyState ( 17 ) == 1 ) { gMotorForce = gMaxMotorPower; bKey = true; } if ( dbKeyState ( 31 ) == 1 ) { gMotorForce = -gMaxMotorPower; bKey = true; } if ( dbKeyState ( 32 ) ) { if (gSteeringValue > (-1.0f + gSteeringDelta)) gSteeringValue -= gSteeringDelta; bKey = true; } if ( dbKeyState ( 30 ) ) { if (gSteeringValue < (1.0f - gSteeringDelta)) gSteeringValue += gSteeringDelta; bKey = true; } if ( !bKey ) { gMotorForce = 0.0f; gSteeringValue = 0.0f; } } TickCar ( ); */ // show the frame rate and handle user input showFPS ( ); userInput ( ); PhysRun ( ); PhysUpdate ( ); // render everything dbSync ( ); } }
void VI_SDL::blit() { SDL_UpdateRect(screen, 0, 0, 0, 0); showFPS(); }
void displayCB() { // get the total elapsed time playTime = (float)timer.getElapsedTime(); // compute rotation angle const float ANGLE_SPEED = 10; // degree/s float angle = ANGLE_SPEED * playTime; // render to texture ////////////////////////////////////////////////////// t1.start(); // adjust viewport and projection matrix to texture dimension glViewport(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f, (float)(TEXTURE_WIDTH)/TEXTURE_HEIGHT, 1.0f, 100.0f); glMatrixMode(GL_MODELVIEW); // camera transform glLoadIdentity(); glTranslatef(0, 0, -CAMERA_DISTANCE); // with MSAA // render to MSAA FBO if(msaaUsed) { // set the rendering destination to FBO glBindFramebuffer(GL_FRAMEBUFFER, fboMsaaId); // clear buffer glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw a rotating object at the origin glPushMatrix(); glRotatef(angle*0.5f, 1, 0, 0); glRotatef(angle, 0, 1, 0); glRotatef(angle*0.7f, 0, 0, 1); glTranslatef(0, -1.575f, 0); drawTeapot(); glPopMatrix(); // copy rendered image from MSAA (multi-sample) to normal (single-sample) FBO // NOTE: The multi samples at a pixel in read buffer will be converted // to a single sample at the target pixel in draw buffer. glBindFramebuffer(GL_READ_FRAMEBUFFER, fboMsaaId); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboId); glBlitFramebuffer(0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT, // src rect 0, 0, TEXTURE_WIDTH, TEXTURE_HEIGHT, // dst rect GL_COLOR_BUFFER_BIT, // buffer mask GL_LINEAR); // scale filter // trigger mipmaps generation explicitly // NOTE: If GL_GENERATE_MIPMAP is set to GL_TRUE, then glCopyTexSubImage2D() // triggers mipmap generation automatically. However, the texture attached // onto a FBO should generate mipmaps manually via glGenerateMipmap(). glBindTexture(GL_TEXTURE_2D, textureId); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); // back to normal window-system-provided framebuffer glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind } // without MSAA // render to non MSAA FBO else { // set the rendering destination to FBO glBindFramebuffer(GL_FRAMEBUFFER, fboId); // clear buffer glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw a rotating object at the origin glPushMatrix(); glRotatef(angle*0.5f, 1, 0, 0); glRotatef(angle, 0, 1, 0); glRotatef(angle*0.7f, 0, 0, 1); glTranslatef(0, -1.575f, 0); drawTeapot(); glPopMatrix(); // trigger mipmaps generation explicitly // NOTE: If GL_GENERATE_MIPMAP is set to GL_TRUE, then glCopyTexSubImage2D() // triggers mipmap generation automatically. However, the texture attached // onto a FBO should generate mipmaps manually via glGenerateMipmap(). glBindTexture(GL_TEXTURE_2D, textureId); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); // back to normal window-system-provided framebuffer glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind } // measure the elapsed time of render-to-texture t1.stop(); renderToTextureTime = t1.getElapsedTimeInMilliSec(); /////////////////////////////////////////////////////////////////////////// // rendering as normal //////////////////////////////////////////////////// // back to normal viewport and projection matrix toPerspective(); // tramsform camera glTranslatef(0, 0, -cameraDistance); glRotatef(cameraAngleX, 1, 0, 0); // pitch glRotatef(cameraAngleY, 0, 1, 0); // heading // clear framebuffer glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glPushMatrix(); // draw a cube with the dynamic texture draw(); glPopMatrix(); // draw info messages showInfo(); showFPS(); glutSwapBuffers(); }