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->addDrawable( geometry );
92 osg::StateSet* state = geode->getOrCreateStateSet();
93 state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
102 osg::ref_ptr< osg::Vec3Array > vertices =
dynamic_cast< osg::Vec3Array*
>( geometry->getVertexArray() );
103 osg::ref_ptr< osg::Vec3Array > tex =
dynamic_cast< osg::Vec3Array*
>( geometry->getTexCoordArray( 0 ) );
104 osg::ref_ptr< osg::Vec3Array > normals =
dynamic_cast< osg::Vec3Array*
>( geometry->getNormalArray() );
105 osg::ref_ptr< osg::Vec4Array > colors =
dynamic_cast< osg::Vec4Array*
>( geometry->getColorArray() );
110 vertices = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
114 tex = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
118 normals = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
122 colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
125 size_t first = vertices->getNumElements();
131 vertices->push_back( position +
WPosition( 0.0, 0.0, 0.0 ) );
132 vertices->push_back( position +
WPosition( size.x(), 0.0, 0.0 ) );
133 vertices->push_back( position +
WPosition( size.x(), size.y(), 0.0 ) );
134 vertices->push_back( position +
WPosition( 0.0, size.y(), 0.0 ) );
135 tex->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
136 tex->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
137 tex->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
138 tex->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
139 normals->push_back( osg::Vec3( 0.0, 0.0, -1.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 ) );
145 vertices->push_back( position +
WPosition( 0.0, 0.0, size.z() ) );
146 vertices->push_back( position +
WPosition( size.x(), 0.0, size.z() ) );
147 vertices->push_back( position +
WPosition( size.x(), size.y(), size.z() ) );
148 vertices->push_back( position +
WPosition( 0.0, size.y(), size.z() ) );
149 tex->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
150 tex->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
151 tex->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
152 tex->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
153 normals->push_back( osg::Vec3( 0.0, 0.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 ) );
159 vertices->push_back( position +
WPosition( 0.0, 0.0, 0.0 ) );
160 vertices->push_back( position +
WPosition( 0.0, size.y(), 0.0 ) );
161 vertices->push_back( position +
WPosition( 0.0, size.y(), size.z() ) );
162 vertices->push_back( position +
WPosition( 0.0, 0.0, size.z() ) );
163 tex->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
164 tex->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
165 tex->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
166 tex->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
167 normals->push_back( osg::Vec3( -1.0, 0.0, 0.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 ) );
173 vertices->push_back( position +
WPosition( size.x(), 0.0, 0.0 ) );
174 vertices->push_back( position +
WPosition( size.x(), size.y(), 0.0 ) );
175 vertices->push_back( position +
WPosition( size.x(), size.y(), size.z() ) );
176 vertices->push_back( position +
WPosition( size.x(), 0.0, size.z() ) );
177 tex->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
178 tex->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
179 tex->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
180 tex->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
181 normals->push_back( osg::Vec3( 1.0, 0.0, 0.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 ) );
187 vertices->push_back( position +
WPosition( 0.0, 0.0, 0.0 ) );
188 vertices->push_back( position +
WPosition( size.x(), 0.0, 0.0 ) );
189 vertices->push_back( position +
WPosition( size.x(), 0.0, size.z() ) );
190 vertices->push_back( position +
WPosition( 0.0, 0.0, size.z() ) );
191 tex->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
192 tex->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
193 tex->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
194 tex->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
195 normals->push_back( osg::Vec3( 0.0, -1.0, 0.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 ) );
201 vertices->push_back( position +
WPosition( 0.0, size.y(), 0.0 ) );
202 vertices->push_back( position +
WPosition( size.x(), size.y(), 0.0 ) );
203 vertices->push_back( position +
WPosition( size.x(), size.y(), size.z() ) );
204 vertices->push_back( position +
WPosition( 0.0, size.y(), size.z() ) );
205 tex->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
206 tex->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
207 tex->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
208 tex->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
209 normals->push_back( osg::Vec3( 0.0, 1.0, 0.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 ) );
215 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, first, 6 * 4 ) );
216 geometry->setVertexArray( vertices );
219 geometry->setTexCoordArray( 0, tex );
222 geometry->setNormalArray( normals );
223 geometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
226 colors->push_back( color );
227 geometry->setColorArray( colors );
228 geometry->setColorBinding( osg::Geometry::BIND_PER_PRIMITIVE_SET );
234 osg::ref_ptr< osg::Geometry > cube =
new osg::Geometry();
248 osg::ref_ptr< osg::Geometry > cube =
new osg::Geometry();
249 osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
250 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
252 vertices->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
253 vertices->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
254 vertices->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
255 vertices->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
256 vertices->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
257 vertices->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
258 vertices->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
259 vertices->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
260 vertices->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
261 vertices->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
263 cube->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, vertices->size() ) );
265 vertices->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
266 vertices->push_back( osg::Vec3( 0.0, 1.0, 1.0 ) );
267 vertices->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
268 vertices->push_back( osg::Vec3( 1.0, 0.0, 1.0 ) );
269 vertices->push_back( osg::Vec3( 1.0, 1.0, 0.0 ) );
270 vertices->push_back( osg::Vec3( 1.0, 1.0, 1.0 ) );
272 cube->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINES, vertices->size() - 6, 6 ) );
275 cube->setVertexArray( vertices );
278 cube->setTexCoordArray( 0, vertices );
281 colors->push_back( color );
282 cube->setColorArray( colors );
283 cube->setColorBinding( osg::Geometry::BIND_OVERALL );
290 WAssert( bb.valid(),
"Invalid bounding box!" );
293 osg::ref_ptr< osg::Geode > cube =
new osg::Geode();
294 cube->setName(
"Solid Bounding Box" );
295 if( threeDTexCoords )
301 osg::ref_ptr< osg::ShapeDrawable > cubeDrawable =
new osg::ShapeDrawable(
new osg::Box( osg::Vec3( 0.5, 0.5, 0.5 ), 1.0 ) );
302 cubeDrawable->setColor( color );
303 cube->addDrawable( cubeDrawable );
307 osg::Matrixd transformM;
309 transformM.makeTranslate( bb.
getMin() );
313 osg::ref_ptr< osg::MatrixTransform > transform =
new osg::MatrixTransform();
314 transform->setMatrix( scaleM * transformM );
315 transform->addChild( cube );
318 osg::StateSet* state = cube->getOrCreateStateSet();
319 state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
330 osg::ref_ptr< osg::Geometry> geometry(
new osg::Geometry );
331 geometry->setVertexArray( mesh->getVertexArray() );
333 osg::DrawElementsUInt* surfaceElement;
335 surfaceElement =
new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
337 std::vector< size_t > tris = mesh->getTriangles();
338 surfaceElement->reserve( tris.size() );
340 for(
unsigned int vertId = 0; vertId < tris.size(); ++vertId )
342 surfaceElement->push_back( tris[vertId] );
344 geometry->addPrimitiveSet( surfaceElement );
347 if( mesh->getVertexColorArray() && useMeshColor )
349 geometry->setColorArray( mesh->getVertexColorArray() );
350 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
354 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
355 colors->push_back( defaultColor );
356 geometry->setColorArray( colors );
357 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
364 geometry->setNormalArray( mesh->getVertexNormalArray() );
365 geometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
370 osg::StateSet* state = geometry->getOrCreateStateSet();
371 osg::ref_ptr<osg::LightModel> lightModel =
new osg::LightModel();
372 lightModel->setTwoSided(
true );
373 state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
374 state->setMode( GL_BLEND, osg::StateAttribute::ON );
376 osg::ref_ptr< osg::Material > material =
new osg::Material();
377 material->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
378 material->setSpecular( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
379 material->setAmbient( osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
380 material->setEmission( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
381 material->setShininess( osg::Material::FRONT, 25.0 );
382 state->setAttribute( material );
388 geometry->setUseDisplayList(
false );
389 geometry->setUseVertexBufferObjects(
true );
400 osg::DrawElementsUInt* surfaceElement;
401 surfaceElement =
new osg::DrawElementsUInt( osg::PrimitiveSet::TRIANGLES, 0 );
402 std::vector< size_t > tris = mesh->getTriangles();
404 osg::ref_ptr< osg::Geometry> geometry(
new osg::Geometry );
405 osg::ref_ptr< osg::Vec3Array > oldVertexArray( mesh->getVertexArray() );
406 osg::ref_ptr< osg::Vec3Array > newVertexArray(
new osg::Vec3Array );
409 for(
size_t index = 0; index < tris.size(); ++index )
411 newVertexArray->push_back( (*oldVertexArray)[tris[index]] );
412 surfaceElement->push_back( index );
414 geometry->setVertexArray( newVertexArray );
415 geometry->addPrimitiveSet( surfaceElement );
418 if( mesh->getVertexColorArray() && useMeshColor )
420 geometry->setColorArray( mesh->getVertexColorArray() );
421 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
425 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
426 colors->push_back( defaultColor );
427 geometry->setColorArray( colors );
428 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
437 osg::ref_ptr< osg::Vec3Array > oldTriNormals = mesh->getTriangleNormalArray(
true );
438 osg::ref_ptr< osg::Vec3Array > newVertNormals(
new osg::Vec3Array );
439 for(
size_t index = 0; index < tris.size(); ++index )
441 newVertNormals->push_back( (*oldTriNormals)[index/3] );
443 geometry->setNormalArray( newVertNormals );
444 geometry->setNormalBinding( osg::Geometry::BIND_PER_VERTEX );
449 osg::StateSet* state = geometry->getOrCreateStateSet();
450 osg::ref_ptr<osg::LightModel> lightModel =
new osg::LightModel();
451 lightModel->setTwoSided(
true );
452 state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
453 state->setMode( GL_BLEND, osg::StateAttribute::ON );
455 osg::ref_ptr< osg::Material > material =
new osg::Material();
456 material->setDiffuse( osg::Material::FRONT, osg::Vec4( 1.0, 1.0, 1.0, 1.0 ) );
457 material->setSpecular( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
458 material->setAmbient( osg::Material::FRONT, osg::Vec4( 0.1, 0.1, 0.1, 1.0 ) );
459 material->setEmission( osg::Material::FRONT, osg::Vec4( 0.0, 0.0, 0.0, 1.0 ) );
460 material->setShininess( osg::Material::FRONT, 25.0 );
461 state->setAttribute( material );
467 geometry->setUseDisplayList(
false );
468 geometry->setUseVertexBufferObjects(
true );
476 bool includeNormals,
bool lighting )
478 osg::ref_ptr< osg::Geometry > geometry =
convertToOsgGeometry( mesh, defaultColor, includeNormals, lighting,
false );
482 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
483 for(
size_t i = 0; i < mesh->vertSize(); ++i )
485 colors->push_back( defaultColor );
487 for( std::map< size_t, WColor >::const_iterator vc = colorMap.
getData().begin(); vc != colorMap.
getData().end(); ++vc )
490 if( vc->first < colors->size() )
492 colors->at( vc->first ) = vc->second;
496 geometry->setColorArray( colors );
497 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
506 osg::ref_ptr< osg::Geometry > geometry(
new osg::Geometry );
507 geometry->setVertexArray( mesh->getVertexArray() );
509 osg::DrawElementsUInt* meshElement;
511 meshElement =
new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, 0 );
513 std::vector< size_t > tris = mesh->getTriangles();
514 meshElement->reserve( tris.size() * 3 );
516 for(
unsigned int triId = 0; triId < tris.size() / 3.; ++triId )
518 for(
size_t edgeId = 0; edgeId < 3; ++edgeId )
520 meshElement->push_back( tris[triId*3 + edgeId] );
521 meshElement->push_back( tris[triId*3 + ( edgeId + 1 ) % 3] );
524 geometry->addPrimitiveSet( meshElement );
527 if( mesh->getVertexColorArray() && useMeshColor )
529 geometry->setColorArray( mesh->getVertexColorArray() );
530 geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
534 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
535 colors->push_back( defaultColor );
536 geometry->setColorArray( colors );
537 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
540 osg::StateSet* stateset = geometry->getOrCreateStateSet();
541 stateset->setAttributeAndModes(
new osg::LineWidth( 1 ), osg::StateAttribute::ON );
542 stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
543 stateset->setMode( GL_BLEND, osg::StateAttribute::ON );
548 osg::ref_ptr< osg::PositionAttitudeTransform >
wge::addLabel( osg::Vec3 position, std::string text )
550 osg::ref_ptr< osgText::Text > label = osg::ref_ptr< osgText::Text >(
new osgText::Text() );
551 osg::ref_ptr< osg::Geode > labelGeode = osg::ref_ptr< osg::Geode >(
new osg::Geode() );
553 labelGeode->addDrawable( label );
557 label->setBackdropType( osgText::Text::OUTLINE );
558 label->setCharacterSize( 6 );
560 label->setText( text );
561 label->setAxisAlignment( osgText::Text::SCREEN );
562 label->setDrawMode( osgText::Text::TEXT );
563 label->setAlignment( osgText::Text::CENTER_TOP );
564 label->setPosition( osg::Vec3( 0.0, 0.0, 0.0 ) );
565 label->setColor( osg::Vec4( 1.0f, 1.0f, 1.0f, 1.0f ) );
567 osg::ref_ptr< osg::PositionAttitudeTransform > labelXform =
568 osg::ref_ptr< osg::PositionAttitudeTransform >(
new osg::PositionAttitudeTransform() );
569 labelXform->setPosition( position );
571 labelXform->addChild( labelGeode );
580 return (
addLabel( position, label ) );
586 ref_ptr< osg::Vec3Array > vertices = ref_ptr< osg::Vec3Array >(
new osg::Vec3Array );
587 ref_ptr< osg::Vec4Array > colors = ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
588 ref_ptr< osg::Geometry > geometry = ref_ptr< osg::Geometry >(
new osg::Geometry );
590 colors->push_back( color );
597 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
598 geometry->setVertexArray( vertices );
599 geometry->setColorArray( colors );
600 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
602 osg::StateSet* stateset =
new osg::StateSet;
603 stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
604 geometry->setStateSet( stateset );
606 osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >(
new osg::Geode );
607 geode->addDrawable( geometry );
611 vertices->push_back( vertices->front() );
612 ref_ptr< osg::Geometry > borderGeom = ref_ptr< osg::Geometry >(
new osg::Geometry );
613 borderGeom->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, 4 ) );
614 borderGeom->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 3, 2 ) );
615 ref_ptr< osg::Vec4Array > colors = ref_ptr< osg::Vec4Array >(
new osg::Vec4Array );
616 colors->push_back( inverseColor( color ) );
617 borderGeom->setColorArray( colors );
618 borderGeom->setColorBinding( osg::Geometry::BIND_OVERALL );
619 borderGeom->setVertexArray( vertices );
620 geode->addDrawable( borderGeom );
625 osg::ref_ptr< osg::Geode >
wge::genFinitePlane( osg::Vec3
const& base, osg::Vec3
const& a, osg::Vec3
const& b,
const WColor& color )
628 osg::ref_ptr< osg::Vec3Array > vertices =
new osg::Vec3Array;
629 osg::ref_ptr< osg::Vec3Array > texcoords0 =
new osg::Vec3Array;
630 osg::ref_ptr< osg::Vec3Array > normals =
new osg::Vec3Array;
631 osg::ref_ptr< osg::Vec4Array > colors =
new osg::Vec4Array;
633 osg::Vec3 aPlusB = a + b;
635 vertices->push_back( base );
636 vertices->push_back( base + a );
637 vertices->push_back( base + aPlusB );
638 vertices->push_back( base + b );
640 osg::Vec3 aCrossB = a ^ b;
647 normals->push_back( aCrossB );
648 colors->push_back( color );
649 texcoords0->push_back( osg::Vec3( 0.0, 0.0, 0.0 ) );
650 texcoords0->push_back( aNorm );
651 texcoords0->push_back( aNorm + bNorm );
652 texcoords0->push_back( bNorm );
655 osg::ref_ptr< osg::Geometry > geometry =
new osg::Geometry();
656 geometry->setVertexArray( vertices );
657 geometry->setTexCoordArray( 0, texcoords0 );
658 geometry->setNormalArray( normals );
659 geometry->setNormalBinding( osg::Geometry::BIND_OVERALL );
660 geometry->setColorArray( colors );
661 geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
662 geometry->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
664 osg::ref_ptr< osg::Geode > geode =
new osg::Geode();
665 geode->addDrawable( geometry );
676 osg::ref_ptr< WGEGroupNode >groupNode =
new WGEGroupNode();
677 osg::ref_ptr< WGEShader > shaderCoordinateSystem(
new WGEShader(
"WGECoordinateSystem" ) );
679 osg::ref_ptr< osg::Geode > graphX(
new osg::Geode );
680 osg::ref_ptr< osg::Geode > graphY(
new osg::Geode );
681 osg::ref_ptr< osg::Geode > graphZ(
new osg::Geode );
683 osg::ref_ptr< osg::Geode > graphXCylinder(
new osg::Geode );
684 osg::ref_ptr< osg::Geode > graphYCylinder(
new osg::Geode );
685 osg::ref_ptr< osg::Geode > graphZCylinder(
new osg::Geode );
688 osg::ref_ptr< osg::ShapeDrawable > cylinderX =
new osg::ShapeDrawable(
new osg::Cylinder(
689 middle, 1, sizeX + ( sizeX * 0.5 )
691 osg::ref_ptr< osg::ShapeDrawable > cylinderXEnd =
new osg::ShapeDrawable(
new osg::Cylinder(
692 osg::Vec3( middle.x(), middle.y(), middle.z() - ( sizeX + ( sizeX * 0.5 ) ) / 2.0 ), 1.0, 1.0
694 osg::ref_ptr< osg::ShapeDrawable > coneX =
new osg::ShapeDrawable(
new osg::Cone(
695 osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeX + ( sizeX * 0.5 ) ) / 2.0 ), 2.0, 5.0
697 cylinderXEnd->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
698 graphXCylinder->addDrawable( cylinderX );
699 graphX->addDrawable( coneX );
700 graphX->addDrawable( cylinderXEnd );
702 osg::ref_ptr< osg::Material > matX =
new osg::Material();
703 matX->setDiffuse( osg::Material::FRONT, WColor( 1.0, 0.0, 0.0, 1.0 ) );
704 cylinderX->getOrCreateStateSet()->setAttribute( matX, osg::StateAttribute::ON );
705 coneX->getOrCreateStateSet()->setAttribute( matX, osg::StateAttribute::ON );
708 osg::ref_ptr< osg::ShapeDrawable > cylinderY =
new osg::ShapeDrawable(
new osg::Cylinder(
709 middle, 1, sizeY + ( sizeY * 0.5 )
711 osg::ref_ptr< osg::ShapeDrawable > cylinderYEnd =
new osg::ShapeDrawable(
new osg::Cylinder(
712 osg::Vec3( middle.x(), middle.y(), middle.z() - ( sizeY + ( sizeY * 0.5 ) ) / 2.0 ), 1.0, 1.0
714 osg::ref_ptr< osg::ShapeDrawable > coneY =
new osg::ShapeDrawable(
new osg::Cone(
715 osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeY + ( sizeY * 0.5 ) ) / 2.0 ), 2.0, 5.0
717 cylinderYEnd->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
719 graphYCylinder->addDrawable( cylinderY );
720 graphY->addDrawable( coneY );
721 graphY->addDrawable( cylinderYEnd );
723 osg::ref_ptr< osg::Material > matY =
new osg::Material();
724 matY->setDiffuse( osg::Material::FRONT, WColor( 0.0, 1.0, 0.0, 1.0 ) );
725 cylinderY->getOrCreateStateSet()->setAttribute( matY, osg::StateAttribute::ON );
726 coneY->getOrCreateStateSet()->setAttribute( matY, osg::StateAttribute::ON );
730 osg::ref_ptr< osg::ShapeDrawable > cylinderZ =
new osg::ShapeDrawable(
new osg::Cylinder(
731 middle, 1, sizeZ + ( sizeZ * 0.5 )
733 osg::ref_ptr< osg::ShapeDrawable > cylinderZEnd =
new osg::ShapeDrawable(
new osg::Cylinder(
734 osg::Vec3( middle.x(), middle.y(), middle.z() - ( sizeZ + ( sizeZ * 0.5 ) ) / 2.0 ), 1.0, 1.0
736 osg::ref_ptr< osg::ShapeDrawable > coneZ =
new osg::ShapeDrawable(
new osg::Cone(
737 osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeZ + ( sizeZ * 0.5 ) ) / 2.0 ), 2.0, 5.0
739 cylinderZEnd->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
741 graphZCylinder->addDrawable( cylinderZ );
742 graphZ->addDrawable( coneZ );
743 graphZ->addDrawable( cylinderZEnd );
745 osg::ref_ptr< osg::Material > matZ =
new osg::Material();
746 matZ->setDiffuse( osg::Material::FRONT, WColor( 0.0, 0.0, 1.0, 1.0 ) );
747 cylinderZ->getOrCreateStateSet()->setAttribute( matZ, osg::StateAttribute::ON );
748 coneZ->getOrCreateStateSet()->setAttribute( matZ, osg::StateAttribute::ON );
750 shaderCoordinateSystem->apply( graphXCylinder );
751 shaderCoordinateSystem->apply( graphYCylinder );
752 shaderCoordinateSystem->apply( graphZCylinder );
754 osg::ref_ptr< WGELabel > graphXLabel =
new WGELabel();
755 graphXLabel->setText(
"X" );
756 graphXLabel->setCharacterSize( 10 );
757 graphXLabel->setPosition( osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeX + ( sizeX * 0.5 ) ) / 2.0 + 5.0 ) );
758 graphXLabel->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
759 graphX->addDrawable( graphXLabel );
761 osg::ref_ptr< WGELabel > graphYLabel =
new WGELabel();
762 graphYLabel->setText(
"Y" );
763 graphYLabel->setCharacterSize( 10 );
764 graphYLabel->setPosition( osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeY + ( sizeY * 0.5 ) ) / 2.0 + 5.0 ) );
765 graphYLabel->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
766 graphY->addDrawable( graphYLabel );
768 osg::ref_ptr< WGELabel > graphZLabel =
new WGELabel();
769 graphZLabel->setText(
"Z" );
770 graphZLabel->setCharacterSize( 10 );
771 graphZLabel->setPosition( osg::Vec3( middle.x(), middle.y(), middle.z() + ( sizeZ + ( sizeZ * 0.5 ) ) / 2.0 + 5.0 ) );
772 graphZLabel->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
773 graphZ->addDrawable( graphZLabel );
776 osg::ref_ptr< osg::MatrixTransform > graphXTransform =
new osg::MatrixTransform();
777 graphXTransform->addChild( graphX );
778 graphXTransform->addChild( graphXCylinder );
779 osg::ref_ptr< osg::MatrixTransform > graphYTransform =
new osg::MatrixTransform();
780 graphYTransform->addChild( graphY );
781 graphYTransform->addChild( graphYCylinder );
782 osg::ref_ptr< osg::MatrixTransform > graphZTransform =
new osg::MatrixTransform();
783 graphZTransform->addChild( graphZ );
784 graphZTransform->addChild( graphZCylinder );
786 osg::Matrixd matrixTranslateTo0 = osg::Matrixd::translate( -middle.x(), -middle.y(), -middle.z() );
787 osg::Matrixd matrixTranslateFrom0 = osg::Matrixd::translate( middle.x(), middle.y(), middle.z() );
789 graphXTransform->setMatrix( matrixTranslateTo0 * osg::Matrixd::rotate(
790 90.0 * piDouble / 180.0,
791 osg::Vec3f( 0.0, 1.0, 0.0 ) ) * matrixTranslateFrom0
793 graphYTransform->setMatrix( matrixTranslateTo0 * osg::Matrixd::rotate(
794 -90.0 * piDouble / 180.0,
795 osg::Vec3f( 1.0, 0.0, 0.0 ) ) * matrixTranslateFrom0
798 groupNode->insert( graphXTransform );
799 groupNode->insert( graphYTransform );
800 groupNode->insert( graphZTransform );
const std::map< size_t, WColor > & getData() const
Reference to the data.
Represents a std::map where for each vertex ID a color is stored.
osg::ref_ptr< osg::Geometry > createUnitCubeAsLines(const WColor &color)
Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates...
static Fonts getAllFonts()
The paths to all fonts supported.
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::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...
This only is a 3d double vector.
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.
Represents a plane with a normal vector and a position in space.
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::BoundingBoxImpl< osg::Vec3 >::vec_type vec_type
Vertex type for min and max positions of this box.
osg::ref_ptr< osg::Group > creatCoordinateSystem(osg::Vec3 middle, double sizeX, double sizeY, double sizeZ)
Create a coordinate system.
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.
Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader...
WPosition getPointInPlane(double x, double y) const
Computes with relative coordinates a point in this plane.
Class to wrap around the osg Group node and providing a thread safe add/removal mechanism.
std::string toString(const T &value)
Convert a given value to a string.
const vec_type & getMin() const
Gives the front lower left aka minimum corner.
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 > generateBoundingBoxGeode(const WBoundingBox &bb, const WColor &color)
Generates an OSG geode for the bounding box.
const vec_type & getMax() const
Gives the back upper right aka maximum corner.
boost::shared_ptr< WTriangleMesh > SPtr
Shared pointer.
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.