[Opal-commits] opal/samples/src BaseOpalApp.h,1.2,1.3 ExampleFrameListener.h,1.2,1.3 TemplateApp.cpp
Status: Inactive
Brought to you by:
tylerstreeter
|
From: tylerstreeter <tyl...@us...> - 2005-04-01 23:14:16
|
Update of /cvsroot/opal/opal/samples/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30340/samples/src Modified Files: BaseOpalApp.h ExampleFrameListener.h TemplateApp.cpp Log Message: added stuff to the playpen sample app Index: TemplateApp.cpp =================================================================== RCS file: /cvsroot/opal/opal/samples/src/TemplateApp.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TemplateApp.cpp 31 Mar 2005 08:47:02 -0000 1.2 --- TemplateApp.cpp 1 Apr 2005 23:13:50 -0000 1.3 *************** *** 7,178 **** { public: ! MyApp() ! : BaseOpalApp() ! { ! mUseShadows = true; ! } ! ~MyApp() ! {} protected: ! virtual void createScene() ! { ! // Setup shadows. ! if (mUseShadows) ! { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE); ! } ! else ! { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE); ! } ! // Set the ambient light level. ! mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); ! // Create a light source. ! Ogre::Light* light = mSceneMgr->createLight("light0"); ! light->setType(Ogre::Light::LT_POINT); ! light->setPosition(-80.0, 30.0, -10.0); ! // Load models, create physical objects, etc. here. ! } ! virtual bool appFrameStarted(opal::real dt) ! { ! // Do per-frame application specific things here. ! // Return true to continue looping. ! return true; ! } ! virtual bool processUnbufferedKeyInput(Ogre::Real dt) ! { ! // Check if we should quit looping. ! if(mInputDevice->isKeyDown(KC_ESCAPE) ! || mInputDevice->isKeyDown(KC_Q)) ! { ! return false; ! } ! // 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; ! if (mUseShadows) ! { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE); ! } ! else ! { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE); ! } ! // Reset the timer for toggle keys. ! mTimeUntilNextToggle = 0.5; ! } ! // Toggle HUD. ! if (mInputDevice->isKeyDown(KC_H) && mTimeUntilNextToggle <= 0) ! { ! mStatsOn = !mStatsOn; ! showDebugOverlay(mStatsOn); ! mTimeUntilNextToggle = 1; ! } ! // Handy screenshot saving procedure. ! if (mInputDevice->isKeyDown(KC_SYSRQ) ! && mTimeUntilNextToggle <= 0) ! { ! char tmp[20]; ! sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots); ! ExampleApplication::mWindow->writeContentsToFile(tmp); ! ExampleApplication::mWindow->setDebugText(String("Wrote ") ! + tmp); ! // Reset the timer for toggle keys. ! mTimeUntilNextToggle = 0.5; ! } ! // Return true to continue looping. ! return true; } ! virtual bool processUnbufferedMouseInput(Ogre::Real dt) { ! // The following code checks the mouse button state. ! if (true == mInputDevice->getMouseButton(0)) { ! // The left mouse button is down. } else { ! // The left mouse button is up. } ! // The following code checks how far the mouse has move since ! // the last poll. This data can be used to rotate the camera ! // around its X and Y axes (yaw and pitch, respectively). ! Ogre::Real rotX = -mInputDevice->getMouseRelativeX(); ! Ogre::Real rotY = -mInputDevice->getMouseRelativeY(); ! // Return true to continue looping. ! return true; } ! private: ! /// Used to toggle shadows on and off. ! bool mUseShadows; ! }; } --- 7,207 ---- { public: ! MyApp(); ! ~MyApp(); protected: ! virtual void createScene(); ! virtual bool appFrameStarted(opal::real dt); ! virtual bool processUnbufferedKeyInput(Ogre::Real dt); ! virtual bool processUnbufferedMouseInput(Ogre::Real dt); ! private: ! /// Used to toggle shadows on and off. ! bool mUseShadows; ! }; ! MyApp::MyApp() ! : BaseOpalApp() ! { ! mUseShadows = true; ! } ! ~MyApp::MyApp() ! { ! } ! void MyApp::createScene() ! { ! // Setup shadows. ! if (mUseShadows) ! { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE); ! } ! else ! { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE); ! } ! // Set the ambient light level. ! mSceneMgr->setAmbientLight(ColourValue(0.3, 0.3, 0.3)); ! // Create a light source. ! Ogre::Light* light = mSceneMgr->createLight("light0"); ! light->setType(Ogre::Light::LT_POINT); ! light->setDiffuseColour(1.0, 1.0, 1.0); ! light->setSpecularColour(1.0, 1.0, 1.0); ! light->setPosition(100.0, 300.0, 100.0); ! // Setup the initial camera position. ! ExampleApplication::mCamera->setPosition( ! Ogre::Vector3(0, 30, 50)); ! ExampleApplication::mCamera->lookAt(0, 0, 0); ! // Load models, create physical objects, etc. here. ! } ! bool MyApp::appFrameStarted(opal::real dt) ! { ! // Do per-frame application specific things here. ! // Return true to continue looping. ! return true; ! } ! bool MyApp::processUnbufferedKeyInput(Ogre::Real dt) ! { ! // Check if we should quit looping. ! if(mInputDevice->isKeyDown(KC_ESCAPE) ! || mInputDevice->isKeyDown(KC_Q)) ! { ! return false; ! } ! // 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 updates the camera's position. ! Ogre::Vector3 cameraDir = Ogre::Vector3::ZERO; ! bool cameraMoved = false; ! if (mInputDevice->isKeyDown(KC_LEFT) ! || mInputDevice->isKeyDown(KC_A)) ! { ! // Move camera left. ! cameraDir.x -= (dt * 5); ! cameraMoved = true; ! } ! if (mInputDevice->isKeyDown(KC_RIGHT) ! || mInputDevice->isKeyDown(KC_D)) ! { ! // Move camera right. ! cameraDir.x += (dt * 5); ! cameraMoved = true; ! } ! if (mInputDevice->isKeyDown(KC_UP) ! || mInputDevice->isKeyDown(KC_W)) ! { ! // Move camera forward. ! cameraDir.z -= (dt * 5); ! cameraMoved = true; ! } ! if (mInputDevice->isKeyDown(KC_DOWN) ! || mInputDevice->isKeyDown(KC_S)) ! { ! // Move camera backward. ! cameraDir.z += (dt * 5); ! cameraMoved = true; ! } ! if (!cameraMoved) ! { ! // Slow physical camera motion if necessary. } ! // Use the camera dir vector to translate the camera. ! ExampleApplication::mCamera->moveRelative(cameraDir); ! ! // Toggle shadows. ! if(mInputDevice->isKeyDown(KC_H) && mTimeUntilNextToggle <= 0) { ! mUseShadows = !mUseShadows; ! ! if (mUseShadows) { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE); } else { ! mSceneMgr->setShadowTechnique(SHADOWTYPE_NONE); } ! // Reset the timer for toggle keys. ! mTimeUntilNextToggle = 0.5; ! } ! // Toggle HUD. ! if (mInputDevice->isKeyDown(KC_H) && mTimeUntilNextToggle <= 0) ! { ! mStatsOn = !mStatsOn; ! showDebugOverlay(mStatsOn); ! mTimeUntilNextToggle = 1; } ! // Handy screenshot saving procedure. ! if (mInputDevice->isKeyDown(KC_SYSRQ) ! && mTimeUntilNextToggle <= 0) ! { ! char tmp[20]; ! sprintf(tmp, "screenshot_%d.png", ++mNumScreenShots); ! ExampleApplication::mWindow->writeContentsToFile(tmp); ! ExampleApplication::mWindow->setDebugText(String("Wrote ") ! + tmp); ! ! // Reset the timer for toggle keys. ! mTimeUntilNextToggle = 0.5; ! } ! ! // Return true to continue looping. ! return true; ! } ! ! bool MyApp::processUnbufferedMouseInput(Ogre::Real dt) ! { ! // The following code checks how far the mouse has move since ! // the last poll. This data can be used to rotate the camera ! // around its X and Y axes (pitch and yaw, respectively). ! Ogre::Radian rotX(-mInputDevice->getMouseRelativeX() * mRotateSpeed); ! Ogre::Radian rotY(-mInputDevice->getMouseRelativeY() * mRotateSpeed); ! ! // The following code checks the mouse button state. ! if (true == mInputDevice->getMouseButton(1)) ! { ! // The right mouse button is down. ! ! // Use the relative mouse motion to rotate the camera. ! ExampleApplication::mCamera->yaw(rotY); ! ExampleApplication::mCamera->pitch(rotX); ! } ! else ! { ! // The right mouse button is up. ! } ! ! // Return true to continue looping. ! return true; ! } } Index: ExampleFrameListener.h =================================================================== RCS file: /cvsroot/opal/opal/samples/src/ExampleFrameListener.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ExampleFrameListener.h 31 Mar 2005 08:47:02 -0000 1.2 --- ExampleFrameListener.h 1 Apr 2005 23:13:50 -0000 1.3 *************** *** 128,133 **** mUseBufferedInputMouse = useBufferedInputMouse; mInputTypeSwitchingOn = mUseBufferedInputKeys || mUseBufferedInputMouse; ! mRotateSpeed = 36; ! mMoveSpeed = 100; if (mInputTypeSwitchingOn) --- 128,133 ---- mUseBufferedInputMouse = useBufferedInputMouse; mInputTypeSwitchingOn = mUseBufferedInputKeys || mUseBufferedInputMouse; ! mRotateSpeed = 0.1; ! mMoveSpeed = 40; if (mInputTypeSwitchingOn) Index: BaseOpalApp.h =================================================================== RCS file: /cvsroot/opal/opal/samples/src/BaseOpalApp.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** BaseOpalApp.h 31 Mar 2005 08:47:02 -0000 1.2 --- BaseOpalApp.h 1 Apr 2005 23:13:50 -0000 1.3 *************** *** 18,28 **** mSimulator->setGravity(opal::Vec3r(0, (opal::real)-9.81, 0)); mPaused = false; } ~BaseOpalApp() { ! mSimulator->destroy(); ! mPhysicalEntityList.clear(); mPhysicalEntityMap.clear(); } --- 18,37 ---- mSimulator->setGravity(opal::Vec3r(0, (opal::real)-9.81, 0)); mPaused = false; + mNamelessObjectCount = 0; } ~BaseOpalApp() { ! while (!mPhysicalEntityList.empty()) ! { ! delete mPhysicalEntityList.back(); ! mPhysicalEntityList.pop_back(); ! } ! ! // Every pointer in the mPhysicalEntityMap is a duplicate of ! // a pointer in mPhysicalEntityList, so we don't need to ! // delete the memory they point to. mPhysicalEntityMap.clear(); + mSimulator->destroy(); } *************** *** 87,91 **** /// 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) --- 96,101 ---- /// 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. The name must be unique; unique ! /// names will be automatically generated for empty name strings. PhysicalEntity* createPhysicalEntity(const std::string& name, Ogre::Entity* e, opal::Solid* s) *************** *** 96,102 **** } // Create an Ogre SceneNode. Ogre::SceneNode* sn = mSceneMgr->getRootSceneNode()-> ! createChildSceneNode(name); // Attach the Entity to the SceneNode. --- 106,122 ---- } + std::string nameStr = name; + if (nameStr.empty()) + { + // Make a unique name. + char newName[20]; + sprintf(newName, "object%d", mNamelessObjectCount); + mNamelessObjectCount++; + nameStr = newName; + } + // Create an Ogre SceneNode. Ogre::SceneNode* sn = mSceneMgr->getRootSceneNode()-> ! createChildSceneNode(nameStr); // Attach the Entity to the SceneNode. *************** *** 104,108 **** // Create a new Physical Entity. ! PhysicalEntity* pe = new PhysicalEntity(name, sn, s); // Store the pointer in the PhysicalEntity list. --- 124,128 ---- // Create a new Physical Entity. ! PhysicalEntity* pe = new PhysicalEntity(nameStr, sn, s); // Store the pointer in the PhysicalEntity list. *************** *** 113,119 **** if (!name.empty()) { ! mPhysicalEntityMap[name] = pe; } return pe; } --- 133,204 ---- if (!name.empty()) { ! mPhysicalEntityMap[nameStr] = pe; ! } ! ! return pe; ! } ! ! /// Creates a PhysicalEntity drawn as a box. The OPAL Solid can ! /// be any shape, however. This is useful for prototyping scenes ! /// when you don't have a specific visual mesh to use. The name ! /// must be unique; unique names will be automatically generated ! /// for empty name strings. ! PhysicalEntity* createPhysicalEntityBox(const std::string& name, ! const std::string& materialName, Ogre::Vector3 dimensions, ! opal::Solid* s) ! { ! PhysicalEntity* pe = NULL; ! ! std::string nameStr = name; ! if (nameStr.empty()) ! { ! // Make a unique name. ! char newName[20]; ! sprintf(newName, "object%d", mNamelessObjectCount); ! mNamelessObjectCount++; ! nameStr = newName; ! } ! ! // This mesh must be stored as a box with dimensions 1x1x1. ! Entity* e = mSceneMgr->createEntity(nameStr, "cube.mesh"); ! e->setMaterialName(materialName); ! pe = createPhysicalEntity(nameStr, e, s); ! ! // Scale the mesh according to the given dimensions. ! e->getParentSceneNode()->scale(dimensions[0], dimensions[1], ! dimensions[2]); ! ! return pe; ! } ! ! /// Creates a PhysicalEntity drawn as a sphere. The OPAL Solid can ! /// be any shape, however. This is useful for prototyping scenes ! /// when you don't have a specific visual mesh to use. The name ! /// must be unique; unique names will be automatically generated ! /// for empty name strings. ! PhysicalEntity* createPhysicalEntitySphere(const std::string& name, ! const std::string& materialName, Ogre::Real radius, ! opal::Solid* s) ! { ! PhysicalEntity* pe = NULL; ! ! std::string nameStr = name; ! if (nameStr.empty()) ! { ! // Make a unique name. ! char newName[20]; ! sprintf(newName, "object%d", mNamelessObjectCount); ! mNamelessObjectCount++; ! nameStr = newName; } + // This mesh must be stored as a sphere with radius 1. + Entity* e = mSceneMgr->createEntity(nameStr, "sphere.mesh"); + e->setMaterialName(materialName); + pe = createPhysicalEntity(nameStr, e, s); + + // Scale the mesh according to the given dimensions. + e->getParentSceneNode()->scale(radius, radius, radius); + return pe; } *************** *** 233,236 **** --- 318,324 ---- bool mPaused; + /// A counter used to generate unique object names. + unsigned int mNamelessObjectCount; + /// Map of named PhysicalEntities. This is just used to find a /// PhysicalEntity by name. |