Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3130


Ignore:
Timestamp:
Jun 9, 2009, 7:51:00 PM (15 years ago)
Author:
rgrieder
Message:

Cleaned out DynamicLines and DynamicRenderable classes and put them in the Ogre namespace since that's where they came from (OGRE wiki).

Location:
code/branches/pch/src/orxonox
Files:
5 edited
2 moved

Legend:

Unmodified
Added
Removed
  • code/branches/pch/src/orxonox/OrxonoxPrereqs.h

    r3099 r3130  
    247247    template <class T>
    248248    class Timer;
    249     class DynamicLines;
    250     class DynamicRenderable;
    251249
    252250    // overlays
     
    254252    class DebugFPSText;
    255253    class DebugRTRText;
     254    class GUIOverlay;
    256255    class HUDBar;
    257256    class HUDNavigation;
     
    292291    class PanelOverlayElement;
    293292    class TextAreaOverlayElement;
     293
     294    // OGRE Wiki adapted code
     295    class DynamicLines;
     296    class DynamicRenderable;
    294297}
    295298
  • code/branches/pch/src/orxonox/objects/RadarViewable.cc

    r3110 r3130  
    2929#include "RadarViewable.h"
    3030
     31#include <OgreSceneManager.h>
    3132#include "util/Debug.h"
    3233#include "util/Exception.h"
     
    101102
    102103            this->line_->end(); */
    103             this->line_ = new DynamicLines(Ogre::RenderOperation::OT_LINE_LIST);
     104            this->line_ = new Ogre::DynamicLines(Ogre::RenderOperation::OT_LINE_LIST);
    104105            this->line_->addPoint( Vector3(0,0,0) );
    105106            this->line_->addPoint( Vector3(0,0,0) );
  • code/branches/pch/src/orxonox/objects/RadarViewable.h

    r3101 r3130  
    105105        Ogre::SceneNode * MapNode_;
    106106        Ogre::Entity * MapEntity_;
    107         DynamicLines* line_;
     107        Ogre::DynamicLines* line_;
    108108        Ogre::SceneNode * LineNode_;
    109109        void addMapEntity();
  • code/branches/pch/src/orxonox/tools/DynamicLines.cc

    r3117 r3130  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Baxissimo, Emmeran, DWORD, EtherDrive (OGRE Wiki)
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
    129#include "DynamicLines.h"
    2 #include <Ogre.h>
     30
    331#include <cassert>
    432#include <cmath>
    533
    6 using namespace Ogre;
     34namespace Ogre
     35{
     36    enum
     37    {
     38        POSITION_BINDING,
     39        TEXCOORD_BINDING
     40    };
    741
    8 namespace orxonox
    9 {
    10 enum {
    11   POSITION_BINDING,
    12   TEXCOORD_BINDING
    13 };
     42    DynamicLines::DynamicLines(OperationType opType)
     43    {
     44        initialize(opType,false);
     45        setMaterial("BaseWhiteNoLighting");
     46        mDirty = true;
     47    }
    1448
    15 DynamicLines::DynamicLines(OperationType opType)
    16 {
    17   initialize(opType,false);
    18   setMaterial("BaseWhiteNoLighting");
    19   mDirty = true;
     49    DynamicLines::~DynamicLines()
     50    {
     51    }
     52
     53    void DynamicLines::setOperationType(OperationType opType)
     54    {
     55        mRenderOp.operationType = opType;
     56    }
     57
     58    RenderOperation::OperationType DynamicLines::getOperationType() const
     59    {
     60        return mRenderOp.operationType;
     61    }
     62
     63    void DynamicLines::addPoint(const Vector3 &p)
     64    {
     65        mPoints.push_back(p);
     66        mDirty = true;
     67    }
     68
     69    void DynamicLines::addPoint(Real x, Real y, Real z)
     70    {
     71        mPoints.push_back(Vector3(x,y,z));
     72        mDirty = true;
     73    }
     74
     75    const Vector3& DynamicLines::getPoint(unsigned short index) const
     76    {
     77        assert(index < mPoints.size() && "Point index is out of bounds!!");
     78        return mPoints[index];
     79    }
     80
     81    unsigned short DynamicLines::getNumPoints(void) const
     82    {
     83        return (unsigned short)mPoints.size();
     84    }
     85
     86    void DynamicLines::setPoint(unsigned short index, const Vector3 &value)
     87    {
     88        assert(index < mPoints.size() && "Point index is out of bounds!!");
     89
     90        mPoints[index] = value;
     91        mDirty = true;
     92    }
     93
     94    void DynamicLines::clear()
     95    {
     96        mPoints.clear();
     97        mDirty = true;
     98    }
     99
     100    void DynamicLines::update()
     101    {
     102        if (mDirty)
     103            fillHardwareBuffers();
     104    }
     105
     106    void DynamicLines::createVertexDeclaration()
     107    {
     108        VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration;
     109        decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION);
     110    }
     111
     112    void DynamicLines::fillHardwareBuffers()
     113    {
     114        int size = mPoints.size();
     115
     116        prepareHardwareBuffers(size,0);
     117
     118        if (!size)
     119        {
     120            mBox.setExtents(Vector3::ZERO,Vector3::ZERO);
     121            mDirty=false;
     122            return;
     123        }
     124
     125        Vector3 vaabMin = mPoints[0];
     126        Vector3 vaabMax = mPoints[0];
     127
     128        HardwareVertexBufferSharedPtr vbuf =
     129            mRenderOp.vertexData->vertexBufferBinding->getBuffer(0);
     130
     131        Real *prPos = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
     132        for (int i = 0; i < size; i++)
     133        {
     134            *prPos++ = mPoints[i].x;
     135            *prPos++ = mPoints[i].y;
     136            *prPos++ = mPoints[i].z;
     137
     138            if (mPoints[i].x < vaabMin.x)
     139                vaabMin.x = mPoints[i].x;
     140            if (mPoints[i].y < vaabMin.y)
     141                vaabMin.y = mPoints[i].y;
     142            if (mPoints[i].z < vaabMin.z)
     143                vaabMin.z = mPoints[i].z;
     144
     145            if (mPoints[i].x > vaabMax.x)
     146                vaabMax.x = mPoints[i].x;
     147            if (mPoints[i].y > vaabMax.y)
     148                vaabMax.y = mPoints[i].y;
     149            if (mPoints[i].z > vaabMax.z)
     150                vaabMax.z = mPoints[i].z;
     151        }
     152        vbuf->unlock();
     153
     154        mBox.setExtents(vaabMin, vaabMax);
     155
     156        mDirty = false;
     157    }
     158
     159    /*
     160    void DynamicLines::getWorldTransforms(Matrix4 *xform) const
     161    {
     162        // return identity matrix to prevent parent transforms
     163        *xform = Matrix4::IDENTITY;
     164    }
     165    */
     166
     167    /*
     168    const Quaternion &DynamicLines::getWorldOrientation(void) const
     169    {
     170        return Quaternion::IDENTITY;
     171    }
     172
     173    const Vector3 &DynamicLines::getWorldPosition(void) const
     174    {
     175        return Vector3::ZERO;
     176    }
     177    */
    20178}
    21 
    22 DynamicLines::~DynamicLines()
    23 {
    24 }
    25 
    26 void DynamicLines::setOperationType(OperationType opType)
    27 {
    28   mRenderOp.operationType = opType;
    29 }
    30 
    31 RenderOperation::OperationType DynamicLines::getOperationType() const
    32 {
    33   return mRenderOp.operationType;
    34 }
    35 
    36 void DynamicLines::addPoint(const Vector3 &p)
    37 {
    38    mPoints.push_back(p);
    39    mDirty = true;
    40 }
    41 void DynamicLines::addPoint(Real x, Real y, Real z)
    42 {
    43    mPoints.push_back(Vector3(x,y,z));
    44    mDirty = true;
    45 }
    46 const Vector3& DynamicLines::getPoint(unsigned short index) const
    47 {
    48    assert(index < mPoints.size() && "Point index is out of bounds!!");
    49    return mPoints[index];
    50 }
    51 unsigned short DynamicLines::getNumPoints(void) const
    52 {
    53   return (unsigned short)mPoints.size();
    54 }
    55 void DynamicLines::setPoint(unsigned short index, const Vector3 &value)
    56 {
    57   assert(index < mPoints.size() && "Point index is out of bounds!!");
    58 
    59   mPoints[index] = value;
    60   mDirty = true;
    61 }
    62 void DynamicLines::clear()
    63 {
    64   mPoints.clear();
    65   mDirty = true;
    66 }
    67 
    68 void DynamicLines::update()
    69 {
    70   if (mDirty) fillHardwareBuffers();
    71 }
    72 
    73 void DynamicLines::createVertexDeclaration()
    74 {
    75   VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration;
    76   decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION);
    77 }
    78 
    79 void DynamicLines::fillHardwareBuffers()
    80 {
    81   int size = mPoints.size();
    82 
    83   prepareHardwareBuffers(size,0);
    84 
    85   if (!size) {
    86     mBox.setExtents(Vector3::ZERO,Vector3::ZERO);
    87     mDirty=false;
    88     return;
    89   }
    90 
    91   Vector3 vaabMin = mPoints[0];
    92   Vector3 vaabMax = mPoints[0];
    93 
    94   HardwareVertexBufferSharedPtr vbuf =
    95     mRenderOp.vertexData->vertexBufferBinding->getBuffer(0);
    96 
    97   Real *prPos = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
    98   {
    99    for(int i = 0; i < size; i++)
    100    {
    101       *prPos++ = mPoints[i].x;
    102       *prPos++ = mPoints[i].y;
    103       *prPos++ = mPoints[i].z;
    104 
    105       if(mPoints[i].x < vaabMin.x)
    106          vaabMin.x = mPoints[i].x;
    107       if(mPoints[i].y < vaabMin.y)
    108          vaabMin.y = mPoints[i].y;
    109       if(mPoints[i].z < vaabMin.z)
    110          vaabMin.z = mPoints[i].z;
    111 
    112       if(mPoints[i].x > vaabMax.x)
    113          vaabMax.x = mPoints[i].x;
    114       if(mPoints[i].y > vaabMax.y)
    115          vaabMax.y = mPoints[i].y;
    116       if(mPoints[i].z > vaabMax.z)
    117          vaabMax.z = mPoints[i].z;
    118    }
    119   }
    120   vbuf->unlock();
    121 
    122   mBox.setExtents(vaabMin, vaabMax);
    123 
    124   mDirty = false;
    125 }
    126 
    127 /*
    128 void DynamicLines::getWorldTransforms(Matrix4 *xform) const
    129 {
    130    // return identity matrix to prevent parent transforms
    131    *xform = Matrix4::IDENTITY;
    132 }
    133 */
    134 /*
    135 const Quaternion &DynamicLines::getWorldOrientation(void) const
    136 {
    137    return Quaternion::IDENTITY;
    138 }
    139 
    140 const Vector3 &DynamicLines::getWorldPosition(void) const
    141 {
    142    return Vector3::ZERO;
    143 }
    144 */
    145 }
  • code/branches/pch/src/orxonox/tools/DynamicLines.h

    r3089 r3130  
    1 #ifndef _DYNAMIC_LINES_H_
    2 #define _DYNAMIC_LINES_H_
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Baxissimo, Emmeran, DWORD, EtherDrive (OGRE Wiki)
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
    328
     29#ifndef _DynamicLines_H__
     30#define _DynamicLines_H__
     31
     32#include "OrxonoxPrereqs.h"
     33
     34#include <vector>
    435#include "DynamicRenderable.h"
    5 #include <vector>
    636
    7 namespace orxonox
     37namespace Ogre
    838{
    9 class DynamicLines : public DynamicRenderable
    10 {
    11   typedef Ogre::Vector3 Vector3;
    12   typedef Ogre::Quaternion Quaternion;
    13   typedef Ogre::Camera Camera;
    14   typedef Ogre::Real Real;
    15   typedef Ogre::RenderOperation::OperationType OperationType;
     39    class DynamicLines : public DynamicRenderable
     40    {
     41        typedef RenderOperation::OperationType OperationType;
    1642
    17 public:
    18   /// Constructor - see setOperationType() for description of argument.
    19   DynamicLines(OperationType opType=Ogre::RenderOperation::OT_LINE_STRIP);
    20   virtual ~DynamicLines();
     43    public:
     44        /// Constructor - see setOperationType() for description of argument.
     45        DynamicLines(OperationType opType = RenderOperation::OT_LINE_STRIP);
     46        virtual ~DynamicLines();
    2147
    22   /// Add a point to the point list
    23   void addPoint(const Ogre::Vector3 &p);
    24   /// Add a point to the point list
    25   void addPoint(Real x, Real y, Real z);
     48        /// Add a point to the point list
     49        void addPoint(const Vector3 &p);
     50        /// Add a point to the point list
     51        void addPoint(Real x, Real y, Real z);
    2652
    27   /// Change the location of an existing point in the point list
    28   void setPoint(unsigned short index, const Vector3 &value);
     53        /// Change the location of an existing point in the point list
     54        void setPoint(unsigned short index, const Vector3 &value);
    2955
    30   /// Return the location of an existing point in the point list
    31   const Vector3& getPoint(unsigned short index) const;
     56        /// Return the location of an existing point in the point list
     57        const Vector3& getPoint(unsigned short index) const;
    3258
    33   /// Return the total number of points in the point list
    34   unsigned short getNumPoints(void) const;
     59        /// Return the total number of points in the point list
     60        unsigned short getNumPoints(void) const;
    3561
    36   /// Remove all points from the point list
    37   void clear();
     62        /// Remove all points from the point list
     63        void clear();
    3864
    39   /// Call this to update the hardware buffer after making changes.
    40   void update();
     65        /// Call this to update the hardware buffer after making changes.
     66        void update();
    4167
    42   /** Set the type of operation to draw with.
    43    * @param opType Can be one of
    44    *    - RenderOperation::OT_LINE_STRIP
    45    *    - RenderOperation::OT_LINE_LIST
    46    *    - RenderOperation::OT_POINT_LIST
    47    *    - RenderOperation::OT_TRIANGLE_LIST
    48    *    - RenderOperation::OT_TRIANGLE_STRIP
    49    *    - RenderOperation::OT_TRIANGLE_FAN
    50    *    The default is OT_LINE_STRIP.
    51    */
    52   void setOperationType(OperationType opType);
    53   OperationType getOperationType() const;
     68        /**
     69        @brief
     70            Set the type of operation to draw with.
     71        @param opType
     72            Can be one of
     73                - RenderOperation::OT_LINE_STRIP
     74                - RenderOperation::OT_LINE_LIST
     75                - RenderOperation::OT_POINT_LIST
     76                - RenderOperation::OT_TRIANGLE_LIST
     77                - RenderOperation::OT_TRIANGLE_STRIP
     78                - RenderOperation::OT_TRIANGLE_FAN
     79            The default is OT_LINE_STRIP.
     80        */
     81        void setOperationType(OperationType opType);
     82        OperationType getOperationType() const;
    5483
    55 protected:
    56   /// Implementation DynamicRenderable, creates a simple vertex-only decl
    57   virtual void createVertexDeclaration();
    58   /// Implementation DynamicRenderable, pushes point list out to hardware memory
    59   virtual void fillHardwareBuffers();
     84    protected:
     85        /// Implementation DynamicRenderable, creates a simple vertex-only decl
     86        virtual void createVertexDeclaration();
     87        /// Implementation DynamicRenderable, pushes point list out to hardware memory
     88        virtual void fillHardwareBuffers();
    6089
    61 private:
    62   std::vector<Vector3> mPoints;
    63   bool mDirty;
    64 };
     90    private:
     91        std::vector<Vector3> mPoints;
     92        bool mDirty;
     93    };
    6594}
    6695
    67 #endif
     96#endif /* _DynamicLines_H__ */
  • code/branches/pch/src/orxonox/tools/DynamicRenderable.cc

    r3117 r3130  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Sinbad, Baxissimo, DWORD, TheBren (OGRE Wiki)
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
    129#include "DynamicRenderable.h"
     30
    231#include <OgreCamera.h>
    332#include <OgreHardwareBufferManager.h>
    433
    5 using namespace Ogre;
    6 
    7 namespace orxonox
     34namespace Ogre
    835{
    9 DynamicRenderable::DynamicRenderable()
    10 {
    11 }
    12 
    13 DynamicRenderable::~DynamicRenderable()
    14 {
    15   delete mRenderOp.vertexData;
    16   delete mRenderOp.indexData;
    17 }
    18 
    19 void DynamicRenderable::initialize(RenderOperation::OperationType operationType,
    20                                    bool useIndices)
    21 {
    22   // Initialize render operation
    23   mRenderOp.operationType = operationType;
    24   mRenderOp.useIndexes = useIndices;
    25   mRenderOp.vertexData = new VertexData;
    26   if (mRenderOp.useIndexes)
    27     mRenderOp.indexData = new IndexData;
    28 
    29   // Reset buffer capacities
    30   mVertexBufferCapacity = 0;
    31   mIndexBufferCapacity = 0;
    32 
    33   // Create vertex declaration
    34   createVertexDeclaration();
    35 }
    36 
    37 void DynamicRenderable::prepareHardwareBuffers(size_t vertexCount,
    38                                                size_t indexCount)
    39 {
    40   // Prepare vertex buffer
    41   size_t newVertCapacity = mVertexBufferCapacity;
    42   if ((vertexCount > mVertexBufferCapacity) ||
    43       (!mVertexBufferCapacity))
    44   {
    45     // vertexCount exceeds current capacity!
    46     // It is necessary to reallocate the buffer.
    47 
    48     // Check if this is the first call
    49     if (!newVertCapacity)
    50       newVertCapacity = 1;
    51 
    52     // Make capacity the next power of two
    53     while (newVertCapacity < vertexCount)
    54       newVertCapacity <<= 1;
    55   }
    56   else if (vertexCount < mVertexBufferCapacity>>1) {
    57     // Make capacity the previous power of two
    58     while (vertexCount < newVertCapacity>>1)
    59       newVertCapacity >>= 1;
    60   }
    61   if (newVertCapacity != mVertexBufferCapacity)
    62   {
    63     mVertexBufferCapacity = newVertCapacity;
    64     // Create new vertex buffer
    65     HardwareVertexBufferSharedPtr vbuf =
    66       HardwareBufferManager::getSingleton().createVertexBuffer(
    67         mRenderOp.vertexData->vertexDeclaration->getVertexSize(0),
    68         mVertexBufferCapacity,
    69         HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
    70 
    71     // Bind buffer
    72     mRenderOp.vertexData->vertexBufferBinding->setBinding(0, vbuf);
    73   }
    74   // Update vertex count in the render operation
    75   mRenderOp.vertexData->vertexCount = vertexCount;
    76 
    77   if (mRenderOp.useIndexes)
    78   {
    79     OgreAssert(indexCount <= std::numeric_limits<unsigned short>::max(), "indexCount exceeds 16 bit");
    80 
    81     size_t newIndexCapacity = mIndexBufferCapacity;
    82     // Prepare index buffer
    83     if ((indexCount > newIndexCapacity) ||
    84         (!newIndexCapacity))
     36    DynamicRenderable::DynamicRenderable()
    8537    {
    86       // indexCount exceeds current capacity!
    87       // It is necessary to reallocate the buffer.
    88 
    89       // Check if this is the first call
    90       if (!newIndexCapacity)
    91         newIndexCapacity = 1;
    92 
    93       // Make capacity the next power of two
    94       while (newIndexCapacity < indexCount)
    95         newIndexCapacity <<= 1;
    96 
    97     }
    98     else if (indexCount < newIndexCapacity>>1)
    99     {
    100       // Make capacity the previous power of two
    101       while (indexCount < newIndexCapacity>>1)
    102         newIndexCapacity >>= 1;
    10338    }
    10439
    105     if (newIndexCapacity != mIndexBufferCapacity)
     40    DynamicRenderable::~DynamicRenderable()
    10641    {
    107       mIndexBufferCapacity = newIndexCapacity;
    108       // Create new index buffer
    109       mRenderOp.indexData->indexBuffer =
    110         HardwareBufferManager::getSingleton().createIndexBuffer(
    111           HardwareIndexBuffer::IT_16BIT,
    112           mIndexBufferCapacity,
    113           HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
     42        delete mRenderOp.vertexData;
     43        delete mRenderOp.indexData;
    11444    }
    11545
    116     // Update index count in the render operation
    117     mRenderOp.indexData->indexCount = indexCount;
    118   }
     46    void DynamicRenderable::initialize(RenderOperation::OperationType operationType,
     47           bool useIndices)
     48    {
     49        // Initialize render operation
     50        mRenderOp.operationType = operationType;
     51        mRenderOp.useIndexes = useIndices;
     52        mRenderOp.vertexData = new VertexData;
     53        if (mRenderOp.useIndexes)
     54            mRenderOp.indexData = new IndexData;
     55
     56        // Reset buffer capacities
     57        mVertexBufferCapacity = 0;
     58        mIndexBufferCapacity = 0;
     59
     60        // Create vertex declaration
     61        createVertexDeclaration();
     62    }
     63
     64    void DynamicRenderable::prepareHardwareBuffers(size_t vertexCount, size_t indexCount)
     65    {
     66        // Prepare vertex buffer
     67        size_t newVertCapacity = mVertexBufferCapacity;
     68        if ((vertexCount > mVertexBufferCapacity) || (!mVertexBufferCapacity))
     69        {
     70            // vertexCount exceeds current capacity!
     71            // It is necessary to reallocate the buffer.
     72
     73            // Check if this is the first call
     74            if (!newVertCapacity)
     75                newVertCapacity = 1;
     76
     77            // Make capacity the next power of two
     78            while (newVertCapacity < vertexCount)
     79                newVertCapacity <<= 1;
     80        }
     81        else if (vertexCount < mVertexBufferCapacity>>1)
     82        {
     83            // Make capacity the previous power of two
     84            while (vertexCount < newVertCapacity>>1)
     85                newVertCapacity >>= 1;
     86        }
     87        if (newVertCapacity != mVertexBufferCapacity)
     88        {
     89            mVertexBufferCapacity = newVertCapacity;
     90            // Create new vertex buffer
     91            HardwareVertexBufferSharedPtr vbuf =
     92                HardwareBufferManager::getSingleton().createVertexBuffer(
     93                mRenderOp.vertexData->vertexDeclaration->getVertexSize(0),
     94                mVertexBufferCapacity,
     95                HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
     96
     97            // Bind buffer
     98            mRenderOp.vertexData->vertexBufferBinding->setBinding(0, vbuf);
     99        }
     100        // Update vertex count in the render operation
     101        mRenderOp.vertexData->vertexCount = vertexCount;
     102
     103        if (mRenderOp.useIndexes)
     104        {
     105            OgreAssert(indexCount <= std::numeric_limits<unsigned short>::max(), "indexCount exceeds 16 bit");
     106
     107            size_t newIndexCapacity = mIndexBufferCapacity;
     108            // Prepare index buffer
     109            if ((indexCount > newIndexCapacity) || (!newIndexCapacity))
     110            {
     111                // indexCount exceeds current capacity!
     112                // It is necessary to reallocate the buffer.
     113
     114                // Check if this is the first call
     115                if (!newIndexCapacity)
     116                    newIndexCapacity = 1;
     117
     118                // Make capacity the next power of two
     119                while (newIndexCapacity < indexCount)
     120                    newIndexCapacity <<= 1;
     121            }
     122            else if (indexCount < newIndexCapacity>>1)
     123            {
     124                // Make capacity the previous power of two
     125                while (indexCount < newIndexCapacity>>1)
     126                    newIndexCapacity >>= 1;
     127            }
     128
     129            if (newIndexCapacity != mIndexBufferCapacity)
     130            {
     131                mIndexBufferCapacity = newIndexCapacity;
     132                // Create new index buffer
     133                mRenderOp.indexData->indexBuffer =
     134                    HardwareBufferManager::getSingleton().createIndexBuffer(
     135                    HardwareIndexBuffer::IT_16BIT,
     136                    mIndexBufferCapacity,
     137                    HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
     138            }
     139
     140            // Update index count in the render operation
     141            mRenderOp.indexData->indexCount = indexCount;
     142        }
     143    }
     144
     145    Real DynamicRenderable::getBoundingRadius(void) const
     146    {
     147        return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
     148    }
     149
     150    Real DynamicRenderable::getSquaredViewDepth(const Camera* cam) const
     151    {
     152        Vector3 vMin, vMax, vMid, vDist;
     153        vMin = mBox.getMinimum();
     154        vMax = mBox.getMaximum();
     155        vMid = ((vMax - vMin) * 0.5) + vMin;
     156        vDist = cam->getDerivedPosition() - vMid;
     157
     158        return vDist.squaredLength();
     159    }
    119160}
    120 
    121 Real DynamicRenderable::getBoundingRadius(void) const
    122 {
    123   return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
    124 }
    125 
    126 Real DynamicRenderable::getSquaredViewDepth(const Camera* cam) const
    127 {
    128    Vector3 vMin, vMax, vMid, vDist;
    129    vMin = mBox.getMinimum();
    130    vMax = mBox.getMaximum();
    131    vMid = ((vMax - vMin) * 0.5) + vMin;
    132    vDist = cam->getDerivedPosition() - vMid;
    133 
    134    return vDist.squaredLength();
    135 }
    136 }
  • code/branches/pch/src/orxonox/tools/DynamicRenderable.h

    r3089 r3130  
    1 #ifndef DYNAMIC_RENDERABLE_H
    2 #define DYNAMIC_RENDERABLE_H
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Sinbad, Baxissimo, DWORD, TheBren (OGRE Wiki)
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
     29#ifndef _DynamicRenderable_H__
     30#define _DynamicRenderable_H__
     31
     32#include "OrxonoxPrereqs.h"
    333
    434#include <OgreSimpleRenderable.h>
    535
    6 namespace orxonox
     36namespace Ogre
    737{
    8 /// Abstract base class providing mechanisms for dynamically growing hardware buffers.
    9 class DynamicRenderable : public Ogre::SimpleRenderable
    10 {
    11 public:
    12   /// Constructor
    13   DynamicRenderable();
    14   /// Virtual destructor
    15   virtual ~DynamicRenderable();
     38    /// Abstract base class providing mechanisms for dynamically growing hardware buffers.
     39    class DynamicRenderable : public SimpleRenderable
     40    {
     41    public:
     42        /// Constructor
     43        DynamicRenderable();
     44        /// Virtual destructor
     45        virtual ~DynamicRenderable();
    1646
    17   /** Initializes the dynamic renderable.
    18    @remarks
    19       This function should only be called once. It initializes the
    20       render operation, and calls the abstract function
    21       createVertexDeclaration().
    22    @param operationType The type of render operation to perform.
    23    @param useIndices Specifies whether to use indices to determine the
    24           vertices to use as input. */
    25   void initialize(Ogre::RenderOperation::OperationType operationType,
    26                   bool useIndices);
     47        /**
     48        @brief
     49            Initializes the dynamic renderable.
     50        @remarks
     51            This function should only be called once. It initializes the
     52            render operation, and calls the abstract function
     53            createVertexDeclaration().
     54        @param operationType
     55            The type of render operation to perform.
     56        @param useIndices
     57            Specifies whether to use indices to determine the vertices to use as input.
     58        */
     59        void initialize(RenderOperation::OperationType operationType,
     60        bool useIndices);
    2761
    28   /// Implementation of Ogre::SimpleRenderable
    29   virtual Ogre::Real getBoundingRadius(void) const;
    30   /// Implementation of Ogre::SimpleRenderable
    31   virtual Ogre::Real getSquaredViewDepth(const Ogre::Camera* cam) const;
     62        /// Implementation of SimpleRenderable
     63        virtual Real getBoundingRadius(void) const;
     64        /// Implementation of SimpleRenderable
     65        virtual Real getSquaredViewDepth(const Camera* cam) const;
    3266
    33 protected:
    34   /// Maximum capacity of the currently allocated vertex buffer.
    35   size_t mVertexBufferCapacity;
    36   /// Maximum capacity of the currently allocated index buffer.
    37   size_t mIndexBufferCapacity;
     67    protected:
     68        /// Maximum capacity of the currently allocated vertex buffer.
     69        size_t mVertexBufferCapacity;
     70        /// Maximum capacity of the currently allocated index buffer.
     71        size_t mIndexBufferCapacity;
    3872
    39   /** Creates the vertex declaration.
    40    @remarks
    41       Override and set mRenderOp.vertexData->vertexDeclaration here.
    42       mRenderOp.vertexData will be created for you before this method
    43       is called. */
    44   virtual void createVertexDeclaration() = 0;
     73        /**
     74        @brief
     75            Creates the vertex declaration.
     76        @remarks
     77            Override and set mRenderOp.vertexData->vertexDeclaration here.
     78            mRenderOp.vertexData will be created for you before this method
     79            is called.
     80        */
     81        virtual void createVertexDeclaration() = 0;
    4582
    46   /** Prepares the hardware buffers for the requested vertex and index counts.
    47    @remarks
    48       This function must be called before locking the buffers in
    49       fillHardwareBuffers(). It guarantees that the hardware buffers
    50       are large enough to hold at least the requested number of
    51       vertices and indices (if using indices). The buffers are
    52       possibly reallocated to achieve this.
    53    @par
    54       The vertex and index count in the render operation are set to
    55       the values of vertexCount and indexCount respectively.
    56    @param vertexCount The number of vertices the buffer must hold.
     83        /**
     84        @brief
     85            Prepares the hardware buffers for the requested vertex and index counts.
     86        @remarks
     87            This function must be called before locking the buffers in
     88            fillHardwareBuffers(). It guarantees that the hardware buffers
     89            are large enough to hold at least the requested number of
     90            vertices and indices (if using indices). The buffers are
     91            possibly reallocated to achieve this.
     92        @par
     93            The vertex and index count in the render operation are set to
     94            the values of vertexCount and indexCount respectively.
     95        @param vertexCount
     96            The number of vertices the buffer must hold.
     97        @param indexCount
     98            The number of indices the buffer must hold. This
     99            parameter is ignored if not using indices.
     100        */
     101        void prepareHardwareBuffers(size_t vertexCount, size_t indexCount);
    57102
    58    @param indexCount The number of indices the buffer must hold. This
    59           parameter is ignored if not using indices. */
    60   void prepareHardwareBuffers(size_t vertexCount, size_t indexCount);
    61 
    62   /** Fills the hardware vertex and index buffers with data.
    63    @remarks
    64       This function must call prepareHardwareBuffers() before locking
    65       the buffers to ensure the they are large enough for the data to
    66       be written. Afterwards the vertex and index buffers (if using
    67       indices) can be locked, and data can be written to them. */
    68   virtual void fillHardwareBuffers() = 0;
    69 };
     103        /**
     104        @brief
     105            Fills the hardware vertex and index buffers with data.
     106        @remarks
     107            This function must call prepareHardwareBuffers() before locking
     108            the buffers to ensure the they are large enough for the data to
     109            be written. Afterwards the vertex and index buffers (if using
     110            indices) can be locked, and data can be written to them.
     111        */
     112        virtual void fillHardwareBuffers() = 0;
     113    };
    70114}
    71115
    72 #endif // DYNAMIC_RENDERABLE_H
     116#endif /* _DynamicRenderable_H__ */
Note: See TracChangeset for help on using the changeset viewer.