31 #include <osg/Geometry>
32 #include <osg/LightModel>
33 #include <osg/LineWidth>
34 #include <osg/Material>
35 #include <osg/MatrixTransform>
36 #include <osg/ShapeDrawable>
39 #include "../common/math/WMath.h"
40 #include "../common/WPathHelper.h"
41 #include "../common/WStringUtils.h"
43 #include "shaders/WGEShader.h"
44 #include "WGEGeodeUtils.h"
45 #include "WGEGeometryUtils.h"
46 #include "WGEGroupNode.h"
47 #include "WGESubdividedPlane.h"
49 #include "widgets/labeling/WGELabel.h"
56 WAssert( pos1[0] <= pos2[0] && pos1[1] <= pos2[1] && pos1[2] <= pos2[2],
"pos1 does not seem to be the frontLowerLeft corner of the BB!" );
58 ref_ptr< osg::Vec3Array > vertices = ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
59 ref_ptr< osg::Vec4Array > colors = ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
60 ref_ptr< osg::Geometry > geometry = ref_ptr< osg::Geometry >(
new osg::Geometry );
62 vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos1[2] ) );
63 vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos1[2] ) );
64 vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos1[2] ) );
65 vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos1[2] ) );
66 vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos1[2] ) );
67 vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos2[2] ) );
68 vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos2[2] ) );
69 vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos2[2] ) );
70 vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos2[2] ) );
71 vertices->push_back( osg::Vec3( pos1[0], pos1[1], pos2[2] ) );
73 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, vertices->size() ) );
75 vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos1[2] ) );
76 vertices->push_back( osg::Vec3( pos1[0], pos2[1], pos2[2] ) );
77 vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos1[2] ) );
78 vertices->push_back( osg::Vec3( pos2[0], pos2[1], pos2[2] ) );
79 vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos1[2] ) );
80 vertices->push_back( osg::Vec3( pos2[0], pos1[1], pos2[2] ) );
82 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINES, vertices->size() - 6, 6 ) );
84 geometry->setVertexArray( vertices );
85 colors->push_back( color );
86 geometry->setColorArray( colors );
87 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
88 osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >(
new osg::Geode );
89 geode->setName(
"Bounding Box Geode" );
90 geode->addDrawable( geometry );
93 osg::StateSet* state = geode->getOrCreateStateSet();
94 state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
103 osg::ref_ptr< osg::Vec3Array > vertices =
dynamic_cast< osg::Vec3Array*
>( geometry->getVertexArray() );
104 osg::ref_ptr< osg::Vec3Array > tex =
dynamic_cast< osg::Vec3Array*
>( geometry->getTexCoordArray( 0 ) );
105 osg::ref_ptr< osg::Vec3Array > normals =
dynamic_cast< osg::Vec3Array*
>( geometry->getNormalArray() );
106 osg::ref_ptr< osg::Vec4Array > colors =
dynamic_cast< osg::Vec4Array*
>( geometry->getColorArray() );
111 vertices = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
115 tex = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
119 normals = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
123 colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
126 size_t first = vertices->getNumElements();
132 vertices->push_back( position +
WPosition( 0.0, 0.0, 0.0 ) );
133 vertices->push_back( position +
WPosition( size.x(), 0.0, 0.0 ) );
134 vertices->push_back( position +
WPosition( size.x(), size.y(), 0.0 ) );
135 vertices->push_back( position +
WPosition( 0.0, size.y(), 0.0 ) );
136 tex->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
137 tex->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
138 tex->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
139 tex->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
140 normals->push_back( osg::Vec3( 0.0, 0.0, -1.0 ) );
141 normals->push_back( osg::Vec3( 0.0, 0.0, -1.0 ) );
142 normals->push_back( osg::Vec3( 0.0, 0.0, -1.0 ) );
143 normals->push_back( osg::Vec3( 0.0, 0.0, -1.0 ) );
146 vertices->push_back( position +
WPosition( 0.0, 0.0, size.z() ) );
147 vertices->push_back( position +
WPosition( size.x(), 0.0, size.z() ) );
148 vertices->push_back( position +
WPosition( size.x(), size.y(), size.z() ) );
149 vertices->push_back( position +
WPosition( 0.0, size.y(), size.z() ) );
150 tex->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
151 tex->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
152 tex->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
153 tex->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
154 normals->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
155 normals->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
156 normals->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
157 normals->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
160 vertices->push_back( position +
WPosition( 0.0, 0.0, 0.0 ) );
161 vertices->push_back( position +
WPosition( 0.0, size.y(), 0.0 ) );
162 vertices->push_back( position +
WPosition( 0.0, size.y(), size.z() ) );
163 vertices->push_back( position +
WPosition( 0.0, 0.0, size.z() ) );
164 tex->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
165 tex->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
166 tex->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
167 tex->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
168 normals->push_back( osg::Vec3( -1.0, 0.0, 0.0 ) );
169 normals->push_back( osg::Vec3( -1.0, 0.0, 0.0 ) );
170 normals->push_back( osg::Vec3( -1.0, 0.0, 0.0 ) );
171 normals->push_back( osg::Vec3( -1.0, 0.0, 0.0 ) );
174 vertices->push_back( position +
WPosition( size.x(), 0.0, 0.0 ) );
175 vertices->push_back( position +
WPosition( size.x(), size.y(), 0.0 ) );
176 vertices->push_back( position +
WPosition( size.x(), size.y(), size.z() ) );
177 vertices->push_back( position +
WPosition( size.x(), 0.0, size.z() ) );
178 tex->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
179 tex->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
180 tex->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
181 tex->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
182 normals->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
183 normals->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
184 normals->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
185 normals->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
188 vertices->push_back( position +
WPosition( 0.0, 0.0, 0.0 ) );
189 vertices->push_back( position +
WPosition( size.x(), 0.0, 0.0 ) );
190 vertices->push_back( position +
WPosition( size.x(), 0.0, size.z() ) );
191 vertices->push_back( position +
WPosition( 0.0, 0.0, size.z() ) );
192 tex->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
193 tex->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
194 tex->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
195 tex->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
196 normals->push_back( osg::Vec3( 0.0, -1.0, 0.0 ) );
197 normals->push_back( osg::Vec3( 0.0, -1.0, 0.0 ) );
198 normals->push_back( osg::Vec3( 0.0, -1.0, 0.0 ) );
199 normals->push_back( osg::Vec3( 0.0, -1.0, 0.0 ) );
202 vertices->push_back( position +
WPosition( 0.0, size.y(), 0.0 ) );
203 vertices->push_back( position +
WPosition( size.x(), size.y(), 0.0 ) );
204 vertices->push_back( position +
WPosition( size.x(), size.y(), size.z() ) );
205 vertices->push_back( position +
WPosition( 0.0, size.y(), size.z() ) );
206 tex->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
207 tex->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
208 tex->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
209 tex->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
210 normals->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
211 normals->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
212 normals->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
213 normals->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
216 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, first, 6 * 4 ) );
217 geometry->setVertexArray( vertices );
220 geometry->setTexCoordArray( 0, tex );
223 geometry->setNormalArray( normals );
224 geometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
227 colors->push_back( color );
228 geometry->setColorArray( colors );
229 geometry->setColorBinding( osg::Geometry::BIND_PER_PRIMITIVE_SET );
235 osg::ref_ptr< osg::Geometry > cube =
new osg::Geometry();
249 osg::ref_ptr< osg::Geometry > cube =
new osg::Geometry();
250 osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
251 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
255 vertices->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
256 vertices->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
257 vertices->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
258 vertices->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
259 vertices->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
260 vertices->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
261 vertices->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
262 vertices->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
263 vertices->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
264 vertices->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
266 cube->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, vertices->size() ) );
268 vertices->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
269 vertices->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
270 vertices->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
271 vertices->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
272 vertices->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
273 vertices->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
275 cube->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINES, vertices->size() - 6, 6 ) );
279 vertices->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
280 vertices->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
282 vertices->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
283 vertices->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
285 vertices->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
286 vertices->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
288 vertices->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
289 vertices->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
291 vertices->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
292 vertices->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
294 vertices->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
295 vertices->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
297 vertices->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
298 vertices->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
300 vertices->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
301 vertices->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
303 vertices->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
304 vertices->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
306 vertices->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
307 vertices->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
309 vertices->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
310 vertices->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
312 vertices->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
313 vertices->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
315 cube->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINES, 0, vertices->size() ) );
319 cube->setVertexArray( vertices );
322 cube->setTexCoordArray( 0, vertices );
325 colors->push_back( color );
326 cube->setColorArray( colors );
327 cube->setColorBinding( osg::Geometry::BIND_OVERALL );
334 WAssert( bb.valid(),
"Invalid bounding box!" );
337 osg::ref_ptr< osg::Geode > cube =
new osg::Geode();
338 cube->setName(
"Solid Bounding Box" );
339 if( threeDTexCoords )
345 osg::ref_ptr< osg::ShapeDrawable > cubeDrawable =
new osg::ShapeDrawable(
new osg::Box( osg::Vec3( 0.5, 0.5, 0.5 ), 1.0 ) );
346 cubeDrawable->setColor( color );
347 cube->addDrawable( cubeDrawable );
351 osg::Matrixd transformM;
353 transformM.makeTranslate( bb.
getMin() );
357 osg::ref_ptr< osg::MatrixTransform > transform =
new osg::MatrixTransform();
358 transform->setMatrix( scaleM * transformM );
359 transform->addChild( cube );
362 osg::StateSet* state = cube->getOrCreateStateSet();
363 state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
374 osg::ref_ptr< osg::Geometry> geometry(
new osg::Geometry );
375 geometry->setVertexArray( mesh->getVertexArray() );
377 osg::DrawElementsUInt* surfaceElement;
379 surfaceElement =
new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
381 std::vector< size_t > tris = mesh->getTriangles();
382 surfaceElement->reserve( tris.size() );
384 for(
unsigned int vertId = 0; vertId < tris.size(); ++vertId )
386 surfaceElement->push_back( tris[vertId] );
388 geometry->addPrimitiveSet( surfaceElement );
391 if( mesh->getVertexColorArray() && useMeshColor )
393 geometry->setColorArray( mesh->getVertexColorArray() );
394 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
398 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
400 geometry->setColorArray( colors );
401 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
408 geometry->setNormalArray( mesh->getVertexNormalArray() );
409 geometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
414 osg::StateSet* state = geometry->getOrCreateStateSet();
415 osg::ref_ptr<osg::LightModel> lightModel =
new osg::LightModel();
416 lightModel->setTwoSided(
true );
417 state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
418 state->setMode( GL_BLEND, osg::StateAttribute::ON );
420 osg::ref_ptr< osg::Material > material =
new osg::Material();
421 material->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
422 material->setSpecular( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
423 material->setAmbient( osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
424 material->setEmission( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
425 material->setShininess( osg::Material::FRONT, 25.0 );
426 state->setAttribute( material );
432 geometry->setUseDisplayList(
false );
433 geometry->setUseVertexBufferObjects(
true );
444 osg::DrawElementsUInt* surfaceElement;
445 surfaceElement =
new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
446 std::vector< size_t > tris = mesh->getTriangles();
448 osg::ref_ptr< osg::Geometry> geometry(
new osg::Geometry );
449 osg::ref_ptr< osg::Vec3Array > oldVertexArray( mesh->getVertexArray() );
450 osg::ref_ptr< osg::Vec3Array > newVertexArray(
new osg::Vec3Array );
451 osg::ref_ptr< osg::Vec4Array > oldVertexColorArray( mesh->getVertexColorArray() );
452 osg::ref_ptr< osg::Vec4Array > newVertexColorArray(
new osg::Vec4Array );
455 for(
size_t index = 0; index < tris.size(); ++index )
457 newVertexArray->push_back( (*oldVertexArray)[tris[index]] );
458 surfaceElement->push_back( index );
459 newVertexColorArray->push_back( (*oldVertexColorArray)[tris[index]] );
461 geometry->setVertexArray( newVertexArray );
462 geometry->addPrimitiveSet( surfaceElement );
465 if( mesh->getVertexColorArray() && useMeshColor )
467 geometry->setColorArray( newVertexColorArray );
468 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
472 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
474 geometry->setColorArray( colors );
475 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
484 osg::ref_ptr< osg::Vec3Array > oldTriNormals = mesh->getTriangleNormalArray(
true );
485 osg::ref_ptr< osg::Vec3Array > newVertNormals(
new osg::Vec3Array );
486 for(
size_t index = 0; index < tris.size(); ++index )
488 newVertNormals->push_back( (*oldTriNormals)[index/3] );
490 geometry->setNormalArray( newVertNormals );
491 geometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
496 osg::StateSet* state = geometry->getOrCreateStateSet();
497 osg::ref_ptr<osg::LightModel> lightModel =
new osg::LightModel();
498 lightModel->setTwoSided(
true );
499 state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
500 state->setMode( GL_BLEND, osg::StateAttribute::ON );
502 osg::ref_ptr< osg::Material > material =
new osg::Material();
503 material->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
504 material->setSpecular( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
505 material->setAmbient( osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
506 material->setEmission( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
507 material->setShininess( osg::Material::FRONT, 25.0 );
508 state->setAttribute( material );
514 geometry->setUseDisplayList(
false );
515 geometry->setUseVertexBufferObjects(
true );
523 bool includeNormals,
bool lighting )
529 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
530 for(
size_t i = 0; i < mesh->vertSize(); ++i )
534 for( std::map< size_t, WColor >::const_iterator vc = colorMap.
getData().begin(); vc != colorMap.
getData().end(); ++vc )
537 if( vc->first < colors->size() )
539 colors->at( vc->first ) = vc->second;
543 geometry->setColorArray( colors );
544 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
553 osg::ref_ptr< osg::Geometry > geometry(
new osg::Geometry );
554 geometry->setVertexArray( mesh->getVertexArray() );
556 osg::DrawElementsUInt* meshElement;
558 meshElement =
new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, 0 );
560 std::vector< size_t > tris = mesh->getTriangles();
561 meshElement->reserve( tris.size() * 3 );
563 for(
unsigned int triId = 0; triId < tris.size() / 3.; ++triId )
565 for(
size_t edgeId = 0; edgeId < 3; ++edgeId )
567 meshElement->push_back( tris[triId*3 + edgeId] );
568 meshElement->push_back( tris[triId*3 + ( edgeId + 1 ) % 3] );
571 geometry->addPrimitiveSet( meshElement );
574 if( mesh->getVertexColorArray() && useMeshColor )
576 geometry->setColorArray( mesh->getVertexColorArray() );
577 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
581 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
583 geometry->setColorArray( colors );
584 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
587 osg::StateSet* stateset = geometry->getOrCreateStateSet();
588 stateset->setAttributeAndModes(
new osg::LineWidth( 1 ), osg::StateAttribute::ON );
589 stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
590 stateset->setMode( GL_BLEND, osg::StateAttribute::ON );
595 osg::ref_ptr< osg::PositionAttitudeTransform >
wge::addLabel( osg::Vec3 position, std::string text )
597 osg::ref_ptr< osgText::Text > label = osg::ref_ptr< osgText::Text >(
new osgText::Text() );
598 osg::ref_ptr< osg::Geode > labelGeode = osg::ref_ptr< osg::Geode >(
new osg::Geode() );
600 labelGeode->addDrawable( label );
604 label->setBackdropType( osgText::Text::OUTLINE );
605 label->setCharacterSize( 6 );
607 label->setText( text );
608 label->setAxisAlignment( osgText::Text::SCREEN );
609 label->setDrawMode( osgText::Text::TEXT );
610 label->setAlignment( osgText::Text::CENTER_TOP );
611 label->setPosition( osg::Vec3( 0.0, 0.0, 0.0 ) );
612 label->setColor( osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
614 osg::ref_ptr< osg::PositionAttitudeTransform > labelXform =
615 osg::ref_ptr< osg::PositionAttitudeTransform >(
new osg::PositionAttitudeTransform() );
616 labelXform->setPosition( position );
618 labelXform->addChild( labelGeode );
627 return (
addLabel( position, label ) );
633 ref_ptr< osg::Vec3Array > vertices = ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
634 ref_ptr< osg::Vec4Array > colors = ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
635 ref_ptr< osg::Geometry > geometry = ref_ptr< osg::Geometry >(
new osg::Geometry );
637 colors->push_back( color );
644 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
645 geometry->setVertexArray( vertices );
646 geometry->setColorArray( colors );
647 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
649 osg::StateSet* stateset =
new osg::StateSet;
650 stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
651 geometry->setStateSet( stateset );
653 osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >(
new osg::Geode );
654 geode->setName(
"Finite Plane Geode" );
655 geode->addDrawable( geometry );
659 vertices->push_back( vertices->front() );
660 ref_ptr< osg::Geometry > borderGeom = ref_ptr< osg::Geometry >(
new osg::Geometry );
661 borderGeom->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, 4 ) );
662 borderGeom->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 3, 2 ) );
663 ref_ptr< osg::Vec4Array > colors = ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
664 colors->push_back( inverseColor( color ) );
665 borderGeom->setColorArray( colors );
666 borderGeom->setColorBinding( osg::Geometry::BIND_OVERALL );
667 borderGeom->setVertexArray( vertices );
668 geode->addDrawable( borderGeom );
673 osg::ref_ptr< osg::Geode >
wge::genFinitePlane( osg::Vec3
const& base, osg::Vec3
const& a, osg::Vec3
const& b,
const WColor& color )
676 osg::ref_ptr< osg::Vec3Array > vertices =
new osg::Vec3Array;
677 osg::ref_ptr< osg::Vec3Array > texcoords0 =
new osg::Vec3Array;
678 osg::ref_ptr< osg::Vec3Array > normals =
new osg::Vec3Array;
679 osg::ref_ptr< osg::Vec4Array > colors =
new osg::Vec4Array;
681 osg::Vec3 aPlusB = a + b;
683 vertices->push_back( base );
684 vertices->push_back( base + a );
685 vertices->push_back( base + aPlusB );
686 vertices->push_back( base + b );
688 osg::Vec3 aCrossB = a ^ b;
695 normals->push_back( aCrossB );
696 normals->push_back( aCrossB );
697 normals->push_back( aCrossB );
698 normals->push_back( aCrossB );
699 colors->push_back( color );
700 colors->push_back( color );
701 colors->push_back( color );
702 colors->push_back( color );
703 texcoords0->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
704 texcoords0->push_back( aNorm );
705 texcoords0->push_back( aNorm + bNorm );
706 texcoords0->push_back( bNorm );
709 osg::ref_ptr< osg::Geometry > geometry =
new osg::Geometry();
710 geometry->setVertexArray( vertices );
711 geometry->setTexCoordArray( 0, texcoords0 );
712 geometry->setNormalArray( normals );
713 geometry->setColorArray( colors );
714 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
716 osg::ref_ptr< osg::Geode > geode =
new osg::Geode();
717 geode->setName(
"Finite Plane" );
718 geode->addDrawable( geometry );
729 osg::ref_ptr< WGEGroupNode >groupNode =
new WGEGroupNode();
730 osg::ref_ptr< WGEShader > shaderCoordinateSystem(
new WGEShader(
"WGECoordinateSystem" ) );
732 osg::ref_ptr< osg::Geode > graphX(
new osg::Geode );
733 osg::ref_ptr< osg::Geode > graphY(
new osg::Geode );
734 osg::ref_ptr< osg::Geode > graphZ(
new osg::Geode );
736 osg::ref_ptr< osg::Geode > graphXCylinder(
new osg::Geode );
737 osg::ref_ptr< osg::Geode > graphYCylinder(
new osg::Geode );
738 osg::ref_ptr< osg::Geode > graphZCylinder(
new osg::Geode );
741 osg::ref_ptr< osg::ShapeDrawable > cylinderX =
new osg::ShapeDrawable(
new osg::Cylinder(
742 middle, 1, sizeX + ( sizeX * 0.5 )
744 osg::ref_ptr< osg::ShapeDrawable > cylinderXEnd =
new osg::ShapeDrawable(
new osg::Cylinder(
745 osg::Vec3( middle.x(), middle.y(), middle.z() - ( sizeX + ( sizeX * 0.5 ) ) / 2.0 ), 1.0, 1.0
747 osg::ref_ptr< osg::ShapeDrawable > coneX =
new osg::ShapeDrawable(
new osg::Cone(
748 osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeX + ( sizeX * 0.5 ) ) / 2.0 ), 2.0, 5.0
750 cylinderXEnd->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
751 graphXCylinder->addDrawable( cylinderX );
752 graphX->addDrawable( coneX );
753 graphX->addDrawable( cylinderXEnd );
755 osg::ref_ptr< osg::Material > matX =
new osg::Material();
756 matX->setDiffuse( osg::Material::FRONT, WColor( 1.0, 0.0, 0.0, 1.0 ) );
757 cylinderX->getOrCreateStateSet()->setAttribute( matX, osg::StateAttribute::ON );
758 coneX->getOrCreateStateSet()->setAttribute( matX, osg::StateAttribute::ON );
761 osg::ref_ptr< osg::ShapeDrawable > cylinderY =
new osg::ShapeDrawable(
new osg::Cylinder(
762 middle, 1, sizeY + ( sizeY * 0.5 )
764 osg::ref_ptr< osg::ShapeDrawable > cylinderYEnd =
new osg::ShapeDrawable(
new osg::Cylinder(
765 osg::Vec3( middle.x(), middle.y(), middle.z() - ( sizeY + ( sizeY * 0.5 ) ) / 2.0 ), 1.0, 1.0
767 osg::ref_ptr< osg::ShapeDrawable > coneY =
new osg::ShapeDrawable(
new osg::Cone(
768 osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeY + ( sizeY * 0.5 ) ) / 2.0 ), 2.0, 5.0
770 cylinderYEnd->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
772 graphYCylinder->addDrawable( cylinderY );
773 graphY->addDrawable( coneY );
774 graphY->addDrawable( cylinderYEnd );
776 osg::ref_ptr< osg::Material > matY =
new osg::Material();
777 matY->setDiffuse( osg::Material::FRONT, WColor( 0.0, 1.0, 0.0, 1.0 ) );
778 cylinderY->getOrCreateStateSet()->setAttribute( matY, osg::StateAttribute::ON );
779 coneY->getOrCreateStateSet()->setAttribute( matY, osg::StateAttribute::ON );
783 osg::ref_ptr< osg::ShapeDrawable > cylinderZ =
new osg::ShapeDrawable(
new osg::Cylinder(
784 middle, 1, sizeZ + ( sizeZ * 0.5 )
786 osg::ref_ptr< osg::ShapeDrawable > cylinderZEnd =
new osg::ShapeDrawable(
new osg::Cylinder(
787 osg::Vec3( middle.x(), middle.y(), middle.z() - ( sizeZ + ( sizeZ * 0.5 ) ) / 2.0 ), 1.0, 1.0
789 osg::ref_ptr< osg::ShapeDrawable > coneZ =
new osg::ShapeDrawable(
new osg::Cone(
790 osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeZ + ( sizeZ * 0.5 ) ) / 2.0 ), 2.0, 5.0
792 cylinderZEnd->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
794 graphZCylinder->addDrawable( cylinderZ );
795 graphZ->addDrawable( coneZ );
796 graphZ->addDrawable( cylinderZEnd );
798 osg::ref_ptr< osg::Material > matZ =
new osg::Material();
799 matZ->setDiffuse( osg::Material::FRONT, WColor( 0.0, 0.0, 1.0, 1.0 ) );
800 cylinderZ->getOrCreateStateSet()->setAttribute( matZ, osg::StateAttribute::ON );
801 coneZ->getOrCreateStateSet()->setAttribute( matZ, osg::StateAttribute::ON );
803 shaderCoordinateSystem->apply( graphXCylinder );
804 shaderCoordinateSystem->apply( graphYCylinder );
805 shaderCoordinateSystem->apply( graphZCylinder );
807 osg::ref_ptr< WGELabel > graphXLabel =
new WGELabel();
808 graphXLabel->setText(
"X" );
809 graphXLabel->setCharacterSize( 10 );
810 graphXLabel->setPosition( osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeX + ( sizeX * 0.5 ) ) / 2.0 + 5.0 ) );
811 graphXLabel->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
812 graphX->addDrawable( graphXLabel );
814 osg::ref_ptr< WGELabel > graphYLabel =
new WGELabel();
815 graphYLabel->setText(
"Y" );
816 graphYLabel->setCharacterSize( 10 );
817 graphYLabel->setPosition( osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeY + ( sizeY * 0.5 ) ) / 2.0 + 5.0 ) );
818 graphYLabel->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
819 graphY->addDrawable( graphYLabel );
821 osg::ref_ptr< WGELabel > graphZLabel =
new WGELabel();
822 graphZLabel->setText(
"Z" );
823 graphZLabel->setCharacterSize( 10 );
824 graphZLabel->setPosition( osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeZ + ( sizeZ * 0.5 ) ) / 2.0 + 5.0 ) );
825 graphZLabel->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
826 graphZ->addDrawable( graphZLabel );
829 osg::ref_ptr< osg::MatrixTransform > graphXTransform =
new osg::MatrixTransform();
830 graphXTransform->addChild( graphX );
831 graphXTransform->addChild( graphXCylinder );
832 osg::ref_ptr< osg::MatrixTransform > graphYTransform =
new osg::MatrixTransform();
833 graphYTransform->addChild( graphY );
834 graphYTransform->addChild( graphYCylinder );
835 osg::ref_ptr< osg::MatrixTransform > graphZTransform =
new osg::MatrixTransform();
836 graphZTransform->addChild( graphZ );
837 graphZTransform->addChild( graphZCylinder );
839 osg::Matrixd matrixTranslateTo0 = osg::Matrixd::translate( -middle.x(), -middle.y(), -middle.z() );
840 osg::Matrixd matrixTranslateFrom0 = osg::Matrixd::translate( middle.x(), middle.y(), middle.z() );
842 graphXTransform->setMatrix( matrixTranslateTo0 * osg::Matrixd::rotate(
844 osg::Vec3f( 0.0, 1.0, 0.0 ) ) * matrixTranslateFrom0
846 graphYTransform->setMatrix( matrixTranslateTo0 * osg::Matrixd::rotate(
847 -90.0 * pi() / 180.0,
848 osg::Vec3f( 1.0, 0.0, 0.0 ) ) * matrixTranslateFrom0
851 groupNode->insert( graphXTransform );
852 groupNode->insert( graphYTransform );
853 groupNode->insert( graphZTransform );
const vec_type & getMax() const
Gives the back upper right aka maximum corner.
const vec_type & getMin() const
Gives the front lower left aka minimum corner.
osg::BoundingBoxImpl< osg::Vec3 >::vec_type vec_type
Vertex type for min and max positions of this box.
Represents a std::map where for each vertex ID a color is stored.
const std::map< size_t, WColor > & getData() const
Reference to the data.
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader.
static Fonts getAllFonts()
The paths to all fonts supported.
Represents a plane with a normal vector and a position in space.
WPosition getPointInPlane(double x, double y) const
Computes with relative coordinates a point in this plane.
This only is a 3d double vector.
std::shared_ptr< WTriangleMesh > SPtr
Shared pointer.
std::string toString(const T &value)
Convert a given value to a string.
osg::ref_ptr< osg::Geometry > convertToOsgGeometryLines(WTriangleMesh::SPtr mesh, const WColor &defaultColor=WColor(1.0, 1.0, 1.0, 1.0), bool useMeshColor=true)
Convert triangle mesh to lines representing it.
osg::ref_ptr< osg::Geode > generateBoundingBoxGeode(const WBoundingBox &bb, const WColor &color)
Generates an OSG geode for the bounding box.
osg::ref_ptr< osg::PositionAttitudeTransform > vector2label(osg::Vec3 position)
helper function to add a label with it's position vector
osg::ref_ptr< osg::Geometry > convertToOsgGeometry(WTriangleMesh::SPtr mesh, const WColor &defaultColor=WColor(1.0, 1.0, 1.0, 1.0), bool includeNormals=false, bool lighting=false, bool useMeshColor=true)
Extract the vertices and triangles from a WTriangleMesh and save them into an osg::Geometry.
osg::ref_ptr< osg::Geometry > createUnitCube(const WColor &color)
Creates a osg::Geometry containing an unit cube, having 3D texture coordinates.
osg::ref_ptr< osg::PositionAttitudeTransform > addLabel(osg::Vec3 position, std::string text)
helper function to add a label somewhere
osg::ref_ptr< osg::Group > creatCoordinateSystem(osg::Vec3 middle, double sizeX, double sizeY, double sizeZ)
Create a coordinate system.
osg::ref_ptr< osg::Geometry > convertToOsgGeometryFlatShaded(WTriangleMesh::SPtr mesh, const WColor &defaultColor=WColor(1.0, 1.0, 1.0, 1.0), bool includeNormals=false, bool lighting=false, bool useMeshColor=true)
Extract the vertices and triangles from a WTriangleMesh and save them into an osg::Geometry in order ...
osg::ref_ptr< osg::Node > generateSolidBoundingBoxNode(const WBoundingBox &bb, const WColor &color, bool threeDTexCoords=true)
Generates an OSG node for the specified bounding box.
osg::ref_ptr< osg::Geode > genFinitePlane(double xSize, double ySize, const WPlane &p, const WColor &color=WColor(0.0, 0.7, 0.7, 1.0), bool border=false)
Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.
void createCube(osg::ref_ptr< osg::Geometry > geometry, const WPosition &position, const WPosition &size, const WColor &color)
Create an arbitrary cube and insert it into the given geometry.
osg::ref_ptr< osg::Geometry > createUnitCubeAsLines(const WColor &color, bool asLines=false)
Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates.