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

Revision 5910, 5.0 kB (checked in by robert, 8 years ago)

Simplified the osganimate and osgautotransform mains, removing most or all of the argument parser code.

  • 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, char**)
161{
162    // construct the viewer.
163    osgViewer::Viewer viewer;
164
165    // set the scene to render
166    viewer.setSceneData(createScene());
167   
168    // run the viewers frame loop
169    return viewer.run();
170}
Note: See TracBrowser for help on using the browser.