[Orxonox-commit 313] r2942 - in branches/map/src: orxonox/objects orxonox/objects/controllers orxonox/overlays/map orxonox/tools util

Naaduun at orxonox.net Naaduun at orxonox.net
Thu Apr 30 14:12:57 CEST 2009


Author: Naaduun
Date: 2009-04-30 14:12:57 +0200 (Thu, 30 Apr 2009)
New Revision: 2942

Added:
   branches/map/src/orxonox/tools/DynamicLines.cpp
   branches/map/src/orxonox/tools/DynamicLines.h
   branches/map/src/orxonox/tools/DynamicRenderable.cpp
   branches/map/src/orxonox/tools/DynamicRenderable.h
Modified:
   branches/map/src/orxonox/objects/RadarViewable.cc
   branches/map/src/orxonox/objects/RadarViewable.h
   branches/map/src/orxonox/objects/controllers/HumanController.cc
   branches/map/src/orxonox/objects/controllers/HumanController.h
   branches/map/src/orxonox/overlays/map/Map.cc
   branches/map/src/orxonox/overlays/map/Map.h
   branches/map/src/orxonox/tools/CMakeLists.txt
   branches/map/src/util/CMakeLists.txt
Log:
=added dynamic libs to orxonox/tools. changed humancontroller to use mouseview

Modified: branches/map/src/orxonox/objects/RadarViewable.cc
===================================================================
--- branches/map/src/orxonox/objects/RadarViewable.cc	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/orxonox/objects/RadarViewable.cc	2009-04-30 12:12:57 UTC (rev 2942)
@@ -34,6 +34,10 @@
 #include "core/CoreIncludes.h"
 #include "objects/worldentities/WorldEntity.h"
 #include "objects/Radar.h"
