void Module::handleEvents() { Events::Event event; while (EventMan.pollEvent(event)) { // Handle console if (_console->processEvent(event)) { if (!_area) return; continue; } if (event.type == Events::kEventKeyDown) { // Menu if (event.key.keysym.sym == SDLK_ESCAPE) { showMenu(); continue; } // Console if ((event.key.keysym.sym == SDLK_d) && (event.key.keysym.mod & KMOD_CTRL)) { _console->show(); continue; } } // Camera if (handleCamera(event)) continue; _area->addEvent(event); } _area->processEventQueue(); }
void Module::handleEvents() { Events::Event event; while (EventMan.pollEvent(event)) { // Handle console if (_console->processEvent(event)) { if (!_currentArea) return; continue; } if (event.type == Events::kEventKeyDown) { // Menu if (event.key.keysym.sym == SDLK_ESCAPE) { // But only if we're not in a conversation, where ESC should abort that if (!_ingameGUI->hasRunningConversation()) { showMenu(); continue; } } // Console if ((event.key.keysym.sym == SDLK_d) && (event.key.keysym.mod & KMOD_CTRL)) { _console->show(); continue; } } // Camera if (handleCamera(event)) continue; _ingameGUI->addEvent(event); _currentArea->addEvent(event); } _currentArea->processEventQueue(); _ingameGUI->processEventQueue(); }
void SceneSerializer::handleNode(const Value& entries, SceneNode* node) { handleNodeProperties(entries, node); // Then recursively handle scene objects and scenen nodes. for (Value::ConstMemberIterator entryIter = entries.MemberBegin(); entryIter != entries.MemberEnd(); ++entryIter) { const Value& entryKey = entryIter->name; const Value& entryValue = entryIter->value; assert(entryKey.IsString()); string key = entryKey.GetString(); if (key == kEntityKey) { handleEntity(entryValue, createChildSceneNode(node, entryValue)); } else if (key == kEntityGroupKey) { handleNode(entryValue, createChildSceneNode(node, entryValue)); } else if (key == kIncludeKey) { assert(entryValue.IsString()); handleInclude(entryValue.GetString(), kDefaultSceneNodeClass, createChildSceneNodeWithNameSuffix(node, "")); } else if (key == kCameraKey) { if (entryValue.IsObject()) { handleCamera(entryValue, createChildSceneNode(node, entryValue)); } else if (entryValue.IsString()) { // This is not a camera definition, but a referance to a camera. string cameraName = entryValue.GetString(); node->addVisibleCamera(cameraName); Camera* camera = mSceneMgr->getCamera(cameraName); assert(camera != NULL && "Error: camera is null. " "Please define camera before using to it."); getDefaultViewport()->addCamera(camera); } } else { // Assume it is a simple node property and it has been handled. } } }
void captureApp::update(){ panel.update(); // this is where an event/callback-based // control panel would be really helpful! int curWavelength = panel.getValueI("wavelength"); if(curWavelength != lastWavelength) { threePhase.setWavelength(curWavelength); threePhase.generate(); twoPlusOne.setWavelength(curWavelength); twoPlusOne.generate(); } lastWavelength = curWavelength; int minBrightness = panel.getValueI("minBrightness"); if (minBrightness != lastMinBrightness) { threePhase.setMinBrightness(minBrightness); threePhase.generate(); twoPlusOne.setMinBrightness(minBrightness); twoPlusOne.generate(); } lastMinBrightness = minBrightness; int curOrientation = panel.getValueI("orientation"); if(curOrientation != lastOrientation) { phaseOrientation orientation = curOrientation == 0 ? PHASE_VERTICAL : PHASE_HORIZONTAL; threePhase.setOrientation(orientation); threePhase.generate(); grayCode.setOrientation(orientation); grayCode.generate(); gradient.setOrientation(orientation); gradient.generate(); twoPlusOne.setOrientation(orientation); twoPlusOne.generate(); } lastOrientation = curOrientation; int curSubdivisions = panel.getValueI("subdivisions"); if(curSubdivisions != lastSubdivisions) { grayCode.setSubdivisions(curSubdivisions); grayCode.generate(); } lastSubdivisions = curSubdivisions; threePhase.setReverse(panel.getValueB("reverse")); twoPlusOne.setReverse(panel.getValueB("reverse")); grayCode.setReverse(panel.getValueB("reverse")); int curFullscreen = panel.getValueB("fullscreen"); if(curFullscreen != lastFullscreen) ofSetFullscreen(curFullscreen); lastFullscreen = curFullscreen; int curPatternType = panel.getValueI("patternType"); if(curPatternType != lastPatternType) { switch(curPatternType) { case 0: curGenerator = &threePhase; break; case 1: curGenerator = &grayCode; break; case 2: curGenerator = &gradient; break; case 3: curGenerator = &twoPlusOne; break; } } lastPatternType = curPatternType; handleCamera(); if(panel.getValueB("projectorLut")) { curGenerator->applyLut(ofToDataPath("projector-lut.csv")); panel.setValueB("projectorLut", false); } }
void Game::gameLoop() { sf::Event currentEvent; _mainWindow.pollEvent(currentEvent); switch (_gameState) { case Game::ShowingMenu: { showMenu(); break; } case Game::ShowingSplash: { showSplashScreen("C:/Users/Anne/Documents/Visual Studio 2013/Projects/MyGame/Graphics/MyGameScreen.png"); break; } case Game::ShowingSplashVictory: { showSplashScreen("C:/Users/Anne/Documents/Visual Studio 2013/Projects/MyGame/Graphics/Victory.png"); break; } case Game::ShowingSplashLoss: { showSplashScreen("C:/Users/Anne/Documents/Visual Studio 2013/Projects/MyGame/Graphics/Loss.png"); break; } case Game::Playing: { rigidBodyCoords = findRigidBodies(); _gameObjectManager.updateAll(); //_mainWindow.draw(*coinstring); _gameObjectManager.drawAll(_mainWindow); PlayerCharacter * player = (PlayerCharacter *)_gameObjectManager.get("PlayerCharacter"); if (player->getPosition().y > 2000) { if (player->lives > 0) { player->lives--; start_again(); } else { _gameState = Loss; start_loss(); break; } } handleSurfaces(); handleCamera(); _mainWindow.setView(_view); _mainWindow.display(); if (currentEvent.type == sf::Event::Closed) _gameState = Game::Exiting; if ((currentEvent.type == sf::Event::KeyPressed) && (currentEvent.key.code == sf::Keyboard::Escape)) showMenu(); if(game_victory != 0) { _gameState = Game::Victory; } break; } case Game::Victory: { _gameState = Game::Uninitialized; start_victory(); break; } case Game::Loss: { _gameState = Game::Uninitialized; start_loss(); break; } } }
hkDemo::Result PlanetGravityDemo::stepDemo() { // Update lighting { // Update the light source to be at the camera float position[3]; m_cameraPosition.store3( position ); m_flashLight->setPosition( position ); // Update the light direction to be pointing toward the character controller rigid body hkVector4 directionVector; directionVector.setSub4( m_cameraPosition, m_characterRigidBody->getPosition() ); directionVector.mul4( -1.0f ); directionVector.normalize3(); float direction[3]; directionVector.store3( direction ); m_flashLight->setDirection( direction ); } // Detach the camera from the character when P is pressed. if( m_env->m_window->getKeyboard().wasKeyPressed('P') ) { m_detachedCamera = !m_detachedCamera; } // Update turrets for( int i = 0; i < m_turrets.getSize(); i++ ) { Turret& turret = m_turrets[i]; turret.cooldown -= m_timestep; // Make the turret spin turret.hinge->setMotorTargetAngle( turret.hinge->getMotorTargetAngle() + ( m_timestep / 5.f ) ); // Bail out if the turret is "hot" if( turret.cooldown > 0.0f ) { continue; } // Generate a curved raycast and shoot the ray // This has to be done every time-step as it's in world-space { const hkReal radius = 14.8f; hkRotation rot; hkVector4 offset; hkVector4 turretDown; rot.set( turret.turretRigidBody->getRotation() ); offset = turret.turretRigidBody->getPosition(); turretDown.setMul4( -1.0f, rot.getColumn(2) ); hkpLinearParametricCurve myCurve; // Move the ray's source a little up so it's coming from the center of the barrel hkTransform localTransform( hkQuaternion::getIdentity(), hkVector4( 0.0f, 0.0f, 0.7f ) ); // Create a curve of 20 points for( int j = 0; j < 20; j++ ) { hkReal angle = HK_REAL_PI * static_cast<hkReal>(j) / 15.0f; hkVector4 newPoint( radius * 2.0f * sin( angle ), 0.0f, radius * 2.0f * cos( angle ) ); newPoint.setTransformedPos( localTransform, newPoint ); newPoint.setTransformedPos( turret.turretRigidBody->getTransform(), newPoint ); newPoint.addMul4( radius * 2.0f, turretDown ); myCurve.addPoint( newPoint ); } // We only need the closest hit (as our lasers can't pass through objects) // so hkpClosestRayHitCollector is used. hkpClosestRayHitCollector raycastOutput; hkReal t = castCurvedRay( raycastOutput, myCurve, 20 ); // Apply a large force to the closest rb we hit, along the tangent at the colliding point if( raycastOutput.hasHit() ) { hkpRigidBody* hitRb = hkGetRigidBody( raycastOutput.getHit().m_rootCollidable ); if( hitRb->getMotionType() != hkpMotion::MOTION_FIXED ) { hkVector4 tangent; myCurve.getTangent( t, tangent ); tangent.mul4( 15000.0f ); applyScaledLinearImpulse( hitRb, tangent ); turret.cooldown = 3.0f; } } } } m_world->markForWrite(); // Update the character context m_characterRigidBody->m_up = m_worldUp; hkReal posX = 0.0f; hkReal posY = 0.0f; if( !m_detachedCamera ) { float deltaAngle; CharacterUtils::getUserInputForCharacter( m_env, deltaAngle, posX, posY ); if( ( ( hkMath::fabs( posX ) < HK_REAL_MAX ) && ( hkMath::fabs( posY ) < HK_REAL_MAX ) ) && ( posX || posY ) ) { // find new orientation in local space hkVector4 newForward( -posY, 0.0f, -posX ); hkVector4 absoluteForward( 1.0f, 0.0f, 0.0f ); hkReal characterAngle = hkMath::acos( absoluteForward.dot3( newForward ) ); // Calculate cross product to get sign of rotation. hkVector4 crossProduct; crossProduct.setCross( absoluteForward, newForward ); if( crossProduct(1) < 0.0f ) { characterAngle *= -1.0f; } // Rotate the character's rigid body to face in the direction it's moving hkRotation newRotation; newRotation.setAxisAngle( m_worldUp, characterAngle ); m_characterForward.setRotatedDir( newRotation, m_cameraForward ); m_characterForward.normalize3(); } // Rotate the camera's forward vector based on world up vector and mouse movement if( deltaAngle != 0.0f && m_characterRigidBody->getRigidBody()->getRotation().hasValidAxis() ) { hkRotation newRotation; newRotation.setAxisAngle( m_worldUp, deltaAngle ); m_cameraForward.setRotatedDir( newRotation, m_cameraForward ); m_cameraForward.normalize3(); } } HK_TIMER_BEGIN( "set character state", HK_NULL ); hkpCharacterInput input; hkpCharacterOutput output; { input.m_atLadder = false; input.m_inputLR = posX; input.m_inputUD = posY; if( m_detachedCamera ) { input.m_wantJump = false; } else { input.m_wantJump = m_env->m_window->getMouse().wasButtonPressed( HKG_MOUSE_LEFT_BUTTON ) || m_env->m_gamePad->wasButtonPressed( HKG_PAD_BUTTON_1 ); } // Check that we have a valid rotation. Probably won't for the first couple of frames. if( !( m_characterRigidBody->getRigidBody()->getRotation().hasValidAxis() ) ) { input.m_up = hkVector4( 0.0f, 0.0f, 1.0f ); input.m_forward = m_cameraForward; } else { input.m_up = m_worldUp; // Recalculate m_forward so it's perpendicular to m_worldUp hkVector4 newRot; newRot.setCross( m_cameraForward, m_worldUp ); m_cameraForward.setCross( m_worldUp, newRot ); // Display character's current heading hkRotation characterRotation; characterRotation.set( m_characterRigidBody->getRigidBody()->getRotation() ); HK_DISPLAY_ARROW( m_characterRigidBody->getPosition(), characterRotation.getColumn(0), hkColor::LIMEGREEN ); input.m_forward = m_cameraForward; } hkStepInfo stepInfo; stepInfo.m_deltaTime = m_timestep; stepInfo.m_invDeltaTime = 1.0f / m_timestep; input.m_stepInfo = stepInfo; input.m_characterGravity.setMul4( -20.0f, m_worldUp ); input.m_velocity = m_characterRigidBody->getRigidBody()->getLinearVelocity(); input.m_position = m_characterRigidBody->getRigidBody()->getPosition(); { hkpSurfaceInfo ground; m_characterRigidBody->checkSupport( stepInfo, ground ); // Avoid accidental state changes (Smooth movement on stairs) // During transition supported->unsupported continue to return N-frames hkpSurfaceInfo data from previous supported state { // Number of frames to skip (continue with previous hkpSurfaceInfo data) const int skipFramesInAir = 6; if( input.m_wantJump ) { m_framesInAir = skipFramesInAir; } hkpSurfaceInfo* currInfo; if( ground.m_supportedState != hkpSurfaceInfo::SUPPORTED ) { if( m_framesInAir < skipFramesInAir ) { input.m_isSupported = true; currInfo = m_previousGround; } else { input.m_isSupported = false; currInfo = &ground; } m_framesInAir++; } else { input.m_isSupported = true; currInfo = &ground; m_previousGround->set( ground ); // reset old number of frames if( m_framesInAir > skipFramesInAir ) { m_framesInAir = 0; } } input.m_surfaceNormal = currInfo->m_surfaceNormal; input.m_surfaceVelocity = currInfo->m_surfaceVelocity; input.m_surfaceMotionType = currInfo->m_surfaceMotionType; } } HK_TIMER_END(); } // Apply the character state machine { HK_TIMER_BEGIN( "update character state", HK_NULL ); m_characterContext->update( input, output ); HK_TIMER_END(); } //Apply the player character controller { HK_TIMER_BEGIN( "simulate character", HK_NULL ); // Set output velocity from state machine into character rigid body m_characterRigidBody->setLinearVelocity( output.m_velocity, m_timestep ); HK_TIMER_END(); m_world->unmarkForWrite(); } // Rotate the character { hkRotation newOrientation; newOrientation.getColumn(0) = m_characterForward; newOrientation.getColumn(1) = m_worldUp; newOrientation.getColumn(2).setCross( newOrientation.getColumn(0), newOrientation.getColumn(1) ); newOrientation.renormalize(); reorientCharacter( newOrientation ); } // Step the world hkDefaultPhysicsDemo::stepDemo(); // Display state { hkpCharacterStateType state = m_characterContext->getState(); char* stateStr; switch( state ) { case HK_CHARACTER_ON_GROUND: { stateStr = "On Ground"; break; } case HK_CHARACTER_JUMPING: { stateStr = "Jumping"; break; } case HK_CHARACTER_IN_AIR: { stateStr = "In Air"; break; } default: { stateStr = "Other"; break; } } char buffer[255]; hkString::snprintf( buffer, 255, "State : %s", stateStr ); m_env->m_textDisplay->outputText( buffer, 20.f, 270.f, 0xffffffff ); } // // Handle crouching (only for capsule) // if( !m_detachedCamera ) { m_world->markForWrite(); hkBool wantCrouch = ( m_env->m_window->getMouse().getButtonState() & HKG_MOUSE_RIGHT_BUTTON ) || ( m_env->m_gamePad->getButtonState() & HKG_PAD_BUTTON_2 ); hkBool isCrouching = ( m_characterRigidBody->getRigidBody()->getCollidable()->getShape() == m_crouchShape ); // We want to stand if( isCrouching && !wantCrouch ) { m_characterRigidBody->getRigidBody()->setShape( m_standShape ); } // We want to crouch else if( !isCrouching && wantCrouch ) { m_characterRigidBody->getRigidBody()->setShape( m_crouchShape ); } m_world->unmarkForWrite(); } // Transparent camera handling if( !m_detachedCamera ) { m_world->markForWrite(); handleCamera(); m_world->unmarkForWrite(); } return hkDemo::DEMO_OK; }
//----------------------------------------------- void captureApp::update(){ panel.update(); if( panel.getValueB("bRestart") ){ if( panel.getValueI("restartHour") == ofGetHours() ){ if( panel.getValueI("restartMinute") == ofGetMinutes() ){ printf("shutting down now!\n"); system(ofToDataPath("reboot.sh").c_str()); } } } char data[10]; memset(data, 0, 10); if (serial.available() > 0){ serial.readBytes((unsigned char*) data, 10); if(state == CAP_STATE_WAITING) startFadeIn(); } if( panel.hasValueChanged("bOverideLight") ){ if( panel.getValueB("bOverideLight") ){ light.lightOn(); }else{ light.lightOff(); } } bEnableOsc = panel.getValueB("use_osc"); if( panel.hasValueChanged("use_osc") ){ if( bEnableOsc && !bOscSetup ){ setupOsc(); printf("------------- setting up osc\n"); } } if( state == CAP_STATE_FADEIN || state == CAP_STATE_CAPTURE ){ panel.setValueB("frameByFrame", false); panel.setValueB("bSpotLight", true); } //the capture part happens in the camera callbacks at the top. //this just checks to make sure that the capture doesn't need to keep running. if( state == CAP_STATE_CAPTURE ){ panel.hidden = true; if( ofGetFrameNum() % 4 == 0 ){ printf("fps is %s\n", ofToString(camFps, 2).c_str()); } if( ofGetElapsedTimef() >= timeToEndCapture ){ printf("time is %f - time to end is %f\n", ofGetElapsedTimef(), timeToEndCapture); endCapture(); } }else if( debugState == CAP_DEBUG ){ panel.hidden = false; } if( debugState == CAP_NORMAL ){ ofHideCursor(); }else { ofShowCursor(); } if( state == CAP_STATE_CAPTURE && ofGetElapsedTimef() >= timeToEndCapture ){ printf("time is %f - time to end is %f\n", ofGetElapsedTimef(), timeToEndCapture); endCapture(); if( panel.getValueB("B_FACE_TRIGGER") ){ bNeedsToLeaveFrame = true; }else{ bNeedsToLeaveFrame = false; } } if( state == CAP_STATE_FADEIN && ofGetElapsedTimef() > fadeInStartTime + panel.getValueF("fadeInTime") ){ startCapture(); } if( state == CAP_STATE_DECODING ){ handleDecode(); }else{ handleProjection(); handleCamera(); handleFaceTrigger(); } panel.clearAllChanged(); }
int main() { if(!initGL()) return 1; Portal::init(); Shader shader("res/shaders/portal_vert.glsl","res/shaders/portal_frag.glsl"); shader.bind(); f32 vCube[24] = { -0.5, 0.5,-0.5, 0.5, 0.5,-0.5, -0.5,-0.5,-0.5, 0.5,-0.5,-0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5,-0.5, 0.5, 0.5,-0.5, 0.5 }; u32 iCube[36] = { 0,1,2, //Front 1,3,2, 0,4,5, //Top 1,0,5, 5,4,6, //Back 5,6,7, 2,3,7, //Bottom 6,2,7, 3,1,5, //Right 3,5,7, 4,0,6, //Left 0,2,6}; Mesh cube = Mesh( "v3f", GL_STATIC_DRAW, 24*sizeof(f32), vCube, GL_UNSIGNED_INT, GL_STATIC_DRAW, GL_TRIANGLES, 36*sizeof(u32), iCube, 36); Material red; red.setColor(vec3(1,0,0)); Material green; green.setColor(vec3(0,1,0)); Material blue; blue.setColor(vec3(0,0,1)); Material white; white.setColor(vec3(1,1,1)); Material yellow; yellow.setColor(vec3(1,1,0)); // MODULE A Model wallFrontA(&cube, &red); wallFrontA.setScale(vec3(10.0,2.0,1.0)); wallFrontA.setPosition(vec3(0,0,-2.5)); Model wallBackA(&cube, &blue); wallBackA.setScale(vec3(10.0,2.0,1.0)); wallBackA.setPosition(vec3(0,0,2.5)); Model wallLeftA(&cube, &green); wallLeftA.setScale(vec3(1.0,2.0,5.0)); wallLeftA.setPosition(vec3(-5.0,0,0)); Model wallRightA(&cube, &yellow); wallRightA.setScale(vec3(1.0,2.0,5.0)); wallRightA.setPosition(vec3(5.0,0,0)); Model floorModelA(&cube, &white); floorModelA.setScale(vec3(10,1,5)); floorModelA.setPosition(vec3(0,-1.5,0)); Model cubeModelA(&cube, &green); cubeModelA.setPosition(vec3(2,0,0)); Portal portalA(1.5f, 2.0f); portalA.setPosition(vec3(-4.49,0,0.0)); portalA.rotateY(90.0f); Portal portalA2(1.5f, 2.0f); portalA2.setPosition(vec3(-4.49,0,1.0)); portalA2.rotateY(90.0f); Module moduleA; moduleA.addGameObject(&wallFrontA); moduleA.addGameObject(&wallBackA); moduleA.addGameObject(&wallLeftA); moduleA.addGameObject(&wallRightA); moduleA.addGameObject(&floorModelA); moduleA.addGameObject(&cubeModelA); moduleA.addGameObject(&portalA); //moduleA.addGameObject(&portalA2); // MODULE B Model wallFrontB(&cube, &white); wallFrontB.setScale(vec3(10.0,2.0,1.0)); wallFrontB.setPosition(vec3(0,0,-2.5)); Model wallBackB(&cube, &green); wallBackB.setScale(vec3(10.0,2.0,1.0)); wallBackB.setPosition(vec3(0,0,2.5)); Model wallLeftB(&cube, &red); wallLeftB.setScale(vec3(1.0,2.0,5.0)); wallLeftB.setPosition(vec3(-5.0,0,0)); Model wallRightB(&cube, &yellow); wallRightB.setScale(vec3(1.0,2.0,5.0)); wallRightB.setPosition(vec3(5.0,0,0)); Model floorModelB(&cube, &blue); floorModelB.setScale(vec3(10,1,5)); floorModelB.setPosition(vec3(0,-1.5,0)); Model cubeModelB(&cube, &yellow); cubeModelB.setPosition(vec3(2,0,0)); Portal portalB(1.5f, 2.0f); portalB.setPosition(vec3(-4.49,0,1.0)); portalB.rotateY(90.0f); Portal portalB2(1.5f, 2.0f); portalB2.setPosition(vec3(-4.49,0,-1.0)); portalB2.rotateY(90.0f); Module moduleB; moduleB.addGameObject(&wallFrontB); moduleB.addGameObject(&wallBackB); moduleB.addGameObject(&wallLeftB); moduleB.addGameObject(&wallRightB); moduleB.addGameObject(&floorModelB); //moduleB.addGameObject(&cubeModelB); //moduleB.addGameObject(&portalB); moduleB.addGameObject(&portalB2); moduleB.addGameObject(&portalB); // Set targets portalA.setTargetPortal(&portalB); portalB.setTargetPortal(&portalA); portalB2.setTargetPortal(&portalA); //portalA2.setTargetPortal(&portalB2); Camera camera; camera.translate(vec3(0, 0, 0)); camera.setupProjection(800,600); // Add camera to moduleA moduleB.addGameObject(&camera); vec4 v(0.5,0.5,-0.1,1); mat4 proj = glm::perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f); proj[2][2] = 1.0f; //proj[2][3] = 0.0f; proj[3][2] = 0.0f; //proj[3][3] = 1.0f; v = proj * v; v = v / v.w; printf("matrix : \n"); for(int i=0; i<4; ++i) { printf("%f, %f, %f, %f \n", proj[0][i], proj[1][i], proj[2][i], proj[3][i]); } printf("vector! ( %f, %f, %f, %f ) \n", v.x, v.y, v.z, v.w); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glfwSwapInterval(1); while(true) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); handleCamera(&camera); camera.render(); glfwSwapBuffers(); if(glfwGetKey(GLFW_KEY_ESC)) break; } Portal::cleanup(); cleanup(); return 0; }