root/OpenSceneGraph/trunk/examples/osgtexture2D/osgtexture2D.cpp @ 6051

Revision 6051, 23.1 kB (checked in by robert, 8 years ago)

Added osg::FrameStamp::set/getSimulationTime().

Added setting of osg_SimulationTime and osg_DeltaSimulationTime to the uniforms set by SceneView?

Added frame(double simulationTime) and advance(double simulationTime) parameters to
osgViewer::SimpleViewer?, Vewer and CompositeViewer?.

Updated various examples and Nodes to use SimulationTime? where appropriate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Node>
2#include <osg/Geometry>
3#include <osg/Notify>
4#include <osg/MatrixTransform>
5#include <osg/Texture2D>
6#include <osg/DrawPixels>
7#include <osg/PolygonOffset>
8#include <osg/Geode>
9
10#include <osgDB/Registry>
11#include <osgDB/ReadFile>
12
13#include <osgText/Text>
14
15#include <osgViewer/Viewer>
16
17///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18//
19// filter wall and animation callback.
20//
21
22class FilterCallback : public osg::NodeCallback
23{
24public:
25
26    FilterCallback(osg::Texture2D* texture,osgText::Text* text,double delay=1.0):
27        _texture(texture),
28        _text(text),
29        _delay(delay),
30        _currPos(0),
31        _prevTime(0.0)
32    {
33        // start with a mip mapped mode to ensure that
34        _minFilterList.push_back(osg::Texture2D::LINEAR_MIPMAP_LINEAR);
35        _magFilterList.push_back(osg::Texture2D::LINEAR);
36        _textList.push_back("Tri-linear mip mapping (default filtering)\nsetFilter(MIN_FILTER,LINEAR_MIP_LINEAR)\nsetFilter(MAG_FILTER,LINEAR)");
37
38        _minFilterList.push_back(osg::Texture2D::NEAREST);
39        _magFilterList.push_back(osg::Texture2D::NEAREST);
40        _textList.push_back("Nearest filtering\nsetFilter(MIN_FILTER,NEAREST)\nsetFilter(MAG_FILTER,NEAREST)");
41       
42        _minFilterList.push_back(osg::Texture2D::LINEAR);
43        _magFilterList.push_back(osg::Texture2D::LINEAR);
44        _textList.push_back("Linear filtering\nsetFilter(MIN_FILTER,LINEAR)\nsetFilter(MAG_FILTER,LINEAR)");
45       
46        _minFilterList.push_back(osg::Texture2D::NEAREST_MIPMAP_NEAREST);
47        _magFilterList.push_back(osg::Texture2D::LINEAR);
48        _textList.push_back("nearest mip mapping (default filtering)\nsetFilter(MIN_FILTER,)\nsetFilter(MAG_FILTER,LINEAR)");
49
50        _minFilterList.push_back(osg::Texture2D::LINEAR_MIPMAP_NEAREST);
51        _magFilterList.push_back(osg::Texture2D::LINEAR);
52        _textList.push_back("bi-linear mip mapping\nsetFilter(MIN_FILTER,LINEAR_MIPMAP_NEAREST)\nsetFilter(MAG_FILTER,LINEAR)");
53
54        _minFilterList.push_back(osg::Texture2D::NEAREST_MIPMAP_LINEAR);
55        _magFilterList.push_back(osg::Texture2D::LINEAR);
56        _textList.push_back("bi-linear mip mapping\nsetFilter(MIN_FILTER,NEAREST_MIPMAP_LINEAR)\nsetFilter(MAG_FILTER,LINEAR)");
57
58       
59        setValues();
60    }
61
62    virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
63    {
64        if (nv->getFrameStamp())
65        {
66            double currTime = nv->getFrameStamp()->getSimulationTime();
67            if (currTime-_prevTime>_delay)
68            {
69                // update filter modes and text.
70                setValues();
71               
72                // advance the current positon, wrap round if required.
73                _currPos++;
74                if (_currPos>=_minFilterList.size()) _currPos=0;                   
75               
76                // record time
77                _prevTime = currTime;
78            }
79        }
80    }
81   
82    void setValues()
83    {
84        _texture->setFilter(osg::Texture2D::MIN_FILTER,_minFilterList[_currPos]);
85        _texture->setFilter(osg::Texture2D::MAG_FILTER,_magFilterList[_currPos]);
86
87        _text->setText(_textList[_currPos]);
88    }
89
90protected:
91
92    typedef std::vector<osg::Texture2D::FilterMode> FilterList;
93    typedef std::vector<std::string>                TextList;
94
95    osg::ref_ptr<osg::Texture2D>    _texture;
96    osg::ref_ptr<osgText::Text>     _text;
97    double                          _delay;
98   
99    FilterList                      _minFilterList;
100    FilterList                      _magFilterList;
101    TextList                        _textList;
102   
103    unsigned int                    _currPos;
104    double                          _prevTime;
105   
106};
107
108osg::Node* createFilterWall(osg::BoundingBox& bb,const std::string& filename)
109{
110    osg::Group* group = new osg::Group;
111   
112    // left hand side of bounding box.
113    osg::Vec3 top_left(bb.xMin(),bb.yMin(),bb.zMax());
114    osg::Vec3 bottom_left(bb.xMin(),bb.yMin(),bb.zMin());
115    osg::Vec3 bottom_right(bb.xMin(),bb.yMax(),bb.zMin());
116    osg::Vec3 top_right(bb.xMin(),bb.yMax(),bb.zMax());
117    osg::Vec3 center(bb.xMin(),(bb.yMin()+bb.yMax())*0.5f,(bb.zMin()+bb.zMax())*0.5f);   
118    float height = bb.zMax()-bb.zMin();
119   
120    // create the geometry for the wall.
121    osg::Geometry* geom = new osg::Geometry;
122   
123    osg::Vec3Array* vertices = new osg::Vec3Array(4);
124    (*vertices)[0] = top_left;
125    (*vertices)[1] = bottom_left;
126    (*vertices)[2] = bottom_right;
127    (*vertices)[3] = top_right;
128    geom->setVertexArray(vertices);
129   
130    osg::Vec2Array* texcoords = new osg::Vec2Array(4);
131    (*texcoords)[0].set(0.0f,1.0f);
132    (*texcoords)[1].set(0.0f,0.0f);
133    (*texcoords)[2].set(1.0f,0.0f);
134    (*texcoords)[3].set(1.0f,1.0f);
135    geom->setTexCoordArray(0,texcoords);
136
137    osg::Vec3Array* normals = new osg::Vec3Array(1);
138    (*normals)[0].set(1.0f,0.0f,0.0f);
139    geom->setNormalArray(normals);
140    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
141   
142    osg::Vec4Array* colors = new osg::Vec4Array(1);
143    (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
144    geom->setColorArray(colors);
145    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
146
147    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
148   
149    osg::Geode* geom_geode = new osg::Geode;
150    geom_geode->addDrawable(geom);
151    group->addChild(geom_geode);
152   
153   
154    // set up the texture state.   
155    osg::Texture2D* texture = new osg::Texture2D;
156    texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
157    texture->setImage(osgDB::readImageFile(filename));
158   
159    osg::StateSet* stateset = geom->getOrCreateStateSet();
160    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
161   
162    // create the text label.
163   
164    osgText::Text* text = new osgText::Text;
165    text->setFont("fonts/arial.ttf");
166    text->setPosition(center);
167    text->setCharacterSize(height*0.03f);
168    text->setAlignment(osgText::Text::CENTER_CENTER);
169    text->setAxisAlignment(osgText::Text::YZ_PLANE);
170   
171    osg::Geode* text_geode = new osg::Geode;
172    text_geode->addDrawable(text);
173   
174    osg::StateSet* text_stateset = text_geode->getOrCreateStateSet();
175    text_stateset->setAttributeAndModes(new osg::PolygonOffset(-1.0f,-1.0f),osg::StateAttribute::ON);
176   
177    group->addChild(text_geode);
178
179    // set the update callback to cycle through the various min and mag filter modes.
180    group->setUpdateCallback(new FilterCallback(texture,text));
181   
182    return group;
183   
184}
185
186///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
187//
188// anisotropic wall and animation callback.
189//
190
191class AnisotropicCallback : public osg::NodeCallback
192{
193public:
194
195    AnisotropicCallback(osg::Texture2D* texture,osgText::Text* text,double delay=1.0):
196        _texture(texture),
197        _text(text),
198        _delay(delay),
199        _currPos(0),
200        _prevTime(0.0)
201    {
202
203        _maxAnisotropyList.push_back(1.0f);
204        _textList.push_back("No anisotropic filtering (default)\nsetMaxAnisotropy(1.0f)");
205       
206        _maxAnisotropyList.push_back(2.0f);
207        _textList.push_back("Anisotropic filtering\nsetMaxAnisotropy(2.0f)");
208       
209        _maxAnisotropyList.push_back(4.0f);
210        _textList.push_back("Anisotropic filtering\nsetMaxAnisotropy(4.0f)");
211       
212        _maxAnisotropyList.push_back(8.0f);
213        _textList.push_back("Anisotropic filtering\nsetMaxAnisotropy(8.0f)");
214       
215        _maxAnisotropyList.push_back(16.0f);
216        _textList.push_back("Higest quality anisotropic filtering\nsetMaxAnisotropy(16.0f)");
217
218        setValues();
219    }
220
221    virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
222    {
223        if (nv->getFrameStamp())
224        {
225            double currTime = nv->getFrameStamp()->getSimulationTime();
226            if (currTime-_prevTime>_delay)
227            {
228                // update filter modes and text.
229                setValues();
230               
231                // advance the current positon, wrap round if required.
232                _currPos++;
233                if (_currPos>=_maxAnisotropyList.size()) _currPos=0;                   
234               
235                // record time
236                _prevTime = currTime;
237            }
238        }
239    }
240   
241    void setValues()
242    {
243        _texture->setMaxAnisotropy(_maxAnisotropyList[_currPos]);
244
245        _text->setText(_textList[_currPos]);
246    }
247
248protected:
249
250    typedef std::vector<float>          AnisotropyList;
251    typedef std::vector<std::string>    TextList;
252
253    osg::ref_ptr<osg::Texture2D>    _texture;
254    osg::ref_ptr<osgText::Text>     _text;
255    double                          _delay;
256   
257    AnisotropyList                  _maxAnisotropyList;
258    TextList                        _textList;
259   
260    unsigned int                    _currPos;
261    double                          _prevTime;
262   
263};
264
265osg::Node* createAnisotripicWall(osg::BoundingBox& bb,const std::string& filename)
266{
267    osg::Group* group = new osg::Group;
268   
269    // left hand side of bounding box.
270    osg::Vec3 top_left(bb.xMin(),bb.yMax(),bb.zMin());
271    osg::Vec3 bottom_left(bb.xMin(),bb.yMin(),bb.zMin());
272    osg::Vec3 bottom_right(bb.xMax(),bb.yMin(),bb.zMin());
273    osg::Vec3 top_right(bb.xMax(),bb.yMax(),bb.zMin());
274    osg::Vec3 center((bb.xMin()+bb.xMax())*0.5f,(bb.yMin()+bb.yMax())*0.5f,bb.zMin());   
275    float height = bb.yMax()-bb.yMin();
276   
277    // create the geometry for the wall.
278    osg::Geometry* geom = new osg::Geometry;
279   
280    osg::Vec3Array* vertices = new osg::Vec3Array(4);
281    (*vertices)[0] = top_left;
282    (*vertices)[1] = bottom_left;
283    (*vertices)[2] = bottom_right;
284    (*vertices)[3] = top_right;
285    geom->setVertexArray(vertices);
286   
287    osg::Vec2Array* texcoords = new osg::Vec2Array(4);
288    (*texcoords)[0].set(0.0f,1.0f);
289    (*texcoords)[1].set(0.0f,0.0f);
290    (*texcoords)[2].set(1.0f,0.0f);
291    (*texcoords)[3].set(1.0f,1.0f);
292    geom->setTexCoordArray(0,texcoords);
293
294    osg::Vec3Array* normals = new osg::Vec3Array(1);
295    (*normals)[0].set(0.0f,0.0f,1.0f);
296    geom->setNormalArray(normals);
297    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
298   
299    osg::Vec4Array* colors = new osg::Vec4Array(1);
300    (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
301    geom->setColorArray(colors);
302    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
303
304    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
305   
306    osg::Geode* geom_geode = new osg::Geode;
307    geom_geode->addDrawable(geom);
308    group->addChild(geom_geode);
309   
310   
311    // set up the texture state.   
312    osg::Texture2D* texture = new osg::Texture2D;
313    texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
314    texture->setImage(osgDB::readImageFile(filename));
315   
316    osg::StateSet* stateset = geom->getOrCreateStateSet();
317    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
318   
319    // create the text label.
320   
321    osgText::Text* text = new osgText::Text;
322    text->setFont("fonts/arial.ttf");
323    text->setPosition(center);
324    text->setCharacterSize(height*0.03f);
325    text->setColor(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
326    text->setAlignment(osgText::Text::CENTER_CENTER);
327    text->setAxisAlignment(osgText::Text::XY_PLANE);
328   
329    osg::Geode* text_geode = new osg::Geode;
330    text_geode->addDrawable(text);
331   
332    osg::StateSet* text_stateset = text_geode->getOrCreateStateSet();
333    text_stateset->setAttributeAndModes(new osg::PolygonOffset(-1.0f,-1.0f),osg::StateAttribute::ON);
334   
335    group->addChild(text_geode);
336
337    // set the update callback to cycle through the various min and mag filter modes.
338    group->setUpdateCallback(new AnisotropicCallback(texture,text));
339   
340    return group;
341   
342}
343
344///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
345//
346// wrap wall and animation callback.
347//
348class WrapCallback : public osg::NodeCallback
349{
350public:
351
352    WrapCallback(osg::Texture2D* texture,osgText::Text* text,double delay=1.0):
353        _texture(texture),
354        _text(text),
355        _delay(delay),
356        _currPos(0),
357        _prevTime(0.0)
358    {
359
360        _wrapList.push_back(osg::Texture2D::CLAMP);
361        _textList.push_back("Default tex coord clamp\nsetWrap(WRAP_S,CLAMP)");
362       
363        _wrapList.push_back(osg::Texture2D::CLAMP_TO_EDGE);
364        _textList.push_back("Clamp to edge extension\nsetWrap(WRAP_S,CLAMP_TO_EDGE)");
365       
366        _wrapList.push_back(osg::Texture2D::CLAMP_TO_BORDER);
367        _textList.push_back("Clamp to border color extension\nsetWrap(WRAP_S,CLAMP_TO_BORDER)");
368       
369        _wrapList.push_back(osg::Texture2D::REPEAT);
370        _textList.push_back("Repeat wrap\nsetWrap(WRAP_S,REPEAT)");
371       
372        _wrapList.push_back(osg::Texture2D::MIRROR);
373        _textList.push_back("Mirror wrap extension\nsetWrap(WRAP_S,MIRROR)");
374
375        setValues();
376    }
377
378    virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
379    {
380        if (nv->getFrameStamp())
381        {
382            double currTime = nv->getFrameStamp()->getSimulationTime();
383            if (currTime-_prevTime>_delay)
384            {
385                // update filter modes and text.
386                setValues();
387               
388                // advance the current positon, wrap round if required.
389                _currPos++;
390                if (_currPos>=_wrapList.size()) _currPos=0;                   
391               
392                // record time
393                _prevTime = currTime;
394            }
395        }
396    }
397   
398    void setValues()
399    {
400        _texture->setWrap(osg::Texture2D::WRAP_S,_wrapList[_currPos]);
401        _texture->setWrap(osg::Texture2D::WRAP_T,_wrapList[_currPos]);
402
403        _text->setText(_textList[_currPos]);
404    }
405
406protected:
407
408    typedef std::vector<osg::Texture2D::WrapMode> WrapList;
409    typedef std::vector<std::string>              TextList;
410
411    osg::ref_ptr<osg::Texture2D>    _texture;
412    osg::ref_ptr<osgText::Text>     _text;
413    double                          _delay;
414   
415    WrapList                        _wrapList;
416    TextList                        _textList;
417   
418    unsigned int                    _currPos;
419    double                          _prevTime;
420   
421};
422
423osg::Node* createWrapWall(osg::BoundingBox& bb,const std::string& filename)
424{
425    osg::Group* group = new osg::Group;
426   
427    // left hand side of bounding box.
428    osg::Vec3 top_left(bb.xMax(),bb.yMax(),bb.zMax());
429    osg::Vec3 bottom_left(bb.xMax(),bb.yMax(),bb.zMin());
430    osg::Vec3 bottom_right(bb.xMax(),bb.yMin(),bb.zMin());
431    osg::Vec3 top_right(bb.xMax(),bb.yMin(),bb.zMax());
432    osg::Vec3 center(bb.xMax(),(bb.yMin()+bb.yMax())*0.5f,(bb.zMin()+bb.zMax())*0.5f);   
433    float height = bb.zMax()-bb.zMin();
434   
435    // create the geometry for the wall.
436    osg::Geometry* geom = new osg::Geometry;
437   
438    osg::Vec3Array* vertices = new osg::Vec3Array(4);
439    (*vertices)[0] = top_left;
440    (*vertices)[1] = bottom_left;
441    (*vertices)[2] = bottom_right;
442    (*vertices)[3] = top_right;
443    geom->setVertexArray(vertices);
444   
445    osg::Vec2Array* texcoords = new osg::Vec2Array(4);
446    (*texcoords)[0].set(-1.0f,2.0f);
447    (*texcoords)[1].set(-1.0f,-1.0f);
448    (*texcoords)[2].set(2.0f,-1.0f);
449    (*texcoords)[3].set(2.0f,2.0f);
450    geom->setTexCoordArray(0,texcoords);
451
452    osg::Vec3Array* normals = new osg::Vec3Array(1);
453    (*normals)[0].set(-1.0f,0.0f,0.0f);
454    geom->setNormalArray(normals);
455    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
456   
457    osg::Vec4Array* colors = new osg::Vec4Array(1);
458    (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
459    geom->setColorArray(colors);
460    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
461
462    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
463   
464    osg::Geode* geom_geode = new osg::Geode;
465    geom_geode->addDrawable(geom);
466    group->addChild(geom_geode);
467   
468   
469    // set up the texture state.   
470    osg::Texture2D* texture = new osg::Texture2D;
471    texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
472    texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,0.5f)); // only used when wrap is set to CLAMP_TO_BORDER
473    texture->setImage(osgDB::readImageFile(filename));
474   
475    osg::StateSet* stateset = geom->getOrCreateStateSet();
476    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
477    stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
478    stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
479   
480    // create the text label.
481   
482    osgText::Text* text = new osgText::Text;
483    text->setFont("fonts/arial.ttf");
484    text->setPosition(center);
485    text->setCharacterSize(height*0.03f);
486    text->setAlignment(osgText::Text::CENTER_CENTER);
487    text->setAxisAlignment(osgText::Text::YZ_PLANE);
488   
489    osg::Geode* text_geode = new osg::Geode;
490    text_geode->addDrawable(text);
491   
492    osg::StateSet* text_stateset = text_geode->getOrCreateStateSet();
493    text_stateset->setAttributeAndModes(new osg::PolygonOffset(-1.0f,-1.0f),osg::StateAttribute::ON);
494   
495    group->addChild(text_geode);
496
497    // set the update callback to cycle through the various min and mag filter modes.
498    group->setUpdateCallback(new WrapCallback(texture,text));
499   
500    return group;
501   
502}
503
504///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
505//
506// sublooad wall and animation callback.
507//
508
509class ImageUpdateCallback : public osg::NodeCallback
510{
511public:
512
513    ImageUpdateCallback(osg::Texture2D* texture,osgText::Text* text,double delay=1.0):
514        _texture(texture),
515        _text(text),
516        _delay(delay),
517        _currPos(0),
518        _prevTime(0.0)
519    {
520
521       
522        _imageList.push_back(osgDB::readImageFile("Images/dog_left_eye.jpg"));
523        _textList.push_back("Subloaded Image 1 - dog_left_eye.jpg");
524       
525        _imageList.push_back(osgDB::readImageFile("Images/dog_right_eye.jpg"));
526        _textList.push_back("Subloaded Image 2 - dog_right_eye.jpg");
527       
528        setValues();
529    }
530
531    virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
532    {
533        if (nv->getFrameStamp())
534        {
535            double currTime = nv->getFrameStamp()->getSimulationTime();
536            if (currTime-_prevTime>_delay)
537            {
538                // update filter modes and text.
539                setValues();
540               
541                // advance the current positon, wrap round if required.
542                _currPos++;
543                if (_currPos>=_imageList.size()) _currPos=0;                   
544               
545                // record time
546                _prevTime = currTime;
547            }
548        }
549    }
550   
551    void setValues()
552    {
553        // Note, as long as the images are the same dimensions subloading will be used
554        // to update the textures.  If dimensions change then the texture objects have
555        // to be deleted and re-recreated.
556        //
557        // The load/subload happens during the draw traversal so doesn't happen on
558        // the setImage which just updates internal pointers and modifed flags.
559
560        _texture->setImage(_imageList[_currPos].get());
561       
562        //_texture->dirtyTextureObject();
563
564        _text->setText(_textList[_currPos]);
565    }
566
567protected:
568
569    typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
570    typedef std::vector<std::string>                TextList;
571
572    osg::ref_ptr<osg::Texture2D>    _texture;
573    osg::ref_ptr<osgText::Text>     _text;
574    double                          _delay;
575   
576    ImageList                       _imageList;
577    TextList                        _textList;
578   
579    unsigned int                    _currPos;
580    double                          _prevTime;
581   
582};
583
584osg::Node* createSubloadWall(osg::BoundingBox& bb)
585{
586    osg::Group* group = new osg::Group;
587   
588    // left hand side of bounding box.
589    osg::Vec3 top_left(bb.xMin(),bb.yMax(),bb.zMax());
590    osg::Vec3 bottom_left(bb.xMin(),bb.yMax(),bb.zMin());
591    osg::Vec3 bottom_right(bb.xMax(),bb.yMax(),bb.zMin());
592    osg::Vec3 top_right(bb.xMax(),bb.yMax(),bb.zMax());
593    osg::Vec3 center((bb.xMax()+bb.xMin())*0.5f,bb.yMax(),(bb.zMin()+bb.zMax())*0.5f);   
594    float height = bb.zMax()-bb.zMin();
595   
596    // create the geometry for the wall.
597    osg::Geometry* geom = new osg::Geometry;
598   
599    osg::Vec3Array* vertices = new osg::Vec3Array(4);
600    (*vertices)[0] = top_left;
601    (*vertices)[1] = bottom_left;
602    (*vertices)[2] = bottom_right;
603    (*vertices)[3] = top_right;
604    geom->setVertexArray(vertices);
605   
606    osg::Vec2Array* texcoords = new osg::Vec2Array(4);
607    (*texcoords)[0].set(0.0f,1.0f);
608    (*texcoords)[1].set(0.0f,0.0f);
609    (*texcoords)[2].set(1.0f,0.0f);
610    (*texcoords)[3].set(1.0f,1.0f);
611    geom->setTexCoordArray(0,texcoords);
612
613    osg::Vec3Array* normals = new osg::Vec3Array(1);
614    (*normals)[0].set(0.0f,-1.0f,0.0f);
615    geom->setNormalArray(normals);
616    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
617   
618    osg::Vec4Array* colors = new osg::Vec4Array(1);
619    (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
620    geom->setColorArray(colors);
621    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
622
623    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
624   
625    osg::Geode* geom_geode = new osg::Geode;
626    geom_geode->addDrawable(geom);
627    group->addChild(geom_geode);
628   
629   
630    // set up the texture state.   
631    osg::Texture2D* texture = new osg::Texture2D;
632    texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
633    texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
634    texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
635   
636    osg::StateSet* stateset = geom->getOrCreateStateSet();
637    stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
638   
639    // create the text label.
640   
641    osgText::Text* text = new osgText::Text;
642    text->setFont("fonts/arial.ttf");
643    text->setPosition(center);
644    text->setCharacterSize(height*0.03f);
645    text->setAlignment(osgText::Text::CENTER_CENTER);
646    text->setAxisAlignment(osgText::Text::XZ_PLANE);
647   
648    osg::Geode* text_geode = new osg::Geode;
649    text_geode->addDrawable(text);
650   
651    osg::StateSet* text_stateset = text_geode->getOrCreateStateSet();
652    text_stateset->setAttributeAndModes(new osg::PolygonOffset(-1.0f,-1.0f),osg::StateAttribute::ON);
653   
654    group->addChild(text_geode);
655
656    // set the update callback to cycle through the various min and mag filter modes.
657    group->setUpdateCallback(new ImageUpdateCallback(texture,text));
658   
659    return group;
660   
661}
662
663
664osg::Node* createModel()
665{
666
667    // create the root node which will hold the model.
668    osg::Group* root = new osg::Group();
669   
670    // turn off lighting
671    root->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
672
673    osg::BoundingBox bb(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f);
674
675    root->addChild(createFilterWall(bb,"Images/lz.rgb"));
676    root->addChild(createAnisotripicWall(bb,"Images/primitives.gif"));
677    root->addChild(createWrapWall(bb,"Images/tree0.rgba"));
678    root->addChild(createSubloadWall(bb));
679   
680    return root;
681}
682
683int main(int , char **)
684{
685    // construct the viewer.
686    osgViewer::Viewer viewer;
687
688    // add model to viewer.
689    viewer.setSceneData( createModel() );
690
691    return viewer.run();
692}
Note: See TracBrowser for help on using the browser.