Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/tools/DynamicRenderable.cpp @ 3106

Last change on this file since 3106 was 3089, checked in by landauf, 15 years ago

merged map branch back to trunk

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1#include "DynamicRenderable.h"
2#include <OgreCamera.h>
3#include <OgreHardwareBufferManager.h>
4
5using namespace Ogre;
6
7namespace orxonox
8{
9DynamicRenderable::DynamicRenderable()
10{
11}
12
13DynamicRenderable::~DynamicRenderable()
14{
15  delete mRenderOp.vertexData;
16  delete mRenderOp.indexData;
17}
18
19void 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
37void 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))
85    {
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;
103    }
104
105    if (newIndexCapacity != mIndexBufferCapacity)
106    {
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_?
114    }
115
116    // Update index count in the render operation
117    mRenderOp.indexData->indexCount = indexCount;
118  }
119}
120
121Real DynamicRenderable::getBoundingRadius(void) const
122{
123  return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
124}
125
126Real 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}
Note: See TracBrowser for help on using the repository browser.