Show
Ignore:
Timestamp:
03/07/11 13:35:30 (3 years ago)
Author:
robert
Message:

Added --color r g b a, --wall-color r g b a, --back-color r g b a options for setting the colours of faces via materials.
and --image filename, --wall-image filename, --back-image filename options to set the textures used, note, texcoords are not currently set up so texels used will be 0,0.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgtext3D/osgtext3D.cpp

    r12069 r12205  
    1818 
    1919#include <osg/ArgumentParser> 
    20 #include <osg/Geode> 
    21 #include <osg/Geometry> 
    22 #include <osg/CullFace> 
    23 #include <osg/TriangleIndexFunctor> 
     20#include <osg/Material> 
    2421#include <osg/PositionAttitudeTransform> 
    25 #include <osgUtil/SmoothingVisitor> 
     22#include <osg/io_utils> 
     23 
     24#include <osgDB/ReadFile> 
    2625#include <osgDB/WriteFile> 
    2726#include <osgGA/StateSetManipulator> 
    28 #include <osgUtil/Tessellator> 
    2927#include <osgViewer/Viewer> 
    3028#include <osgViewer/ViewerEventHandlers> 
    31 #include <osg/io_utils> 
    3229 
    3330#include <osgText/Text3D> 
     
    3532#include "TextNode.h" 
    3633 
    37 extern int main_orig(int, char**); 
    38 extern int main_test(int, char**); 
    39  
    40  
    41 int main_size(int argc, char** argv) 
    42 { 
    43     osg::ArgumentParser arguments(&argc, argv); 
    44  
    45     osgViewer::Viewer viewer(arguments); 
    46  
    47     std::string fontFile("arial.ttf"); 
    48     while(arguments.read("-f",fontFile)) {} 
    49  
    50     osg::ref_ptr<osgText::Font> font = osgText::readFontFile(fontFile); 
    51     if (!font) return 1; 
    52     OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl; 
    53  
    54     osg::Geode* geode = new osg::Geode; 
    55  
    56     geode->addDrawable( osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(1.0f,0.0,0.0),osg::Vec3(0.0f,0.0,1.0), 0.0, 0.0, 1.0, 1.0) ); 
    57  
    58     osgText::Text3D* text3d = new osgText::Text3D; 
    59     text3d->setPosition(osg::Vec3(1.0f,0.0f,0.0f)); 
    60     text3d->setFont(osgText::readFontFile("arial.ttf")); 
    61     text3d->setCharacterSizeMode(osgText::Text3D::OBJECT_COORDS); 
    62     text3d->setCharacterSize(1.0f); 
    63     text3d->setCharacterDepth(0.1f); 
    64     text3d->setAxisAlignment(osgText::Text3D::XZ_PLANE); 
    65     text3d->setText("This is a size test"); 
    66  
    67     geode->addDrawable(text3d); 
    68  
    69     viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); 
    70     viewer.addEventHandler(new osgViewer::StatsHandler); 
    71  
    72     viewer.setSceneData(geode); 
    73  
    74     return viewer.run(); 
    75 } 
    76  
    77  
    7834 
    7935int main(int argc, char** argv) 
    8036{ 
    8137    osg::ArgumentParser arguments(&argc, argv); 
    82  
    83     if (arguments.read("--test")) 
    84     { 
    85         return main_test(argc,argv); 
    86     } 
    87     else if (arguments.read("--original") || arguments.read("--orig")) 
    88     { 
    89         return main_orig(argc,argv); 
    90     } 
    91     else if (arguments.read("--size-test")) 
    92     { 
    93         return main_size(argc,argv); 
    94     } 
    9538 
    9639    osgViewer::Viewer viewer(arguments); 
     
    12669    while(arguments.read("--outline",r)) { style->setOutlineRatio(r); } 
    12770 
     71 
    12872    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); 
    12973    viewer.addEventHandler(new osgViewer::StatsHandler); 
    13074 
    13175#if 1 
    132     osg::Geode* geode = new osg::Geode; 
     76    osg::ref_ptr<osg::Group> group = new osg::Group; 
    13377 
    13478    float characterSize = 1.0f; 
     
    14488        text2D->setAxisAlignment(osgText::Text::XZ_PLANE); 
    14589        text2D->setText(word); 
     90        osg::Geode* geode = new osg::Geode; 
    14691        geode->addDrawable(text2D); 
     92        group->addChild(geode); 
    14793    } 
    148         if (!arguments.read("--no-3d")) 
     94 
     95    if (arguments.read("--TextNode")) 
     96    { 
     97        // experimental text node 
     98        osgText::TextNode* text = new osgText::TextNode; 
     99        text->setFont(font.get()); 
     100        text->setStyle(style.get()); 
     101        text->setTextTechnique(new osgText::TextTechnique); 
     102        text->setText(word); 
     103        text->update(); 
     104 
     105        group->addChild(text); 
     106    } 
     107    else if (!arguments.read("--no-3d")) 
    149108    { 
    150109        osgText::Text3D* text3D = new osgText::Text3D; 
     
    155114        text3D->setAxisAlignment(osgText::Text3D::XZ_PLANE); 
    156115        text3D->setText(word); 
     116 
     117        osg::Geode* geode = new osg::Geode; 
    157118        geode->addDrawable(text3D); 
     119        group->addChild(geode); 
     120 
     121        osg::Vec4 color(1.0f, 1.0f, 1.0f, 1.0f); 
     122        while(arguments.read("--color",color.r(),color.g(),color.b(),color.a())) 
     123        { 
     124            OSG_NOTICE<<"--color "<<color<<std::endl; 
     125            text3D->setColor(color); 
     126        } 
     127 
     128        std::string imageFilename; 
     129        while(arguments.read("--image",imageFilename)) 
     130        { 
     131            OSG_NOTICE<<"--image "<<imageFilename<<std::endl; 
     132            osg::ref_ptr<osg::Image> image = osgDB::readImageFile(imageFilename); 
     133            if (image.valid()) 
     134            { 
     135                OSG_NOTICE<<"  loaded image "<<imageFilename<<std::endl; 
     136                osg::StateSet* stateset = text3D->getOrCreateStateSet(); 
     137                stateset->setTextureAttributeAndModes(0, new osg::Texture2D(image.get()), osg::StateAttribute::ON); 
     138            } 
     139        } 
     140 
     141        while(arguments.read("--wall-color",color.r(),color.g(),color.b(),color.a())) 
     142        { 
     143            osg::StateSet* stateset = text3D->getOrCreateWallStateSet(); 
     144            osg::Material* material = new osg::Material; 
     145            material->setDiffuse(osg::Material::FRONT_AND_BACK, color); 
     146            stateset->setAttribute(material); 
     147        } 
     148 
     149        while(arguments.read("--wall-image",imageFilename)) 
     150        { 
     151            osg::ref_ptr<osg::Image> image = osgDB::readImageFile(imageFilename); 
     152            if (image.valid()) 
     153            { 
     154                osg::StateSet* stateset = text3D->getOrCreateWallStateSet(); 
     155                stateset->setTextureAttributeAndModes(0, new osg::Texture2D(image.get()), osg::StateAttribute::ON); 
     156            } 
     157        } 
     158 
     159        while(arguments.read("--back-color",color.r(),color.g(),color.b(),color.a())) 
     160        { 
     161            osg::StateSet* stateset = text3D->getOrCreateBackStateSet(); 
     162            osg::Material* material = new osg::Material; 
     163            material->setDiffuse(osg::Material::FRONT_AND_BACK, color); 
     164            stateset->setAttribute(material); 
     165        } 
     166 
     167        while(arguments.read("--back-image",imageFilename)) 
     168        { 
     169            osg::ref_ptr<osg::Image> image = osgDB::readImageFile(imageFilename); 
     170            if (image.valid()) 
     171            { 
     172                osg::StateSet* stateset = text3D->getOrCreateBackStateSet(); 
     173                stateset->setTextureAttributeAndModes(0, new osg::Texture2D(image.get()), osg::StateAttribute::ON); 
     174            } 
     175        } 
     176 
     177        if (arguments.read("--size-quad")) 
     178        { 
     179            geode->addDrawable( osg::createTexturedQuadGeometry(osg::Vec3(0.0f,characterSize*thickness,0.0f),osg::Vec3(characterSize,0.0,0.0),osg::Vec3(0.0f,0.0,characterSize), 0.0, 0.0, 1.0, 1.0) ); 
     180        } 
    158181    } 
     182 
    159183     
     184    viewer.setSceneData(group.get()); 
    160185 
    161     if (arguments.read("--size-quad")) 
    162     { 
    163         geode->addDrawable( osg::createTexturedQuadGeometry(osg::Vec3(0.0f,characterSize*thickness,0.0f),osg::Vec3(characterSize,0.0,0.0),osg::Vec3(0.0f,0.0,characterSize), 0.0, 0.0, 1.0, 1.0) ); 
    164     } 
    165      
    166     viewer.setSceneData(geode); 
    167 #else 
    168     osgText::TextNode* text = new osgText::TextNode; 
    169     text->setFont(font.get()); 
    170     text->setStyle(style.get()); 
    171     text->setTextTechnique(new osgText::TextTechnique); 
    172     text->setText(word); 
    173     text->update(); 
    174     viewer.setSceneData(text); 
    175186#endif 
    176187