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

Revision 4717, 8.0 kB (checked in by robert, 9 years ago)

Changed the NO_ROTATION label so it is aligned with the XZ plane to make it easier to see.

  • 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, osgText::Text::AxisAlignment axisAlignment)
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->setAxisAlignment(axisAlignment);
42        text->setAlignment(osgText::Text::CENTER_CENTER);
43        text->setText(label);
44       
45    }
46   
47    return geode;   
48}
49
50
51osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label)
52{
53    osg::Geode* geode = new osg::Geode();
54   
55    std::string timesFont("fonts/arial.ttf");
56
57    {
58        osgText::Text* text = new  osgText::Text;
59        geode->addDrawable( text );
60
61        text->setFont(timesFont);
62        text->setPosition(pos);
63        text->setFontResolution(40,40);
64        text->setCharacterSize(size);
65        text->setAlignment(osgText::Text::CENTER_CENTER);
66        text->setAutoRotateToScreen(true);
67        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
68        text->setText(label);
69       
70    }
71   
72    return geode;   
73}
74
75osg::Node* createAxis(const osg::Vec3& s, const osg::Vec3& e, int numReps, osg::AutoTransform::AutoRotateMode autoRotateMode, osgText::Text::AxisAlignment axisAlignment, const std::string& str)
76{
77    osg::Group* group = new osg::Group;
78
79    osg::Vec3 dv = e-s;
80    dv /= float(numReps-1);
81
82    osg::Vec3 pos = s;
83
84    bool useAuto = true;
85    if (useAuto)
86    {
87        osg::Vec3Array* vertices = new osg::Vec3Array;
88
89        for(int i=0;i<numReps;++i)
90        {
91            osg::AutoTransform* at = new osg::AutoTransform;
92            at->setPosition(pos);
93            at->setAutoRotateMode(autoRotateMode);
94            at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),dv.length()*0.2f,str, axisAlignment));
95            vertices->push_back(pos);
96            pos += dv;
97
98
99            group->addChild(at);
100        }
101
102        osg::Vec4Array* colors = new osg::Vec4Array;
103        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
104
105        osg::Geometry* geom = new osg::Geometry;
106        geom->setVertexArray(vertices);
107        geom->setColorArray(colors);
108        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
109
110        osg::Geode* geode = new osg::Geode;
111        geode->addDrawable(geom);
112
113        group->addChild(geode);
114    }
115    else
116    {
117        osg::Vec3Array* vertices = new osg::Vec3Array;
118
119        for(int i=0;i<numReps;++i)
120        {
121            group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,str));
122            vertices->push_back(pos);
123            pos += dv;
124
125
126        }
127
128        osg::Vec4Array* colors = new osg::Vec4Array;
129        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
130
131        osg::Geometry* geom = new osg::Geometry;
132        geom->setVertexArray(vertices);
133        geom->setColorArray(colors);
134        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
135
136        osg::Geode* geode = new osg::Geode;
137        geode->addDrawable(geom);
138
139        group->addChild(geode);
140    }
141   
142    return group;
143}
144
145osg::Node* createScene()
146{
147    osg::Group* root = new osg::Group;
148   
149//    int numReps = 3333;
150    int numReps = 10;
151    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"));
152    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"));
153    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"));   
154   
155    return root;
156}
157
158int main( int argc, char **argv )
159{
160
161    // use an ArgumentParser object to manage the program arguments.
162    osg::ArgumentParser arguments(&argc,argv);
163   
164    // set up the usage document, in case we need to print out how to use this program.
165    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
166    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
167    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
168   
169
170    // construct the viewer.
171    osgProducer::Viewer viewer(arguments);
172
173    // set up the value with sensible default event handlers.
174    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
175
176    // get details on keyboard and mouse bindings used by the viewer.
177    viewer.getUsage(*arguments.getApplicationUsage());
178
179    // if user request help write it out to cout.
180    if (arguments.read("-h") || arguments.read("--help"))
181    {
182        arguments.getApplicationUsage()->write(std::cout);
183        return 1;
184    }
185
186    // any option left unread are converted into errors to write out later.
187    arguments.reportRemainingOptionsAsUnrecognized();
188
189    // report any errors if they have occured when parsing the program aguments.
190    if (arguments.errors())
191    {
192        arguments.writeErrorMessages(std::cout);
193        return 1;
194    }
195   
196
197    // set the scene to render
198    viewer.setSceneData(createScene());
199
200    // create the windows and run the threads.
201    viewer.realize();
202
203    while( !viewer.done() )
204    {
205        // wait for all cull and draw threads to complete.
206        viewer.sync();
207
208        // update the scene by traversing it with the the update visitor which will
209        // call all node update callbacks and animations.
210        viewer.update();
211         
212        // fire off the cull and draw traversals of the scene.
213        viewer.frame();
214       
215    }
216   
217    // wait for all cull and draw threads to complete before exit.
218    viewer.sync();
219   
220    return 0;
221
222
223//     osg::Node* obj = new osg::Node;
224//     cout << "obj = "<<obj<<endl;
225//     cout << "sizeof(obj) = "<<sizeof(*obj)<<endl;
226//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
227//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
228//     cout << "vtbl = "<<*((unsigned int*)obj)<<endl;
229//     cout << "next = "<<*((unsigned int*)obj+1)<<endl;
230//
231//     osg::Node* obj2 = new osg::Node;
232//     obj2->ref();
233//     obj2->ref();
234//     obj2->ref();
235//     cout << "obj = "<<obj2<<endl;
236//     cout << "sizeof(obj) = "<<sizeof(*obj)<<endl;
237//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
238//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
239//     cout << "vtbl = "<<*((unsigned int*)obj2)<<endl;
240//     cout << "next = "<<*((unsigned int*)obj2+1)<<endl;
241//
242//     osg::Group* obj3 = new osg::Group;
243//     cout << "obj = "<<obj3<<endl;
244//     cout << "sizeof(obj) = "<<sizeof(*obj3)<<endl;
245//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
246//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
247//     cout << "vtbl = "<<*((unsigned int*)obj3)<<endl;
248//     cout << "next = "<<*((unsigned int*)obj3+1)<<endl;
249}
Note: See TracBrowser for help on using the browser.