[2942] | 1 | #include "DynamicRenderable.h" |
---|
| 2 | #include <OgreCamera.h> |
---|
| 3 | #include <OgreHardwareBufferManager.h> |
---|
| 4 | |
---|
| 5 | using namespace Ogre; |
---|
| 6 | |
---|
[3089] | 7 | namespace orxonox |
---|
| 8 | { |
---|
[2942] | 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 | |
---|
[3089] | 37 | void DynamicRenderable::prepareHardwareBuffers(size_t vertexCount, |
---|
[2942] | 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 | } |
---|
[3089] | 61 | if (newVertCapacity != mVertexBufferCapacity) |
---|
[2942] | 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 | } |
---|
[3089] | 98 | else if (indexCount < newIndexCapacity>>1) |
---|
[2942] | 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 | |
---|
| 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(); |
---|
[3089] | 135 | } |
---|
| 136 | } |
---|