root/OpenSceneGraph/trunk/examples/osgtext3D/osgtext3D.cpp @ 7645

Revision 7645, 4.8 kB (checked in by robert, 6 years ago)

From David Callu, added support of 3D text to osgText and associated plugins.

Line 
1/* OpenSceneGraph example, osgtext.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19
20#include <osgViewer/Viewer>
21#include <osgViewer/ViewerEventHandlers>
22#include <osgDB/ReadFile>
23
24#include <osgGA/TrackballManipulator>
25#include <osgGA/StateSetManipulator>
26
27#include <osg/Geode>
28#include <osg/Geometry>
29#include <osg/Material>
30#include <osg/Shape>
31#include <osg/ShapeDrawable>
32#include <osgText/Text3D>
33
34#include <iostream>
35#include <sstream>
36
37
38
39// create text which sits in 3D space such as would be inserted into a normal model
40osg::Group* create3DText(const osg::Vec3& center,float radius)
41{
42
43    osg::Geode* geode  = new osg::Geode;
44
45////////////////////////////////////////////////////////////////////////////////////////////////////////
46//   
47// Examples of how to set up axis/orientation alignments
48//
49
50    float characterSize=radius*0.2f;
51    float characterDepth=characterSize*0.2f;
52   
53    osg::Vec3 pos(center.x()-radius*.5f,center.y()-radius*.5f,center.z()-radius*.5f);
54
55    osgText::Text3D* text1 = new osgText::Text3D;
56    text1->setFont("fonts/arial.ttf");
57    text1->setCharacterSize(characterSize);
58    text1->setCharacterDepth(characterDepth);
59    text1->setPosition(pos);
60    text1->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX);
61    text1->setAxisAlignment(osgText::Text3D::XY_PLANE);
62    text1->setText("XY_PLANE");
63    geode->addDrawable(text1);
64
65    osgText::Text3D* text2 = new osgText::Text3D;
66    text2->setFont("fonts/times.ttf");
67    text2->setCharacterSize(characterSize);
68    text2->setCharacterDepth(characterDepth);
69    text2->setPosition(pos);
70    text2->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX);
71    text2->setAxisAlignment(osgText::Text3D::YZ_PLANE);
72    text2->setText("YZ_PLANE");
73    geode->addDrawable(text2);
74
75    osgText::Text3D* text3 = new osgText::Text3D;
76    text3->setFont("fonts/dirtydoz.ttf");
77    text3->setCharacterSize(characterSize);
78    text3->setCharacterDepth(characterDepth);
79    text3->setPosition(pos);
80    text3->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX);
81    text3->setAxisAlignment(osgText::Text3D::XZ_PLANE);
82    text3->setText("XZ_PLANE");
83    geode->addDrawable(text3);
84
85    osgText::Text3D* text7 = new osgText::Text3D;
86    text7->setFont("fonts/times.ttf");
87    text7->setCharacterSize(characterSize);
88    text7->setCharacterDepth(characterSize*0.2f);
89    text7->setPosition(center - osg::Vec3(0.0, 0.0, 0.6));
90    text7->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX);
91    text7->setAxisAlignment(osgText::Text3D::SCREEN);
92    text7->setCharacterSizeMode(osgText::Text3D::OBJECT_COORDS);
93    text7->setText("CharacterSizeMode OBJECT_COORDS (default)");
94    geode->addDrawable(text7);
95
96    osg::ShapeDrawable* shape = new osg::ShapeDrawable(new osg::Sphere(center,characterSize*0.2f));
97    shape->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON);
98    geode->addDrawable(shape);
99
100    osg::Group* rootNode = new osg::Group;
101    rootNode->addChild(geode);
102
103    osg::Material* front = new osg::Material;
104    front->setAlpha(osg::Material::FRONT_AND_BACK,1);
105    front->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(0.2,0.2,0.2,1.0));
106    front->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(.0,.0,1.0,1.0));
107    rootNode->getOrCreateStateSet()->setAttributeAndModes(front);
108   
109   
110    return rootNode;   
111}
112
113int main(int, char**)
114{
115        osgViewer::Viewer viewer;
116
117    osg::Vec3 center(0.0f,0.0f,0.0f);
118    float radius = 1.0f;
119       
120        osg::Group* root = new osg::Group;
121        root->addChild(create3DText(center, radius));
122
123        viewer.setSceneData(root);
124        viewer.setCameraManipulator(new osgGA::TrackballManipulator());
125        viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
126
127    viewer.addEventHandler(new osgViewer::ThreadingHandler);
128    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
129    viewer.addEventHandler(new osgViewer::StatsHandler);
130
131
132        viewer.run();
133       
134        return 0;
135}
136
Note: See TracBrowser for help on using the browser.