diff --git a/examples/osgtext3D/osgtext3D.cpp b/examples/osgtext3D/osgtext3D.cpp index bbb402a24..09930710d 100644 --- a/examples/osgtext3D/osgtext3D.cpp +++ b/examples/osgtext3D/osgtext3D.cpp @@ -36,541 +36,6 @@ extern int main_orig(int, char**); extern int main_test(int, char**); - - -class Boundary -{ -public: - - typedef std::pair Segment; - typedef std::vector Segments; - osg::ref_ptr _vertices; - unsigned int _start; - unsigned int _count; - Segments _segments; - - Boundary(osg::Vec3Array* vertices, unsigned int start, unsigned int count) - { - _vertices = vertices; - _start = start; - _count = count; - - if ((*_vertices)[start]==(*_vertices)[start+count-1]) - { - // OSG_NOTICE<<"Boundary is a line loop"< 0.0f) - { - // OSG_NOTICE<<" computeBisectorNormal(a=["< maxThickness) - { - maxThickness = thickness; - maxThickness_i = i; - } - } - - return maxThickness_i != _segments.size(); - } - - - void removeAllSegmentsAboveThickness(float targetThickness) - { - // OSG_NOTICE<<"removeAllSegmentsBelowThickness("<reserve(new_vertices->size() + _segments.size()+1 + _count); - - // create vertices - unsigned int previous_second = _segments[0].second; - osg::Vec3 newPoint = computeBisectorPoint(0, targetThickness); - unsigned int first = new_vertices->size(); - new_vertices->push_back(newPoint); - - if (_segments[0].first != _start) - { - //OSG_NOTICE<<"We have pruned from the start"<push_back(first); - } - } - else - { - face->push_back(first); - } - - - for(unsigned int i=1; i<_segments.size(); ++i) - { - newPoint = computeBisectorPoint(i, targetThickness); - unsigned int vi = new_vertices->size(); - new_vertices->push_back(newPoint); - - if (previous_second != _segments[i].first) - { - //OSG_NOTICE<<"Gap in boundary"<push_back(vi); - } - } - else - { - face->push_back(vi); - } - - previous_second = _segments[i].second; - } - - // fill the end of the polygon with repititions of the first index in the polygon to ensure - // that the orignal and new boundary polygons have the same number and pairing of indices. - // This ensures that the bevel can be created coherently. - while(face->size() < _count) - { - face->push_back(first); - } - - // add face primitive set for polygon - geometry->addPrimitiveSet(face); - - osg::DrawElementsUShort* bevel = new osg::DrawElementsUShort(GL_QUAD_STRIP); - bevel->setName("bevel"); - bevel->reserve(_count*2); - for(unsigned int i=0; i<_count; ++i) - { - unsigned int vi = new_vertices->size(); - new_vertices->push_back((*_vertices)[_start+i]); - bevel->push_back(vi); - bevel->push_back((*face)[i]); - } - geometry->addPrimitiveSet(bevel); - } - -}; - -osg::Geometry* getGeometryComponent(osg::Geometry* geometry, bool bevel) -{ - osg::Vec3Array* vertices = dynamic_cast(geometry->getVertexArray()); - if (!vertices) return 0; - - osg::Geometry* new_geometry = new osg::Geometry; - osg::Vec3Array* new_vertices = new osg::Vec3Array(*vertices); - new_geometry->setVertexArray(new_vertices); - - for(unsigned int i=0; igetNumPrimitiveSets(); ++i) - { - osg::PrimitiveSet* primitiveSet = geometry->getPrimitiveSet(i); - if (primitiveSet->getName()=="bevel") - { - if (bevel) new_geometry->addPrimitiveSet(primitiveSet); - } - else - { - if (!bevel) new_geometry->addPrimitiveSet(primitiveSet); - } - } - - osg::Vec4Array* new_colours = new osg::Vec4Array; - 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)); - new_geometry->setColorArray(new_colours); - new_geometry->setColorBinding(osg::Geometry::BIND_OVERALL); - - if (!bevel) - { - osg::Vec3Array* normals = new osg::Vec3Array; - normals->push_back(osg::Vec3(0.0,0.0,1.0)); - new_geometry->setNormalArray(normals); - new_geometry->setNormalBinding(osg::Geometry::BIND_OVERALL); - } - - return new_geometry; -} - -osg::Geometry* computeBevelGeometry(osg::Geometry* geometry, osgText::BevelProfile& profile, float width) -{ - if (!geometry) return 0; - - osg::Vec3Array* orig_vertices = dynamic_cast(geometry->getVertexArray()); - if (!orig_vertices) return 0; - - if (geometry->getNumPrimitiveSets()==0) return 0; - - osg::Geometry* new_geometry = new osg::Geometry; - - osg::Vec3Array* new_vertices = new osg::Vec3Array; - new_geometry->setVertexArray(new_vertices); - - osg::Vec3 forward(0.0f, 0.0f, -width); - - for(unsigned int prim_i=0; prim_igetNumPrimitiveSets(); ++prim_i) - { - osg::DrawElementsUShort* bevel = dynamic_cast(geometry->getPrimitiveSet(prim_i)); - if (!bevel) continue; - - unsigned int no_vertices_on_boundary = bevel->size()/2; - - osgText::BevelProfile::Vertices& profileVertices = profile.getVertices(); - unsigned int no_vertices_on_bevel = profileVertices.size(); - - unsigned int start = new_vertices->size(); - - // populate vertices - for(unsigned int i=0; isize()-1;) - { - osg::Vec3& top_vertex = (*orig_vertices)[ (*bevel)[i++] ]; - osg::Vec3& base_vertex = (*orig_vertices)[ (*bevel)[i++] ]; - osg::Vec3 up = top_vertex-base_vertex; - for(unsigned int j=0; jpush_back(pos); - } - } - - osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_TRIANGLES); - for(unsigned int i = 0; i< no_vertices_on_boundary-1; ++i) - { - for(unsigned int j=0; jpush_back(base); - elements->push_back(next); - elements->push_back(base+1); - - elements->push_back(base+1); - elements->push_back(next); - elements->push_back(next+1); - } - } - - new_geometry->addPrimitiveSet(elements); - } - - return new_geometry; -} - -struct CollectTriangleIndicesFunctor -{ - CollectTriangleIndicesFunctor() {} - - typedef std::vector Indices; - Indices _indices; - - void operator() (unsigned int p1, unsigned int p2, unsigned int p3) - { - if (p1==p2 || p2==p3 || p1==p3) - { - return; - } - - _indices.push_back(p1); - _indices.push_back(p3); - _indices.push_back(p2); - - } -}; - -osg::Geometry* computeFrontAndBackGeometry(osg::Geometry* geometry, float width) -{ - if (!geometry) return 0; - - osg::Vec3Array* orig_vertices = dynamic_cast(geometry->getVertexArray()); - if (!orig_vertices) return 0; - - if (geometry->getNumPrimitiveSets()==0) return 0; - - osg::TriangleIndexFunctor ctif; - geometry->accept(ctif); - CollectTriangleIndicesFunctor::Indices& face = ctif._indices; - - osg::Geometry* new_geometry = new osg::Geometry; - - osg::Vec3Array* new_vertices = new osg::Vec3Array; - new_geometry->setVertexArray(new_vertices); - - osg::Vec3 forward(0.0f, 0.0f, -width); - - // front face - osg::DrawElementsUShort* front_face = new osg::DrawElementsUShort(GL_TRIANGLES); - new_geometry->addPrimitiveSet(front_face); - for(unsigned int i=0; ipush_back(new_vertices->size()); - new_vertices->push_back(vertex); - } - - // back face - osg::DrawElementsUShort* back_face = new osg::DrawElementsUShort(GL_TRIANGLES); - new_geometry->addPrimitiveSet(back_face); - for(unsigned int i=0; ipush_back(new_vertices->size()); - new_vertices->push_back(vertex); - } - - return new_geometry; -} - - -osg::Geometry* createShell(osg::Geometry* geometry, float distance) -{ - if (!geometry) return 0; - - osg::Vec3Array* orig_vertices = dynamic_cast(geometry->getVertexArray()); - if (!orig_vertices) return 0; - - osg::Vec3Array* orig_normals = dynamic_cast(geometry->getNormalArray()); - if (!orig_normals) return 0; - - if (orig_vertices->size() != orig_normals->size()) return 0; - - osg::Geometry* new_geometry = new osg::Geometry; - osg::Vec3Array* new_vertices = new osg::Vec3Array(orig_vertices->size()); - for(unsigned int i=0; isize(); ++i) - { - (*new_vertices)[i] = (*orig_vertices)[i] + (*orig_normals)[i]*distance; - } - - new_geometry->setVertexArray(new_vertices); - - osg::Vec4Array* new_colours = new osg::Vec4Array; - new_colours->push_back(osg::Vec4(1.0,1.0,1.0,0.2)); - new_geometry->setColorArray(new_colours); - new_geometry->setColorBinding(osg::Geometry::BIND_OVERALL); - - osg::TriangleIndexFunctor ctif; - geometry->accept(ctif); - - // front face - osg::DrawElementsUInt* shell = new osg::DrawElementsUInt(GL_TRIANGLES); - new_geometry->addPrimitiveSet(shell); - CollectTriangleIndicesFunctor::Indices& face = ctif._indices; - for(unsigned int i=0; ipush_back(face[i]); - } - - osg::StateSet* stateset = new_geometry->getOrCreateStateSet(); - stateset->setMode(GL_BLEND, osg::StateAttribute::ON); - stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); - stateset->setAttributeAndModes(new osg::CullFace, osg::StateAttribute::ON); - stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); - return new_geometry; -} - - -osg::Geometry* computeThickness(osg::Geometry* orig_geometry, float thickness) -{ - // OSG_NOTICE<<"computeThickness("<(orig_geometry->getVertexArray()); - osg::Geometry::PrimitiveSetList& orig_primitives = orig_geometry->getPrimitiveSetList(); - - osg::Geometry* new_geometry = new osg::Geometry; - - for(osg::Geometry::PrimitiveSetList::iterator itr = orig_primitives.begin(); - itr != orig_primitives.end(); - ++itr) - { - osg::DrawArrays* drawArray = dynamic_cast(itr->get()); - if (drawArray && drawArray->getMode()==GL_POLYGON) - { - Boundary boundary(orig_vertices, drawArray->getFirst(), drawArray->getCount()); - if (thickness>0.0f) boundary.removeAllSegmentsBelowThickness(thickness); - else if (thickness<0.0f) boundary.removeAllSegmentsAboveThickness(thickness); - boundary.addBoundaryToGeometry(new_geometry, thickness); - } - } - return new_geometry; -} - int main(int argc, char** argv) { osg::ArgumentParser arguments(&argc, argv); @@ -625,14 +90,24 @@ int main(int argc, char** argv) while(arguments.read("--rounded2",ratio)) { profile.roundedBevel2(ratio); } while(arguments.read("--flat",ratio)) { profile.flatBevel(ratio); } + bool outline = false; + while(arguments.read("--outline")) { outline = true; } + while(arguments.read("--no-outline")) { outline = false; } + + bool smooth = true; + while(arguments.read("--flat")) { smooth = false; } + while(arguments.read("--smooth")) { smooth = false; } + + unsigned int numSamples = 10; + while(arguments.read("--samples", numSamples)) {} + font->setNumberCurveSamples(numSamples); + profile.print(std::cout); osg::ref_ptr group = new osg::Group; osg::Vec3 position; - - for(unsigned int i=0; i glyph = font->getGlyph(word[i]); @@ -646,74 +121,17 @@ int main(int argc, char** argv) osg::ref_ptr geode = new osg::Geode; -#if 1 osg::ref_ptr glyphGeometry = osgText::computeGlyphGeometry(glyph.get(), thickness, width); osg::ref_ptr textGeometry = osgText::computeTextGeometry(glyphGeometry.get(), profile, width); - osg::ref_ptr shellGeometry = osgText::computeShellGeometry(glyphGeometry.get(), profile, width); + osg::ref_ptr shellGeometry = outline ? osgText::computeShellGeometry(glyphGeometry.get(), profile, width) : 0; if (textGeometry.valid()) geode->addDrawable(textGeometry.get()); if (shellGeometry.valid()) geode->addDrawable(shellGeometry.get()); // create the normals - if (true) + if (smooth && textGeometry.valid()) { - osgUtil::SmoothingVisitor smoother; - smoother.setCreaseAngle(osg::DegreesToRadians(creaseAngle)); - geode->accept(smoother); + osgUtil::SmoothingVisitor::smooth(*textGeometry, osg::DegreesToRadians(creaseAngle)); } -#else - osg::Vec3Array* vertices = glyph->getRawVertexArray(); - osg::Geometry::PrimitiveSetList& primitives = glyph->getRawFacePrimitiveSetList(); - - osg::ref_ptr geometry = new osg::Geometry; - geometry->setVertexArray(vertices); - geometry->setPrimitiveSetList(primitives); - osg::Vec4Array* colours = new osg::Vec4Array; - colours->push_back(osg::Vec4(1.0,1.0,1.0,1.0)); - geometry->setColorArray(colours); - geometry->setColorBinding(osg::Geometry::BIND_OVERALL); - - osg::ref_ptr face_and_bevel = computeThickness(geometry, thickness); - - osg::ref_ptr face = getGeometryComponent(face_and_bevel, false); - - if (face.valid()) - { - osgUtil::Tessellator ts; - ts.setWindingType(osgUtil::Tessellator::TESS_WINDING_POSITIVE); - ts.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); - ts.retessellatePolygons(*face); - - osg::ref_ptr faces = computeFrontAndBackGeometry(face.get(), width); - if (faces.valid()) geode->addDrawable(faces.get()); - } - - osg::ref_ptr bevel_strip = getGeometryComponent(face_and_bevel, true); - osg::ref_ptr bevel = computeBevelGeometry(bevel_strip, profile, width); - - if (bevel.valid()) - { - geode->addDrawable(bevel.get()); - } - - // create the normals - { - osgUtil::SmoothingVisitor smoother; - geode->accept(smoother); - } - - osg::ref_ptr shell = createShell(bevel.get(), width); - - { - osgUtil::SmoothingVisitor smoother; - smoother.setCreaseAngle(osg::DegreesToRadians(creaseAngle)); - geode->accept(smoother); - } - - if (shell.valid()) - { - geode->addDrawable(shell.get()); - } -#endif transform->addChild(geode.get()); @@ -726,5 +144,6 @@ int main(int argc, char** argv) osgViewer::Viewer viewer(arguments); viewer.setSceneData(group.get()); viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); + viewer.addEventHandler(new osgViewer::StatsHandler); return viewer.run(); }