User Tools

Site Tools


public:t-gede-13-1:lab5

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
public:t-gede-13-1:lab5 [2013/02/19 12:39] – [Lab Project] hannespublic:t-gede-13-1:lab5 [2024/04/29 13:33] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== LAB5: Scene Graph ====== ====== LAB5: Scene Graph ======
  
-This lab is based on "Chapter 6: Scene Managers" in the book "Ogre 3d 1.7 Beginner's Guide"+This lab is based on "Chapter 6: Scene Managers" in the book "Ogre 3d 1.7 Beginner's Guide" by Felix Kerger
  
 ===== Discussion ===== ===== Discussion =====
Line 49: Line 49:
  grassNode->attachObject(manual);  grassNode->attachObject(manual);
 </code>To add the two missing quads, use the following corner vertex positions: Quad 2 = (2.5,0,4.3),(-2.5,10,-4.3), (-2.0,0,-4.3), (2.5,10,4.3), Quad 3 = (2.5,0,-4.3),(-2.5,10,4.3),(-2.0,0,4.3),(2.5,10,-4.3). Don't forget to give the vertices texture coordinates, which are the same for all quads. Now call ''createGrass()'' from your ''createScene()'' method and you should see a single bundle of grass right in the middle of the scene. </code>To add the two missing quads, use the following corner vertex positions: Quad 2 = (2.5,0,4.3),(-2.5,10,-4.3), (-2.0,0,-4.3), (2.5,10,4.3), Quad 3 = (2.5,0,-4.3),(-2.5,10,4.3),(-2.0,0,4.3),(2.5,10,-4.3). Don't forget to give the vertices texture coordinates, which are the same for all quads. Now call ''createGrass()'' from your ''createScene()'' method and you should see a single bundle of grass right in the middle of the scene.
-  - **Instance Grass Object** To create multiple instances of the object you created, you need to first convert it into a proper Ogre **Mesh**. In the ''createGrass()'' method, **replace** your last two lines with the following line:<code>  +  - **Instance Grass Object** To create multiple instances of the object you created, you need to first convert it into a proper Ogre **Mesh**. In the ''createGrass()'' method, **replace** your last two lines with the following line:<code>manual->convertToMesh("BladesOfGrass");
-  manual->convertToMesh("BladesOfGrass");+
 </code> This essentially creates a new model with the name "BladesOfGrass", which you can now refer to in subsequent ''createEntity(<name>)'' calls. So do that next: Let's create 2500 instances of this model, spread across the ground in a 50x50 square patch! You can do this using a nested for-loop (over i=0-49 and j=0-49) and inside the loop do the following:<code> </code> This essentially creates a new model with the name "BladesOfGrass", which you can now refer to in subsequent ''createEntity(<name>)'' calls. So do that next: Let's create 2500 instances of this model, spread across the ground in a 50x50 square patch! You can do this using a nested for-loop (over i=0-49 and j=0-49) and inside the loop do the following:<code>
- Ogre::Entity* ent = _sceneManager->createEntity("BladesOfGrass"); +Ogre::Entity* ent = _sceneManager->createEntity("BladesOfGrass"); 
- Ogre::SceneNode* node = _sceneManager->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(i*3,-10,j*3)); +Ogre::SceneNode* node = _sceneManager->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(i*3,-10,j*3)); 
- node->attachObject(ent);+node->attachObject(ent);
 </code>Notice that you are not naming each entity or node when you create them! But Ogre requires unique names for each of its nodes and entities. When a name is not given, Ogre actually assigns unique names. you can take a look at these names by printing them out inside the loop:<code> </code>Notice that you are not naming each entity or node when you create them! But Ogre requires unique names for each of its nodes and entities. When a name is not given, Ogre actually assigns unique names. you can take a look at these names by printing them out inside the loop:<code>
 std::cout << node->getName() << "::"<< ent->getName() << std::endl; std::cout << node->getName() << "::"<< ent->getName() << std::endl;
