Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Hover_FS18/src/libraries/tools/DynamicRenderable.cc @ 11821

Last change on this file since 11821 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
Line 
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
29#include "DynamicRenderable.h"
30
31#include <OgreCamera.h>
32#include <OgreHardwareBufferManager.h>
33
34namespace Ogre
35{
36    DynamicRenderable::DynamicRenderable()
37    {
38    }
39
40    DynamicRenderable::~DynamicRenderable()
41    {
42        delete mRenderOp.vertexData;
43        delete mRenderOp.indexData;
44    }
45
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    }
160}
Note: See TracBrowser for help on using the repository browser.