| 1 | |
|---|
| 2 | #include <osgParticle/Particle> |
|---|
| 3 | #include <osgParticle/Interpolator> |
|---|
| 4 | #include <osgParticle/range> |
|---|
| 5 | |
|---|
| 6 | #include <iostream> |
|---|
| 7 | #include <string> |
|---|
| 8 | |
|---|
| 9 | #include <osg/Vec3> |
|---|
| 10 | #include <osg/Vec4> |
|---|
| 11 | #include <osg/Notify> |
|---|
| 12 | |
|---|
| 13 | #include <osgDB/Input> |
|---|
| 14 | #include <osgDB/Output> |
|---|
| 15 | |
|---|
| 16 | bool read_particle(osgDB::Input &fr, osgParticle::Particle &P) |
|---|
| 17 | { |
|---|
| 18 | if (fr[0].matchString("{")) { |
|---|
| 19 | ++fr; |
|---|
| 20 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 21 | bool itAdvanced = true; |
|---|
| 22 | while (!fr.eof() && fr[0].getNoNestedBrackets() >= entry && itAdvanced) { |
|---|
| 23 | itAdvanced = false; |
|---|
| 24 | if (fr[0].matchWord("shape")) { |
|---|
| 25 | const char *ptstr = fr[1].getStr(); |
|---|
| 26 | if (ptstr) { |
|---|
| 27 | if (std::string(ptstr) == "QUAD") { |
|---|
| 28 | P.setShape(osgParticle::Particle::QUAD); |
|---|
| 29 | } else if (std::string(ptstr) == "HEXAGON") { |
|---|
| 30 | P.setShape(osgParticle::Particle::HEXAGON); |
|---|
| 31 | } else if (std::string(ptstr) == "POINT") { |
|---|
| 32 | P.setShape(osgParticle::Particle::POINT); |
|---|
| 33 | } else if (std::string(ptstr) == "QUAD_TRIANGLESTRIP") { |
|---|
| 34 | P.setShape(osgParticle::Particle::QUAD_TRIANGLESTRIP); |
|---|
| 35 | } else if (std::string(ptstr) == "LINE") { |
|---|
| 36 | P.setShape(osgParticle::Particle::LINE); |
|---|
| 37 | } else if (std::string(ptstr) == "USER") { |
|---|
| 38 | P.setShape(osgParticle::Particle::USER); |
|---|
| 39 | } else { |
|---|
| 40 | osg::notify(osg::WARN) << "Particle reader warning: invalid shape: " << ptstr << std::endl; |
|---|
| 41 | } |
|---|
| 42 | fr += 2; |
|---|
| 43 | itAdvanced = true; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | if (fr[0].matchWord("lifeTime")) { |
|---|
| 47 | float lt; |
|---|
| 48 | if (fr[1].getFloat(lt)) { |
|---|
| 49 | P.setLifeTime(lt); |
|---|
| 50 | fr += 2; |
|---|
| 51 | itAdvanced = true; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | if (fr[0].matchWord("sizeRange")) { |
|---|
| 55 | osgParticle::rangef r; |
|---|
| 56 | if (fr[1].getFloat(r.minimum) && fr[2].getFloat(r.maximum)) { |
|---|
| 57 | P.setSizeRange(r); |
|---|
| 58 | fr += 3; |
|---|
| 59 | itAdvanced = true; |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | if (fr[0].matchWord("alphaRange")) { |
|---|
| 63 | osgParticle::rangef r; |
|---|
| 64 | if (fr[1].getFloat(r.minimum) && fr[2].getFloat(r.maximum)) { |
|---|
| 65 | P.setAlphaRange(r); |
|---|
| 66 | fr += 3; |
|---|
| 67 | itAdvanced = true; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | if (fr[0].matchWord("colorRange")) { |
|---|
| 71 | osgParticle::rangev4 r; |
|---|
| 72 | if (fr[1].getFloat(r.minimum.x()) && fr[2].getFloat(r.minimum.y()) && fr[3].getFloat(r.minimum.z()) && fr[4].getFloat(r.minimum.w()) && |
|---|
| 73 | fr[5].getFloat(r.maximum.x()) && fr[6].getFloat(r.maximum.y()) && fr[7].getFloat(r.maximum.z()) && fr[8].getFloat(r.maximum.w())) { |
|---|
| 74 | P.setColorRange(r); |
|---|
| 75 | fr += 9; |
|---|
| 76 | itAdvanced = true; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | if (fr[0].matchWord("position")) { |
|---|
| 80 | osg::Vec3 v; |
|---|
| 81 | if (fr[1].getFloat(v.x()) && fr[2].getFloat(v.y()) && fr[3].getFloat(v.z())) { |
|---|
| 82 | P.setPosition(v); |
|---|
| 83 | fr += 4; |
|---|
| 84 | itAdvanced = true; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | if (fr[0].matchWord("velocity")) { |
|---|
| 88 | osg::Vec3 v; |
|---|
| 89 | if (fr[1].getFloat(v.x()) && fr[2].getFloat(v.y()) && fr[3].getFloat(v.z())) { |
|---|
| 90 | P.setVelocity(v); |
|---|
| 91 | fr += 4; |
|---|
| 92 | itAdvanced = true; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | if (fr[0].matchWord("angle")) { |
|---|
| 96 | osg::Vec3 v; |
|---|
| 97 | if (fr[1].getFloat(v.x()) && fr[2].getFloat(v.y()) && fr[3].getFloat(v.z())) { |
|---|
| 98 | P.setAngle(v); |
|---|
| 99 | fr += 4; |
|---|
| 100 | itAdvanced = true; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | if (fr[0].matchWord("angularVelocity")) { |
|---|
| 104 | osg::Vec3 v; |
|---|
| 105 | if (fr[1].getFloat(v.x()) && fr[2].getFloat(v.y()) && fr[3].getFloat(v.z())) { |
|---|
| 106 | P.setAngularVelocity(v); |
|---|
| 107 | fr += 4; |
|---|
| 108 | itAdvanced = true; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | if (fr[0].matchWord("radius")) { |
|---|
| 112 | float f; |
|---|
| 113 | if (fr[1].getFloat(f)) { |
|---|
| 114 | P.setRadius(f); |
|---|
| 115 | fr += 2; |
|---|
| 116 | itAdvanced = true; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | if (fr[0].matchWord("mass")) { |
|---|
| 121 | float f; |
|---|
| 122 | if (fr[1].getFloat(f)) { |
|---|
| 123 | P.setMass(f); |
|---|
| 124 | fr += 2; |
|---|
| 125 | itAdvanced = true; |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | if (fr[0].matchWord("textureTile")) { |
|---|
| 130 | int sTile,tTile,numTiles; |
|---|
| 131 | if (fr[1].getInt(sTile) && fr[2].getInt(tTile) && fr[3].getInt(numTiles)) { |
|---|
| 132 | P.setTextureTile(sTile,tTile,numTiles); |
|---|
| 133 | fr += 4; |
|---|
| 134 | itAdvanced = true; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | if (fr[0].matchWord("sizeInterpolator") && fr[1].matchString("{")) { |
|---|
| 142 | fr += 2; |
|---|
| 143 | itAdvanced = true; |
|---|
| 144 | osgParticle::Interpolator *ip = dynamic_cast<osgParticle::Interpolator *>(fr.readObject()); |
|---|
| 145 | if (ip) { |
|---|
| 146 | P.setSizeInterpolator(ip); |
|---|
| 147 | } |
|---|
| 148 | ++fr; |
|---|
| 149 | } |
|---|
| 150 | if (fr[0].matchWord("alphaInterpolator") && fr[1].matchString("{")) { |
|---|
| 151 | fr += 2; |
|---|
| 152 | itAdvanced = true; |
|---|
| 153 | osgParticle::Interpolator *ip = dynamic_cast<osgParticle::Interpolator *>(fr.readObject()); |
|---|
| 154 | if (ip) { |
|---|
| 155 | P.setAlphaInterpolator(ip); |
|---|
| 156 | } |
|---|
| 157 | ++fr; |
|---|
| 158 | } |
|---|
| 159 | if (fr[0].matchWord("colorInterpolator") && fr[1].matchString("{")) { |
|---|
| 160 | fr += 2; |
|---|
| 161 | itAdvanced = true; |
|---|
| 162 | osgParticle::Interpolator *ip = dynamic_cast<osgParticle::Interpolator *>(fr.readObject()); |
|---|
| 163 | if (ip) { |
|---|
| 164 | P.setColorInterpolator(ip); |
|---|
| 165 | } |
|---|
| 166 | ++fr; |
|---|
| 167 | } |
|---|
| 168 | if (fr[0].matchWord("drawable") && fr[1].matchString("{")) { |
|---|
| 169 | fr += 2; |
|---|
| 170 | itAdvanced = true; |
|---|
| 171 | osg::Drawable *drawable = dynamic_cast<osg::Drawable *>(fr.readObject()); |
|---|
| 172 | if (drawable) { |
|---|
| 173 | P.setDrawable(drawable); |
|---|
| 174 | } |
|---|
| 175 | ++fr; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | return true; |
|---|
| 179 | } |
|---|
| 180 | return false; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | void write_particle(const osgParticle::Particle &P, osgDB::Output &fw) |
|---|
| 184 | { |
|---|
| 185 | fw << "{" << std::endl; |
|---|
| 186 | fw.moveIn(); |
|---|
| 187 | |
|---|
| 188 | fw.indent() << "shape "; |
|---|
| 189 | switch (P.getShape()) |
|---|
| 190 | { |
|---|
| 191 | case osgParticle::Particle::POINT: fw << "POINT" << std::endl; break; |
|---|
| 192 | case osgParticle::Particle::HEXAGON: fw << "HEXAGON" << std::endl; break; |
|---|
| 193 | case osgParticle::Particle::QUAD_TRIANGLESTRIP: fw << "QUAD_TRIANGLESTRIP" << std::endl; break; |
|---|
| 194 | case osgParticle::Particle::QUAD: fw << "QUAD" << std::endl; break; |
|---|
| 195 | case osgParticle::Particle::LINE: fw << "LINE" << std::endl; break; |
|---|
| 196 | case osgParticle::Particle::USER: |
|---|
| 197 | default: fw << "USER" << std::endl; break; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | fw.indent() << "lifeTime " << P.getLifeTime() << std::endl; |
|---|
| 201 | |
|---|
| 202 | osgParticle::rangef rf = P.getSizeRange(); |
|---|
| 203 | fw.indent() << "sizeRange " << rf.minimum << " " << rf.maximum << std::endl; |
|---|
| 204 | |
|---|
| 205 | rf = P.getAlphaRange(); |
|---|
| 206 | fw.indent() << "alphaRange " << rf.minimum << " " << rf.maximum << std::endl; |
|---|
| 207 | |
|---|
| 208 | osgParticle::rangev4 rv4 = P.getColorRange(); |
|---|
| 209 | fw.indent() << "colorRange "; |
|---|
| 210 | fw << rv4.minimum.x() << " " << rv4.minimum.y() << " " << rv4.minimum.z() << " " << rv4.minimum.w() << " "; |
|---|
| 211 | fw << rv4.maximum.x() << " " << rv4.maximum.y() << " " << rv4.maximum.z() << " " << rv4.maximum.w() << std::endl; |
|---|
| 212 | |
|---|
| 213 | osg::Vec3 v = P.getPosition(); |
|---|
| 214 | fw.indent() << "position "; |
|---|
| 215 | fw << v.x() << " " << v.y() << " " << v.z() << std::endl; |
|---|
| 216 | |
|---|
| 217 | v = P.getVelocity(); |
|---|
| 218 | fw.indent() << "velocity "; |
|---|
| 219 | fw << v.x() << " " << v.y() << " " << v.z() << std::endl; |
|---|
| 220 | |
|---|
| 221 | v = P.getAngle(); |
|---|
| 222 | fw.indent() << "angle "; |
|---|
| 223 | fw << v.x() << " " << v.y() << " " << v.z() << std::endl; |
|---|
| 224 | |
|---|
| 225 | v = P.getAngularVelocity(); |
|---|
| 226 | fw.indent() << "angularVelocity "; |
|---|
| 227 | fw << v.x() << " " << v.y() << " " << v.z() << std::endl; |
|---|
| 228 | |
|---|
| 229 | fw.indent() << "radius " << P.getRadius() << std::endl; |
|---|
| 230 | fw.indent() << "mass " << P.getMass() << std::endl; |
|---|
| 231 | fw.indent() << "textureTile " << P.getTileS() << " " << P.getTileT() << " " << P.getNumTiles() << std::endl; |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | fw.indent() << "sizeInterpolator {" << std::endl; |
|---|
| 236 | fw.moveIn(); |
|---|
| 237 | fw.writeObject(*P.getSizeInterpolator()); |
|---|
| 238 | fw.moveOut(); |
|---|
| 239 | fw.indent() << "}" << std::endl; |
|---|
| 240 | |
|---|
| 241 | fw.indent() << "alphaInterpolator {" << std::endl; |
|---|
| 242 | fw.moveIn(); |
|---|
| 243 | fw.writeObject(*P.getAlphaInterpolator()); |
|---|
| 244 | fw.moveOut(); |
|---|
| 245 | fw.indent() << "}" << std::endl; |
|---|
| 246 | |
|---|
| 247 | fw.indent() << "colorInterpolator {" << std::endl; |
|---|
| 248 | fw.moveIn(); |
|---|
| 249 | fw.writeObject(*P.getColorInterpolator()); |
|---|
| 250 | fw.moveOut(); |
|---|
| 251 | fw.indent() << "}" << std::endl; |
|---|
| 252 | |
|---|
| 253 | if ( P.getDrawable() != NULL ) { |
|---|
| 254 | fw.indent() << "drawable {" << std::endl; |
|---|
| 255 | fw.moveIn(); |
|---|
| 256 | fw.writeObject(*P.getDrawable()); |
|---|
| 257 | fw.moveOut(); |
|---|
| 258 | fw.indent() << "}" << std::endl; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | fw.moveOut(); |
|---|
| 262 | fw.indent() << "}" << std::endl; |
|---|
| 263 | } |
|---|