-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicLineDrawer.cpp
More file actions
213 lines (175 loc) · 5.64 KB
/
DynamicLineDrawer.cpp
File metadata and controls
213 lines (175 loc) · 5.64 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "DynamicLineDrawer.hpp"
enum {
POSITION_BINDING,
COLOUR_BINDING
};
CDynamicLineDrawer::CDynamicLineDrawer(void)
{
Initialize();
Dirty = true;
}
CDynamicLineDrawer::~CDynamicLineDrawer(void)
{
delete mRenderOp.vertexData;
delete mRenderOp.indexData;
}
void CDynamicLineDrawer::Initialize()
{
mRenderOp.operationType = Ogre::RenderOperation::OT_LINE_LIST;
mRenderOp.useIndexes = false;
mRenderOp.vertexData = new Ogre::VertexData;
// Reset buffer capacities
VertexBufferCapacity = 0;
// Create vertex declaration
CreateVertexDeclaration();
}
Ogre::Real CDynamicLineDrawer::getBoundingRadius() const
{
return Ogre::Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength()));
}
Ogre::Real CDynamicLineDrawer::getSquaredViewDepth(const Ogre::Camera* Cam) const
{
Ogre::Vector3 VMin, VMax, VMid, VDist;
VMin = mBox.getMinimum();
VMax = mBox.getMaximum();
VMid = ((VMax - VMin) * 0.5f) + VMin;
VDist = Cam->getDerivedPosition() - VMid;
return VDist.squaredLength();
}
void CDynamicLineDrawer::AddPoint(const Ogre::Vector3 &Point, const Ogre::ColourValue &Colour)
{
Points.push_back(Point);
Colours.push_back(Colour);
Dirty = true;
}
void CDynamicLineDrawer::AddPoint(Ogre::Real X, Ogre::Real Y, Ogre::Real Z, Ogre::Real R, Ogre::Real G, Ogre::Real B)
{
Points.push_back(Ogre::Vector3(X, Y, Z));
Colours.push_back(Ogre::ColourValue(R, G, B));
Dirty = true;
}
void CDynamicLineDrawer::SetPoint(unsigned short Index, const Ogre::Vector3 &Value, const Ogre::ColourValue &ColourValue)
{
assert(Index < Points.size() && "Point index is out of bounds!!");
Points[Index] = Value;
Colours[Index] = ColourValue;
Dirty = true;
}
const Ogre::Vector3& CDynamicLineDrawer::GetPoint(unsigned short Index) const
{
assert(Index < Points.size() && "Point index is out of bounds!!");
return Points[Index];
}
unsigned short CDynamicLineDrawer::GetNumPoints() const
{
return (unsigned short) Points.size();
}
void CDynamicLineDrawer::Clear()
{
Points.clear();
Colours.clear();
Dirty = true;
}
void CDynamicLineDrawer::Update()
{
if ( Dirty )
FillHardwareBuffers();
}
void CDynamicLineDrawer::CreateVertexDeclaration()
{
// Points
Ogre::VertexDeclaration *Decl = mRenderOp.vertexData->vertexDeclaration;
Decl->addElement(POSITION_BINDING, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
// Colour
Decl->addElement(COLOUR_BINDING, 0, Ogre::VET_COLOUR, Ogre::VES_DIFFUSE);
}
void CDynamicLineDrawer::PrepareHardwareBuffers(size_t VertexCount)
{
// Prepare vertex buffer
size_t NewVertCapacity = VertexBufferCapacity;
if ( ( VertexCount > VertexBufferCapacity ) || !VertexBufferCapacity )
{
// 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 < VertexBufferCapacity >> 1 )
{
// Make capacity the previous power of two
while ( VertexCount < NewVertCapacity >> 1 )
NewVertCapacity >>= 1;
}
if ( NewVertCapacity != VertexBufferCapacity )
{
VertexBufferCapacity = NewVertCapacity;
// Create new vertex buffer
Ogre::HardwareVertexBufferSharedPtr VBuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
mRenderOp.vertexData->vertexDeclaration->getVertexSize(0),
VertexBufferCapacity,
Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY );
// Bind buffer
mRenderOp.vertexData->vertexBufferBinding->setBinding(0, VBuf);
// Create new colour buffer
size_t offset = Ogre::VertexElement::getTypeSize(Ogre::VET_COLOUR);
VBuf = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
offset,
VertexBufferCapacity,
Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY );
// Bind buffer
mRenderOp.vertexData->vertexBufferBinding->setBinding(1, VBuf);
}
// Update vertex count in the render operation
mRenderOp.vertexData->vertexCount = VertexCount;
}
void CDynamicLineDrawer::FillHardwareBuffers()
{
int Size = int(Points.size());
PrepareHardwareBuffers(Size);
if (!Size) {
mBox.setExtents( Ogre::Vector3::ZERO, Ogre::Vector3::ZERO );
Dirty = false;
return;
}
Ogre::Vector3 AABMin = Points[0];
Ogre::Vector3 AABMax = Points[0];
Ogre::HardwareVertexBufferSharedPtr VBuf =
mRenderOp.vertexData->vertexBufferBinding->getBuffer(0);
Ogre::HardwareVertexBufferSharedPtr CBuf =
mRenderOp.vertexData->vertexBufferBinding->getBuffer(1);
// get rendersystem to pack colours
Ogre::RenderSystem* RS = Ogre::Root::getSingleton().getRenderSystem();
Ogre::Real* VPrPos = static_cast<Ogre::Real*>(VBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
Ogre::RGBA* CPrPos = static_cast<Ogre::RGBA*>(CBuf->lock(Ogre::HardwareBuffer::HBL_DISCARD));
for(int i = 0; i < Size; i++)
{
*VPrPos++ = Points[i].x;
*VPrPos++ = Points[i].y;
*VPrPos++ = Points[i].z;
Ogre::RGBA color;
RS->convertColourValue(Colours[i], &color);
*CPrPos++ = color;
//*CPrPos++ = unsigned int(Colours[i].g);
//*CPrPos++ = unsigned int(Colours[i].b);
if(Points[i].x < AABMin.x)
AABMin.x = Points[i].x;
if(Points[i].y < AABMin.y)
AABMin.y = Points[i].y;
if(Points[i].z < AABMin.z)
AABMin.z = Points[i].z;
if(Points[i].x > AABMax.x)
AABMax.x = Points[i].x;
if(Points[i].y > AABMax.y)
AABMax.y = Points[i].y;
if(Points[i].z > AABMax.z)
AABMax.z = Points[i].z;
}
VBuf->unlock();
CBuf->unlock();
mBox.setExtents(AABMin, AABMax);
Dirty = false;
}