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

Revision 5905, 7.3 kB (checked in by robert, 7 years ago)

Moved osgautotransform across to osgViewer

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 <osgViewer/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
28#include <iostream>
29
30osg::Node* createLabel(const osg::Vec3& pos, float size, const std::string& label, osgText::Text::AxisAlignment axisAlignment)
31{
32    osg::Geode* geode = new osg::Geode();
33   
34    std::string timesFont("fonts/arial.ttf");
35
36    {
37        osgText::Text* text = new  osgText::Text;
38        geode->addDrawable( text );
39
40        text->setFont(timesFont);
41        text->setPosition(pos);
42        text->setCharacterSize(size);
43        text->setAxisAlignment(axisAlignment);
44        text->setAlignment(osgText::Text::CENTER_CENTER);
45        text->setText(label);
46       
47    }
48   
49    return geode;   
50}
51
52
53osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label)
54{
55    osg::Geode* geode = new osg::Geode();
56   
57    std::string timesFont("fonts/arial.ttf");
58
59    {
60        osgText::Text* text = new  osgText::Text;
61        geode->addDrawable( text );
62
63        text->setFont(timesFont);
64        text->setPosition(pos);
65        text->setFontResolution(40,40);
66        text->setCharacterSize(size);
67        text->setAlignment(osgText::Text::CENTER_CENTER);
68        text->setAutoRotateToScreen(true);
69        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
70        text->setText(label);
71       
72    }
73   
74    return geode;   
75}
76
77osg::Node* createAxis(const osg::Vec3& s, const osg::Vec3& e, int numReps, osg::AutoTransform::AutoRotateMode autoRotateMode, osgText::Text::AxisAlignment axisAlignment, const std::string& str)
78{
79    osg::Group* group = new osg::Group;
80
81    osg::Vec3 dv = e-s;
82    dv /= float(numReps-1);
83
84    osg::Vec3 pos = s;
85
86    bool useAuto = true;
87    if (useAuto)
88    {
89        osg::Vec3Array* vertices = new osg::Vec3Array;
90
91        for(int i=0;i<numReps;++i)
92        {
93            osg::AutoTransform* at = new osg::AutoTransform;
94            at->setPosition(pos);
95            at->setAutoRotateMode(autoRotateMode);
96            at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),dv.length()*0.2f,str, axisAlignment));
97            vertices->push_back(pos);
98            pos += dv;
99
100
101            group->addChild(at);
102        }
103
104        osg::Vec4Array* colors = new osg::Vec4Array;
105        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
106
107        osg::Geometry* geom = new osg::Geometry;
108        geom->setVertexArray(vertices);
109        geom->setColorArray(colors);
110        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
111
112        osg::Geode* geode = new osg::Geode;
113        geode->addDrawable(geom);
114
115        group->addChild(geode);
116    }
117    else
118    {
119        osg::Vec3Array* vertices = new osg::Vec3Array;
120
121        for(int i=0;i<numReps;++i)
122        {
123            group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,str));
124            vertices->push_back(pos);
125            pos += dv;
126
127
128        }
129
130        osg::Vec4Array* colors = new osg::Vec4Array;
131        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
132
133        osg::Geometry* geom = new osg::Geometry;
134        geom->setVertexArray(vertices);
135        geom->setColorArray(colors);
136        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
137
138        osg::Geode* geode = new osg::Geode;
139        geode->addDrawable(geom);
140
141        group->addChild(geode);
142    }
143   
144    return group;
145}
146
147osg::Node* createScene()
148{
149    osg::Group* root = new osg::Group;
150   
151//    int numReps = 3333;
152    int numReps = 10;
153    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"));
154    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"));
155    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"));   
156   
157    return root;
158}
159
160int main( int argc, char **argv )
161{
162
163    // use an ArgumentParser object to manage the program arguments.
164    osg::ArgumentParser arguments(&argc,argv);
165   
166    // set up the usage document, in case we need to print out how to use this program.
167    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
168    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
169    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
170   
171
172    // construct the viewer.
173    osgViewer::Viewer viewer;
174
175    // if user request help write it out to cout.
176    if (arguments.read("-h") || arguments.read("--help"))
177    {
178        arguments.getApplicationUsage()->write(std::cout);
179        return 1;
180    }
181
182    // any option left unread are converted into errors to write out later.
183    arguments.reportRemainingOptionsAsUnrecognized();
184
185    // report any errors if they have occured when parsing the program aguments.
186    if (arguments.errors())
187    {
188        arguments.writeErrorMessages(std::cout);
189        return 1;
190    }
191   
192
193    // set the scene to render
194    viewer.setSceneData(createScene());
195   
196    return viewer.run();
197
198
199//     osg::Node* obj = new osg::Node;
200//     cout << "obj = "<<obj<<endl;
201//     cout << "sizeof(obj) = "<<sizeof(*obj)<<endl;
202//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
203//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
204//     cout << "vtbl = "<<*((unsigned int*)obj)<<endl;
205//     cout << "next = "<<*((unsigned int*)obj+1)<<endl;
206//
207//     osg::Node* obj2 = new osg::Node;
208//     obj2->ref();
209//     obj2->ref();
210//     obj2->ref();
211//     cout << "obj = "<<obj2<<endl;
212//     cout << "sizeof(obj) = "<<sizeof(*obj)<<endl;
213//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
214//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
215//     cout << "vtbl = "<<*((unsigned int*)obj2)<<endl;
216//     cout << "next = "<<*((unsigned int*)obj2+1)<<endl;
217//
218//     osg::Group* obj3 = new osg::Group;
219//     cout << "obj = "<<obj3<<endl;
220//     cout << "sizeof(obj) = "<<sizeof(*obj3)<<endl;
221//     cout << "sizeof(osg::Object) = "<<sizeof(osg::Object)<<endl;
222//     cout << "sizeof(osg::Referenced) = "<<sizeof(osg::Referenced)<<endl;
223//     cout << "vtbl = "<<*((unsigned int*)obj3)<<endl;
224//     cout << "next = "<<*((unsigned int*)obj3+1)<<endl;
225}
Note: See TracBrowser for help on using the browser.