[Opal-commits] opal/samples/src BaseOpalApp.h,1.1,1.2 ExampleApplication.h,1.1,1.2 ExampleFrameListe
Status: Inactive
Brought to you by:
tylerstreeter
|
From: tylerstreeter <tyl...@us...> - 2005-03-31 08:47:41
|
Update of /cvsroot/opal/opal/samples/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17811/samples/src Modified Files: BaseOpalApp.h ExampleApplication.h ExampleFrameListener.h TemplateApp.cpp Log Message: added some helper functions to Simulator; added data for ogre sample apps Index: TemplateApp.cpp =================================================================== RCS file: /cvsroot/opal/opal/samples/src/TemplateApp.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** TemplateApp.cpp 30 Mar 2005 23:26:25 -0000 1.1 --- TemplateApp.cpp 31 Mar 2005 08:47:02 -0000 1.2 *************** *** 4,8 **** namespace myAppNamespace { ! class MyApp : public BaseOpalApp { public: --- 4,8 ---- namespace myAppNamespace { ! class MyApp : public opalSamples::BaseOpalApp { public: *************** *** 57,84 **** } // The following code can be used to update a camera's position. if (mInputDevice->isKeyDown(KC_LEFT)) { // Move camera left. } ! else if (mInputDevice->isKeyDown(KC_RIGHT)) { // Move camera right. } ! else if (mInputDevice->isKeyDown(KC_UP)) { // Move camera forward. } ! else if (mInputDevice->isKeyDown(KC_DOWN)) { // Move camera backward. } ! else { ! // Stop camera motion if necessary. } // Toggle shadows. ! if( mInputDevice->isKeyDown(KC_S)) { mUseShadows = !mUseShadows; --- 57,110 ---- } + // Check if we should pause physics. + if(mInputDevice->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0) + { + mPaused = !mPaused; + + // Reset the timer for toggle keys. + mTimeUntilNextToggle = 0.5; + } + // The following code can be used to update a camera's position. + Ogre::Vector3 cameraDir = Ogre::Vector3::ZERO; + bool cameraMoved = false; + if (mInputDevice->isKeyDown(KC_LEFT)) { // Move camera left. + cameraDir.x -= (dt * 5); + cameraMoved = true; } ! ! if (mInputDevice->isKeyDown(KC_RIGHT)) { // Move camera right. + cameraDir.x += (dt * 5); + cameraMoved = true; } ! ! if (mInputDevice->isKeyDown(KC_UP)) { // Move camera forward. + cameraDir.z -= (dt * 5); + cameraMoved = true; } ! ! if (mInputDevice->isKeyDown(KC_DOWN)) { // Move camera backward. + cameraDir.z += (dt * 5); + cameraMoved = true; } ! ! if (!cameraMoved) { ! // Slow physical camera motion if necessary. } + // Here, use the camera dir vector to affect the camera. + // Toggle shadows. ! if(mInputDevice->isKeyDown(KC_S) && mTimeUntilNextToggle <= 0) { mUseShadows = !mUseShadows; *************** *** 93,98 **** } ! // Set this to keep from handling the key press ! // continually while it is down. mTimeUntilNextToggle = 0.5; } --- 119,123 ---- } ! // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.5; } *************** *** 116,121 **** + tmp); ! // Set this to keep from handling the key press ! // continually while it is down. mTimeUntilNextToggle = 0.5; } --- 141,145 ---- + tmp); ! // Reset the timer for toggle keys. mTimeUntilNextToggle = 0.5; } Index: ExampleApplication.h =================================================================== RCS file: /cvsroot/opal/opal/samples/src/ExampleApplication.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ExampleApplication.h 30 Mar 2005 23:26:25 -0000 1.1 --- ExampleApplication.h 31 Mar 2005 08:47:02 -0000 1.2 *************** *** 124,127 **** --- 124,129 ---- mSceneMgr = mRoot->getSceneManager(ST_GENERIC); } + + // This function was modifed for the Opal sample apps. virtual void createCamera(void) { *************** *** 129,138 **** mCamera = mSceneMgr->createCamera("PlayerCam"); ! // Position it at 500 in Z direction ! mCamera->setPosition(Vector3(0,0,500)); // Look back along -Z mCamera->lookAt(Vector3(0,0,-300)); - mCamera->setNearClipDistance(5); } --- 131,141 ---- mCamera = mSceneMgr->createCamera("PlayerCam"); ! // Position it at the origin. ! mCamera->setPosition(Vector3(0, 0, 0)); ! // Look back along -Z mCamera->lookAt(Vector3(0,0,-300)); + mCamera->setNearClipDistance(0.5); } Index: ExampleFrameListener.h =================================================================== RCS file: /cvsroot/opal/opal/samples/src/ExampleFrameListener.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ExampleFrameListener.h 30 Mar 2005 23:26:25 -0000 1.1 --- ExampleFrameListener.h 31 Mar 2005 08:47:02 -0000 1.2 *************** *** 95,99 **** // your app from BaseOpalApp, you should be fine. ExampleFrameListener() ! {} // Takes a RenderWindow because it uses that to determine input context. --- 95,121 ---- // your app from BaseOpalApp, you should be fine. ExampleFrameListener() ! { ! mDebugOverlay = NULL; ! mUseBufferedInputKeys = false; ! mUseBufferedInputMouse = false; ! mInputTypeSwitchingOn = false; ! mRotateSpeed = 36; ! mMoveSpeed = 100; ! ! mEventProcessor = NULL; ! mInputDevice = NULL; ! ! mCamera = NULL; ! mWindow = NULL; ! mStatsOn = true; ! mNumScreenShots = 0; ! mTimeUntilNextToggle = 0; ! mSceneDetailIndex = 0; ! mMoveScale = 0.0f; ! mRotScale = 0.0f; ! mTranslateVector = Vector3::ZERO; ! mAniso = 1; ! mFiltering = TFO_BILINEAR; ! } // Takes a RenderWindow because it uses that to determine input context. *************** *** 142,146 **** { if (mInputTypeSwitchingOn) - { delete mEventProcessor; --- 164,167 ---- *************** *** 148,152 **** else { ! PlatformManager::getSingleton().destroyInputReader( mInputDevice ); } } --- 169,173 ---- else { ! PlatformManager::getSingleton().destroyInputReader(mInputDevice); } } Index: BaseOpalApp.h =================================================================== RCS file: /cvsroot/opal/opal/samples/src/BaseOpalApp.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** BaseOpalApp.h 30 Mar 2005 23:26:24 -0000 1.1 --- BaseOpalApp.h 31 Mar 2005 08:47:02 -0000 1.2 *************** *** 1,110 **** ! #ifndef BASE_OPAL_APP_H ! #define BASE_OPAL_APP_H #include "ExampleApplication.h" #include "ExampleFrameListener.h" #include <opal/opal.h> ! class BaseOpalApp : public ExampleApplication, public ExampleFrameListener { ! public: ! BaseOpalApp() ! : ExampleApplication(), ExampleFrameListener() { ! mSimulator = opal::createSimulator(); ! } ! ~BaseOpalApp() ! { ! mSimulator->destroy(); ! } ! protected: ! /// Register the frame listener with the Ogre root so it will ! /// call the listener at the appropriate time. ! virtual void createFrameListener() ! { ! ExampleFrameListener::init(ExampleApplication::mWindow, ! ExampleApplication::mCamera); ! mRoot->addFrameListener(this); ! } ! /// Initial scene setup function. Create and position lights, load ! /// models, create a camera, etc. ! virtual void createScene() = 0; ! /// Called once per frame after input and physics have been updated. ! /// This is a place for applications to do application-specific updates ! /// per frame. It takes the dt since the last frame as a parameter. ! /// This should return false if we should break out of the main loop. ! virtual bool appFrameStarted(opal::real dt) = 0; ! /// Called at the start of each frame. Calculate dt since the last ! /// frame, update input, update physics, etc. Returns false if we ! /// should break out of the main loop. Applications should not override ! /// this; instead, they should override 'appFrameStarted'. ! virtual bool frameStarted(const FrameEvent& evt) ! { ! if(ExampleApplication::mWindow->isClosed()) { ! return false; ! } ! Ogre::Real dt = evt.timeSinceLastFrame; ! if (false == handleInput(dt)) ! { ! return false; ! } ! // Simulate physics. ! mSimulator->simulate(dt); ! // Notify the application about the new frame. ! appFrameStarted((opal::real)dt); ! return true; ! } ! /// Returns false if we should break out of the main loop. ! bool handleInput(Ogre::Real dt) ! { ! // This variable can be used to keep keys from repeating too fast. ! if (mTimeUntilNextToggle >= 0) { ! mTimeUntilNextToggle -= dt; } ! // Acquire data from input devices. ! mInputDevice->capture(); ! // Handle keyboard input. ! if (processUnbufferedKeyInput(dt) == false) { ! return false; } ! // Handle mouse input. ! if (processUnbufferedMouseInput(dt) == false) { ! return false; } ! return true; ! } ! /// Process keyboard input here. Returns false if we should break out ! /// of the main loop. ! virtual bool processUnbufferedKeyInput(Ogre::Real dt) = 0; ! /// Process mouse input here. Returns false if we should break out of ! /// the main loop. ! virtual bool processUnbufferedMouseInput(Ogre::Real dt) = 0; ! /// Pointer to the OPAL Simulator. ! opal::Simulator* mSimulator; ! private: ! }; #endif --- 1,246 ---- ! #ifndef OPAL_SAMPLES_BASE_OPAL_APP_H ! #define OPAL_SAMPLES_BASE_OPAL_APP_H #include "ExampleApplication.h" #include "ExampleFrameListener.h" + #include "PhysicalEntity.h" #include <opal/opal.h> ! namespace opalSamples { ! class BaseOpalApp : public ExampleApplication, public ExampleFrameListener { ! public: ! BaseOpalApp() ! : ExampleApplication(), ExampleFrameListener() ! { ! mSimulator = opal::createSimulator(); ! mSimulator->setGravity(opal::Vec3r(0, (opal::real)-9.81, 0)); ! mPaused = false; ! } ! ~BaseOpalApp() ! { ! mSimulator->destroy(); ! mPhysicalEntityList.clear(); ! mPhysicalEntityMap.clear(); ! } ! protected: ! /// Register the frame listener with the Ogre root so it will ! /// call the listener at the appropriate time. ! virtual void createFrameListener() ! { ! ExampleFrameListener::init(ExampleApplication::mWindow, ! ExampleApplication::mCamera); ! mRoot->addFrameListener(this); ! } ! /// Initial scene setup function. Create and position lights, load ! /// models, create a camera, etc. ! virtual void createScene() = 0; ! /// Called once per frame after input and physics have been updated. ! /// This is a place for applications to do application-specific ! /// updates per frame. It takes the dt since the last frame as a ! /// parameter. This should return false if we should break out of ! /// the main loop. ! virtual bool appFrameStarted(opal::real dt) = 0; ! /// Called at the start of each frame. Calculate dt since the last ! /// frame, update input, update physics, etc. Returns false if we ! /// should break out of the main loop. Applications should not ! /// override this; instead, they should override 'appFrameStarted'. ! virtual bool frameStarted(const FrameEvent& evt) { ! if(ExampleApplication::mWindow->isClosed()) ! { ! return false; ! } ! Ogre::Real dt = evt.timeSinceLastFrame; ! if (false == handleInput(dt)) ! { ! return false; ! } ! if (!mPaused) ! { ! // Simulate physics. ! mSimulator->simulate(dt); ! } ! // Update the PhysicalEntities. ! size_t size = mPhysicalEntityList.size(); ! for(size_t i = 0; i<size; ++i) ! { ! mPhysicalEntityList.at(i)->update(dt); ! } ! // Notify the application about the new frame. ! appFrameStarted((opal::real)dt); ! return true; ! } ! ! /// Creates and returns a pointer to a PhysicalEntity. Takes ! /// the name of the new PhysicalEntity, the Ogre Entity, and a ! /// pointer to an OPAL Solid. ! PhysicalEntity* createPhysicalEntity(const std::string& name, ! Ogre::Entity* e, opal::Solid* s) { ! if (!s) ! { ! return NULL; ! } ! ! // Create an Ogre SceneNode. ! Ogre::SceneNode* sn = mSceneMgr->getRootSceneNode()-> ! createChildSceneNode(name); ! ! // Attach the Entity to the SceneNode. ! sn->attachObject(e); ! ! // Create a new Physical Entity. ! PhysicalEntity* pe = new PhysicalEntity(name, sn, s); ! ! // Store the pointer in the PhysicalEntity list. ! mPhysicalEntityList.push_back(pe); ! ! // If the name is not empty, also store the pointer in the ! // map of named PhysicalEntities. ! if (!name.empty()) ! { ! mPhysicalEntityMap[name] = pe; ! } ! ! return pe; } ! /// Finds a PhysicalEntity pointer by name. Returns NULL if the ! /// PhysicalEntity could not be found. ! PhysicalEntity* getPhysicalEntity(const std::string& name)const ! { ! std::map<std::string, PhysicalEntity*>::const_iterator iter = ! mPhysicalEntityMap.find(name); ! if (mPhysicalEntityMap.end() == iter) ! { ! // The named PhysicalEntity could not be found in the map. ! return NULL; ! } ! else ! { ! return (*iter).second; ! } ! } ! ! /// Destroys the given PhysicalEntity. ! void destroyPhysicalEntity(PhysicalEntity* pe) { ! if (!pe) ! { ! return; ! } ! ! // If the PhysicalEntity has a name, remove it from the ! // PhysicalEntity map. ! std::string name = pe->getName(); ! if (!name.empty()) ! { ! mPhysicalEntityMap.erase(name); ! } ! ! // Now remove the Solid. ! for(size_t i = 0; i<mPhysicalEntityList.size(); ++i) ! { ! if(mPhysicalEntityList[i] == pe) ! { ! // Destroy the PhysicalEntity's OPAL Solid. ! mSimulator->destroySolid(pe->getSolid()); ! ! // Destroy the PhysicalEntity's Ogre SceneNode. ! Ogre::SceneNode* sn = pe->getSceneNode(); ! ! // Detach and destroy all objects from the SceneNode. ! while(sn->numAttachedObjects() > 0) ! { ! MovableObject* thisObject = ! sn->detachObject(static_cast<unsigned short>(0)); ! delete thisObject; ! } ! ! // Destroy the SceneNodes all of its children. ! sn->removeAndDestroyAllChildren(); ! mSceneMgr->getRootSceneNode()->removeAndDestroyChild( ! sn->getName()); ! ! // Delete the PhysicalEntity. ! delete pe; ! ! // Copy the last element over this one and pop the last ! // one off, removing it from the PhysicalEntity list. ! mPhysicalEntityList[i] = mPhysicalEntityList.back(); ! mPhysicalEntityList.pop_back(); ! ! // Return early since we've already found the desired ! // element. ! return; ! } ! } } ! /// Returns false if we should break out of the main loop. ! bool handleInput(Ogre::Real dt) { ! // This variable can be used to keep keys from repeating too fast. ! if (mTimeUntilNextToggle >= 0) ! { ! mTimeUntilNextToggle -= dt; ! } ! ! // Acquire data from input devices. ! mInputDevice->capture(); ! ! // Handle keyboard input. ! if (processUnbufferedKeyInput(dt) == false) ! { ! return false; ! } ! ! // Handle mouse input. ! if (processUnbufferedMouseInput(dt) == false) ! { ! return false; ! } ! ! return true; } ! /// Process keyboard input here. Returns false if we should break out ! /// of the main loop. ! virtual bool processUnbufferedKeyInput(Ogre::Real dt) = 0; ! /// Process mouse input here. Returns false if we should break out of ! /// the main loop. ! virtual bool processUnbufferedMouseInput(Ogre::Real dt) = 0; ! /// Pointer to the OPAL Simulator. ! opal::Simulator* mSimulator; ! /// True when the physics simulation is paused. ! bool mPaused; ! /// Map of named PhysicalEntities. This is just used to find a ! /// PhysicalEntity by name. ! std::vector<PhysicalEntity*> mPhysicalEntityList; ! ! /// Map of named PhysicalEntities. ! std::map<std::string, PhysicalEntity*> mPhysicalEntityMap; ! ! private: ! }; ! } #endif |