root/OpenSceneGraph/trunk/examples/osgautotransform/osgautotransform.cpp @ 7967

Revision 7967, 6.5 kB (checked in by robert, 6 years ago)

Added example code showing how to set the minimum and maximum scales

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgautotransform.
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#include <osgUtil/Optimizer>
20#include <osgDB/ReadFile>
21#include <osgViewer/Viewer>
22
23#include <osg/Material>
24#include <osg/Geode>
25#include <osg/BlendFunc>
26#include <osg/Depth>
27#include <osg/Projection>
28#include <osg/AutoTransform>
29#include <osg/Geometry>
30
31#include <osgText/Text>
32
33#include <iostream>
34
35osg::Node* createLabel(const osg::Vec3& pos, float size, const std::string& label, osgText::Text::AxisAlignment axisAlignment)
36{
37    osg::Geode* geode = new osg::Geode();
38   
39    std::string timesFont("fonts/arial.ttf");
40
41    {
42        osgText::Text* text = new  osgText::Text;
43        geode->addDrawable( text );
44
45        text->setFont(timesFont);
46        text->setPosition(pos);
47        text->setCharacterSize(size);
48        text->setAxisAlignment(axisAlignment);
49        text->setAlignment(osgText::Text::CENTER_CENTER);
50        text->setText(label);
51       
52    }
53   
54    return geode;   
55}
56
57
58osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label)
59{
60    osg::Geode* geode = new osg::Geode();
61   
62    std::string timesFont("fonts/arial.ttf");
63
64    {
65        osgText::Text* text = new  osgText::Text;
66        geode->addDrawable( text );
67
68        text->setFont(timesFont);
69        text->setPosition(pos);
70        text->setFontResolution(40,40);
71        text->setCharacterSize(size);
72        text->setAlignment(osgText::Text::CENTER_CENTER);
73        text->setAutoRotateToScreen(true);
74        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
75        text->setText(label);
76       
77    }
78   
79    return geode;   
80}
81
82osg::Node* createAxis(const osg::Vec3& s, const osg::Vec3& e, int numReps, osg::AutoTransform::AutoRotateMode autoRotateMode, osgText::Text::AxisAlignment axisAlignment, const std::string& str)
83{
84    osg::Group* group = new osg::Group;
85
86    osg::Vec3 dv = e-s;
87    dv /= float(numReps-1);
88
89    osg::Vec3 pos = s;
90
91    bool useAuto = true;
92    if (useAuto)
93    {
94        osg::Vec3Array* vertices = new osg::Vec3Array;
95
96        for(int i=0;i<numReps;++i)
97        {
98            osg::AutoTransform* at = new osg::AutoTransform;
99            at->setPosition(pos);
100            at->setAutoRotateMode(autoRotateMode);
101            at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),dv.length()*0.2f,str, axisAlignment));
102            vertices->push_back(pos);
103            pos += dv;
104
105
106            group->addChild(at);
107        }
108
109        osg::Vec4Array* colors = new osg::Vec4Array;
110        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
111
112        osg::Geometry* geom = new osg::Geometry;
113        geom->setVertexArray(vertices);
114        geom->setColorArray(colors);
115        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
116
117        osg::Geode* geode = new osg::Geode;
118        geode->addDrawable(geom);
119
120        group->addChild(geode);
121    }
122    else
123    {
124        osg::Vec3Array* vertices = new osg::Vec3Array;
125
126        for(int i=0;i<numReps;++i)
127        {
128            group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,str));
129            vertices->push_back(pos);
130            pos += dv;
131
132
133        }
134
135        osg::Vec4Array* colors = new osg::Vec4Array;
136        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
137
138        osg::Geometry* geom = new osg::Geometry;
139        geom->setVertexArray(vertices);
140        geom->setColorArray(colors);
141        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
142
143        osg::Geode* geode = new osg::Geode;
144        geode->addDrawable(geom);
145
146        group->addChild(geode);
147    }
148   
149    return group;
150}
151
152osg::Node* createAutoScale(const osg::Vec3& position, float characterSize, const std::string& message, float minScale=0.0, float maxScale=FLT_MAX)
153{
154    std::string timesFont("fonts/arial.ttf");
155
156    osgText::Text* text = new osgText::Text;
157    text->setCharacterSize(characterSize);
158    text->setText(message);
159    text->setFont(timesFont);
160    text->setAlignment(osgText::Text::CENTER_CENTER);
161
162    osg::Geode* geode = new osg::Geode;
163    geode->addDrawable(text);
164    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
165
166    osg::AutoTransform* at = new osg::AutoTransform;
167    at->addChild(geode);
168   
169    at->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
170    at->setAutoScaleToScreen(true);
171    at->setMinimumScale(minScale);
172    at->setMaximumScale(maxScale);
173    at->setPosition(position);
174   
175    return at;
176}
177
178osg::Node* createScene()
179{
180    osg::Group* root = new osg::Group;
181   
182//    int numReps = 3333;
183    int numReps = 10;
184    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(1000.0,0.0,0.0),numReps,osg::AutoTransform::ROTATE_TO_CAMERA,osgText::Text::XY_PLANE, "ROTATE_TO_CAMERA"));
185    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,1000.0,0.0),numReps,osg::AutoTransform::ROTATE_TO_SCREEN,osgText::Text::XY_PLANE, "ROTATE_TO_SCREEN"));
186    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,0.0,1000.0),numReps,osg::AutoTransform::NO_ROTATION,osgText::Text::XZ_PLANE, "NO_ROTATION"));   
187
188    root->addChild(createAutoScale(osg::Vec3(500.0,500.0,500.0), 25.0, "AutoScale with no min, max limits"));
189    root->addChild(createAutoScale(osg::Vec3(500.0,500.0,300.0), 25.0, "AutoScale with minScale = 1, maxScale = 2.0 ", 1, 2.0));
190    root->addChild(createAutoScale(osg::Vec3(500.0,500.0,700.0), 25.0, "AutoScale with minScale = 0.0, maxScale = 5.0 ", 0.0, 5.0));
191    return root;
192}
193
194int main(int, char**)
195{
196    // construct the viewer.
197    osgViewer::Viewer viewer;
198
199    // set the scene to render
200    viewer.setSceneData(createScene());
201   
202    // run the viewers frame loop
203    return viewer.run();
204}
Note: See TracBrowser for help on using the browser.