+#include "util/String.h"
+#include <OgreManualObject.h>
+#include "overlays/map/Map.h"
+#include "orxonox/tools/DynamicLines.h"
 
 namespace orxonox
 {
@@ -46,10 +50,66 @@
         , radarObjectDescription_("staticObject")
         , MapNode_(NULL)
         , MapEntity_(NULL)
+        , line_(NULL)
     {
         RegisterRootObject(RadarViewable);
+
+        this->uniqueId_=getUniqueNumberString();
+/*
+        if(Map::getSingletonPtr() && Map::getSingletonPtr()->getMapSceneManagerPtr())
+        {
+            this->addEntity();
+        }
+
+        */
     }
 
+
+    RadarViewable::~RadarViewable()
+    {
+        delete MapNode_;
+        delete MapEntity_;
+    }
+
+    void RadarViewable::addMapEntity()
+    { //TODO Check shape and add accordantly
+        if( this->MapNode_ && !this->MapEntity_ && Map::getSingletonPtr() && Map::getSingletonPtr()->getMapSceneManagerPtr() )
+        {
+            COUT(0) << "Adding " << this->uniqueId_ << " to Map.\n";
+            this->MapEntity_ = Map::getSingletonPtr()->getMapSceneManagerPtr()->createEntity( this->uniqueId_, "drone.mesh");
+            /*this->line_ =  Map::getSingletonPtr()->getMapSceneManagerPtr()->createManualObject(this->uniqueId_ + "_l"); 
+            this->line_->begin("Map/line_", Ogre::RenderOperation::OT_LINE_STRIP); 
+            //line_->position(0, -it->getRVWorldPosition().y, 0); 
+            //this->line_->position(0, -20, 0); 
+            this->line_->position(0, 0, -10); //Front Arrow
+            this->line_->position(0, 0, 0);
+
+            this->line_->end(); */
+            this->line_ = new DynamicLines(Ogre::RenderOperation::OT_LINE_LIST);
+            this->line_->addPoint( Vector3(0,0,0) );
+            this->line_->addPoint( Vector3(0,0,0) );
+
+            this->MapNode_->attachObject( this->MapEntity_ );
+            this->MapNode_->attachObject( this->line_ );
+        }
+        else
+        {
+            COUT(0) << "Unable to load " << this->uniqueId_ << " to Map.\n";
+        }
+    }
+
+    void RadarViewable::updateMapPosition()
+    {
+        if( this->MapNode_ )
+        {
+            this->MapNode_->setPosition( this->getRVWorldPosition() );
+            this->MapNode_->translate( this->getRVOrientedVelocity(), (Ogre::Node::TransformSpace)3 );
+            this->MapNode_->setOrientation( this->getWorldEntity()->getOrientation() );
+Vector3 v = this->getRVWorldPosition();
+            this->line_->setPoint(1, Vector3(0,v.y,0) );
+        }
+    }
+
     void RadarViewable::setRadarObjectDescription(const std::string& str)
     {
         Radar* radar = Radar::getInstancePtr();

Modified: branches/map/src/orxonox/objects/RadarViewable.h
===================================================================
--- branches/map/src/orxonox/objects/RadarViewable.h	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/orxonox/objects/RadarViewable.h	2009-04-30 12:12:57 UTC (rev 2942)
@@ -36,8 +36,11 @@
 #include "util/Debug.h"
 #include "core/OrxonoxClass.h"
 
+#include <string>
 #include <OgreSceneNode.h>
 #include <OgreEntity.h>
+#include <OgreManualObject.h>
+#include "orxonox/tools/DynamicLines.h"
 
 namespace orxonox
 {
@@ -54,9 +57,10 @@
             Triangle
         };
 
+
     public:
         RadarViewable();
-        virtual ~RadarViewable() { }
+        virtual ~RadarViewable();
 
         inline void setRadarObjectCamouflage(float camouflage)
             { this->radarObjectCamouflage_ = camouflage; }
@@ -95,7 +99,9 @@
         //Used for Map
         Ogre::SceneNode * MapNode_;
         Ogre::Entity * MapEntity_;
-
+        DynamicLines* line_;
+        void addMapEntity();
+        void updateMapPosition();
     private:
         void validate(const WorldEntity* object) const
         {
@@ -106,6 +112,9 @@
             }
         }
 
+        
+        std::string uniqueId_;
+
         float radarObjectCamouflage_;
         Shape radarObjectShape_;
         std::string radarObjectDescription_;

Modified: branches/map/src/orxonox/objects/controllers/HumanController.cc
===================================================================
--- branches/map/src/orxonox/objects/controllers/HumanController.cc	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/orxonox/objects/controllers/HumanController.cc	2009-04-30 12:12:57 UTC (rev 2942)
@@ -34,6 +34,7 @@
 #include "objects/worldentities/ControllableEntity.h"
 #include "objects/worldentities/pawns/Pawn.h"
 #include "objects/gametypes/Gametype.h"
+#include "overlays/map/Map.h"
 
 namespace orxonox
 {
@@ -91,12 +92,24 @@
 
     void HumanController::rotateYaw(const Vector2& value)
     {
+        //Hack to enable mouselook in map
+        if ( Map::getSingletonPtr() && Map::getSingletonPtr()->getVisibility() && HumanController::localController_s->controllableEntity_->isInMouseLook() )
+        {
+            Map::getSingletonPtr()->rotateYaw(value);
+            return;
+        }
         if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
             HumanController::localController_s->controllableEntity_->rotateYaw(value);
     }
 
     void HumanController::rotatePitch(const Vector2& value)
     {
+        //Hack to enable mouselook in map
+        if ( Map::getSingletonPtr() && Map::getSingletonPtr()->getVisibility() && HumanController::localController_s->controllableEntity_->isInMouseLook() )
+        {
+            Map::getSingletonPtr()->rotatePitch(value);
+            return;
+        }
         if (HumanController::localController_s && HumanController::localController_s->controllableEntity_)
             HumanController::localController_s->controllableEntity_->rotatePitch(value);
     }

Modified: branches/map/src/orxonox/objects/controllers/HumanController.h
===================================================================
--- branches/map/src/orxonox/objects/controllers/HumanController.h	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/orxonox/objects/controllers/HumanController.h	2009-04-30 12:12:57 UTC (rev 2942)
@@ -65,6 +65,9 @@
             static void addBots(unsigned int amount);
             static void killBots(unsigned int amount = 0);
 
+            //friend class, for mouselook
+            friend class Map;
+
         private:
             static HumanController* localController_s;
     };

Modified: branches/map/src/orxonox/overlays/map/Map.cc
===================================================================
--- branches/map/src/orxonox/overlays/map/Map.cc	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/orxonox/overlays/map/Map.cc	2009-04-30 12:12:57 UTC (rev 2942)
@@ -27,16 +27,18 @@
 #include "Map.h"
  
 #include <string>
+#include "util/String.h"
 #include <OgreSceneManager.h>
 #include <OgreSceneNode.h>
 #include <OgreEntity.h>
 #include <OgreNode.h>
 
+
 #include <OgreRenderWindow.h>
 #include <OgreRenderTexture.h>
 #include <OgreTexture.h>
 #include <OgreViewport.h>
-#include <OgreMaterial.h>
+
 #include <OgreMaterialManager.h>
 #include <OgreRoot.h>
 #include <OgreHardwarePixelBuffer.h>
@@ -52,16 +54,21 @@
 #include "core/ConsoleCommand.h"
 #include "objects/Scene.h"
 #include "objects/RadarViewable.h"
+#include "objects/controllers/HumanController.h"
  
  namespace orxonox
  {
     CreateFactory(Map);
-     SetConsoleCommand(Map, openMap, true);
+    SetConsoleCommand(Map, openMap, true);
+    SetConsoleCommand(Map, rotateYaw, true).setAsInputCommand();
+    SetConsoleCommand(Map, rotatePitch, true).setAsInputCommand();
+
+    Map* Map::singletonMap_s = 0;
     
     Map::Map(BaseObject* creator) : OrxonoxOverlay(creator)
     {
         RegisterObject(Map);
-        
+        Map::singletonMap_s=this;
         //Getting Scene Manager (Hack)
         ObjectList<Scene>::iterator it = ObjectList<Scene>::begin();
         this->sManager_ = it->getSceneManager();
@@ -87,8 +94,8 @@
         overlay_->add3D(sNode_);
         */
 	
+        this->mouseLookSpeed_ = 200;
         
-        
 
         
         // Alter the camera aspect ratio to match the viewport
@@ -98,6 +105,7 @@
         mReflectCam_->lookAt(0,0,0);
         mReflectCam_->setAspectRatio(1);
 
+        //Create overlay material
         std::string camMat_id = "RttMat";
         Ogre::MaterialPtr material = this->createRenderCamera(mReflectCam_, camMat_id);
 /*
@@ -144,8 +152,39 @@
         this->isVisible_=false;
         overlay_->hide();
 
+        //Create plane in map
+        Ogre::Entity* plane_ent = this->mapSceneM_->createEntity( "MapPlane", "plane.mesh");
+        Ogre::SceneNode* plane_node = this->mapSceneM_->getRootSceneNode()->createChildSceneNode();
+        
+        //Ogre::MaterialPtr plane_mat = Ogre::MaterialManager::getSingleton().create("mapgrid", "General");
+        //plane_mat->getTechnique(0)->getPass(0)->createTextureUnitState("mapgrid.tga");
+        //plane_ent->setMaterialName("mapgrid");
+        plane_ent->setMaterialName("Map/Grid");
+        plane_node->attachObject(plane_ent);
+        plane_node->scale(10,1,10);
+        //Ogre::Material plane_mat = Ogre::MaterialManager::getSingletonPtr()->getByName("rock");
+        
+
+        //ToDo create material script
+        Ogre::MaterialPtr myManualObjectMaterial = Ogre::MaterialManager::getSingleton().create("Map/Line","General"); 
+        myManualObjectMaterial->setReceiveShadows(false); 
+        myManualObjectMaterial->getTechnique(0)->setLightingEnabled(true); 
+        myManualObjectMaterial->getTechnique(0)->getPass(0)->setDiffuse(1,1,0,0); 
+        myManualObjectMaterial->getTechnique(0)->getPass(0)->setAmbient(1,1,0); 
+        myManualObjectMaterial->getTechnique(0)->getPass(0)->setSelfIllumination(1,1,0);
+
     }
-    
+
+    Map::~Map()
+    {
+        singletonMap_s = 0;
+        //delete sManager_;
+        //delete rootNode_;
+        //delete oManager_;
+        //delete mReflectCam_;
+        //delete mapSceneM_;
+    }
+
     Ogre::MaterialPtr Map::createRenderCamera(Ogre::Camera * cam, std::string matName)
     {
         Ogre::TexturePtr rttTex = Ogre::TextureManager::getSingleton().createManual(matName+"_tex", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, 512, 512, 0, Ogre::PF_R8G8B8, Ogre::TU_RENDERTARGET);
@@ -167,13 +206,12 @@
 
     void Map::updatePositions()
     {
+
 //Ogre::Entity * ent;// = mapSceneM_->createEntity("ent1", "drone.mesh");
-       int i=0; 
        for(ObjectList<orxonox::RadarViewable>::iterator it = ObjectList<orxonox::RadarViewable>::begin();
             it!=ObjectList<orxonox::RadarViewable>::end();
             it++)
         {
-            i++;
             //COUT(0) << "Radar_Position: " << it->getRVWorldPosition() << std::endl;
             //Ogre::SceneNode node = it->getMapNode();
             //Ogre::Entity ent = it->getMapEntity();
@@ -181,14 +219,23 @@
             {
                 it->MapNode_ = rootNode_->createChildSceneNode( it->getRVWorldPosition() );
                 //it->MapNode_->translate( it->getRVOrientedVelocity(), Ogre::TS_WORLD );
+                /*if(it->getRadarObjectShape() == RadarViewable::Dot)
+                {
+                    //if( !(it->MapEntity_) )//check wether the entity is already attached
+                    //{
+                        //it->MapEntity_ = this->mapSceneM_->createEntity( getUniqueNumberString(), "drone.mesh");
+                        //it->addEntity();
+                        //it->MapNode_->attachObject( it->MapEntity_ );
+                        //it->MapNode_->attachObject( it->line_ );
+                   // }
+                }*/
+                it->addMapEntity();
             }
-            it->MapNode_->setPosition( it->getRVWorldPosition() );
-            it->MapNode_->translate( it->getRVOrientedVelocity(), (Ogre::Node::TransformSpace)3 );
-            if( !(it->MapEntity_) )
-            {
-                it->MapEntity_ = mapSceneM_->createEntity( "MapEnt"+i, "drone.mesh");
-                it->MapNode_->attachObject( it->MapEntity_ );
-            }
+
+            it->updateMapPosition();
+
+
+
             
         
             
@@ -217,12 +264,16 @@
         {
             this->overlay_->show();
             this->isVisible_=1;
-            //updatePositions();
+            //set mouselook when showing map
+            if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && !HumanController::localController_s->controllableEntity_->isInMouseLook())
+            HumanController::localController_s->controllableEntity_->mouseLook();
         }
         else
         {
             this->overlay_->hide();
             this->isVisible_=0;
+            if (HumanController::localController_s && HumanController::localController_s->controllableEntity_ && HumanController::localController_s->controllableEntity_->isInMouseLook())
+            HumanController::localController_s->controllableEntity_->mouseLook();
         }
     }
     
