Changeset 3130 for code/branches/pch/src/orxonox/tools/DynamicRenderable.cc
- Timestamp:
- Jun 9, 2009, 7:51:00 PM (15 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
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 1 29 #include "DynamicRenderable.h" 30 2 31 #include <OgreCamera.h> 3 32 #include <OgreHardwareBufferManager.h> 4 33 5 using namespace Ogre; 6 7 namespace orxonox 34 namespace Ogre 8 35 { 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() 85 37 { 86 // indexCount exceeds current capacity!87 // It is necessary to reallocate the buffer.88 89 // Check if this is the first call90 if (!newIndexCapacity)91 newIndexCapacity = 1;92 93 // Make capacity the next power of two94 while (newIndexCapacity < indexCount)95 newIndexCapacity <<= 1;96 97 }98 else if (indexCount < newIndexCapacity>>1)99 {100 // Make capacity the previous power of two101 while (indexCount < newIndexCapacity>>1)102 newIndexCapacity >>= 1;103 38 } 104 39 105 if (newIndexCapacity != mIndexBufferCapacity)40 DynamicRenderable::~DynamicRenderable() 106 41 { 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; 114 44 } 115 45 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 } 119 160 } 120 121 Real DynamicRenderable::getBoundingRadius(void) const122 {123 return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));124 }125 126 Real DynamicRenderable::getSquaredViewDepth(const Camera* cam) const127 {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 TracChangeset
for help on using the changeset viewer.