00001 #include "DynamicRenderable.h"
00002 #include "OgreCamera.h"
00003 #include "OgreHardwareBufferManager.h"
00004
00005 using namespace Ogre;
00006
00007 namespace ASR
00008 {
00009
00010 DynamicRenderable::DynamicRenderable()
00011 {
00012 }
00013
00014 DynamicRenderable::~DynamicRenderable()
00015 {
00016 delete mRenderOp.vertexData;
00017 delete mRenderOp.indexData;
00018 }
00019
00020 void DynamicRenderable::initialize(RenderOperation::OperationType operationType,
00021 bool useIndices)
00022 {
00023
00024 mRenderOp.operationType = operationType;
00025 mRenderOp.useIndexes = useIndices;
00026 mRenderOp.vertexData = new VertexData;
00027 if (mRenderOp.useIndexes)
00028 mRenderOp.indexData = new IndexData;
00029
00030
00031 mVertexBufferCapacity = 0;
00032 mIndexBufferCapacity = 0;
00033
00034
00035 createVertexDeclaration();
00036 }
00037
00038 void DynamicRenderable::prepareHardwareBuffers(size_t vertexCount,
00039 size_t indexCount)
00040 {
00041
00042 size_t newVertCapacity = mVertexBufferCapacity;
00043 if ((vertexCount > mVertexBufferCapacity) ||
00044 (!mVertexBufferCapacity))
00045 {
00046
00047
00048
00049
00050 if (!newVertCapacity)
00051 newVertCapacity = 1;
00052
00053
00054 while (newVertCapacity < vertexCount)
00055 newVertCapacity <<= 1;
00056 }
00057 else if (vertexCount < mVertexBufferCapacity>>1) {
00058
00059 while (vertexCount < newVertCapacity>>1)
00060 newVertCapacity >>= 1;
00061 }
00062 if (newVertCapacity != mVertexBufferCapacity)
00063 {
00064 mVertexBufferCapacity = newVertCapacity;
00065
00066 HardwareVertexBufferSharedPtr vbuf =
00067 HardwareBufferManager::getSingleton().createVertexBuffer(
00068 mRenderOp.vertexData->vertexDeclaration->getVertexSize(0),
00069 mVertexBufferCapacity,
00070 HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY);
00071
00072
00073 mRenderOp.vertexData->vertexBufferBinding->setBinding(0, vbuf);
00074 }
00075
00076 mRenderOp.vertexData->vertexCount = vertexCount;
00077
00078 if (mRenderOp.useIndexes)
00079 {
00080 OgreAssert(indexCount <= std::numeric_limits<unsigned short>::max(), "indexCount exceeds 16 bit");
00081
00082 size_t newIndexCapacity = mIndexBufferCapacity;
00083
00084 if ((indexCount > newIndexCapacity) ||
00085 (!newIndexCapacity))
00086 {
00087
00088
00089
00090
00091 if (!newIndexCapacity)
00092 newIndexCapacity = 1;
00093
00094
00095 while (newIndexCapacity < indexCount)
00096 newIndexCapacity <<= 1;
00097
00098 }
00099 else if (indexCount < newIndexCapacity>>1)
00100 {
00101
00102 while (indexCount < newIndexCapacity>>1)
00103 newIndexCapacity >>= 1;
00104 }
00105
00106 if (newIndexCapacity != mIndexBufferCapacity)
00107 {
00108 mIndexBufferCapacity = newIndexCapacity;
00109
00110 mRenderOp.indexData->indexBuffer =
00111 HardwareBufferManager::getSingleton().createIndexBuffer(
00112 HardwareIndexBuffer::IT_16BIT,
00113 mIndexBufferCapacity,
00114 HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY);
00115 }
00116
00117
00118 mRenderOp.indexData->indexCount = indexCount;
00119 }
00120 }
00121
00122 Real DynamicRenderable::getBoundingRadius(void) const
00123 {
00124 return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
00125 }
00126
00127 Real DynamicRenderable::getSquaredViewDepth(const Camera* cam) const
00128 {
00129 Vector3 vMin, vMax, vMid, vDist;
00130 vMin = mBox.getMinimum();
00131 vMax = mBox.getMaximum();
00132 vMid = ((vMin - vMax) * 0.5) + vMin;
00133 vDist = cam->getDerivedPosition() - vMid;
00134
00135 return vDist.squaredLength();
00136 }
00137 }