@@ -234,7 +285,7 @@
             it++)
         {
         //Map * m = it->getMap();
-        COUT(0) << it->isVisible_ << std::endl;
+        //COUT(0) << it->isVisible_ << std::endl;
         it->toggleVisibility();
         //it->updatePositions();
         }
@@ -243,8 +294,26 @@
     void Map::tick(float dt)
     {
         //sNode_->lookAt(Vector3::NEGATIVE_UNIT_Z, Ogre::Node::TS_WORLD, Vector3::NEGATIVE_UNIT_Z);
-        updatePositions();
+        if( this->isVisible_ )
+            updatePositions();
+        //mReflectCam_->roll(Degree(1));
         
     }
+
+    void Map::rotateYaw(const Vector2& value)
+    {
+        //if (this->bMouseLook_)
+            //this->mReflectCam_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
+        if(Map::singletonMap_s)
+            singletonMap_s->mReflectCam_->setOrientation(singletonMap_s->mReflectCam_->getOrientation() * Quaternion( (Degree)(value.y * singletonMap_s->mouseLookSpeed_) , Vector3::UNIT_Y));
+    }
+
+    void Map::rotatePitch(const Vector2& value)
+    {
+        //if (this->bMouseLook_)
+            //this->mReflectCam_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
+        if(Map::singletonMap_s)
+            singletonMap_s->mReflectCam_->setOrientation(singletonMap_s->mReflectCam_->getOrientation() * Quaternion( (Degree)(-value.y * singletonMap_s->mouseLookSpeed_) , Vector3::UNIT_X));
+    }
     
  }

