1 #include "osg/LOD" 2 #include <osg/io_utils> 3 4 #include "osgDB/Registry" 5 #include "osgDB/Input" 6 #include "osgDB/Output" 7 8 using namespace osg; 9 using namespace osgDB; 10 11 // forward declare functions to use later. 12 bool LOD_readLocalData(Object& obj, Input& fr); 13 bool LOD_writeLocalData(const Object& obj, Output& fw); 14 15 // register the read and write functions with the osgDB::Registry. 16 REGISTER_DOTOSGWRAPPER(LOD) 17 ( 18 new osg::LOD, 19 "LOD", 20 "Object Node LOD Group", 21 &LOD_readLocalData, 22 &LOD_writeLocalData 23 ); ImageSequence_readLocalData(Object & obj,Input & fr)24 25 bool LOD_readLocalData(Object& obj, Input& fr) 26 { 27 bool iteratorAdvanced = false; 28 29 LOD& lod = static_cast<LOD&>(obj); 30 31 if (fr.matchSequence("Center %f %f %f")) 32 { 33 Vec3 center; 34 fr[1].getFloat(center[0]); 35 fr[2].getFloat(center[1]); 36 fr[3].getFloat(center[2]); 37 lod.setCenter(center); 38 39 iteratorAdvanced = true; 40 fr+=4; 41 } 42 43 float radius; 44 if (fr[0].matchWord("Radius") && fr[1].getFloat(radius)) 45 { 46 lod.setRadius(radius); 47 fr+=2; 48 iteratorAdvanced = true; 49 } 50 51 52 if(fr[0].matchWord("RangeMode")){ 53 if(fr[1].matchWord("DISTANCE_FROM_EYE_POINT")){ 54 lod.setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT); 55 } 56 else { 57 lod.setRangeMode(osg::LOD::PIXEL_SIZE_ON_SCREEN); 58 } 59 fr+=2; 60 iteratorAdvanced = true; 61 } 62 63 64 65 // For backwards compatibility with old style LOD's (pre October 2002). 66 bool matchFirst = false; 67 if ((matchFirst=fr.matchSequence("Ranges {")) || fr.matchSequence("Ranges %i {")) 68 { 69 70 // set up coordinates. 71 int entry = fr[0].getNoNestedBrackets(); 72 if (matchFirst) 73 { 74 fr += 2; 75 } 76 else 77 { 78 fr += 3; 79 } 80 81 float minRange=0.0; 82 float maxRange=0.0; 83 unsigned int i=0; 84 while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) 85 { 86 if (fr[0].getFloat(maxRange)) 87 { 88 if (i>0) lod.setRange(i-1,minRange,maxRange); 89 ++fr; 90 ++i; 91 minRange = maxRange; 92 } 93 else 94 { 95 ++fr; 96 } 97 } 98 99 iteratorAdvanced = true; 100 ++fr; 101 102 } 103 104 if ((matchFirst=fr.matchSequence("RangeList {")) || fr.matchSequence("RangeList %i {")) 105 { 106 107 // set up coordinates. 108 int entry = fr[0].getNoNestedBrackets(); 109 if (matchFirst) 110 { 111 fr += 2; 112 } 113 else 114 { 115 fr += 3; 116 } 117 118 float minRange=0.0; 119 float maxRange=0.0; 120 unsigned int i=0; 121 while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) 122 { 123 if (fr[0].getFloat(minRange) && fr[1].getFloat(maxRange) ) 124 { 125 lod.setRange(i,minRange,maxRange); 126 fr+=2; 127 ++i; 128 } 129 else 130 { 131 ++fr; 132 } 133 } 134 135 iteratorAdvanced = true; 136 ++fr; 137 138 } 139 return iteratorAdvanced; 140 } 141 142 143 bool LOD_writeLocalData(const Object& obj, Output& fw) 144 { 145 const LOD& lod = static_cast<const LOD&>(obj); 146 147 if (lod.getCenterMode()==osg::LOD::USER_DEFINED_CENTER) fw.indent() << "Center "<< lod.getCenter() << std::endl; 148 149 fw.indent() << "Radius "<<lod.getRadius()<<std::endl; 150 151 if(lod.getRangeMode()==osg::LOD::DISTANCE_FROM_EYE_POINT){ 152 fw.indent() << "RangeMode DISTANCE_FROM_EYE_POINT"<<std::endl; 153 } 154 else { 155 fw.indent() << "RangeMode PIXEL_SIZE_ON_SCREEN"<<std::endl; 156 } 157 158 fw.indent() << "RangeList "<<lod.getNumRanges()<<" {"<< std::endl; 159 fw.moveIn(); 160 161 for(unsigned int i=0; i<lod.getNumRanges();++i) 162 { 163 fw.indent() << lod.getMinRange(i) << " "<<lod.getMaxRange(i)<<std::endl; 164 } 165 fw.moveOut(); 166 fw.indent() << "}"<< std::endl; 167 168 return true; 169 } 170