Changeset 11685 for OpenSceneGraph/trunk/examples/osgtext3D/osgtext3D.cpp
- Timestamp:
- 07/26/10 13:12:45 (3 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgtext3D/osgtext3D.cpp
r11684 r11685 21 21 #include <osg/Geometry> 22 22 #include <osg/PositionAttitudeTransform> 23 #include <osgUtil/SmoothingVisitor> 23 24 #include <osgText/Font3D> 24 25 #include <osgDB/WriteFile> … … 33 34 34 35 35 36 36 class Boundary 37 37 { … … 41 41 typedef std::vector<Segment> Segments; 42 42 osg::ref_ptr<osg::Vec3Array> _vertices; 43 unsigned int _start; 44 unsigned int _count; 43 45 Segments _segments; 44 46 … … 46 48 { 47 49 _vertices = vertices; 50 _start = start; 51 _count = count; 48 52 49 53 if ((*_vertices)[start]==(*_vertices)[start+count-1]) … … 57 61 58 62 _segments.reserve(count-1); 59 for(unsigned int i=start; i<start+count- 1; ++i)63 for(unsigned int i=start; i<start+count-2; ++i) 60 64 { 61 65 _segments.push_back(Segment(i,i+1)); 62 66 } 67 _segments.push_back(Segment(start+count-2,start)); 63 68 64 69 } … … 67 72 { 68 73 float denominator = ( cn.x() * an.y() - cn.y() * an.x()); 69 if (denominator==0.0 )74 if (denominator==0.0f) 70 75 { 71 76 //OSG_NOTICE<<"computeRayIntersectionPoint()<<denominator==0.0"<<std::endl; 72 77 // line segments must be parallel. 73 return (a+c)*0.5 ;78 return (a+c)*0.5f; 74 79 } 75 80 … … 138 143 return thickness; 139 144 } 145 140 146 141 147 float computeThickness(unsigned int i) … … 206 212 // OSG_NOTICE<<"bisector_abcd = "<<bisector_abcd<<", ab_sidevector="<<ab_sidevector<<", b-a="<<b-a<<", scale_factor="<<scale_factor<<std::endl; 207 213 208 new_vertex.z() += 0.5f;214 new_vertex.z() += 5.0f; 209 215 return new_vertex; 210 216 } … … 217 223 osg::Vec3Array* new_vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); 218 224 219 unsigned int first = new_vertices->size(); 220 unsigned int count = 0; 225 // allocate the primitive set to store the face geometry 226 osg::DrawElementsUShort* face = new osg::DrawElementsUShort(GL_POLYGON); 227 face->setName("face"); 221 228 222 229 // reserve enough space in the vertex array to accomodate the vertices associated with the segments 223 // new_vertices->reserve(new_vertices->size()+_segments.size()+1);230 new_vertices->reserve(new_vertices->size() + _segments.size()+1 + _count); 224 231 225 232 // create vertices 226 233 unsigned int previous_second = _segments[0].second; 227 234 osg::Vec3 newPoint = computeBisectorPoint(0, targetThickness); 235 unsigned int first = new_vertices->size(); 228 236 new_vertices->push_back(newPoint); 229 ++count; 237 238 if (_segments[0].first != _start) 239 { 240 //OSG_NOTICE<<"We have pruned from the start"<<std::endl; 241 for(unsigned int j=_start; j<=_segments[0].first;++j) 242 { 243 face->push_back(first); 244 } 245 } 246 else 247 { 248 face->push_back(first); 249 } 250 230 251 231 252 for(unsigned int i=1; i<_segments.size(); ++i) 232 253 { 254 newPoint = computeBisectorPoint(i, targetThickness); 255 unsigned int vi = new_vertices->size(); 256 new_vertices->push_back(newPoint); 257 258 if (previous_second != _segments[i].first) 259 { 260 //OSG_NOTICE<<"Gap in boundary"<<previous_second<<" to "<<_segments[i].first<<std::endl; 261 for(unsigned int j=previous_second; j<=_segments[i].first;++j) 262 { 263 face->push_back(vi); 264 } 265 } 266 else 267 { 268 face->push_back(vi); 269 } 270 233 271 previous_second = _segments[i].second; 234 newPoint = computeBisectorPoint(i, targetThickness); 235 new_vertices->push_back(newPoint); 236 ++count; 237 } 238 239 // repeat the first point to make it a full closed loop 240 new_vertices->push_back((*new_vertices)[first]); 241 ++count; 242 243 // add DrawArrays primitive set for polygon 244 if (count!=0) geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, first, count)); 272 } 273 274 // fill the end of the polygon with repititions of the first index in the polygon to ensure 275 // that the orignal and new boundary polygons have the same number and pairing of indices. 276 // This ensures that the bevel can be created coherently. 277 while(face->size() < _count) 278 { 279 face->push_back(first); 280 } 281 282 // add face primitive set for polygon 283 geometry->addPrimitiveSet(face); 284 285 osg::DrawElementsUShort* bevel = new osg::DrawElementsUShort(GL_QUAD_STRIP); 286 bevel->setName("bevel"); 287 bevel->reserve(_count*2); 288 for(unsigned int i=0; i<_count; ++i) 289 { 290 unsigned int vi = new_vertices->size(); 291 new_vertices->push_back((*_vertices)[_start+i]); 292 bevel->push_back(vi); 293 bevel->push_back((*face)[i]); 294 } 295 geometry->addPrimitiveSet(bevel); 245 296 } 246 297 247 298 }; 299 300 osg::Geometry* getGeometryComponent(osg::Geometry* geometry, bool bevel) 301 { 302 osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); 303 if (!vertices) return 0; 304 305 osg::Geometry* new_geometry = new osg::Geometry; 306 osg::Vec3Array* new_vertices = new osg::Vec3Array(*vertices); 307 new_geometry->setVertexArray(new_vertices); 308 309 for(unsigned int i=0; i<geometry->getNumPrimitiveSets(); ++i) 310 { 311 osg::PrimitiveSet* primitiveSet = geometry->getPrimitiveSet(i); 312 if (primitiveSet->getName()=="bevel") 313 { 314 if (bevel) new_geometry->addPrimitiveSet(primitiveSet); 315 } 316 else 317 { 318 if (!bevel) new_geometry->addPrimitiveSet(primitiveSet); 319 } 320 } 321 322 osg::Vec4Array* new_colours = new osg::Vec4Array; 323 new_colours->push_back(bevel ? osg::Vec4(1.0,1.0,0.0,1.0) : osg::Vec4(1.0,0.0,0.0,1.0)); 324 new_geometry->setColorArray(new_colours); 325 new_geometry->setColorBinding(osg::Geometry::BIND_OVERALL); 326 327 if (!bevel) 328 { 329 osg::Vec3Array* normals = new osg::Vec3Array; 330 normals->push_back(osg::Vec3(0.0,0.0,1.0)); 331 new_geometry->setNormalArray(normals); 332 new_geometry->setNormalBinding(osg::Geometry::BIND_OVERALL); 333 } 334 335 return new_geometry; 336 } 248 337 249 338 … … 256 345 osg::Geometry* new_geometry = new osg::Geometry; 257 346 258 osg::Vec4Array* new_colours = new osg::Vec4Array;259 new_colours->push_back(osg::Vec4(1.0,1.0,0.0,1.0));260 new_geometry->setColorArray(new_colours);261 new_geometry->setColorBinding(osg::Geometry::BIND_OVERALL);262 263 347 for(osg::Geometry::PrimitiveSetList::iterator itr = orig_primitives.begin(); 264 348 itr != orig_primitives.end(); … … 269 353 { 270 354 Boundary boundary(orig_vertices, drawArray->getFirst(), drawArray->getCount()); 271 boundary.computeAllThickness();272 273 355 boundary.removeAllSegmentsBelowThickness(thickness); 274 356 boundary.addBoundaryToGeometry(new_geometry, thickness); … … 344 426 geometry->setColorBinding(osg::Geometry::BIND_OVERALL); 345 427 346 osg::Geometry* bevel = computeThickness(geometry, thickness); 347 348 if (bevel) geode->addDrawable(bevel); 349 428 osg::ref_ptr<osg::Geometry> face_and_bevel = computeThickness(geometry, thickness); 429 430 osg::Geometry* bevel = getGeometryComponent(face_and_bevel, true); 431 if (bevel) 432 { 433 geode->addDrawable(bevel); 434 osgUtil::SmoothingVisitor smoother; 435 smoother.smooth(*bevel); 436 } 437 438 osg::Geometry* face = getGeometryComponent(face_and_bevel, false); 439 if (face) geode->addDrawable(face); 440 441 350 442 if (useTessellator) 351 443 { … … 358 450 } 359 451 360 if ( bevel)452 if (face) 361 453 { 362 454 osgUtil::Tessellator ts; 363 455 ts.setWindingType(osgUtil::Tessellator::TESS_WINDING_POSITIVE); 364 456 ts.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); 365 ts.retessellatePolygons(* bevel);457 ts.retessellatePolygons(*face); 366 458 } 367 459 … … 374 466 group->addChild(transform.get()); 375 467 } 468 469 376 470 377 471 std::string filename;