Modified: branches/map/src/orxonox/overlays/map/Map.h
===================================================================
--- branches/map/src/orxonox/overlays/map/Map.h	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/orxonox/overlays/map/Map.h	2009-04-30 12:12:57 UTC (rev 2942)
@@ -53,7 +53,7 @@
 
     public: // functions
         Map(BaseObject* creator);
-        virtual ~Map() {}
+        ~Map();
 
         virtual void XMLPort(Element& xmlElement, XMLPort::Mode mode);
         virtual void tick(float dt);
@@ -64,21 +64,44 @@
         static void openMap();
 
         static const int maxRange_s=1000;
+
+        static void rotateYaw(const Vector2& value);
+        static void rotatePitch(const Vector2& value);
+
+        // variables
+
+
+        bool inline getVisibility()
+            { return this->isVisible_; };
+
+        static inline Ogre::SceneManager* getMapSceneManagerPtr()
+        {
+            return Map::singletonMap_s->mapSceneM_;
+        }
+        static inline Map* getSingletonPtr()
+        {
+            return Map::singletonMap_s;
+        }
+
+
+
         
     private: // functions
+
         void toggleVisibility();
         void updatePositions();
+        
 
+    private: // variables
+        static Map* singletonMap_s;
 
-    private: // variables
         Ogre::SceneManager* sManager_;
         Ogre::SceneManager* mapSceneM_;
         Ogre::SceneNode* rootNode_;
         Ogre::OverlayManager * oManager_;
         Ogre::Camera* mReflectCam_;
