-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicRenderable.cpp
More file actions
142 lines (118 loc) · 3.88 KB
/
DynamicRenderable.cpp
File metadata and controls
142 lines (118 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "DynamicRenderable.h"
#include <OgreCamera.h>
#include <OgreHardwareBufferManager.h>
using namespace Ogre;
#if OGRE_VERSION == ((2 << 16) | (0 << 8) | 0)
DynamicRenderable::DynamicRenderable(Ogre::ObjectMemoryManager* mgr):
Ogre::SimpleRenderable(Ogre::Id::generateNewId<DynamicRenderable>(), mgr)
#else
DynamicRenderable::DynamicRenderable()
#endif
{
}
DynamicRenderable::~DynamicRenderable()
{
delete mRenderOp.vertexData;
delete mRenderOp.indexData;
}
void DynamicRenderable::initialize (RenderOperation::OperationType operationType,
bool useIndices)
{
// Initialize render operation
mRenderOp.operationType = operationType;
mRenderOp.useIndexes = useIndices;
mRenderOp.vertexData = new VertexData;
if (mRenderOp.useIndexes)
mRenderOp.indexData = new IndexData;
// Reset buffer capacities
mVertexBufferCapacity = 0;
mIndexBufferCapacity = 0;
// Create vertex declaration
createVertexDeclaration();
}
void DynamicRenderable::prepareHardwareBuffers (size_t vertexCount,
size_t indexCount)
{
// Prepare vertex buffer
size_t newVertCapacity = mVertexBufferCapacity;
if ( (vertexCount > mVertexBufferCapacity) ||
(!mVertexBufferCapacity) )
{
// vertexCount exceeds current capacity!
// It is necessary to reallocate the buffer.
// Check if this is the first call
if (!newVertCapacity)
newVertCapacity = 1;
// Make capacity the next power of two
while (newVertCapacity < vertexCount)
newVertCapacity <<= 1;
}
else if (vertexCount < mVertexBufferCapacity >> 1) {
// Make capacity the previous power of two
while (vertexCount < newVertCapacity >> 1)
newVertCapacity >>= 1;
}
if (newVertCapacity != mVertexBufferCapacity)
{
mVertexBufferCapacity = newVertCapacity;
// Create new vertex buffer
HardwareVertexBufferSharedPtr vbuf =
HardwareBufferManager::getSingleton().createVertexBuffer (
mRenderOp.vertexData->vertexDeclaration->getVertexSize (0),
mVertexBufferCapacity,
HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
// Bind buffer
mRenderOp.vertexData->vertexBufferBinding->setBinding (0, vbuf);
}
// Update vertex count in the render operation
mRenderOp.vertexData->vertexCount = vertexCount;
if (mRenderOp.useIndexes)
{
OgreAssert (indexCount <= std::numeric_limits<unsigned short>::max(), "indexCount exceeds 16 bit");
size_t newIndexCapacity = mIndexBufferCapacity;
// Prepare index buffer
if ( (indexCount > newIndexCapacity) ||
(!newIndexCapacity) )
{
// indexCount exceeds current capacity!
// It is necessary to reallocate the buffer.
// Check if this is the first call
if (!newIndexCapacity)
newIndexCapacity = 1;
// Make capacity the next power of two
while (newIndexCapacity < indexCount)
newIndexCapacity <<= 1;
}
else if (indexCount < newIndexCapacity >> 1)
{
// Make capacity the previous power of two
while (indexCount < newIndexCapacity >> 1)
newIndexCapacity >>= 1;
}
if (newIndexCapacity != mIndexBufferCapacity)
{
mIndexBufferCapacity = newIndexCapacity;
// Create new index buffer
mRenderOp.indexData->indexBuffer =
HardwareBufferManager::getSingleton().createIndexBuffer (
HardwareIndexBuffer::IT_16BIT,
mIndexBufferCapacity,
HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); // TODO: Custom HBU_?
}
// Update index count in the render operation
mRenderOp.indexData->indexCount = indexCount;
}
}
Real DynamicRenderable::getBoundingRadius (void) const
{
return Math::Sqrt (std::max (mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength() ) );
}
Real DynamicRenderable::getSquaredViewDepth (const Camera* cam) const
{
Vector3 vMin, vMax, vMid, vDist;
vMin = mBox.getMinimum();
vMax = mBox.getMaximum();
vMid = ( (vMax - vMin) * 0.5) + vMin;
vDist = cam->getDerivedPosition() - vMid;
return vDist.squaredLength();
}