Line 60: Line 59:
   - **Optimize using Static Geometry** If we assume that the each grass model will stay in the same location in world space throughout your application life, we can optimize rendering by bundling them up into a packet of pre-transformed static geometry. First create a single new ''StaticGeometry'' instance. Add this line after the ''convertToMesh'' call:<code>Ogre::StaticGeometry* field = _sceneManager->createStaticGeometry("FieldOfGrass");   - **Optimize using Static Geometry** If we assume that the each grass model will stay in the same location in world space throughout your application life, we can optimize rendering by bundling them up into a packet of pre-transformed static geometry. First create a single new ''StaticGeometry'' instance. Add this line after the ''convertToMesh'' call:<code>Ogre::StaticGeometry* field = _sceneManager->createStaticGeometry("FieldOfGrass");
 </code> And now, instead of instancing the grass entities into the scene graph directly, we add them into the static geometry object in the body of our nested for-loop. So, the body of the loop becomes: <code> </code> And now, instead of instancing the grass entities into the scene graph directly, we add them into the static geometry object in the body of our nested for-loop. So, the body of the loop becomes: <code>
- Ogre::Entity* ent = _sceneManager->createEntity("BladesOfGrass"); +Ogre::Entity* ent = _sceneManager->createEntity("BladesOfGrass"); 
- field->addEntity(ent,Ogre::Vector3(i*3,-10,j*3));+field->addEntity(ent,Ogre::Vector3(i*3,-10,j*3));
 </code> Finally, after we have added all the entities to our ''field'', we need to ask the static geometry to "bake"/optimize with the following call: <code>field->build(); </code> Finally, after we have added all the entities to our ''field'', we need to ask the static geometry to "bake"/optimize with the following call: <code>field->build();
 </code>Now you should try running the scene again and see if there is any improved performance!  </code>Now you should try running the scene again and see if there is any improved performance! 
 +  - **Testing Another Scene Manager** The so called "world geometry" of a game is often considered static and can be highly optimized for rendering. One optimized format for representing a static world is the BSP level format used originally by id's Quake engine. Ogre can read worlds in this format and display them using the ''BspSceneManager''. To try this, add the following method in your application<code> void loadQuakeMap() {     
 + // add the Quake archive to the world resource group
 + Ogre::ResourceGroupManager& rgm = Ogre::ResourceGroupManager::getSingleton();
 + rgm.addResourceLocation("/Development/OgreSDK_vc10_v1-8-1/media/packs/chiropteraDM.pk3", "Zip", rgm.getWorldResourceGroupName(), true);
 + // associate the world geometry with the world resource group, and then load the group
 + rgm.linkWorldGeometryToResourceGroup(rgm.getWorldResourceGroupName(),"maps/chiropteradm.bsp", _sceneManager);
 + rgm.initialiseResourceGroup(rgm.getWorldResourceGroupName());
 + rgm.loadResourceGroup(rgm.getWorldResourceGroupName(), false);
 + }
 +</code>Of course replace the absolute path here with a path that is correct on your system. To initialize the camera to properly view this map (it's actually rotated), add the following camera initialization method:<code> void setupQuakeMapView(Ogre::Camera* cam){
 + // modify camera for close work
 + cam->setNearClipDistance(4);
 + cam->setFarClipDistance(4000);
 + // set a random player starting point
 + Ogre::ViewPoint vp = _sceneManager->getSuggestedViewpoint(true);
 + // Quake uses the Z axis as the up axis, so make necessary adjustments
 + cam->setFixedYawAxis(true, Ogre::Vector3::UNIT_Z);
 + cam->pitch(Ogre::Degree(90));
 + cam->setPosition(vp.position);
 + cam->rotate(vp.orientation);
 + }  
 +</code>
 +Finally, you need to tell Ogre to use the BSP scene manager, which you do when you create your scene manager in the ''startup()'' method of your application. You can name the scene manager explicitly like this:<code>_sceneManager =_root->createSceneManager("BspSceneManager");
 +</code> Now you have everything you need to display the Quake level, all you have to do is to call ''loadQuakeMap()'' and ''setupQuakeMapView(camera) from your ''startup'' method and comment out some of the other things you don't wish to include (e.g. the grass above). Also, you may want to free up the camera movement (i.e. go back to the free mouse movement of camera, instead of having camera following the Ogre).
 +
 +
  
  
/var/www/cadia.ru.is/wiki/data/attic/public/t-gede-13-1/lab5.1361277544.txt.gz · Last modified: 2024/04/29 13:32 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki