Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/tools/DynamicRenderable.cc @ 9601

Last change on this file since 9601 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

  • Property svn:eol-style set to native
File size: 5.4 KB
RevLine 
[3130]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
[2942]29#include "DynamicRenderable.h"
[3130]30
[2942]31#include <OgreCamera.h>
32#include <OgreHardwareBufferManager.h>
33
[3130]34namespace Ogre
[3089]35{
[3130]36    DynamicRenderable::DynamicRenderable()
37    {
38    }
[2942]39
[3130]40    DynamicRenderable::~DynamicRenderable()
41    {
42        delete mRenderOp.vertexData;
43        delete mRenderOp.indexData;
44    }
[2942]45
[3130]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;
[2942]55
[3130]56        // Reset buffer capacities
57        mVertexBufferCapacity = 0;
58        mIndexBufferCapacity = 0;
[2942]59
[3130]60        // Create vertex declaration
61        createVertexDeclaration();
62    }
[2942]63
[3130]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.
[2942]72
[3130]73            // Check if this is the first call
74            if (!newVertCapacity)
75                newVertCapacity = 1;
[2942]76
[3130]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_?
[2942]96
[3130]97            // Bind buffer
98            mRenderOp.vertexData->vertexBufferBinding->setBinding(0, vbuf);
99        }
100        // Update vertex count in the render operation
101        mRenderOp.vertexData->vertexCount = vertexCount;
[2942]102
[3130]103        if (mRenderOp.useIndexes)
104        {
105            OgreAssert(indexCount <= std::numeric_limits<unsigned short>::max(), "indexCount exceeds 16 bit");
[2942]106
[3130]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.
[2942]113
[3130]114                // Check if this is the first call
115                if (!newIndexCapacity)
116                    newIndexCapacity = 1;
[2942]117
[3130]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            }
[2942]128
[3130]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        }
[2942]143    }
[3130]144
145    Real DynamicRenderable::getBoundingRadius(void) const
[2942]146    {
[3130]147        return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
[2942]148    }
149
[3130]150    Real DynamicRenderable::getSquaredViewDepth(const Camera* cam) const
[2942]151    {
[3130]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();
[2942]159    }
160}
Note: See TracBrowser for help on using the repository browser.