Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/map/src/orxonox/tools/DynamicRenderable.cpp @ 2942

Last change on this file since 2942 was 2942, checked in by Naaduun, 15 years ago

=added dynamic libs to orxonox/tools. changed humancontroller to use mouseview

File size: 3.8 KB
Line 
1#include "DynamicRenderable.h"
2#include <OgreCamera.h>
3#include <OgreHardwareBufferManager.h>
4
5using namespace Ogre;
6
7DynamicRenderable::DynamicRenderable()
8{
9}
10
11DynamicRenderable::~DynamicRenderable()
12{
13  delete mRenderOp.vertexData;
14  delete mRenderOp.indexData;
15}
16
17void DynamicRenderable::initialize(RenderOperation::OperationType operationType,
18                                   bool useIndices)
19{
20  // Initialize render operation
21  mRenderOp.operationType = operationType;
22  mRenderOp.useIndexes = useIndices;
23  mRenderOp.vertexData = new VertexData;
24  if (mRenderOp.useIndexes)
25    mRenderOp.indexData = new IndexData;
26
27  // Reset buffer capacities
28  mVertexBufferCapacity = 0;
29  mIndexBufferCapacity = 0;
30
31  // Create vertex declaration
32  createVertexDeclaration();
33}
34
35void DynamicRenderable::prepareHardwareBuffers(size_t vertexCount, 
36                                               size_t indexCount)
37{
38  // Prepare vertex buffer
39  size_t newVertCapacity = mVertexBufferCapacity;
40  if ((vertexCount > mVertexBufferCapacity) ||
41      (!mVertexBufferCapacity))
42  {
43    // vertexCount exceeds current capacity!
44    // It is necessary to reallocate the buffer.
45
46    // Check if this is the first call
47    if (!newVertCapacity)
48      newVertCapacity = 1;
49
50    // Make capacity the next power of two
51    while (newVertCapacity < vertexCount)
52      newVertCapacity <<= 1;
53  }
54  else if (vertexCount < mVertexBufferCapacity>>1) {
55    // Make capacity the previous power of two
56    while (vertexCount < newVertCapacity>>1)
57      newVertCapacity >>= 1;
58  }
59  if (newVertCapacity != mVertexBufferCapacity) 
60  {
61    mVertexBufferCapacity = newVertCapacity;
62    // Create new vertex buffer
63    HardwareVertexBufferSharedPtr vbuf =
64      HardwareBufferManager::getSingleton().createVertexBuffer(
65        mRenderOp.vertexData->vertexDeclaration->getVertexSize(0),
66        mVertexBufferCapacity,
67        HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
68
69    // Bind buffer
70    mRenderOp.vertexData->vertexBufferBinding->setBinding(0, vbuf);
71  }
72  // Update vertex count in the render operation
73  mRenderOp.vertexData->vertexCount = vertexCount;
74
75  if (mRenderOp.useIndexes)
76  {
77    OgreAssert(indexCount <= std::numeric_limits<unsigned short>::max(), "indexCount exceeds 16 bit");
78
79    size_t newIndexCapacity = mIndexBufferCapacity;
80    // Prepare index buffer
81    if ((indexCount > newIndexCapacity) ||
82        (!newIndexCapacity))
83    {
84      // indexCount exceeds current capacity!
85      // It is necessary to reallocate the buffer.
86
87      // Check if this is the first call
88      if (!newIndexCapacity)
89        newIndexCapacity = 1;
90
91      // Make capacity the next power of two
92      while (newIndexCapacity < indexCount)
93        newIndexCapacity <<= 1;
94
95    }
96    else if (indexCount < newIndexCapacity>>1) 
97    {
98      // Make capacity the previous power of two
99      while (indexCount < newIndexCapacity>>1)
100        newIndexCapacity >>= 1;
101    }
102
103    if (newIndexCapacity != mIndexBufferCapacity)
104    {
105      mIndexBufferCapacity = newIndexCapacity;
106      // Create new index buffer
107      mRenderOp.indexData->indexBuffer =
108        HardwareBufferManager::getSingleton().createIndexBuffer(
109          HardwareIndexBuffer::IT_16BIT,
110          mIndexBufferCapacity,
111          HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
112    }
113
114    // Update index count in the render operation
115    mRenderOp.indexData->indexCount = indexCount;
116  }
117}
118
119Real DynamicRenderable::getBoundingRadius(void) const
120{
121  return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
122}
123
124Real DynamicRenderable::getSquaredViewDepth(const Camera* cam) const
125{
126   Vector3 vMin, vMax, vMid, vDist;
127   vMin = mBox.getMinimum();
128   vMax = mBox.getMaximum();
129   vMid = ((vMax - vMin) * 0.5) + vMin;
130   vDist = cam->getDerivedPosition() - vMid;
131
132   return vDist.squaredLength();
133}
Note: See TracBrowser for help on using the repository browser.