-
-        
-        
+        //Ogre::SceneNode* playerShipNode_;
+        int mouseLookSpeed_;
         bool isVisible_;
     };
 }

Modified: branches/map/src/orxonox/tools/CMakeLists.txt
===================================================================
--- branches/map/src/orxonox/tools/CMakeLists.txt	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/orxonox/tools/CMakeLists.txt	2009-04-30 12:12:57 UTC (rev 2942)
@@ -6,4 +6,6 @@
   TextureGenerator.cc
   Timer.cc
   WindowEventListener.cc
+  DynamicLines.cpp
+  DynamicRenderable.cpp
 )

Added: branches/map/src/orxonox/tools/DynamicLines.cpp
===================================================================
--- branches/map/src/orxonox/tools/DynamicLines.cpp	                        (rev 0)
+++ branches/map/src/orxonox/tools/DynamicLines.cpp	2009-04-30 12:12:57 UTC (rev 2942)
@@ -0,0 +1,142 @@
+#include "DynamicLines.h"
+#include <Ogre.h>
+#include <cassert>
+#include <cmath>
+
+using namespace Ogre;
+
+enum {
+  POSITION_BINDING,
+  TEXCOORD_BINDING
+};
+
+DynamicLines::DynamicLines(OperationType opType)
+{
+  initialize(opType,false);
+  setMaterial("BaseWhiteNoLighting");
+  mDirty = true;
+}
+
+DynamicLines::~DynamicLines()
+{
+}
+
+void DynamicLines::setOperationType(OperationType opType)
+{
+  mRenderOp.operationType = opType;
+}
+
+RenderOperation::OperationType DynamicLines::getOperationType() const
+{
+  return mRenderOp.operationType;
+}
+
+void DynamicLines::addPoint(const Vector3 &p)
+{
+   mPoints.push_back(p);
+   mDirty = true;
+}
+void DynamicLines::addPoint(Real x, Real y, Real z)
+{
+   mPoints.push_back(Vector3(x,y,z));
+   mDirty = true;
+}
+const Vector3& DynamicLines::getPoint(unsigned short index) const
+{
+   assert(index < mPoints.size() && "Point index is out of bounds!!");
+   return mPoints[index];
+}
+unsigned short DynamicLines::getNumPoints(void) const
+{
+  return (unsigned short)mPoints.size();
+}
+void DynamicLines::setPoint(unsigned short index, const Vector3 &value)
+{
+  assert(index < mPoints.size() && "Point index is out of bounds!!");
+
+  mPoints[index] = value;
+  mDirty = true;
+}
+void DynamicLines::clear()
+{
+  mPoints.clear();
+  mDirty = true;
+}
+
+void DynamicLines::update()
+{
+  if (mDirty) fillHardwareBuffers();
+}
+
+void DynamicLines::createVertexDeclaration()
+{
+  VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration;
+  decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION);
+}
+
+void DynamicLines::fillHardwareBuffers()
+{
+  int size = mPoints.size();
+
+  prepareHardwareBuffers(size,0);
+
+  if (!size) { 
+    mBox.setExtents(Vector3::ZERO,Vector3::ZERO);
+    mDirty=false;
+    return;
+  }
+  
+  Vector3 vaabMin = mPoints[0];
+  Vector3 vaabMax = mPoints[0];
+
+  HardwareVertexBufferSharedPtr vbuf =
+    mRenderOp.vertexData->vertexBufferBinding->getBuffer(0);
+
+  Real *prPos = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
+  {
+   for(int i = 0; i < size; i++)
+   {
+      *prPos++ = mPoints[i].x;
+      *prPos++ = mPoints[i].y;
+      *prPos++ = mPoints[i].z;
+
+      if(mPoints[i].x < vaabMin.x)
+         vaabMin.x = mPoints[i].x;
+      if(mPoints[i].y < vaabMin.y)
+         vaabMin.y = mPoints[i].y;
+      if(mPoints[i].z < vaabMin.z)
+         vaabMin.z = mPoints[i].z;
+
+      if(mPoints[i].x > vaabMax.x)
+         vaabMax.x = mPoints[i].x;
+      if(mPoints[i].y > vaabMax.y)
+         vaabMax.y = mPoints[i].y;
+      if(mPoints[i].z > vaabMax.z)
+         vaabMax.z = mPoints[i].z;
+   }
+  }
+  vbuf->unlock();
+
+  mBox.setExtents(vaabMin, vaabMax);
+
+  mDirty = false;
+}
+
+/*
+void DynamicLines::getWorldTransforms(Matrix4 *xform) const
+{
+   // return identity matrix to prevent parent transforms
+   *xform = Matrix4::IDENTITY;
+}
+*/
+/*
+const Quaternion &DynamicLines::getWorldOrientation(void) const
+{
+   return Quaternion::IDENTITY;
+}
+
+const Vector3 &DynamicLines::getWorldPosition(void) const
+{
+   return Vector3::ZERO;
+}
+*/
\ No newline at end of file

