| 1 | #include <osgViewer/View> |
|---|
| 2 | |
|---|
| 3 | #include <iostream> |
|---|
| 4 | #include <string> |
|---|
| 5 | |
|---|
| 6 | #include <osgDB/Registry> |
|---|
| 7 | #include <osgDB/Input> |
|---|
| 8 | #include <osgDB/Output> |
|---|
| 9 | #include <osgDB/ReadFile> |
|---|
| 10 | #include <osgDB/ParameterOutput> |
|---|
| 11 | |
|---|
| 12 | bool View_readLocalData(osg::Object &obj, osgDB::Input &fr); |
|---|
| 13 | bool View_writeLocalData(const osg::Object &obj, osgDB::Output &fw); |
|---|
| 14 | |
|---|
| 15 | REGISTER_DOTOSGWRAPPER(View_Proxy) |
|---|
| 16 | ( |
|---|
| 17 | new osgViewer::View, |
|---|
| 18 | "View", |
|---|
| 19 | "Object View", |
|---|
| 20 | View_readLocalData, |
|---|
| 21 | View_writeLocalData |
|---|
| 22 | ); |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | static bool readMatrix(osg::Matrix& matrix, osgDB::Input& fr, const char* keyword) |
|---|
| 26 | { |
|---|
| 27 | bool iteratorAdvanced = false; |
|---|
| 28 | |
|---|
| 29 | if (fr[0].matchWord(keyword) && fr[1].isOpenBracket()) |
|---|
| 30 | { |
|---|
| 31 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 32 | |
|---|
| 33 | fr += 2; |
|---|
| 34 | |
|---|
| 35 | int row=0; |
|---|
| 36 | int col=0; |
|---|
| 37 | double v; |
|---|
| 38 | while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 39 | { |
|---|
| 40 | if (fr[0].getFloat(v)) |
|---|
| 41 | { |
|---|
| 42 | matrix(row,col)=v; |
|---|
| 43 | ++col; |
|---|
| 44 | if (col>=4) |
|---|
| 45 | { |
|---|
| 46 | col = 0; |
|---|
| 47 | ++row; |
|---|
| 48 | } |
|---|
| 49 | ++fr; |
|---|
| 50 | } |
|---|
| 51 | else fr.advanceOverCurrentFieldOrBlock(); |
|---|
| 52 | } |
|---|
| 53 | iteratorAdvanced = true; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | return iteratorAdvanced; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | #if 0 |
|---|
| 60 | static bool writeMatrix(const osg::Matrix& matrix, osgDB::Output& fw, const char* keyword) |
|---|
| 61 | { |
|---|
| 62 | fw.indent() << keyword <<" {" << std::endl; |
|---|
| 63 | fw.moveIn(); |
|---|
| 64 | fw.indent() << matrix(0,0) << " " << matrix(0,1) << " " << matrix(0,2) << " " << matrix(0,3) << std::endl; |
|---|
| 65 | fw.indent() << matrix(1,0) << " " << matrix(1,1) << " " << matrix(1,2) << " " << matrix(1,3) << std::endl; |
|---|
| 66 | fw.indent() << matrix(2,0) << " " << matrix(2,1) << " " << matrix(2,2) << " " << matrix(2,3) << std::endl; |
|---|
| 67 | fw.indent() << matrix(3,0) << " " << matrix(3,1) << " " << matrix(3,2) << " " << matrix(3,3) << std::endl; |
|---|
| 68 | fw.moveOut(); |
|---|
| 69 | fw.indent() << "}"<< std::endl; |
|---|
| 70 | return true; |
|---|
| 71 | } |
|---|
| 72 | #endif |
|---|
| 73 | |
|---|
| 74 | osg::Image* readIntensityImage(osgDB::Input& fr, bool& itrAdvanced) |
|---|
| 75 | { |
|---|
| 76 | if (fr.matchSequence("intensityMap {")) |
|---|
| 77 | { |
|---|
| 78 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 79 | fr += 2; |
|---|
| 80 | |
|---|
| 81 | typedef std::map<float,float> IntensityMap; |
|---|
| 82 | IntensityMap intensityMap; |
|---|
| 83 | while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 84 | { |
|---|
| 85 | float position, intensity; |
|---|
| 86 | if (fr.read(position,intensity)) |
|---|
| 87 | { |
|---|
| 88 | intensityMap[position] = intensity; |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | { |
|---|
| 92 | ++fr; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | ++fr; |
|---|
| 96 | |
|---|
| 97 | itrAdvanced = true; |
|---|
| 98 | |
|---|
| 99 | if (!intensityMap.empty()) |
|---|
| 100 | { |
|---|
| 101 | unsigned int numPixels = 256; |
|---|
| 102 | |
|---|
| 103 | osg::Image* image = new osg::Image; |
|---|
| 104 | image->allocateImage(1,numPixels,1,GL_LUMINANCE,GL_FLOAT); |
|---|
| 105 | |
|---|
| 106 | float intensityMultiplier = 0.01f; |
|---|
| 107 | float* ptr = reinterpret_cast<float*>(image->data()); |
|---|
| 108 | for(unsigned int i=0; i<numPixels; ++i) |
|---|
| 109 | { |
|---|
| 110 | float position = (1.0f - float(i)/float(numPixels-1)) * 180.0f; |
|---|
| 111 | float intensity = 1.0f; |
|---|
| 112 | if (position <= intensityMap.begin()->first) |
|---|
| 113 | { |
|---|
| 114 | intensity = intensityMap.begin()->second * intensityMultiplier; |
|---|
| 115 | } |
|---|
| 116 | else if (position>=intensityMap.rbegin()->first) |
|---|
| 117 | { |
|---|
| 118 | intensity = intensityMap.rbegin()->second * intensityMultiplier; |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | IntensityMap::iterator above_itr = intensityMap.lower_bound(position); |
|---|
| 123 | if (above_itr != intensityMap.begin()) |
|---|
| 124 | { |
|---|
| 125 | IntensityMap::iterator below_itr = above_itr; |
|---|
| 126 | --below_itr; |
|---|
| 127 | float r = (position - below_itr->first) / (above_itr->first - below_itr->first); |
|---|
| 128 | intensity = (below_itr->second + (above_itr->second - below_itr->second) * r) * intensityMultiplier; |
|---|
| 129 | } |
|---|
| 130 | else |
|---|
| 131 | { |
|---|
| 132 | intensity = above_itr->second * intensityMultiplier; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | *ptr++ = intensity; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | return image; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | } |
|---|
| 144 | return 0; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | bool View_readLocalData(osg::Object &obj, osgDB::Input &fr) |
|---|
| 148 | { |
|---|
| 149 | osgViewer::View& view = dynamic_cast<osgViewer::View&>(obj); |
|---|
| 150 | bool iteratorAdvanced = false; |
|---|
| 151 | |
|---|
| 152 | bool matchedFirst = false; |
|---|
| 153 | if ((matchedFirst = fr.matchSequence("setUpViewFor3DSphericalDisplay {")) || |
|---|
| 154 | fr.matchSequence("setUpViewForPanoramicSphericalDisplay {")) |
|---|
| 155 | { |
|---|
| 156 | double radius = 1.0; |
|---|
| 157 | double collar = 0.45; |
|---|
| 158 | unsigned int screenNum = 0; |
|---|
| 159 | unsigned int intensityFormat = 8; |
|---|
| 160 | osg::Matrix matrix; |
|---|
| 161 | std::string filename; |
|---|
| 162 | osg::ref_ptr<osg::Image> intensityMap; |
|---|
| 163 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 164 | |
|---|
| 165 | fr += 2; |
|---|
| 166 | |
|---|
| 167 | while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 168 | { |
|---|
| 169 | bool local_itrAdvanced = false; |
|---|
| 170 | if (fr.read("radius",radius)) local_itrAdvanced = true; |
|---|
| 171 | if (fr.read("collar",collar)) local_itrAdvanced = true; |
|---|
| 172 | if (fr.read("screenNum",screenNum)) local_itrAdvanced = true; |
|---|
| 173 | if (fr.read("intensityFile",filename)) local_itrAdvanced = true; |
|---|
| 174 | if (fr.matchSequence("intensityMap {")) intensityMap = readIntensityImage(fr,local_itrAdvanced); |
|---|
| 175 | if (fr.read("intensityFormat",intensityFormat)) local_itrAdvanced = true; |
|---|
| 176 | if (readMatrix(matrix,fr,"projectorMatrix")) local_itrAdvanced = true; |
|---|
| 177 | |
|---|
| 178 | if (!local_itrAdvanced) ++fr; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | ++fr; |
|---|
| 183 | |
|---|
| 184 | iteratorAdvanced = true; |
|---|
| 185 | |
|---|
| 186 | if (!filename.empty()) |
|---|
| 187 | { |
|---|
| 188 | intensityMap = osgDB::readRefImageFile(filename); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | if (intensityMap.valid()) |
|---|
| 192 | { |
|---|
| 193 | if (intensityFormat==16) intensityMap->setInternalTextureFormat(GL_LUMINANCE16F_ARB); |
|---|
| 194 | else if (intensityFormat==32) intensityMap->setInternalTextureFormat(GL_LUMINANCE32F_ARB); |
|---|
| 195 | |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | if (matchedFirst) view.setUpViewFor3DSphericalDisplay(radius, collar, screenNum, intensityMap.get(), matrix); |
|---|
| 200 | else view.setUpViewForPanoramicSphericalDisplay(radius, collar, screenNum, intensityMap.get(), matrix); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | int x = 0; |
|---|
| 204 | int y = 0; |
|---|
| 205 | int width = 128; |
|---|
| 206 | int height = 1024; |
|---|
| 207 | unsigned int screenNum = 0; |
|---|
| 208 | |
|---|
| 209 | if (fr.read("setUpViewOnSingleScreen",screenNum)) |
|---|
| 210 | { |
|---|
| 211 | view.setUpViewOnSingleScreen(screenNum); |
|---|
| 212 | iteratorAdvanced = true; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | if (fr.read("setUpViewAcrossAllScreens")) |
|---|
| 216 | { |
|---|
| 217 | view.setUpViewAcrossAllScreens(); |
|---|
| 218 | iteratorAdvanced = true; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | if (fr.read("setUpViewInWindow",x,y,width,height,screenNum)) |
|---|
| 222 | { |
|---|
| 223 | view.setUpViewInWindow(x, y, width, height, screenNum); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | if (fr.read("setUpViewInWindow",x,y,width,height)) |
|---|
| 227 | { |
|---|
| 228 | view.setUpViewInWindow(x, y, width, height); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | osg::ref_ptr<osg::Object> readObject; |
|---|
| 233 | while((readObject=fr.readObjectOfType(osgDB::type_wrapper<osg::Camera>())).valid()) |
|---|
| 234 | { |
|---|
| 235 | view.setCamera(static_cast<osg::Camera*>(readObject.get())); |
|---|
| 236 | iteratorAdvanced = true; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | if (fr.matchSequence("Slaves {")) |
|---|
| 240 | { |
|---|
| 241 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 242 | |
|---|
| 243 | fr += 2; |
|---|
| 244 | |
|---|
| 245 | while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 246 | { |
|---|
| 247 | readObject = fr.readObjectOfType(osgDB::type_wrapper<osg::Camera>()); |
|---|
| 248 | if (readObject.valid()) view.addSlave(static_cast<osg::Camera*>(readObject.get())); |
|---|
| 249 | else ++fr; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | ++fr; |
|---|
| 254 | |
|---|
| 255 | iteratorAdvanced = true; |
|---|
| 256 | |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | return iteratorAdvanced; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | bool View_writeLocalData(const osg::Object &obj, osgDB::Output &fw) |
|---|
| 263 | { |
|---|
| 264 | const osgViewer::View& view = dynamic_cast<const osgViewer::View&>(obj); |
|---|
| 265 | |
|---|
| 266 | osg::notify(osg::NOTICE)<<"View_writeLocalData"<<std::endl; |
|---|
| 267 | |
|---|
| 268 | if (view.getCamera()) |
|---|
| 269 | { |
|---|
| 270 | fw.writeObject(*view.getCamera()); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | if (view.getNumSlaves() != 0) |
|---|
| 274 | { |
|---|
| 275 | fw.indent()<<"Slaves {"<<std::endl; |
|---|
| 276 | fw.moveIn(); |
|---|
| 277 | |
|---|
| 278 | for(unsigned int i=0; i<view.getNumSlaves(); ++i) |
|---|
| 279 | { |
|---|
| 280 | const osg::Camera* camera = view.getSlave(i)._camera.get(); |
|---|
| 281 | if (camera) |
|---|
| 282 | { |
|---|
| 283 | fw.writeObject(*camera); |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | fw.moveOut(); |
|---|
| 288 | fw.indent()<<"}"<<std::endl; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | return true; |
|---|
| 292 | } |
|---|