[1] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | You may use this sample code for anything you like, it is not covered by the |
---|
| 11 | LGPL like the rest of the engine. |
---|
| 12 | ----------------------------------------------------------------------------- |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | /* |
---|
| 16 | ----------------------------------------------------------------------------- |
---|
| 17 | Filename: BspCollision.cpp |
---|
| 18 | Description: Somewhere to play in the sand... |
---|
| 19 | ----------------------------------------------------------------------------- |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "OgreReferenceAppLayer.h" |
---|
| 23 | |
---|
| 24 | #include "ExampleRefAppApplication.h" |
---|
| 25 | #include "OgreStringConverter.h" |
---|
| 26 | |
---|
| 27 | // Hacky globals |
---|
| 28 | ApplicationObject *ball; |
---|
| 29 | |
---|
| 30 | SceneNode* targetNode; |
---|
| 31 | RaySceneQuery* rsq = 0; |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | // Event handler to add ability to alter curvature |
---|
| 35 | class BspCollisionListener : public ExampleRefAppFrameListener |
---|
| 36 | { |
---|
| 37 | protected: |
---|
| 38 | public: |
---|
| 39 | BspCollisionListener(RenderWindow* win, CollideCamera* cam) |
---|
| 40 | : ExampleRefAppFrameListener(win, cam) |
---|
| 41 | { |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | bool frameEnded(const FrameEvent& evt) |
---|
| 46 | { |
---|
| 47 | // local just to stop toggles flipping too fast |
---|
| 48 | static Real timeUntilNextToggle = 0; |
---|
| 49 | |
---|
| 50 | // Deal with time delays that are too large |
---|
| 51 | // If we exceed this limit, we ignore |
---|
| 52 | static const Real MAX_TIME_INCREMENT = 0.5f; |
---|
| 53 | if (evt.timeSinceLastEvent > MAX_TIME_INCREMENT) |
---|
| 54 | { |
---|
| 55 | return true; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | if (timeUntilNextToggle >= 0) |
---|
| 59 | timeUntilNextToggle -= evt.timeSinceLastFrame; |
---|
| 60 | |
---|
| 61 | // Call superclass |
---|
| 62 | bool ret = ExampleRefAppFrameListener::frameEnded(evt); |
---|
| 63 | |
---|
| 64 | if (mKeyboard->isKeyDown(OIS::KC_SPACE) && timeUntilNextToggle <= 0) |
---|
| 65 | { |
---|
| 66 | timeUntilNextToggle = 2; |
---|
| 67 | ball->setPosition(mCamera->getPosition() + |
---|
| 68 | mCamera->getDirection() * mCamera->getNearClipDistance() * 2); |
---|
| 69 | ball->setLinearVelocity(mCamera->getDirection() * 200); |
---|
| 70 | ball->setAngularVelocity(Vector3::ZERO); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | // Move the targeter |
---|
| 74 | rsq->setRay(mCamera->getRealCamera()->getCameraToViewportRay(0.5, 0.5)); |
---|
| 75 | RaySceneQueryResult& rsqResult = rsq->execute(); |
---|
| 76 | RaySceneQueryResult::iterator ri = rsqResult.begin(); |
---|
| 77 | if (ri != rsqResult.end()) |
---|
| 78 | { |
---|
| 79 | RaySceneQueryResultEntry& res = *ri; |
---|
| 80 | targetNode->setPosition(rsq->getRay().getPoint(res.distance)); |
---|
| 81 | |
---|
| 82 | } |
---|
| 83 | return ret; |
---|
| 84 | } |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | class BspCollisionApplication : public ExampleRefAppApplication |
---|
| 88 | { |
---|
| 89 | public: |
---|
| 90 | BspCollisionApplication() { |
---|
| 91 | |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | ~BspCollisionApplication() |
---|
| 95 | { |
---|
| 96 | delete rsq; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | protected: |
---|
| 100 | |
---|
| 101 | void chooseSceneManager(void) |
---|
| 102 | { |
---|
| 103 | mSceneMgr = mRoot->createSceneManager("BspSceneManager"); |
---|
| 104 | } |
---|
| 105 | void createWorld(void) |
---|
| 106 | { |
---|
| 107 | // Create BSP-specific world |
---|
| 108 | mWorld = new World(mSceneMgr, World::WT_REFAPP_BSP); |
---|
| 109 | } |
---|
| 110 | void createScene(void) |
---|
| 111 | { |
---|
| 112 | mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_MODULATIVE); |
---|
| 113 | // Set ambient light |
---|
| 114 | mSceneMgr->setAmbientLight(ColourValue(0.2, 0.2, 0.2)); |
---|
| 115 | // Create a point light |
---|
| 116 | Light* l = mSceneMgr->createLight("MainLight"); |
---|
| 117 | l->setPosition(-100,50,100); |
---|
| 118 | l->setAttenuation(8000,1,0,0); |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | // Setup World |
---|
| 122 | mWorld->setGravity(Vector3(0, 0, -60)); |
---|
| 123 | mWorld->getSceneManager()->setWorldGeometry("ogretestmap.bsp"); |
---|
| 124 | |
---|
| 125 | // modify camera for close work |
---|
| 126 | mCamera->setNearClipDistance(10); |
---|
| 127 | mCamera->setFarClipDistance(20000); |
---|
| 128 | |
---|
| 129 | // Also change position, and set Quake-type orientation |
---|
| 130 | // Get random player start point |
---|
| 131 | ViewPoint vp = mSceneMgr->getSuggestedViewpoint(true); |
---|
| 132 | mCamera->setPosition(vp.position); |
---|
| 133 | mCamera->pitch(Degree(90)); // Quake uses X/Y horizon, Z up |
---|
| 134 | mCamera->rotate(vp.orientation); |
---|
| 135 | // Don't yaw along variable axis, causes leaning |
---|
| 136 | mCamera->setFixedYawAxis(true, Vector3::UNIT_Z); |
---|
| 137 | // Look at the boxes |
---|
| 138 | mCamera->lookAt(-150,40,30); |
---|
| 139 | |
---|
| 140 | ball = mWorld->createBall("ball", 7, vp.position + Vector3(0,0,80)); |
---|
| 141 | ball->setDynamicsEnabled(true); |
---|
| 142 | ball->getEntity()->setMaterialName("Ogre/Eyes"); |
---|
| 143 | |
---|
| 144 | OgreRefApp::Box* box = mWorld->createBox("shelf", 75, 125, 5, Vector3(-150, 40, 30)); |
---|
| 145 | box->getEntity()->setMaterialName("Examples/Rocky"); |
---|
| 146 | |
---|
| 147 | static const Real BOX_SIZE = 15.0f; |
---|
| 148 | static const int num_rows = 3; |
---|
| 149 | |
---|
| 150 | for (int row = 0; row < num_rows; ++row) |
---|
| 151 | { |
---|
| 152 | for (int i = 0; i < (num_rows-row); ++i) |
---|
| 153 | { |
---|
| 154 | Real row_size = (num_rows - row) * BOX_SIZE * 1.25; |
---|
| 155 | String name = "box"; |
---|
| 156 | name += StringConverter::toString((row*num_rows) + i); |
---|
| 157 | box = mWorld->createBox(name, BOX_SIZE,BOX_SIZE,BOX_SIZE , |
---|
| 158 | Vector3(-150, |
---|
| 159 | 40 - (row_size * 0.5) + (i * BOX_SIZE * 1.25) , |
---|
| 160 | 32.5 + (BOX_SIZE / 2) + (row * BOX_SIZE))); |
---|
| 161 | box->setDynamicsEnabled(false, true); |
---|
| 162 | box->getEntity()->setMaterialName("Examples/10PointBlock"); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | mCamera->setCollisionEnabled(false); |
---|
| 166 | mCamera->getRealCamera()->setQueryFlags(0); |
---|
| 167 | |
---|
| 168 | // Create the targeting sphere |
---|
| 169 | Entity* targetEnt = mSceneMgr->createEntity("testray", "sphere.mesh"); |
---|
| 170 | MaterialPtr mat = MaterialManager::getSingleton().create("targeter", |
---|
| 171 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); |
---|
| 172 | Pass* pass = mat->getTechnique(0)->getPass(0); |
---|
| 173 | TextureUnitState* tex = pass->createTextureUnitState(); |
---|
| 174 | tex->setColourOperationEx(LBX_SOURCE1, LBS_MANUAL, LBS_CURRENT, |
---|
| 175 | ColourValue::Red); |
---|
| 176 | pass->setLightingEnabled(false); |
---|
| 177 | pass->setSceneBlending(SBT_ADD); |
---|
| 178 | pass->setDepthWriteEnabled(false); |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | targetEnt->setMaterialName("targeter"); |
---|
| 182 | targetEnt->setCastShadows(false); |
---|
| 183 | targetEnt->setQueryFlags(0); |
---|
| 184 | targetNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
| 185 | targetNode->scale(0.025, 0.025, 0.025); |
---|
| 186 | targetNode->attachObject(targetEnt); |
---|
| 187 | |
---|
| 188 | rsq = mSceneMgr->createRayQuery(Ray()); |
---|
| 189 | rsq->setSortByDistance(true, 1); |
---|
| 190 | rsq->setWorldFragmentType(SceneQuery::WFT_SINGLE_INTERSECTION); |
---|
| 191 | } |
---|
| 192 | // Create new frame listener |
---|
| 193 | void createFrameListener(void) |
---|
| 194 | { |
---|
| 195 | mFrameListener= new BspCollisionListener(mWindow, mCamera); |
---|
| 196 | mRoot->addFrameListener(mFrameListener); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | public: |
---|
| 200 | |
---|
| 201 | }; |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 206 | #define WIN32_LEAN_AND_MEAN |
---|
| 207 | #include "windows.h" |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) |
---|
| 211 | #else |
---|
| 212 | int main(int argc, char **argv) |
---|
| 213 | #endif |
---|
| 214 | { |
---|
| 215 | // Create application object |
---|
| 216 | BspCollisionApplication app; |
---|
| 217 | |
---|
| 218 | try { |
---|
| 219 | app.go(); |
---|
| 220 | } catch( Exception& e ) { |
---|
| 221 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
| 222 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
| 223 | #else |
---|
| 224 | std::cerr << "An exception has occured: " << e.getFullDescription(); |
---|
| 225 | #endif |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | return 0; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | |
---|