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

Revision 13574, 6.6 kB (checked in by robert, 6 days ago)

Fixed comment

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgautotransform.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgUtil/Optimizer>
20#include <osgDB/ReadFile>
21#include <osgViewer/Viewer>
22
23#include <osg/Material>
24#include <osg/Geode>
25#include <osg/BlendFunc>
26#include <osg/Depth>
27#include <osg/Projection>
28#include <osg/AutoTransform>
29#include <osg/Geometry>
30
31#include <osgDB/WriteFile>
32
33#include <osgText/Text>
34
35#include <iostream>
36
37osg::Node* createLabel(const osg::Vec3& pos, float size, const std::string& label, osgText::Text::AxisAlignment axisAlignment)
38{
39    osg::Geode* geode = new osg::Geode();
40
41    std::string timesFont("fonts/arial.ttf");
42
43    {
44        osgText::Text* text = new  osgText::Text;
45        geode->addDrawable( text );
46
47        text->setFont(timesFont);
48        text->setPosition(pos);
49        text->setCharacterSize(size);
50        text->setAxisAlignment(axisAlignment);
51        text->setAlignment(osgText::Text::CENTER_CENTER);
52        text->setText(label);
53
54    }
55
56    return geode;
57}
58
59
60osg::Node* createLabel3(const osg::Vec3& pos, float size, const std::string& label)
61{
62    osg::Geode* geode = new osg::Geode();
63
64    std::string timesFont("fonts/arial.ttf");
65
66    {
67        osgText::Text* text = new  osgText::Text;
68        geode->addDrawable( text );
69
70        text->setFont(timesFont);
71        text->setPosition(pos);
72        text->setFontResolution(40,40);
73        text->setCharacterSize(size);
74        text->setAlignment(osgText::Text::CENTER_CENTER);
75        text->setAutoRotateToScreen(true);
76        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
77        text->setText(label);
78
79    }
80
81    return geode;
82}
83
84osg::Node* createAxis(const osg::Vec3& s, const osg::Vec3& e, int numReps, osg::AutoTransform::AutoRotateMode autoRotateMode, osgText::Text::AxisAlignment axisAlignment, const std::string& str)
85{
86    osg::Group* group = new osg::Group;
87
88    osg::Vec3 dv = e-s;
89    dv /= float(numReps-1);
90
91    osg::Vec3 pos = s;
92
93    bool useAuto = true;
94    if (useAuto)
95    {
96        osg::Vec3Array* vertices = new osg::Vec3Array;
97
98        for(int i=0;i<numReps;++i)
99        {
100            osg::AutoTransform* at = new osg::AutoTransform;
101            at->setPosition(pos);
102            at->setAutoRotateMode(autoRotateMode);
103            at->addChild(createLabel(osg::Vec3(0.0f,0.0f,0.0f),dv.length()*0.2f,str, axisAlignment));
104            vertices->push_back(pos);
105            pos += dv;
106
107
108            group->addChild(at);
109        }
110
111        osg::Vec4Array* colors = new osg::Vec4Array;
112        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
113
114        osg::Geometry* geom = new osg::Geometry;
115        geom->setVertexArray(vertices);
116        geom->setColorArray(colors, osg::Array::BIND_OVERALL);
117        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
118
119        osg::Geode* geode = new osg::Geode;
120        geode->addDrawable(geom);
121
122        group->addChild(geode);
123    }
124    else
125    {
126        osg::Vec3Array* vertices = new osg::Vec3Array;
127
128        for(int i=0;i<numReps;++i)
129        {
130            group->addChild(createLabel3(osg::Vec3(pos),dv.length()*0.5f,str));
131            vertices->push_back(pos);
132            pos += dv;
133
134
135        }
136
137        osg::Vec4Array* colors = new osg::Vec4Array;
138        colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
139
140        osg::Geometry* geom = new osg::Geometry;
141        geom->setVertexArray(vertices);
142        geom->setColorArray(colors, osg::Array::BIND_OVERALL);
143        geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP,0,vertices->size()));
144
145        osg::Geode* geode = new osg::Geode;
146        geode->addDrawable(geom);
147
148        group->addChild(geode);
149    }
150
151    return group;
152}
153
154osg::Node* createAutoScale(const osg::Vec3& position, float characterSize, const std::string& message, float minScale=0.0, float maxScale=FLT_MAX)
155{
156    std::string timesFont("fonts/arial.ttf");
157
158    osgText::Text* text = new osgText::Text;
159    text->setCharacterSize(characterSize);
160    text->setText(message);
161    text->setFont(timesFont);
162    text->setAlignment(osgText::Text::CENTER_CENTER);
163
164    osg::Geode* geode = new osg::Geode;
165    geode->addDrawable(text);
166    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
167
168    osg::AutoTransform* at = new osg::AutoTransform;
169    at->addChild(geode);
170
171    at->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
172    at->setAutoScaleToScreen(true);
173    at->setMinimumScale(minScale);
174    at->setMaximumScale(maxScale);
175    at->setPosition(position);
176
177    return at;
178}
179
180osg::Node* createScene()
181{
182    osg::Group* root = new osg::Group;
183
184//    int numReps = 3333;
185    int numReps = 10;
186    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"));
187    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"));
188    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"));
189
190    root->addChild(createAutoScale(osg::Vec3(500.0,500.0,500.0), 25.0, "AutoScale with no min, max limits"));
191    root->addChild(createAutoScale(osg::Vec3(500.0,500.0,300.0), 25.0, "AutoScale with minScale = 1, maxScale = 2.0 ", 1, 2.0));
192    root->addChild(createAutoScale(osg::Vec3(500.0,500.0,700.0), 25.0, "AutoScale with minScale = 0.0, maxScale = 5.0 ", 0.0, 5.0));
193    return root;
194}
195
196int main(int, char**)
197{
198    // construct the viewer.
199    osgViewer::Viewer viewer;
200
201    // set the scene to render
202    viewer.setSceneData(createScene());
203
204    // run the viewers frame loop
205    return viewer.run();
206}
Note: See TracBrowser for help on using the browser.