Added: branches/map/src/orxonox/tools/DynamicLines.h
===================================================================
--- branches/map/src/orxonox/tools/DynamicLines.h	                        (rev 0)
+++ branches/map/src/orxonox/tools/DynamicLines.h	2009-04-30 12:12:57 UTC (rev 2942)
@@ -0,0 +1,66 @@
+#ifndef _DYNAMIC_LINES_H_
+#define _DYNAMIC_LINES_H_
+
+#include "DynamicRenderable.h"
+#include <vector>
+
+
+class DynamicLines : public DynamicRenderable
+{
+  typedef Ogre::Vector3 Vector3;
+  typedef Ogre::Quaternion Quaternion;
+  typedef Ogre::Camera Camera;
+  typedef Ogre::Real Real;
+  typedef Ogre::RenderOperation::OperationType OperationType;
+
+public:
+  /// Constructor - see setOperationType() for description of argument.
+  DynamicLines(OperationType opType=Ogre::RenderOperation::OT_LINE_STRIP);
+  virtual ~DynamicLines();
+
+  /// Add a point to the point list
+  void addPoint(const Ogre::Vector3 &p);
+  /// Add a point to the point list
+  void addPoint(Real x, Real y, Real z);
+
+  /// Change the location of an existing point in the point list
+  void setPoint(unsigned short index, const Vector3 &value);
+
+  /// Return the location of an existing point in the point list
+  const Vector3& getPoint(unsigned short index) const;
+
+  /// Return the total number of points in the point list
+  unsigned short getNumPoints(void) const;
+
+  /// Remove all points from the point list
+  void clear();
+
+  /// Call this to update the hardware buffer after making changes.  
+  void update();
+
+  /** Set the type of operation to draw with.
+   * @param opType Can be one of 
+   *    - RenderOperation::OT_LINE_STRIP
+   *    - RenderOperation::OT_LINE_LIST
+   *    - RenderOperation::OT_POINT_LIST
+   *    - RenderOperation::OT_TRIANGLE_LIST
+   *    - RenderOperation::OT_TRIANGLE_STRIP
+   *    - RenderOperation::OT_TRIANGLE_FAN
+   *    The default is OT_LINE_STRIP.
+   */
+  void setOperationType(OperationType opType);
+  OperationType getOperationType() const;
+
+protected:
+  /// Implementation DynamicRenderable, creates a simple vertex-only decl
+  virtual void createVertexDeclaration();
+  /// Implementation DynamicRenderable, pushes point list out to hardware memory
+  virtual void fillHardwareBuffers();
+
+private:
+  std::vector<Vector3> mPoints;
+  bool mDirty;
+};
+
+
+#endif
\ No newline at end of file

