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

Revision 2022, 7.6 kB (checked in by robert, 11 years ago)

Added support for texture object manager, which provides an automatic mechansim
for reusing deleted textures.

  • 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)
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 = false;
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->setAutoRotateToScreen(true);
93            at->setAutoScaleToScreen(true);
94            at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),40.0f,"Test 2"));
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,"Test 2"));
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));
152    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,1000.0,0.0),numReps));
153    root->addChild(createAxis(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,0.0,1000.0),numReps));   
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.