1 #include <osg/Notify>
2 #include <osg/io_utils>
3 
4 #include <osgDB/Registry>
5 #include "IO_LightPoint.h"
6 
7 using namespace osgSim;
8 
readLightPoint(LightPoint & lp,osgDB::Input & fr)9 bool readLightPoint(LightPoint & lp, osgDB::Input &fr)
10 {
11     if (fr.matchSequence("lightPoint {"))
12     {
13         fr += 2;
14         int entry = fr[0].getNoNestedBrackets();
15         bool itAdvanced = true;
16         while (!fr.eof() && fr[0].getNoNestedBrackets() >= entry && itAdvanced) {
17             itAdvanced = false;
18             if (fr[0].matchWord("isOn")) {
19                 const char * ptstr = fr[1].getStr();
20                 if (ptstr) {
21                     if (std::string(ptstr) == "TRUE") {
22                         lp._on = true;
23                     } else if (std::string(ptstr) == "FALSE") {
24                         lp._on = false;
25                     } else {
26                         osg::notify(osg::WARN) << "osg::Sim reader warning: invalid isOn: " << ptstr << std::endl;
27                     }
28                     fr += 2;
29                     itAdvanced = true;
30                 }
31             }
32             if (fr[0].matchWord("position")) {
33                 float x, y, z;
34                 if (fr[1].getFloat(x) && fr[2].getFloat(y) && fr[3].getFloat(z)) {
35                     lp._position.set(x, y, z);
36                     fr += 4;
37                     itAdvanced = true;
38                 }
39             }
40             if (fr[0].matchWord("color")) {
41                 float r, g, b, a;
42                 if (fr[1].getFloat(r) && fr[2].getFloat(g) && fr[3].getFloat(b) && fr[4].getFloat(a)) {
43                     lp._color.set(r, g, b, a);
44                     fr += 5;
45                     itAdvanced = true;
46                 }
47             }
48             if (fr[0].matchWord("intensity")) {
49                 if (fr[1].getFloat(lp._intensity)) {
50                     fr += 2;
51                     itAdvanced = true;
52                 }
53             }
54             if (fr[0].matchWord("radius")) {
55                 if (fr[1].getFloat(lp._radius)) {
56                     fr += 2;
57                     itAdvanced = true;
58                 }
59             }
60             if (fr[0].matchWord("blendingMode")) {
61                 const char * ptstr = fr[1].getStr();
62                 if (ptstr) {
63                     if (std::string(ptstr) == "ADDITIVE") {
64                         lp._blendingMode = LightPoint::ADDITIVE;
65                         fr += 2;
66                         itAdvanced = true;
67                     } else if (std::string(ptstr) == "BLENDED") {
68                         lp._blendingMode = LightPoint::BLENDED;
69                         fr += 2;
70                         itAdvanced = true;
71                     } else {
72                         osg::notify(osg::WARN) << "osg::Sim reader warning: invalid blendingMode: " << ptstr << std::endl;
73                     }
74                 }
75             }
76             Sector * sector = static_cast<Sector *>(fr.readObjectOfType(osgDB::type_wrapper<Sector>()));
77             if (sector) {
78                 lp._sector = sector;
79                 itAdvanced = true;
80             }
81             BlinkSequence * seq = static_cast<BlinkSequence *>(fr.readObjectOfType(osgDB::type_wrapper<BlinkSequence>()));
82             if (seq) {
83                 lp._blinkSequence = seq;
84                 itAdvanced = true;
85             }
86         }
87         return true;
88     }
89     return false;
90 }
91 
writeLightPoint(const LightPoint & lp,osgDB::Output & fw)92 bool writeLightPoint(const LightPoint & lp, osgDB::Output &fw)
93 {
94     fw.indent() << "lightPoint {" << std::endl;
95     fw.moveIn();
96     fw.indent() << "isOn " << ( lp._on ? "TRUE" : "FALSE") << std::endl;
97     fw.indent() << "position " << lp._position << std::endl;
98     fw.indent() << "color " << lp._color << std::endl;
99     fw.indent() << "intensity " << lp._intensity << std::endl;
100     fw.indent() << "radius " << lp._radius << std::endl;
101     fw.indent() << "blendingMode ";
102     switch (lp._blendingMode) {
103         case LightPoint::ADDITIVE:
104             fw << "ADDITIVE" << std::endl;
105             break;
106         case LightPoint::BLENDED:
107         default :
108             fw << "BLENDED" << std::endl;
109             break;
110     }
111     if (lp._sector.valid()) {
112         fw.writeObject(*lp._sector);
113     }
114     if (lp._blinkSequence.valid()) {
115         fw.writeObject(*lp._blinkSequence);
116     }
117 
118     fw.moveOut();
119     fw.indent() << "}" << std::endl;
120     return true;
121 }
122