Added: branches/map/src/orxonox/tools/DynamicRenderable.cpp
===================================================================
--- branches/map/src/orxonox/tools/DynamicRenderable.cpp	                        (rev 0)
+++ branches/map/src/orxonox/tools/DynamicRenderable.cpp	2009-04-30 12:12:57 UTC (rev 2942)
@@ -0,0 +1,133 @@
+#include "DynamicRenderable.h"
+#include <OgreCamera.h>
+#include <OgreHardwareBufferManager.h>
+
+using namespace Ogre;
+
+DynamicRenderable::DynamicRenderable()
+{
+}
+
+DynamicRenderable::~DynamicRenderable()
+{
+  delete mRenderOp.vertexData;
+  delete mRenderOp.indexData;
+}
+
+void DynamicRenderable::initialize(RenderOperation::OperationType operationType,
+                                   bool useIndices)
+{
+  // Initialize render operation
+  mRenderOp.operationType = operationType;
+  mRenderOp.useIndexes = useIndices;
+  mRenderOp.vertexData = new VertexData;
+  if (mRenderOp.useIndexes)
+    mRenderOp.indexData = new IndexData;
+
+  // Reset buffer capacities
+  mVertexBufferCapacity = 0;
+  mIndexBufferCapacity = 0;
+
+  // Create vertex declaration
+  createVertexDeclaration();
+}
+
+void DynamicRenderable::prepareHardwareBuffers(size_t vertexCount, 
+                                               size_t indexCount)
+{
+  // Prepare vertex buffer
+  size_t newVertCapacity = mVertexBufferCapacity;
+  if ((vertexCount > mVertexBufferCapacity) ||
+      (!mVertexBufferCapacity))
+  {
+    // vertexCount exceeds current capacity!
+    // It is necessary to reallocate the buffer.
+
+    // Check if this is the first call
+    if (!newVertCapacity)
+      newVertCapacity = 1;
+
+    // Make capacity the next power of two
+    while (newVertCapacity < vertexCount)
+      newVertCapacity <<= 1;
+  }
+  else if (vertexCount < mVertexBufferCapacity>>1) {
+    // Make capacity the previous power of two
+    while (vertexCount < newVertCapacity>>1)
+      newVertCapacity >>= 1;
+  }
+  if (newVertCapacity != mVertexBufferCapacity) 
+  {
+    mVertexBufferCapacity = newVertCapacity;
+    // Create new vertex buffer
+    HardwareVertexBufferSharedPtr vbuf =
+      HardwareBufferManager::getSingleton().createVertexBuffer(
+        mRenderOp.vertexData->vertexDeclaration->getVertexSize(0),
+        mVertexBufferCapacity,
+        HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
+
+    // Bind buffer
+    mRenderOp.vertexData->vertexBufferBinding->setBinding(0, vbuf);
+  }
+  // Update vertex count in the render operation
+  mRenderOp.vertexData->vertexCount = vertexCount;
+
+  if (mRenderOp.useIndexes)
+  {
+    OgreAssert(indexCount <= std::numeric_limits<unsigned short>::max(), "indexCount exceeds 16 bit");
+
+    size_t newIndexCapacity = mIndexBufferCapacity;
+    // Prepare index buffer
+    if ((indexCount > newIndexCapacity) ||
+        (!newIndexCapacity))
+    {
+      // indexCount exceeds current capacity!
+      // It is necessary to reallocate the buffer.
+
+      // Check if this is the first call
+      if (!newIndexCapacity)
+        newIndexCapacity = 1;
+
+      // Make capacity the next power of two
+      while (newIndexCapacity < indexCount)
+        newIndexCapacity <<= 1;
+
+    }
+    else if (indexCount < newIndexCapacity>>1) 
+    {
+      // Make capacity the previous power of two
+      while (indexCount < newIndexCapacity>>1)
+        newIndexCapacity >>= 1;
+    }
+
+    if (newIndexCapacity != mIndexBufferCapacity)
+    {
+      mIndexBufferCapacity = newIndexCapacity;
+      // Create new index buffer
+      mRenderOp.indexData->indexBuffer =
+        HardwareBufferManager::getSingleton().createIndexBuffer(
+          HardwareIndexBuffer::IT_16BIT,
+          mIndexBufferCapacity,
+          HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
+    }
+
+    // Update index count in the render operation
+    mRenderOp.indexData->indexCount = indexCount;
+  }
+}
+
+Real DynamicRenderable::getBoundingRadius(void) const
+{
+  return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
+}
+
+Real DynamicRenderable::getSquaredViewDepth(const Camera* cam) const
+{
+   Vector3 vMin, vMax, vMid, vDist;
+   vMin = mBox.getMinimum();
+   vMax = mBox.getMaximum();
+   vMid = ((vMax - vMin) * 0.5) + vMin;
+   vDist = cam->getDerivedPosition() - vMid;
+
+   return vDist.squaredLength();
+}
\ No newline at end of file

Added: branches/map/src/orxonox/tools/DynamicRenderable.h
===================================================================
--- branches/map/src/orxonox/tools/DynamicRenderable.h	                        (rev 0)
+++ branches/map/src/orxonox/tools/DynamicRenderable.h	2009-04-30 12:12:57 UTC (rev 2942)
@@ -0,0 +1,69 @@
+#ifndef DYNAMIC_RENDERABLE_H
+#define DYNAMIC_RENDERABLE_H
+
+#include <OgreSimpleRenderable.h>
+
+/// Abstract base class providing mechanisms for dynamically growing hardware buffers.
+class DynamicRenderable : public Ogre::SimpleRenderable
+{
+public:
+  /// Constructor
+  DynamicRenderable();
+  /// Virtual destructor
+  virtual ~DynamicRenderable();
+
+  /** Initializes the dynamic renderable.
+   @remarks
+      This function should only be called once. It initializes the
+      render operation, and calls the abstract function
+      createVertexDeclaration().
+   @param operationType The type of render operation to perform.
+   @param useIndices Specifies whether to use indices to determine the
+          vertices to use as input. */
+  void initialize(Ogre::RenderOperation::OperationType operationType,
+                  bool useIndices);
+
+  /// Implementation of Ogre::SimpleRenderable
+  virtual Ogre::Real getBoundingRadius(void) const;
+  /// Implementation of Ogre::SimpleRenderable
+  virtual Ogre::Real getSquaredViewDepth(const Ogre::Camera* cam) const;
+
+protected:
+  /// Maximum capacity of the currently allocated vertex buffer.
+  size_t mVertexBufferCapacity;
+  /// Maximum capacity of the currently allocated index buffer.
+  size_t mIndexBufferCapacity;
+
+  /** Creates the vertex declaration.
+   @remarks
+      Override and set mRenderOp.vertexData->vertexDeclaration here.
+      mRenderOp.vertexData will be created for you before this method
+      is called. */
+  virtual void createVertexDeclaration() = 0;
+
+  /** Prepares the hardware buffers for the requested vertex and index counts.
+   @remarks
+      This function must be called before locking the buffers in
+      fillHardwareBuffers(). It guarantees that the hardware buffers
+      are large enough to hold at least the requested number of
+      vertices and indices (if using indices). The buffers are
+      possibly reallocated to achieve this.
+   @par
+      The vertex and index count in the render operation are set to
+      the values of vertexCount and indexCount respectively.
+   @param vertexCount The number of vertices the buffer must hold.
+
+   @param indexCount The number of indices the buffer must hold. This
+          parameter is ignored if not using indices. */
+  void prepareHardwareBuffers(size_t vertexCount, size_t indexCount);
+
+  /** Fills the hardware vertex and index buffers with data.
+   @remarks
+      This function must call prepareHardwareBuffers() before locking
+      the buffers to ensure the they are large enough for the data to
+      be written. Afterwards the vertex and index buffers (if using
+      indices) can be locked, and data can be written to them. */
+  virtual void fillHardwareBuffers() = 0;
+};
+
+#endif // DYNAMIC_RENDERABLE_H
\ No newline at end of file

Modified: branches/map/src/util/CMakeLists.txt
===================================================================
--- branches/map/src/util/CMakeLists.txt	2009-04-29 18:07:43 UTC (rev 2941)
+++ branches/map/src/util/CMakeLists.txt	2009-04-30 12:12:57 UTC (rev 2942)
@@ -36,7 +36,7 @@
   SubString.h
   UtilPrereqs.h
   mbool.h
-
+  
   Clipboard.cc
   CRC32.cc
   Exception.cc




More information about the Orxonox-commit mailing list