root/OpenSceneGraph/trunk/examples/osgmovie/osgmovie.cpp @ 2693

Revision 2693, 5.8 kB (checked in by robert, 10 years ago)

Added osgmovie example, derived from Ulrich Hertlien's original videotex
example, and brought up to date.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// -*-c++-*-
2
3#include <osgProducer/Viewer>
4
5#include <osgDB/ReadFile>
6
7#include <osg/Geode>
8#include <osg/Geometry>
9#include <osg/StateSet>
10#include <osg/Material>
11#include <osg/Texture2D>
12#include <osg/TextureRectangle>
13#include <osg/TexMat>
14#include <osg/CullFace>
15
16#include <osgGA/TrackballManipulator>
17
18#include <math.h>
19
20#include "MpegImageStream.h"
21
22
23/*
24 * Create morphed textured geometry
25 */
26osg::Geode* morphGeom(osg::Vec3Array* coords,
27                      osg::Vec3Array* normals,
28                      osg::Vec2Array* texCoords,
29                      osg::Image* image,
30                      osg::TexMat* texMat)
31{
32    /*
33     * GeoSet
34     */
35    osg::Geometry* gset = new osg::Geometry();
36
37    gset->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,4));
38
39    gset->setVertexArray(coords);
40
41    gset->setNormalArray(normals);
42    gset->setNormalBinding(osg::Geometry::BIND_OVERALL);
43
44    gset->setTexCoordArray(0,texCoords);
45
46    /*
47     * StateSet
48     */
49    osg::StateSet* state = new osg::StateSet;
50
51    osg::Material* mtl = new osg::Material();
52    osg::Vec4 white( 1.0f, 1.0f, 1.0f, 1.0f );
53    mtl->setEmission( osg::Material::FRONT_AND_BACK, white );
54    mtl->setAmbient( osg::Material::FRONT_AND_BACK, white );
55    mtl->setDiffuse( osg::Material::FRONT_AND_BACK, white );
56    mtl->setSpecular( osg::Material::FRONT_AND_BACK, white );
57    state->setAttribute(mtl);
58
59    //osg::Texture2D* tex = new osg::Texture2D;
60    osg::TextureRectangle* tex = new osg::TextureRectangle;
61    if (!image) {
62        image = osgDB::readImageFile("lz.rgb");
63    }
64    tex->setImage(image);
65    tex->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
66    tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
67    tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
68    state->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON);
69
70    if (texMat)
71        state->setTextureAttributeAndModes(0, texMat, osg::StateAttribute::ON);
72
73    // don't cull faces
74    osg::CullFace* cull = new osg::CullFace;
75    state->setAttributeAndModes(cull, osg::StateAttribute::OFF);
76
77    /*
78     * Geode
79     */
80    osg::Geode* geode = new osg::Geode;
81    geode->setStateSet( state );
82    geode->addDrawable( gset );
83
84    return geode;
85}
86
87
88/*
89 * Main
90 */
91int main(int argc, char** argv)
92{
93    // coordinates
94    osg::Vec3Array* coords = new osg::Vec3Array(4);
95    (*coords)[0].set( -1.0f, 0.0f, -1.0f );
96    (*coords)[1].set(  1.0f, 0.0f, -1.0f );
97    (*coords)[2].set(  1.0f, 0.0f,  1.0f );
98    (*coords)[3].set( -1.0f, 0.0f,  1.0f );
99
100
101    // normals
102    osg::Vec3Array* normals = new osg::Vec3Array(1);
103    (*normals)[0].set( 0.0f, 1.0f, 0.0f );
104
105    // texture coordinates
106    osg::Vec2Array* texCoords = new osg::Vec2Array(4);
107    (*texCoords)[0].set(0.0f, 0.0f);
108    (*texCoords)[1].set(1.0f, 0.0f);
109    (*texCoords)[2].set(1.0f, 1.0f);
110    (*texCoords)[3].set(0.0f, 1.0f);
111
112
113    // open MpegImageStream
114    osg::MpegImageStream* mpeg = NULL;
115    osg::TexMat* texMat = NULL;
116    if (argc > 1) {
117        mpeg = new osg::MpegImageStream(argv[1]);
118        mpeg->start();
119        texMat = (osg::TexMat*) mpeg->getTexMat();
120    }
121
122    // Create morphed geometry
123    osg::Geode* geode = morphGeom(coords,
124                                  normals, texCoords, mpeg, texMat);
125    //coordMorph.addGeode(geode);
126
127
128
129
130    // use an ArgumentParser object to manage the program arguments.
131    osg::ArgumentParser arguments(&argc,argv);
132   
133    // set up the usage document, in case we need to print out how to use this program.
134    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
135    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
136    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
137    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
138   
139
140    // construct the viewer.
141    osgProducer::Viewer viewer(arguments);
142
143    // set up the value with sensible default event handlers.
144    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
145
146    // get details on keyboard and mouse bindings used by the viewer.
147    viewer.getUsage(*arguments.getApplicationUsage());
148
149    // if user request help write it out to cout.
150    if (arguments.read("-h") || arguments.read("--help"))
151    {
152        arguments.getApplicationUsage()->write(std::cout);
153        return 1;
154    }
155
156    // report any errors if they have occured when parsing the program aguments.
157    if (arguments.errors())
158    {
159        arguments.writeErrorMessages(std::cout);
160        return 1;
161    }
162   
163    if (arguments.argc()<=1)
164    {
165        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
166        return 1;
167    }
168
169
170    // any option left unread are converted into errors to write out later.
171    arguments.reportRemainingOptionsAsUnrecognized();
172
173    // report any errors if they have occured when parsing the program aguments.
174    if (arguments.errors())
175    {
176        arguments.writeErrorMessages(std::cout);
177    }
178
179    // set the scene to render
180    viewer.setSceneData(geode);
181
182    // create the windows and run the threads.
183    viewer.realize();
184
185    while( !viewer.done() )
186    {
187        // wait for all cull and draw threads to complete.
188        viewer.sync();
189       
190        // update the geoemtry
191        //coordMorph.update(viewer.getFrameStamp()->getReferenceTime());
192
193        // update the scene by traversing it with the the update visitor which will
194        // call all node update callbacks and animations.
195        viewer.update();
196         
197        // fire off the cull and draw traversals of the scene.
198        viewer.frame();
199       
200    }
201   
202    // wait for all cull and draw threads to complete before exit.
203    viewer.sync();
204
205    return 0;
206
207
208}
Note: See TracBrowser for help on using the browser.