-
Notifications
You must be signed in to change notification settings - Fork 52
/
Ogre2Marker.cc
397 lines (355 loc) · 9.93 KB
/
Ogre2Marker.cc
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#else
#ifndef _WIN32
#include <GL/gl.h>
#endif
#endif
#include <gz/common/Console.hh>
#include "gz/rendering/ogre2/Ogre2Capsule.hh"
#include "gz/rendering/ogre2/Ogre2Conversions.hh"
#include "gz/rendering/ogre2/Ogre2DynamicRenderable.hh"
#include "gz/rendering/ogre2/Ogre2Marker.hh"
#include "gz/rendering/ogre2/Ogre2Material.hh"
#include "gz/rendering/ogre2/Ogre2Mesh.hh"
#include "gz/rendering/ogre2/Ogre2RenderEngine.hh"
#include "gz/rendering/ogre2/Ogre2Scene.hh"
#include "gz/rendering/ogre2/Ogre2Visual.hh"
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <OgreItem.h>
#include <OgreMaterialManager.h>
#include <OgreRoot.h>
#include <OgreTechnique.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
class gz::rendering::Ogre2MarkerPrivate
{
/// \brief Marker material
public: Ogre2MaterialPtr material = nullptr;
/// \brief Flag to indicate whether or not this mesh should be
/// responsible for destroying the material
public: bool ownsMaterial = false;
/// \brief Geometry Object for primitive shapes
public: Ogre2GeometryPtr geom{nullptr};
/// \brief DynamicLines Object to display
public: std::shared_ptr<Ogre2DynamicRenderable> dynamicRenderable;
};
using namespace gz;
using namespace rendering;
//////////////////////////////////////////////////
Ogre2Marker::Ogre2Marker()
: dataPtr(new Ogre2MarkerPrivate)
{
}
//////////////////////////////////////////////////
Ogre2Marker::~Ogre2Marker()
{
this->Destroy();
}
//////////////////////////////////////////////////
void Ogre2Marker::PreRender()
{
if (this->markerType == MarkerType::MT_POINTS &&
this->dataPtr->dynamicRenderable &&
this->dataPtr->dynamicRenderable->PointCount() > 0u)
{
Ogre::Item *item = dynamic_cast<Ogre::Item *>(
this->dataPtr->dynamicRenderable->OgreObject());
if (!item->getSubItem(0)->getMaterial() ||
item->getSubItem(0)->getMaterial()->getName() != "PointCloudPoint")
{
// enable GL_PROGRAM_POINT_SIZE so we can set gl_PointSize in vertex
// shader
auto engine = Ogre2RenderEngine::Instance();
std::string renderSystemName =
engine->OgreRoot()->getRenderSystem()->getFriendlyName();
if (renderSystemName.find("OpenGL") != std::string::npos)
{
#ifdef __APPLE__
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
#else
#ifndef _WIN32
glEnable(GL_PROGRAM_POINT_SIZE);
#endif
#endif
}
Ogre::MaterialPtr pointsMat =
Ogre::MaterialManager::getSingleton().getByName(
"PointCloudPoint");
item->getSubItem(0)->setMaterial(pointsMat);
}
// point renderables use low level materials
// get the material and set size uniform variable
auto pass = item->getSubItem(0)->getMaterial()->getTechnique(0)->getPass(0);
auto vertParams = pass->getVertexProgramParameters();
vertParams->setNamedConstant("size", static_cast<Ogre::Real>(this->size));
}
this->dataPtr->dynamicRenderable->Update();
}
//////////////////////////////////////////////////
void Ogre2Marker::Destroy()
{
BaseMarker::Destroy();
if (this->dataPtr->geom)
{
this->dataPtr->geom->Destroy();
this->dataPtr->geom.reset();
}
if (this->dataPtr->dynamicRenderable)
{
this->dataPtr->dynamicRenderable->Destroy();
this->dataPtr->dynamicRenderable.reset();
}
if (this->dataPtr->material && this->dataPtr->ownsMaterial && this->Scene())
{
this->Scene()->DestroyMaterial(this->dataPtr->material);
this->dataPtr->material.reset();
}
}
//////////////////////////////////////////////////
Ogre::MovableObject *Ogre2Marker::OgreObject() const
{
switch (this->markerType)
{
case MT_NONE:
return nullptr;
case MT_BOX:
case MT_CAPSULE:
case MT_CONE:
case MT_CYLINDER:
case MT_SPHERE:
{
if (nullptr != this->dataPtr->geom)
{
return this->dataPtr->geom->OgreObject();
}
return nullptr;
}
case MT_LINE_STRIP:
case MT_LINE_LIST:
case MT_POINTS:
case MT_TRIANGLE_FAN:
case MT_TRIANGLE_LIST:
case MT_TRIANGLE_STRIP:
{
if (nullptr != this->dataPtr->dynamicRenderable)
{
return this->dataPtr->dynamicRenderable->OgreObject();
}
return nullptr;
}
default:
gzerr << "Invalid Marker type " << this->markerType << "\n";
return nullptr;
}
}
//////////////////////////////////////////////////
void Ogre2Marker::Init()
{
this->Create();
}
//////////////////////////////////////////////////
void Ogre2Marker::Create()
{
this->markerType = MT_NONE;
this->dataPtr->dynamicRenderable.reset(new Ogre2DynamicRenderable(
this->scene));
if (!this->dataPtr->geom)
{
this->dataPtr->geom =
std::dynamic_pointer_cast<Ogre2Geometry>(this->scene->CreateBox());
}
}
//////////////////////////////////////////////////
void Ogre2Marker::SetMaterial(MaterialPtr _material, bool _unique)
{
if (nullptr == _material)
{
gzerr << "Cannot assign null material" << std::endl;
return;
}
_material = (_unique) ? _material->Clone() : _material;
Ogre2MaterialPtr derived =
std::dynamic_pointer_cast<Ogre2Material>(_material);
if (!derived)
{
gzerr << "Cannot assign material created by another render-engine"
<< std::endl;
return;
}
std::string materialName = derived->Name();
derived->SetReceiveShadows(false);
derived->SetCastShadows(false);
derived->SetLightingEnabled(false);
switch (this->markerType)
{
case MT_NONE:
break;
case MT_BOX:
case MT_CAPSULE:
case MT_CONE:
case MT_CYLINDER:
case MT_SPHERE:
{
if (nullptr != this->dataPtr->geom)
{
this->dataPtr->geom->SetMaterial(derived, false);
}
else
{
gzerr << "Failed to set material, null geometry." << std::endl;
}
break;
}
case MT_LINE_STRIP:
case MT_LINE_LIST:
case MT_POINTS:
case MT_TRIANGLE_FAN:
case MT_TRIANGLE_LIST:
case MT_TRIANGLE_STRIP:
{
if (nullptr != this->dataPtr->dynamicRenderable)
{
this->dataPtr->dynamicRenderable->SetMaterial(derived, false);
}
else
{
gzerr << "Failed to set material, null renderable." << std::endl;
}
break;
}
default:
gzerr << "Invalid Marker type " << this->markerType << "\n";
break;
}
if (this->dataPtr->material && this->dataPtr->ownsMaterial)
this->Scene()->DestroyMaterial(this->dataPtr->material);
this->dataPtr->material = derived;
this->dataPtr->ownsMaterial = _unique;
}
//////////////////////////////////////////////////
MaterialPtr Ogre2Marker::Material() const
{
return this->dataPtr->material;
}
//////////////////////////////////////////////////
void Ogre2Marker::SetPoint(unsigned int _index,
const math::Vector3d &_value)
{
BaseMarker::SetPoint(_index, _value);
this->dataPtr->dynamicRenderable->SetPoint(_index, _value);
}
//////////////////////////////////////////////////
void Ogre2Marker::AddPoint(const math::Vector3d &_pt,
const math::Color &_color)
{
BaseMarker::AddPoint(_pt, _color);
this->dataPtr->dynamicRenderable->AddPoint(_pt, _color);
}
//////////////////////////////////////////////////
void Ogre2Marker::ClearPoints()
{
BaseMarker::ClearPoints();
this->dataPtr->dynamicRenderable->Clear();
}
//////////////////////////////////////////////////
void Ogre2Marker::SetType(MarkerType _markerType)
{
if (_markerType == this->markerType)
return;
this->markerType = _markerType;
auto visual = std::dynamic_pointer_cast<Ogre2Visual>(this->Parent());
// clear geom if needed
if (this->dataPtr->geom)
{
if (visual)
{
visual->RemoveGeometry(
std::dynamic_pointer_cast<Geometry>(shared_from_this()));
}
this->dataPtr->geom->Destroy();
}
bool isGeom{false};
GeometryPtr newGeom;
switch (_markerType)
{
case MT_NONE:
break;
case MT_BOX:
isGeom = true;
newGeom = this->scene->CreateBox();
break;
case MT_CAPSULE:
isGeom = true;
newGeom = this->scene->CreateCapsule();
break;
case MT_CONE:
isGeom = true;
newGeom = this->scene->CreateCone();
break;
case MT_CYLINDER:
isGeom = true;
newGeom = this->scene->CreateCylinder();
break;
case MT_SPHERE:
isGeom = true;
newGeom = this->scene->CreateSphere();
break;
case MT_LINE_STRIP:
case MT_LINE_LIST:
case MT_POINTS:
case MT_TRIANGLE_FAN:
case MT_TRIANGLE_LIST:
case MT_TRIANGLE_STRIP:
this->dataPtr->dynamicRenderable->SetOperationType(_markerType);
break;
default:
gzerr << "Invalid Marker type [" << _markerType << "]" << std::endl;
break;
}
if (nullptr != newGeom)
{
this->dataPtr->geom = std::dynamic_pointer_cast<Ogre2Geometry>(newGeom);
if (nullptr == this->dataPtr->geom)
{
gzerr << "Failed to cast to [Ogre2Geom], type [" << _markerType << "]"
<< std::endl;
}
else if (visual)
{
visual->AddGeometry(
std::dynamic_pointer_cast<Geometry>(shared_from_this()));
}
}
else if (isGeom)
{
gzerr << "Failed to create geometry for marker type [" << _markerType
<< "]" << std::endl;
}
}
//////////////////////////////////////////////////
MarkerType Ogre2Marker::Type() const
{
return this->markerType;
}