| 1 | |
|---|
| 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 | |
|---|
| 25 | |
|---|
| 26 | osg::Geode* morphGeom(osg::Vec3Array* coords, |
|---|
| 27 | osg::Vec3Array* normals, |
|---|
| 28 | osg::Vec2Array* texCoords, |
|---|
| 29 | osg::Image* image, |
|---|
| 30 | osg::TexMat* texMat) |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 74 | osg::CullFace* cull = new osg::CullFace; |
|---|
| 75 | state->setAttributeAndModes(cull, osg::StateAttribute::OFF); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 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 | |
|---|
| 90 | |
|---|
| 91 | int main(int argc, char** argv) |
|---|
| 92 | { |
|---|
| 93 | |
|---|
| 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 | |
|---|
| 102 | osg::Vec3Array* normals = new osg::Vec3Array(1); |
|---|
| 103 | (*normals)[0].set( 0.0f, 1.0f, 0.0f ); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 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 | |
|---|
| 114 | osg::MpegImageStream* mpeg = NULL; |
|---|
| 115 | if (argc > 1) { |
|---|
| 116 | mpeg = new osg::MpegImageStream(argv[1]); |
|---|
| 117 | mpeg->start(); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | osg::TexMat* texMat = new osg::TexMat; |
|---|
| 121 | texMat->setMatrix(osg::Matrix::scale(mpeg->s(),-mpeg->t(),1.0f)*osg::Matrix::translate(0.0f,mpeg->t(),0.0f)); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | osg::Geode* geode = morphGeom(coords, |
|---|
| 126 | normals, texCoords, mpeg, texMat); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 137 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); |
|---|
| 138 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 139 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | osgProducer::Viewer viewer(arguments); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 153 | { |
|---|
| 154 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 155 | return 1; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | if (arguments.errors()) |
|---|
| 160 | { |
|---|
| 161 | arguments.writeErrorMessages(std::cout); |
|---|
| 162 | return 1; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | if (arguments.argc()<=1) |
|---|
| 166 | { |
|---|
| 167 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 168 | return 1; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | if (arguments.errors()) |
|---|
| 177 | { |
|---|
| 178 | arguments.writeErrorMessages(std::cout); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | viewer.setSceneData(geode); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | viewer.realize(); |
|---|
| 186 | |
|---|
| 187 | while( !viewer.done() ) |
|---|
| 188 | { |
|---|
| 189 | |
|---|
| 190 | viewer.sync(); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | viewer.update(); |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | viewer.frame(); |
|---|
| 201 | |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | viewer.sync(); |
|---|
| 206 | |
|---|
| 207 | return 0; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | } |
|---|