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

Revision 3245, 7.8 kB (checked in by robert, 10 years ago)

Added support for osg::AutoTransform::setAutoRotateMode(AutoRotateMode?) deprecating
the previous setAutoRotateToScreen(). Added support for ROTATE_TO_CAMERA mode.

Cleaned up the autotransform demo to use a sensible number of labels

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgUtil/Optimizer>
15#include <osgDB/ReadFile>
16#include <osgProducer/Viewer>
17
18#include <osg/Material>
19#include <osg/Geode>
20#include <osg/BlendFunc>
21#include <osg/Depth>
22#include <osg/Projection>
23#include <osg/AutoTransform>
24#include <osg/Geometry>
25
26#include <osgText/Text>
27
28osg::Node* createLabel(const osg::Vec3& pos, float size, const std::string& label)
29{
30    osg::Geode* geode = new osg::Geode();
31   
32    std::string timesFont("fonts/arial.ttf");
33
34    {
35        osgText::Text* text = new  osgText::Text;
36        geode->addDrawable( text );
37
38        text->setFont(timesFont);
39        text->setPosition(pos);
40        text->setCharacterSize(size);
41        text->setAlignment(osgText::Text::CENTER_CENTER);
42        text->setText(label);
43       
44    }
45   
46    return geode;   
47}
48
49
50osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label)
51{
52    osg::Geode* geode = new osg::Geode();
53   
54    std::string timesFont("fonts/arial.ttf");
55
56    {
57        osgText::Text* text = new  osgText::Text;
58        geode->addDrawable( text );
59
60        text->setFont(timesFont);
61        text->setPosition(pos);
62        text->setFontResolution(40,40);
63        text->setCharacterSize(size);
64        text->setAlignment(osgText::Text::CENTER_CENTER);
65        text->setAutoRotateToScreen(true);
66        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
67        text->setText(label);
68       
69    }
70   
71    return geode;   
72}
73
74osg::Node* createAxis(const osg::Vec3& s, const osg::Vec3& e, int numReps, osg::AutoTransform::AutoRotateMode autoRotateMode, const std::string& str)
75{
76    osg::Group* group = new osg::Group;
77
78    osg::Vec3 dv = e-s;
79    dv /= float(numReps-1);
80
81    osg::Vec3 pos = s;
82
83    bool useAuto = true;
84    if (useAuto)
85    {
86        osg::Vec3Array* vertices = new osg::Vec3Array;
87
88        for(int i=0;i<numReps;++i)
89        {
90            osg::AutoTransform* at = new osg::AutoTransform;
91            at->setPosition(pos);
92            at->setAutoRotateMode(autoRotateMode);
93            at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),dv.length()*0.2f,str));
94            vertices->push_back(pos);
95            pos += dv;
96
97
98            group->addChild(at);
99        }
100
101        osg::Vec4Array* colors = new osg::Vec4Array;
102        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
103
104        osg::Geometry* geom = new osg::Geometry;
105        geom->setVertexArray(vertices);
106        geom->setColorArray(colors);
107        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
108
109        osg::Geode* geode = new osg::Geode;
110        geode->addDrawable(geom);
111
112        group->addChild(geode);
113    }
114    else
115    {
116        osg::Vec3Array* vertices = new osg::Vec3Array;
117
118        for(int i=0;i<numReps;++i)
119        {
120            group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,str));
121            vertices->push_back(pos);
122            pos += dv;
123
124
125        }
126
127        osg::Vec4Array* colors = new osg::Vec4Array;
128        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
129
130        osg::Geometry* geom = new osg::Geometry;
131        geom->setVertexArray(vertices);
132        geom->setColorArray(colors);
133        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
134
135        osg::Geode* geode = new osg::Geode;
136        geode->addDrawable(geom);
137
138        group->addChild(geode);
139    }
140   
141    return group;
142}
143
144osg::Node* createScene()
145{
146    osg::Group* root = new osg::Group;
147   
148//    int numReps = 3333;
149    int numReps = 10;
150    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,"ROTATE_TO_CAMERA"));
151    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,"ROTATE_TO_SCREEN"));
152    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,0.0,1000.0),numReps,osg::AutoTransform::NO_ROTATION,"NO_ROTATION"));   
153   
154    return root;
155}
156
157int main( int argc, char **argv )
158{
159
160    // use an ArgumentParser object to manage the program arguments.
161    osg::ArgumentParser arguments(&argc,argv);
162   
163    // set up the usage document, in case we need to print out how to use this program.
164    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
165    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
166    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
167   
168
169    // construct the viewer.
170    osgProducer::Viewer viewer(arguments);
171
172    // set up the value with sensible default event handlers.
173    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
174
175    // get details on keyboard and mouse bindings used by the viewer.
176    viewer.getUsage(*arguments.getApplicationUsage());
177
178    // if user request help write it out to cout.
179    if (arguments.read("-h") || arguments.read("--help"))
180    {
181        arguments.getApplicationUsage()->write(std::cout);
182        return 1;
183    }
184
185    // any option left unread are converted into errors to write out later.
186    arguments.reportRemainingOptionsAsUnrecognized();
187
188    // report any errors if they have occured when parsing the program aguments.
189    if (arguments.errors())
190    {
191        arguments.writeErrorMessages(std::cout);
192        return 1;
193    }
194   
195
196    // set the scene to render
197    viewer.setSceneData(createScene());
198
199    // create the windows and run the threads.
200    viewer.realize();
201
202    while( !viewer.done() )
203    {
204        // wait for all cull and draw threads to complete.
205        viewer.sync();
206
207        // update the scene by traversing it with the the update visitor which will
208        // call all node update callbacks and animations.
209        viewer.update();
210         
211        // fire off the cull and draw traversals of the scene.
212        viewer.frame();
213       
214    }
215   
216    // wait for all cull and draw threads to complete before exit.
217    viewer.sync();
218   
219    return 0;
220
221
222//     osg::Node* obj = new osg::Node;
223//     cout << "obj = "<<obj<<endl;
224//     cout << "sizeof(obj) = "<<sizeof(*obj)<<endl;
225//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
226//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
227//     cout << "vtbl = "<<*((unsigned int*)obj)<<endl;
228//     cout << "next = "<<*((unsigned int*)obj+1)<<endl;
229//
230//     osg::Node* obj2 = new osg::Node;
231//     obj2->ref();
232//     obj2->ref();
233//     obj2->ref();
234//     cout << "obj = "<<obj2<<endl;
235//     cout << "sizeof(obj) = "<<sizeof(*obj)<<endl;
236//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
237//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
238//     cout << "vtbl = "<<*((unsigned int*)obj2)<<endl;
239//     cout << "next = "<<*((unsigned int*)obj2+1)<<endl;
240//
241//     osg::Group* obj3 = new osg::Group;
242//     cout << "obj = "<<obj3<<endl;
243//     cout << "sizeof(obj) = "<<sizeof(*obj3)<<endl;
244//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
245//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
246//     cout << "vtbl = "<<*((unsigned int*)obj3)<<endl;
247//     cout << "next = "<<*((unsigned int*)obj3+1)<<endl;
248}
Note: See TracBrowser for help on using the browser.