| 1 | #include <osg/StateSet> |
|---|
| 2 | #include <osg/Texture1D> |
|---|
| 3 | #include <osg/Texture2D> |
|---|
| 4 | #include <osg/TextureCubeMap> |
|---|
| 5 | #include <osg/TextureRectangle> |
|---|
| 6 | #include <osg/TexGen> |
|---|
| 7 | #include <osg/PolygonOffset> |
|---|
| 8 | #include <osg/LineStipple> |
|---|
| 9 | #include <osg/Light> |
|---|
| 10 | #include <osg/ClipPlane> |
|---|
| 11 | #include <osg/AlphaFunc> |
|---|
| 12 | #include <osg/Point> |
|---|
| 13 | #include <osg/Material> |
|---|
| 14 | #include <osg/Fog> |
|---|
| 15 | #include <osg/GL2Extensions> |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/Registry> |
|---|
| 18 | #include <osgDB/Input> |
|---|
| 19 | #include <osgDB/Output> |
|---|
| 20 | |
|---|
| 21 | #include <OpenThreads/Mutex> |
|---|
| 22 | #include <OpenThreads/ScopedLock> |
|---|
| 23 | |
|---|
| 24 | #include <set> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | |
|---|
| 27 | using namespace osg; |
|---|
| 28 | using namespace osgDB; |
|---|
| 29 | using namespace std; |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | bool GeoState_readLocalData(Object& obj, Input& fr); |
|---|
| 33 | |
|---|
| 34 | bool StateSet_readLocalData(Object& obj, Input& fr); |
|---|
| 35 | bool StateSet_writeLocalData(const Object& obj, Output& fw); |
|---|
| 36 | |
|---|
| 37 | bool StateSet_matchModeStr(const char* str,StateAttribute::GLModeValue& mode); |
|---|
| 38 | const char* StateSet_getModeStr(StateAttribute::GLModeValue mode); |
|---|
| 39 | |
|---|
| 40 | bool StateSet_matchRenderBinModeStr(const char* str,StateSet::RenderBinMode& mode); |
|---|
| 41 | const char* StateSet_getRenderBinModeStr(StateSet::RenderBinMode mode); |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | REGISTER_DOTOSGWRAPPER(StateSet) |
|---|
| 45 | ( |
|---|
| 46 | new osg::StateSet, |
|---|
| 47 | "StateSet", |
|---|
| 48 | "Object StateSet", |
|---|
| 49 | &StateSet_readLocalData, |
|---|
| 50 | &StateSet_writeLocalData, |
|---|
| 51 | DotOsgWrapper::READ_AND_WRITE |
|---|
| 52 | ); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | REGISTER_DOTOSGWRAPPER(GeoState) |
|---|
| 56 | ( |
|---|
| 57 | new osg::StateSet, |
|---|
| 58 | "GeoState", |
|---|
| 59 | "Object GeoState", |
|---|
| 60 | &GeoState_readLocalData, |
|---|
| 61 | NULL, |
|---|
| 62 | DotOsgWrapper::READ_ONLY |
|---|
| 63 | ); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | typedef std::map<std::string,StateAttribute::GLMode> GLNameToGLModeMap; |
|---|
| 69 | typedef std::map<StateAttribute::GLMode,std::string> GLModeToGLNameMap; |
|---|
| 70 | typedef std::set<StateAttribute::GLMode> TextureGLModeSet; |
|---|
| 71 | |
|---|
| 72 | GLNameToGLModeMap s_GLNameToGLModeMap; |
|---|
| 73 | GLModeToGLNameMap s_GLModeToGLNameMap; |
|---|
| 74 | TextureGLModeSet s_TextureGLModeSet; |
|---|
| 75 | |
|---|
| 76 | #define ADD_NAME(name,mode) s_GLNameToGLModeMap[name]=mode; s_GLModeToGLNameMap[mode]=name; |
|---|
| 77 | |
|---|
| 78 | void initGLNames() |
|---|
| 79 | { |
|---|
| 80 | static bool first_time = true; |
|---|
| 81 | if (!first_time) return; |
|---|
| 82 | |
|---|
| 83 | static OpenThreads::Mutex s_initGLNames; |
|---|
| 84 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_initGLNames); |
|---|
| 85 | |
|---|
| 86 | if (!first_time) return; |
|---|
| 87 | |
|---|
| 88 | ADD_NAME("GL_ALPHA_TEST",GL_ALPHA_TEST) |
|---|
| 89 | ADD_NAME("GL_BLEND",GL_BLEND) |
|---|
| 90 | ADD_NAME("GL_COLOR_MATERIAL",GL_COLOR_MATERIAL) |
|---|
| 91 | ADD_NAME("GL_CULL_FACE",GL_CULL_FACE) |
|---|
| 92 | ADD_NAME("GL_DEPTH_TEST",GL_DEPTH_TEST) |
|---|
| 93 | ADD_NAME("GL_FOG",GL_FOG) |
|---|
| 94 | ADD_NAME("GL_LIGHTING",GL_LIGHTING) |
|---|
| 95 | ADD_NAME("GL_POINT_SMOOTH",GL_POINT_SMOOTH) |
|---|
| 96 | ADD_NAME("GL_LINE_STIPPLE",GL_LINE_STIPPLE) |
|---|
| 97 | ADD_NAME("GL_POLYGON_OFFSET_FILL",GL_POLYGON_OFFSET_FILL) |
|---|
| 98 | ADD_NAME("GL_POLYGON_OFFSET_LINE",GL_POLYGON_OFFSET_LINE) |
|---|
| 99 | ADD_NAME("GL_POLYGON_OFFSET_POINT",GL_POLYGON_OFFSET_POINT) |
|---|
| 100 | ADD_NAME("GL_COLOR_SUM",GL_COLOR_SUM); |
|---|
| 101 | ADD_NAME("GL_NORMALIZE",GL_NORMALIZE); |
|---|
| 102 | ADD_NAME("GL_RESCALE_NORMAL",GL_RESCALE_NORMAL); |
|---|
| 103 | |
|---|
| 104 | ADD_NAME("GL_TEXTURE_1D",GL_TEXTURE_1D) |
|---|
| 105 | ADD_NAME("GL_TEXTURE_2D",GL_TEXTURE_2D) |
|---|
| 106 | ADD_NAME("GL_TEXTURE_3D",GL_TEXTURE_3D) |
|---|
| 107 | |
|---|
| 108 | ADD_NAME("GL_TEXTURE_CUBE_MAP",GL_TEXTURE_CUBE_MAP); |
|---|
| 109 | ADD_NAME("GL_TEXTURE_RECTANGLE",GL_TEXTURE_RECTANGLE); |
|---|
| 110 | |
|---|
| 111 | ADD_NAME("GL_TEXTURE_GEN_Q",GL_TEXTURE_GEN_Q) |
|---|
| 112 | ADD_NAME("GL_TEXTURE_GEN_R",GL_TEXTURE_GEN_R) |
|---|
| 113 | ADD_NAME("GL_TEXTURE_GEN_S",GL_TEXTURE_GEN_S) |
|---|
| 114 | ADD_NAME("GL_TEXTURE_GEN_T",GL_TEXTURE_GEN_T) |
|---|
| 115 | |
|---|
| 116 | ADD_NAME("GL_STENCIL_TEST",GL_STENCIL_TEST) |
|---|
| 117 | |
|---|
| 118 | ADD_NAME("GL_CLIP_PLANE0",GL_CLIP_PLANE0); |
|---|
| 119 | ADD_NAME("GL_CLIP_PLANE1",GL_CLIP_PLANE1); |
|---|
| 120 | ADD_NAME("GL_CLIP_PLANE2",GL_CLIP_PLANE2); |
|---|
| 121 | ADD_NAME("GL_CLIP_PLANE3",GL_CLIP_PLANE3); |
|---|
| 122 | ADD_NAME("GL_CLIP_PLANE4",GL_CLIP_PLANE4); |
|---|
| 123 | ADD_NAME("GL_CLIP_PLANE5",GL_CLIP_PLANE5); |
|---|
| 124 | |
|---|
| 125 | ADD_NAME("GL_LIGHT0",GL_LIGHT0); |
|---|
| 126 | ADD_NAME("GL_LIGHT1",GL_LIGHT1); |
|---|
| 127 | ADD_NAME("GL_LIGHT2",GL_LIGHT2); |
|---|
| 128 | ADD_NAME("GL_LIGHT3",GL_LIGHT3); |
|---|
| 129 | ADD_NAME("GL_LIGHT4",GL_LIGHT4); |
|---|
| 130 | ADD_NAME("GL_LIGHT5",GL_LIGHT5); |
|---|
| 131 | ADD_NAME("GL_LIGHT6",GL_LIGHT6); |
|---|
| 132 | ADD_NAME("GL_LIGHT7",GL_LIGHT7); |
|---|
| 133 | |
|---|
| 134 | #if defined(OSG_GL3_AVAILABLE) |
|---|
| 135 | #define GL_VERTEX_PROGRAM_POINT_SIZE 0x8642 |
|---|
| 136 | #define GL_VERTEX_PROGRAM_TWO_SIDE 0x8643 |
|---|
| 137 | #endif |
|---|
| 138 | ADD_NAME("GL_VERTEX_PROGRAM_POINT_SIZE", GL_VERTEX_PROGRAM_POINT_SIZE) |
|---|
| 139 | ADD_NAME("GL_VERTEX_PROGRAM_TWO_SIDE", GL_VERTEX_PROGRAM_TWO_SIDE) |
|---|
| 140 | |
|---|
| 141 | s_TextureGLModeSet.insert(GL_TEXTURE_1D); |
|---|
| 142 | s_TextureGLModeSet.insert(GL_TEXTURE_2D); |
|---|
| 143 | s_TextureGLModeSet.insert(GL_TEXTURE_3D); |
|---|
| 144 | |
|---|
| 145 | s_TextureGLModeSet.insert(GL_TEXTURE_CUBE_MAP); |
|---|
| 146 | s_TextureGLModeSet.insert(GL_TEXTURE_RECTANGLE); |
|---|
| 147 | |
|---|
| 148 | s_TextureGLModeSet.insert(GL_TEXTURE_GEN_Q); |
|---|
| 149 | s_TextureGLModeSet.insert(GL_TEXTURE_GEN_R); |
|---|
| 150 | s_TextureGLModeSet.insert(GL_TEXTURE_GEN_S); |
|---|
| 151 | s_TextureGLModeSet.insert(GL_TEXTURE_GEN_T); |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | first_time = false; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | bool GeoState_readLocalData(Object& obj, Input& fr) |
|---|
| 168 | { |
|---|
| 169 | bool iteratorAdvanced = false; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | StateSet& statset = static_cast<StateSet&>(obj); |
|---|
| 173 | |
|---|
| 174 | statset.setRenderingHint(StateSet::OPAQUE_BIN); |
|---|
| 175 | |
|---|
| 176 | StateAttribute::GLModeValue mode; |
|---|
| 177 | if (fr[0].matchWord("transparency") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 178 | { |
|---|
| 179 | if (mode&StateAttribute::ON) |
|---|
| 180 | { |
|---|
| 181 | statset.setRenderingHint(StateSet::TRANSPARENT_BIN); |
|---|
| 182 | } |
|---|
| 183 | statset.setMode(GL_BLEND,mode); |
|---|
| 184 | fr+=2; |
|---|
| 185 | iteratorAdvanced = true; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | if (fr[0].matchWord("antialiasing") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 189 | { |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | fr+=2; |
|---|
| 193 | iteratorAdvanced = true; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | if (fr[0].matchWord("face_culling") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 197 | { |
|---|
| 198 | statset.setMode(GL_CULL_FACE,mode); |
|---|
| 199 | fr+=2; |
|---|
| 200 | iteratorAdvanced = true; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | if (fr[0].matchWord("lighting") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 204 | { |
|---|
| 205 | statset.setMode(GL_LIGHTING,mode); |
|---|
| 206 | fr+=2; |
|---|
| 207 | iteratorAdvanced = true; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | if (fr[0].matchWord("texturing") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 211 | { |
|---|
| 212 | statset.setTextureMode(0,GL_TEXTURE_2D,mode); |
|---|
| 213 | fr+=2; |
|---|
| 214 | iteratorAdvanced = true; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if (fr[0].matchWord("fogging") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 218 | { |
|---|
| 219 | statset.setMode(GL_FOG,mode); |
|---|
| 220 | fr+=2; |
|---|
| 221 | iteratorAdvanced = true; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | if (fr[0].matchWord("colortable") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 225 | { |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | fr+=2; |
|---|
| 229 | iteratorAdvanced = true; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | StateAttribute::GLModeValue texgening = StateAttribute::OFF; |
|---|
| 233 | if (fr[0].matchWord("texgening") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | texgening = mode; |
|---|
| 239 | fr+=2; |
|---|
| 240 | iteratorAdvanced = true; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | if (fr[0].matchWord("point_smoothing") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 244 | { |
|---|
| 245 | statset.setMode(GL_POINT_SMOOTH,mode); |
|---|
| 246 | fr+=2; |
|---|
| 247 | iteratorAdvanced = true; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | if (fr[0].matchWord("polygon_offset") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 252 | { |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | fr+=2; |
|---|
| 256 | iteratorAdvanced = true; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | if (fr[0].matchWord("alpha_test") && StateSet_matchModeStr(fr[1].getStr(),mode)) |
|---|
| 260 | { |
|---|
| 261 | statset.setMode(GL_ALPHA_TEST,mode); |
|---|
| 262 | fr+=2; |
|---|
| 263 | iteratorAdvanced = true; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | StateAttribute* attribute = NULL; |
|---|
| 269 | while((attribute=fr.readStateAttribute())!=NULL) |
|---|
| 270 | { |
|---|
| 271 | if (attribute->isTextureAttribute()) |
|---|
| 272 | { |
|---|
| 273 | |
|---|
| 274 | statset.setTextureAttribute(0,attribute); |
|---|
| 275 | } |
|---|
| 276 | else |
|---|
| 277 | { |
|---|
| 278 | statset.setAttribute(attribute); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | if (attribute->getType()==StateAttribute::TEXGEN) |
|---|
| 282 | statset.setAssociatedModes(attribute,texgening); |
|---|
| 283 | |
|---|
| 284 | iteratorAdvanced = true; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | return iteratorAdvanced; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | bool StateSet_readLocalData(Object& obj, Input& fr) |
|---|
| 291 | { |
|---|
| 292 | bool iteratorAdvanced = false; |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | StateSet& stateset = static_cast<StateSet&>(obj); |
|---|
| 296 | |
|---|
| 297 | initGLNames(); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | if (fr[0].matchWord("rendering_hint")) |
|---|
| 301 | { |
|---|
| 302 | if (fr[1].matchWord("DEFAULT_BIN")) |
|---|
| 303 | { |
|---|
| 304 | stateset.setRenderingHint(StateSet::DEFAULT_BIN); |
|---|
| 305 | fr+=2; |
|---|
| 306 | iteratorAdvanced = true; |
|---|
| 307 | } |
|---|
| 308 | else if (fr[1].matchWord("OPAQUE_BIN")) |
|---|
| 309 | { |
|---|
| 310 | stateset.setRenderingHint(StateSet::OPAQUE_BIN); |
|---|
| 311 | fr+=2; |
|---|
| 312 | iteratorAdvanced = true; |
|---|
| 313 | } |
|---|
| 314 | else if (fr[1].matchWord("TRANSPARENT_BIN")) |
|---|
| 315 | { |
|---|
| 316 | stateset.setRenderingHint(StateSet::TRANSPARENT_BIN); |
|---|
| 317 | fr+=2; |
|---|
| 318 | iteratorAdvanced = true; |
|---|
| 319 | } |
|---|
| 320 | else if (fr[1].isInt()) |
|---|
| 321 | { |
|---|
| 322 | int value; |
|---|
| 323 | fr[1].getInt(value); |
|---|
| 324 | stateset.setRenderingHint(value); |
|---|
| 325 | fr+=2; |
|---|
| 326 | iteratorAdvanced = true; |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | bool setRenderBinDetails=false; |
|---|
| 331 | StateSet::RenderBinMode rbmode = stateset.getRenderBinMode(); |
|---|
| 332 | if (fr[0].matchWord("renderBinMode") && StateSet_matchRenderBinModeStr(fr[1].getStr(),rbmode)) |
|---|
| 333 | { |
|---|
| 334 | setRenderBinDetails=true; |
|---|
| 335 | fr+=2; |
|---|
| 336 | iteratorAdvanced = true; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | int binNumber = stateset.getBinNumber(); |
|---|
| 340 | if (fr[0].matchWord("binNumber") && fr[1].getInt(binNumber)) |
|---|
| 341 | { |
|---|
| 342 | setRenderBinDetails=true; |
|---|
| 343 | fr+=2; |
|---|
| 344 | iteratorAdvanced = true; |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | std::string binName = stateset.getBinName(); |
|---|
| 348 | if (fr[0].matchWord("binName")) |
|---|
| 349 | { |
|---|
| 350 | setRenderBinDetails=true; |
|---|
| 351 | binName = fr[1].getStr(); |
|---|
| 352 | |
|---|
| 353 | fr+=2; |
|---|
| 354 | iteratorAdvanced = true; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | if (setRenderBinDetails) |
|---|
| 358 | { |
|---|
| 359 | stateset.setRenderBinDetails(binNumber,binName,rbmode); |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | static ref_ptr<StateSet::Callback> s_callback = new osg::StateSet::Callback; |
|---|
| 363 | while (fr.matchSequence("UpdateCallback {")) |
|---|
| 364 | { |
|---|
| 365 | |
|---|
| 366 | fr += 2; |
|---|
| 367 | StateSet::Callback* callback = dynamic_cast<StateSet::Callback*>(fr.readObjectOfType(*s_callback)); |
|---|
| 368 | if (callback) { |
|---|
| 369 | stateset.setUpdateCallback(callback); |
|---|
| 370 | } |
|---|
| 371 | iteratorAdvanced = true; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | while (fr.matchSequence("EventCallback {")) |
|---|
| 375 | { |
|---|
| 376 | |
|---|
| 377 | fr += 2; |
|---|
| 378 | StateSet::Callback* callback = dynamic_cast<StateSet::Callback*>(fr.readObjectOfType(*s_callback)); |
|---|
| 379 | if (callback) { |
|---|
| 380 | stateset.setEventCallback(callback); |
|---|
| 381 | } |
|---|
| 382 | iteratorAdvanced = true; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | bool readingMode = true; |
|---|
| 386 | StateAttribute::GLModeValue value; |
|---|
| 387 | while (readingMode) |
|---|
| 388 | { |
|---|
| 389 | |
|---|
| 390 | readingMode=false; |
|---|
| 391 | if (fr[0].isInt()) |
|---|
| 392 | { |
|---|
| 393 | if (StateSet_matchModeStr(fr[1].getStr(),value)) |
|---|
| 394 | { |
|---|
| 395 | int mode; |
|---|
| 396 | fr[0].getInt(mode); |
|---|
| 397 | |
|---|
| 398 | if (s_TextureGLModeSet.find(mode)!=s_TextureGLModeSet.end()) |
|---|
| 399 | { |
|---|
| 400 | |
|---|
| 401 | stateset.setTextureMode(0,(StateAttribute::GLMode)mode,value); |
|---|
| 402 | } |
|---|
| 403 | else |
|---|
| 404 | { |
|---|
| 405 | stateset.setMode((StateAttribute::GLMode)mode,value); |
|---|
| 406 | } |
|---|
| 407 | fr+=2; |
|---|
| 408 | iteratorAdvanced = true; |
|---|
| 409 | readingMode=true; |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | else |
|---|
| 413 | if (fr[0].getStr()) |
|---|
| 414 | { |
|---|
| 415 | if (StateSet_matchModeStr(fr[1].getStr(),value)) |
|---|
| 416 | { |
|---|
| 417 | GLNameToGLModeMap::iterator nitr = s_GLNameToGLModeMap.find(fr[0].getStr()); |
|---|
| 418 | if (nitr!=s_GLNameToGLModeMap.end()) |
|---|
| 419 | { |
|---|
| 420 | StateAttribute::GLMode mode = nitr->second; |
|---|
| 421 | if (s_TextureGLModeSet.find(mode)!=s_TextureGLModeSet.end()) |
|---|
| 422 | { |
|---|
| 423 | |
|---|
| 424 | stateset.setTextureMode(0,mode,value); |
|---|
| 425 | } |
|---|
| 426 | else |
|---|
| 427 | { |
|---|
| 428 | stateset.setMode(mode,value); |
|---|
| 429 | } |
|---|
| 430 | fr+=2; |
|---|
| 431 | iteratorAdvanced = true; |
|---|
| 432 | readingMode=true; |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | } |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | Uniform* uniform = NULL; |
|---|
| 440 | while((uniform=fr.readUniform())!=NULL) |
|---|
| 441 | { |
|---|
| 442 | stateset.addUniform(uniform); |
|---|
| 443 | iteratorAdvanced = true; |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | StateAttribute* attribute = NULL; |
|---|
| 449 | while((attribute=fr.readStateAttribute())!=NULL) |
|---|
| 450 | { |
|---|
| 451 | if (attribute->isTextureAttribute()) |
|---|
| 452 | { |
|---|
| 453 | |
|---|
| 454 | stateset.setTextureAttribute(0,attribute); |
|---|
| 455 | } |
|---|
| 456 | else |
|---|
| 457 | { |
|---|
| 458 | stateset.setAttribute(attribute); |
|---|
| 459 | } |
|---|
| 460 | iteratorAdvanced = true; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | while(fr.matchSequence("textureUnit %i {")) |
|---|
| 464 | { |
|---|
| 465 | int entry = fr[0].getNoNestedBrackets(); |
|---|
| 466 | |
|---|
| 467 | unsigned int unit=0; |
|---|
| 468 | fr[1].getUInt(unit); |
|---|
| 469 | fr+=3; |
|---|
| 470 | |
|---|
| 471 | while (!fr.eof() && fr[0].getNoNestedBrackets()>entry) |
|---|
| 472 | { |
|---|
| 473 | bool localIteratorAdvanced = false; |
|---|
| 474 | |
|---|
| 475 | bool readingMode = true; |
|---|
| 476 | StateAttribute::GLModeValue value; |
|---|
| 477 | while (readingMode) |
|---|
| 478 | { |
|---|
| 479 | readingMode=false; |
|---|
| 480 | if (fr[0].isInt()) |
|---|
| 481 | { |
|---|
| 482 | if (StateSet_matchModeStr(fr[1].getStr(),value)) |
|---|
| 483 | { |
|---|
| 484 | int mode; |
|---|
| 485 | fr[0].getInt(mode); |
|---|
| 486 | stateset.setTextureMode(unit,(StateAttribute::GLMode)mode,value); |
|---|
| 487 | fr+=2; |
|---|
| 488 | localIteratorAdvanced = true; |
|---|
| 489 | readingMode=true; |
|---|
| 490 | } |
|---|
| 491 | } |
|---|
| 492 | else |
|---|
| 493 | if (fr[0].getStr()) |
|---|
| 494 | { |
|---|
| 495 | if (StateSet_matchModeStr(fr[1].getStr(),value)) |
|---|
| 496 | { |
|---|
| 497 | GLNameToGLModeMap::iterator nitr = s_GLNameToGLModeMap.find(fr[0].getStr()); |
|---|
| 498 | if (nitr!=s_GLNameToGLModeMap.end()) |
|---|
| 499 | { |
|---|
| 500 | StateAttribute::GLMode mode = nitr->second; |
|---|
| 501 | stateset.setTextureMode(unit,mode,value); |
|---|
| 502 | fr+=2; |
|---|
| 503 | localIteratorAdvanced = true; |
|---|
| 504 | readingMode=true; |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | StateAttribute* attribute = NULL; |
|---|
| 511 | while((attribute=fr.readStateAttribute())!=NULL) |
|---|
| 512 | { |
|---|
| 513 | stateset.setTextureAttribute(unit,attribute); |
|---|
| 514 | localIteratorAdvanced = true; |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | if (!localIteratorAdvanced) |
|---|
| 518 | fr.advanceOverCurrentFieldOrBlock(); |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | ++fr; |
|---|
| 523 | |
|---|
| 524 | iteratorAdvanced = true; |
|---|
| 525 | |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | return iteratorAdvanced; |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | template<class T> |
|---|
| 536 | T mymax(const T& a,const T& b) |
|---|
| 537 | { |
|---|
| 538 | return (((a) > (b)) ? (a) : (b)); |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | bool StateSet_writeLocalData(const Object& obj, Output& fw) |
|---|
| 542 | { |
|---|
| 543 | |
|---|
| 544 | const StateSet& stateset = static_cast<const StateSet&>(obj); |
|---|
| 545 | |
|---|
| 546 | initGLNames(); |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | fw.indent()<<"rendering_hint "; |
|---|
| 550 | switch(stateset.getRenderingHint()) |
|---|
| 551 | { |
|---|
| 552 | case(StateSet::DEFAULT_BIN): |
|---|
| 553 | fw<<"DEFAULT_BIN"<< std::endl; |
|---|
| 554 | break; |
|---|
| 555 | case(StateSet::OPAQUE_BIN): |
|---|
| 556 | fw<<"OPAQUE_BIN"<< std::endl; |
|---|
| 557 | break; |
|---|
| 558 | case(StateSet::TRANSPARENT_BIN): |
|---|
| 559 | fw<<"TRANSPARENT_BIN"<< std::endl; |
|---|
| 560 | break; |
|---|
| 561 | default: |
|---|
| 562 | fw<<stateset.getRenderingHint()<< std::endl; |
|---|
| 563 | break; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | fw.indent()<<"renderBinMode "<<StateSet_getRenderBinModeStr(stateset.getRenderBinMode())<< std::endl; |
|---|
| 567 | if (stateset.getRenderBinMode()!=StateSet::INHERIT_RENDERBIN_DETAILS) |
|---|
| 568 | { |
|---|
| 569 | fw.indent()<<"binNumber "<<stateset.getBinNumber()<< std::endl; |
|---|
| 570 | fw.indent()<<"binName "<<stateset.getBinName()<< std::endl; |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | const StateSet::ModeList& ml = stateset.getModeList(); |
|---|
| 575 | for(StateSet::ModeList::const_iterator mitr=ml.begin(); |
|---|
| 576 | mitr!=ml.end(); |
|---|
| 577 | ++mitr) |
|---|
| 578 | { |
|---|
| 579 | GLModeToGLNameMap::iterator nitr = s_GLModeToGLNameMap.find(mitr->first); |
|---|
| 580 | if (nitr!=s_GLModeToGLNameMap.end()) |
|---|
| 581 | { |
|---|
| 582 | fw.indent() << nitr->second << " " << StateSet_getModeStr(mitr->second) << std::endl; |
|---|
| 583 | } |
|---|
| 584 | else |
|---|
| 585 | { |
|---|
| 586 | |
|---|
| 587 | fw.indent() << "0x" << hex << (unsigned int)mitr->first << dec <<" " << StateSet_getModeStr(mitr->second) << std::endl; |
|---|
| 588 | } |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | const StateSet::UniformList& ul = stateset.getUniformList(); |
|---|
| 592 | for(StateSet::UniformList::const_iterator uitr=ul.begin(); |
|---|
| 593 | uitr!=ul.end(); |
|---|
| 594 | ++uitr) |
|---|
| 595 | { |
|---|
| 596 | fw.writeObject(*(uitr->second.first)); |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | const StateSet::AttributeList& sl = stateset.getAttributeList(); |
|---|
| 600 | for(StateSet::AttributeList::const_iterator sitr=sl.begin(); |
|---|
| 601 | sitr!=sl.end(); |
|---|
| 602 | ++sitr) |
|---|
| 603 | { |
|---|
| 604 | fw.writeObject(*(sitr->second.first)); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | const StateSet::TextureModeList& tml = stateset.getTextureModeList(); |
|---|
| 609 | const StateSet::TextureAttributeList& tal = stateset.getTextureAttributeList(); |
|---|
| 610 | unsigned int maxUnit = mymax(tml.size(),tal.size()); |
|---|
| 611 | for(unsigned int unit=0;unit<maxUnit;++unit) |
|---|
| 612 | { |
|---|
| 613 | fw.indent()<<"textureUnit "<<unit<<" {"<< std::endl; |
|---|
| 614 | fw.moveIn(); |
|---|
| 615 | |
|---|
| 616 | if (unit<tml.size()) |
|---|
| 617 | { |
|---|
| 618 | const StateSet::ModeList& ml = tml[unit]; |
|---|
| 619 | for(StateSet::ModeList::const_iterator mitr=ml.begin(); |
|---|
| 620 | mitr!=ml.end(); |
|---|
| 621 | ++mitr) |
|---|
| 622 | { |
|---|
| 623 | GLModeToGLNameMap::iterator nitr = s_GLModeToGLNameMap.find(mitr->first); |
|---|
| 624 | if (nitr!=s_GLModeToGLNameMap.end()) |
|---|
| 625 | { |
|---|
| 626 | fw.indent() << nitr->second << " " << StateSet_getModeStr(mitr->second) << std::endl; |
|---|
| 627 | } |
|---|
| 628 | else |
|---|
| 629 | { |
|---|
| 630 | |
|---|
| 631 | fw.indent() << "0x" << hex << (unsigned int)mitr->first << dec <<" " << StateSet_getModeStr(mitr->second) << std::endl; |
|---|
| 632 | } |
|---|
| 633 | } |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | if (unit<tal.size()) |
|---|
| 637 | { |
|---|
| 638 | const StateSet::AttributeList& sl = tal[unit]; |
|---|
| 639 | for(StateSet::AttributeList::const_iterator sitr=sl.begin(); |
|---|
| 640 | sitr!=sl.end(); |
|---|
| 641 | ++sitr) |
|---|
| 642 | { |
|---|
| 643 | fw.writeObject(*(sitr->second.first)); |
|---|
| 644 | } |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | fw.moveOut(); |
|---|
| 648 | fw.indent()<<"}"<< std::endl; |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | if (stateset.getUpdateCallback()) |
|---|
| 652 | { |
|---|
| 653 | fw.indent() << "UpdateCallback {" << std::endl; |
|---|
| 654 | fw.moveIn(); |
|---|
| 655 | fw.writeObject(*stateset.getUpdateCallback()); |
|---|
| 656 | fw.moveOut(); |
|---|
| 657 | fw.indent() << "}" << std::endl; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | if (stateset.getEventCallback()) |
|---|
| 661 | { |
|---|
| 662 | fw.indent() << "EventCallback {" << std::endl; |
|---|
| 663 | fw.moveIn(); |
|---|
| 664 | fw.writeObject(*stateset.getEventCallback()); |
|---|
| 665 | fw.moveOut(); |
|---|
| 666 | fw.indent() << "}" << std::endl; |
|---|
| 667 | } |
|---|
| 668 | |
|---|
| 669 | return true; |
|---|
| 670 | } |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | bool StateSet_matchModeStr(const char* str,StateAttribute::GLModeValue& mode) |
|---|
| 674 | { |
|---|
| 675 | if (strcmp(str,"INHERIT")==0) mode = StateAttribute::INHERIT; |
|---|
| 676 | else if (strcmp(str,"ON")==0) mode = StateAttribute::ON; |
|---|
| 677 | else if (strcmp(str,"OFF")==0) mode = StateAttribute::OFF; |
|---|
| 678 | else if (strcmp(str,"OVERRIDE_ON")==0) mode = StateAttribute::OVERRIDE|StateAttribute::ON; |
|---|
| 679 | else if (strcmp(str,"OVERRIDE_OFF")==0) mode = StateAttribute::OVERRIDE|StateAttribute::OFF; |
|---|
| 680 | else if (strcmp(str,"OVERRIDE|ON")==0) mode = StateAttribute::OVERRIDE|StateAttribute::ON; |
|---|
| 681 | else if (strcmp(str,"OVERRIDE|OFF")==0) mode = StateAttribute::OVERRIDE|StateAttribute::OFF; |
|---|
| 682 | else if (strcmp(str,"PROTECTED|ON")==0) mode = StateAttribute::PROTECTED|StateAttribute::ON; |
|---|
| 683 | else if (strcmp(str,"PROTECTED|OFF")==0) mode = StateAttribute::PROTECTED|StateAttribute::OFF; |
|---|
| 684 | else if (strcmp(str,"PROTECTED|OVERRIDE|ON")==0) mode = StateAttribute::PROTECTED|StateAttribute::OVERRIDE|StateAttribute::ON; |
|---|
| 685 | else if (strcmp(str,"PROTECTED|OVERRIDE|OFF")==0) mode = StateAttribute::PROTECTED|StateAttribute::OVERRIDE|StateAttribute::OFF; |
|---|
| 686 | else return false; |
|---|
| 687 | return true; |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | const char* StateSet_getModeStr(StateAttribute::GLModeValue value) |
|---|
| 692 | { |
|---|
| 693 | switch(value) |
|---|
| 694 | { |
|---|
| 695 | case(StateAttribute::INHERIT): return "INHERIT"; |
|---|
| 696 | case(StateAttribute::ON): return "ON"; |
|---|
| 697 | case(StateAttribute::OFF): return "OFF"; |
|---|
| 698 | case(StateAttribute::OVERRIDE|StateAttribute::ON): return "OVERRIDE|ON"; |
|---|
| 699 | case(StateAttribute::OVERRIDE|StateAttribute::OFF): return "OVERRIDE|OFF"; |
|---|
| 700 | case(StateAttribute::PROTECTED|StateAttribute::ON): return "PROTECTED|ON"; |
|---|
| 701 | case(StateAttribute::PROTECTED|StateAttribute::OFF): return "PROTECTED|OFF"; |
|---|
| 702 | case(StateAttribute::PROTECTED|StateAttribute::OVERRIDE|StateAttribute::ON): return "PROTECTED|OVERRIDE|ON"; |
|---|
| 703 | case(StateAttribute::PROTECTED|StateAttribute::OVERRIDE|StateAttribute::OFF): return "PROTECTED|OVERRIDE|OFF"; |
|---|
| 704 | } |
|---|
| 705 | return ""; |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | bool StateSet_matchRenderBinModeStr(const char* str,StateSet::RenderBinMode& mode) |
|---|
| 709 | { |
|---|
| 710 | if (strcmp(str,"INHERIT")==0) mode = StateSet::INHERIT_RENDERBIN_DETAILS; |
|---|
| 711 | else if (strcmp(str,"USE")==0) mode = StateSet::USE_RENDERBIN_DETAILS; |
|---|
| 712 | else if (strcmp(str,"OVERRIDE")==0) mode = StateSet::OVERRIDE_RENDERBIN_DETAILS; |
|---|
| 713 | else if (strcmp(str,"ENCLOSE")==0) mode = StateSet::USE_RENDERBIN_DETAILS; |
|---|
| 714 | else return false; |
|---|
| 715 | return true; |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | const char* StateSet_getRenderBinModeStr(StateSet::RenderBinMode mode) |
|---|
| 719 | { |
|---|
| 720 | switch(mode) |
|---|
| 721 | { |
|---|
| 722 | case(StateSet::INHERIT_RENDERBIN_DETAILS): return "INHERIT"; |
|---|
| 723 | case(StateSet::USE_RENDERBIN_DETAILS): return "USE"; |
|---|
| 724 | case(StateSet::OVERRIDE_RENDERBIN_DETAILS): return "OVERRIDE"; |
|---|
| 725 | } |
|---|
| 726 | return ""; |
|---|
| 727 | } |
|---|