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

Revision 7968, 6.7 kB (checked in by robert, 7 years ago)

To osg::AutoTransform? added support for MinimumScale?, MaximumScale? and AutoScaleTransitionWidth? parameters
and a new scheme for computing the scaling when using autoscale that introduces smooth
transitions to the scaling of the subgraph so that it looks more natural.

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