root/OpenSceneGraph/trunk/src/osgWrappers/serializers/osgParticle/Particle.cpp @ 11020

Revision 11020, 4.6 kB (checked in by robert, 4 years ago)

From Wang Rui, added osgParticle serializers

Line 
1#include <osgParticle/Particle>
2#include <osgDB/ObjectWrapper>
3#include <osgDB/InputStream>
4#include <osgDB/OutputStream>
5
6BEGIN_USER_TABLE( Shape, osgParticle::Particle );
7    ADD_USER_VALUE( POINT );
8    ADD_USER_VALUE( QUAD );
9    ADD_USER_VALUE( QUAD_TRIANGLESTRIP );
10    ADD_USER_VALUE( HEXAGON );
11    ADD_USER_VALUE( LINE );
12END_USER_TABLE()
13
14USER_READ_FUNC( Shape, readShapeValue )
15USER_WRITE_FUNC( Shape, writeShapeValue )
16
17bool readParticle( osgDB::InputStream& is, osgParticle::Particle& p )
18{
19    is >> osgDB::BEGIN_BRACKET;
20   
21    is >> osgDB::PROPERTY("Shape");
22    p.setShape( static_cast<osgParticle::Particle::Shape>(readShapeValue(is)) );
23   
24    double lifeTime; is >> osgDB::PROPERTY("LifeTime") >> lifeTime;
25    p.setLifeTime( lifeTime );
26   
27    float min, max; osg::Vec4d minV, maxV;
28    is >> osgDB::PROPERTY("SizeRange") >> min >> max; p.setSizeRange( osgParticle::rangef(min, max) );
29    is >> osgDB::PROPERTY("AlphaRange") >> min >> max; p.setAlphaRange( osgParticle::rangef(min, max) );
30    is >> osgDB::PROPERTY("ColorRange") >> minV >> maxV; p.setColorRange( osgParticle::rangev4(minV, maxV) );
31   
32    bool hasInterpolator = false;
33    is >> osgDB::PROPERTY("SizeInterpolator") >> hasInterpolator;
34    if ( hasInterpolator )
35    {
36        is >> osgDB::BEGIN_BRACKET;
37        p.setSizeInterpolator( static_cast<osgParticle::Interpolator*>(is.readObject()) );
38        is >> osgDB::END_BRACKET;
39    }
40    is >> osgDB::PROPERTY("AlphaInterpolator") >> hasInterpolator;
41    if ( hasInterpolator )
42    {
43        is >> osgDB::BEGIN_BRACKET;
44        p.setAlphaInterpolator( static_cast<osgParticle::Interpolator*>(is.readObject()) );
45        is >> osgDB::END_BRACKET;
46    }
47    is >> osgDB::PROPERTY("ColorInterpolator") >> hasInterpolator;
48    if ( hasInterpolator )
49    {
50        is >> osgDB::BEGIN_BRACKET;
51        p.setColorInterpolator( static_cast<osgParticle::Interpolator*>(is.readObject()) );
52        is >> osgDB::END_BRACKET;
53    }
54   
55    float radius; is >> osgDB::PROPERTY("Radius") >> radius;
56    float mass; is >> osgDB::PROPERTY("Mass") >> mass;
57    osg::Vec3d pos; is >> osgDB::PROPERTY("Position") >> pos;
58    osg::Vec3d vel; is >> osgDB::PROPERTY("Velocity") >> vel;
59    osg::Vec3d angle; is >> osgDB::PROPERTY("Angle") >> angle;
60    osg::Vec3d angleV; is >> osgDB::PROPERTY("AngularVelocity") >> angleV;
61    int s, t, num; is >> osgDB::PROPERTY("TextureTile") >> s >> t >> num;
62   
63    p.setRadius( radius );
64    p.setMass( mass );
65    p.setPosition( pos );
66    p.setVelocity( vel );
67    p.setAngle( angle );
68    p.setAngularVelocity( angleV );
69    p.setTextureTile( s, t, num );
70   
71    is >> osgDB::END_BRACKET;
72    return true;
73}
74
75bool writeParticle( osgDB::OutputStream& os, const osgParticle::Particle& p )
76{
77    os << osgDB::BEGIN_BRACKET << std::endl;
78   
79    os << osgDB::PROPERTY("Shape"); writeShapeValue( os, (int)p.getShape() ); os << std::endl;
80   
81    os << osgDB::PROPERTY("LifeTime") << p.getLifeTime() << std::endl;
82    os << osgDB::PROPERTY("SizeRange") << p.getSizeRange().minimum << p.getSizeRange().maximum << std::endl;
83    os << osgDB::PROPERTY("AlphaRange") << p.getAlphaRange().minimum << p.getAlphaRange().maximum << std::endl;
84    os << osgDB::PROPERTY("ColorRange") << osg::Vec4d(p.getColorRange().minimum)
85                                        << osg::Vec4d(p.getColorRange().maximum) << std::endl;
86   
87    os << osgDB::PROPERTY("SizeInterpolator") << (p.getSizeInterpolator()!=NULL);
88    if ( p.getSizeInterpolator()!=NULL )
89        os << osgDB::BEGIN_BRACKET << std::endl << p.getSizeInterpolator() << osgDB::END_BRACKET << std::endl;
90    os << osgDB::PROPERTY("AlphaInterpolator") << (p.getAlphaInterpolator()!=NULL);
91    if ( p.getAlphaInterpolator()!=NULL )
92        os << osgDB::BEGIN_BRACKET << std::endl << p.getAlphaInterpolator() << osgDB::END_BRACKET << std::endl;
93    os << osgDB::PROPERTY("ColorInterpolator") << (p.getColorInterpolator()!=NULL);
94    if ( p.getColorInterpolator()!=NULL )
95        os << osgDB::BEGIN_BRACKET << std::endl << p.getColorInterpolator() << osgDB::END_BRACKET << std::endl;
96   
97    os << osgDB::PROPERTY("Radius") << p.getRadius() << std::endl;
98    os << osgDB::PROPERTY("Mass") << p.getMass() << std::endl;
99    os << osgDB::PROPERTY("Position") << osg::Vec3d(p.getPosition()) << std::endl;
100    os << osgDB::PROPERTY("Velocity") << osg::Vec3d(p.getVelocity()) << std::endl;
101    os << osgDB::PROPERTY("Angle") << osg::Vec3d(p.getAngle()) << std::endl;
102    os << osgDB::PROPERTY("AngularVelocity") << osg::Vec3d(p.getAngularVelocity()) << std::endl;
103    os << osgDB::PROPERTY("TextureTile") << p.getTileS() << p.getTileT() << p.getNumTiles() << std::endl;
104   
105    os << osgDB::END_BRACKET << std::endl;
106    return true;
107}
Note: See TracBrowser for help on using the browser.