| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <assert.h> |
|---|
| 21 | #include <osg/Geode> |
|---|
| 22 | #include <osg/Billboard> |
|---|
| 23 | #include <osg/Geometry> |
|---|
| 24 | #include <osg/Texture2D> |
|---|
| 25 | #include <osg/CullFace> |
|---|
| 26 | #include <osg/BlendFunc> |
|---|
| 27 | #include <osgUtil/TransformAttributeFunctor> |
|---|
| 28 | #include "Registry.h" |
|---|
| 29 | #include "Document.h" |
|---|
| 30 | #include "RecordInputStream.h" |
|---|
| 31 | |
|---|
| 32 | #include <algorithm> |
|---|
| 33 | |
|---|
| 34 | namespace flt { |
|---|
| 35 | |
|---|
| 36 | template<class ARRAY> |
|---|
| 37 | void reverseWindingOrder( ARRAY* data, GLenum mode, GLint first, GLint last ) |
|---|
| 38 | { |
|---|
| 39 | switch( mode ) |
|---|
| 40 | { |
|---|
| 41 | case osg::PrimitiveSet::TRIANGLES: |
|---|
| 42 | case osg::PrimitiveSet::QUADS: |
|---|
| 43 | case osg::PrimitiveSet::POLYGON: |
|---|
| 44 | |
|---|
| 45 | std::reverse(data->begin()+first, data->begin()+last); |
|---|
| 46 | break; |
|---|
| 47 | case osg::PrimitiveSet::TRIANGLE_STRIP: |
|---|
| 48 | case osg::PrimitiveSet::QUAD_STRIP: |
|---|
| 49 | |
|---|
| 50 | for( GLint i = first; i < last-1; i+=2 ) |
|---|
| 51 | { |
|---|
| 52 | std::swap( (*data)[i], (*data)[i+1] ); |
|---|
| 53 | } |
|---|
| 54 | break; |
|---|
| 55 | case osg::PrimitiveSet::TRIANGLE_FAN: |
|---|
| 56 | |
|---|
| 57 | std::reverse(data->begin()+first+1, data->begin()+last); |
|---|
| 58 | break; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void addDrawableAndReverseWindingOrder( osg::Geode* geode ) |
|---|
| 63 | { |
|---|
| 64 | |
|---|
| 65 | std::vector<osg::Geometry*> new_drawables; |
|---|
| 66 | |
|---|
| 67 | for (size_t i=0; i<geode->getNumDrawables(); ++i) |
|---|
| 68 | { |
|---|
| 69 | const osg::Geometry* geometry = dynamic_cast<const osg::Geometry*>(geode->getDrawable(i)); |
|---|
| 70 | if(geometry) |
|---|
| 71 | { |
|---|
| 72 | osg::Geometry* geom = new osg::Geometry(*geometry |
|---|
| 73 | , osg::CopyOp::DEEP_COPY_ARRAYS | osg::CopyOp::DEEP_COPY_PRIMITIVES); |
|---|
| 74 | new_drawables.push_back(geom); |
|---|
| 75 | |
|---|
| 76 | for( size_t i = 0; i < geom->getNumPrimitiveSets( ); ++i ) |
|---|
| 77 | { |
|---|
| 78 | osg::DrawArrays* drawarray = dynamic_cast<osg::DrawArrays*>( geom->getPrimitiveSet( i ) ); |
|---|
| 79 | if( drawarray ) |
|---|
| 80 | { |
|---|
| 81 | GLint first = drawarray->getFirst(); |
|---|
| 82 | GLint last = drawarray->getFirst()+drawarray->getCount(); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray()); |
|---|
| 86 | if( vertices ) |
|---|
| 87 | { |
|---|
| 88 | reverseWindingOrder( vertices, drawarray->getMode(), first, last ); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | if( geom->getNormalBinding( ) == osg::Geometry::BIND_PER_VERTEX ) |
|---|
| 92 | { |
|---|
| 93 | osg::Vec3Array* normals = dynamic_cast<osg::Vec3Array*>(geom->getNormalArray()); |
|---|
| 94 | if( normals ) |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | for( GLint i = first; i < last; ++i ) |
|---|
| 98 | { |
|---|
| 99 | (*normals)[i] = -(*normals)[i]; |
|---|
| 100 | } |
|---|
| 101 | reverseWindingOrder( normals, drawarray->getMode(), first, last ); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | if( geom->getColorBinding( ) == osg::Geometry::BIND_PER_VERTEX ) |
|---|
| 106 | { |
|---|
| 107 | osg::Vec4Array* colors = dynamic_cast<osg::Vec4Array*>(geom->getColorArray()); |
|---|
| 108 | if( colors ) |
|---|
| 109 | { |
|---|
| 110 | reverseWindingOrder( colors, drawarray->getMode(), first, last ); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | for( size_t i = 0; i < geom->getNumTexCoordArrays(); ++i ) |
|---|
| 115 | { |
|---|
| 116 | osg::Vec2Array* UVs = dynamic_cast<osg::Vec2Array*>(geom->getTexCoordArray(i)); |
|---|
| 117 | if( UVs ) |
|---|
| 118 | { |
|---|
| 119 | reverseWindingOrder( UVs, drawarray->getMode(), first, last ); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | for( size_t i = 0; i < new_drawables.size( ); ++i ) |
|---|
| 129 | { |
|---|
| 130 | geode->addDrawable( new_drawables[i] ); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | class Face : public PrimaryRecord |
|---|
| 137 | { |
|---|
| 138 | |
|---|
| 139 | static const unsigned int TERRAIN_BIT = 0x80000000u >> 0; |
|---|
| 140 | static const unsigned int NO_COLOR_BIT = 0x80000000u >> 1; |
|---|
| 141 | static const unsigned int NO_ALT_COLOR_BIT = 0x80000000u >> 2; |
|---|
| 142 | static const unsigned int PACKED_COLOR_BIT = 0x80000000u >> 3; |
|---|
| 143 | static const unsigned int FOOTPRINT_BIT = 0x80000000u >> 4; |
|---|
| 144 | static const unsigned int HIDDEN_BIT = 0x80000000u >> 5; |
|---|
| 145 | static const unsigned int ROOFLINE_BIT = 0x80000000u >> 6; |
|---|
| 146 | |
|---|
| 147 | osg::Vec4 _primaryColor; |
|---|
| 148 | uint8 _drawFlag; |
|---|
| 149 | uint8 _template; |
|---|
| 150 | uint16 _transparency; |
|---|
| 151 | uint32 _flags; |
|---|
| 152 | uint8 _lightMode; |
|---|
| 153 | |
|---|
| 154 | osg::ref_ptr<osg::Geode> _geode; |
|---|
| 155 | osg::ref_ptr<osg::Geometry> _geometry; |
|---|
| 156 | |
|---|
| 157 | public: |
|---|
| 158 | |
|---|
| 159 | Face() : |
|---|
| 160 | _primaryColor(1,1,1,1), |
|---|
| 161 | _drawFlag(SOLID_NO_BACKFACE), |
|---|
| 162 | _template(FIXED_NO_ALPHA_BLENDING), |
|---|
| 163 | _transparency(0), |
|---|
| 164 | _flags(0), |
|---|
| 165 | _lightMode(FACE_COLOR) |
|---|
| 166 | { |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | META_Record(Face) |
|---|
| 170 | |
|---|
| 171 | META_setID(_geode) |
|---|
| 172 | META_setComment(_geode) |
|---|
| 173 | META_setMultitexture(_geode) |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | enum DrawMode |
|---|
| 177 | { |
|---|
| 178 | SOLID_BACKFACED = 0, |
|---|
| 179 | SOLID_NO_BACKFACE = 1, |
|---|
| 180 | WIREFRAME_CLOSED = 2, |
|---|
| 181 | WIREFRAME_NOT_CLOSED = 3, |
|---|
| 182 | SURROUND_ALTERNATE_COLOR = 4, |
|---|
| 183 | OMNIDIRECTIONAL_LIGHT = 8, |
|---|
| 184 | UNIDIRECTIONAL_LIGHT = 9, |
|---|
| 185 | BIDIRECTIONAL_LIGHT = 10 |
|---|
| 186 | }; |
|---|
| 187 | |
|---|
| 188 | inline DrawMode getDrawMode() const { return (DrawMode)_drawFlag; } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | enum LightMode |
|---|
| 192 | { |
|---|
| 193 | FACE_COLOR = 0, |
|---|
| 194 | VERTEX_COLOR = 1, |
|---|
| 195 | FACE_COLOR_LIGHTING = 2, |
|---|
| 196 | VERTEX_COLOR_LIGHTING = 3 |
|---|
| 197 | }; |
|---|
| 198 | |
|---|
| 199 | inline LightMode getLightMode() const { return (LightMode)_lightMode; } |
|---|
| 200 | inline bool isLit() const { return (_lightMode==FACE_COLOR_LIGHTING) || (_lightMode==VERTEX_COLOR_LIGHTING); } |
|---|
| 201 | inline bool isGouraud() const { return (_lightMode==VERTEX_COLOR) || (_lightMode==VERTEX_COLOR_LIGHTING); } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | inline bool noColor() const { return (_flags & NO_COLOR_BIT)!=0; } |
|---|
| 205 | inline bool isHidden() const { return (_flags & HIDDEN_BIT)!=0; } |
|---|
| 206 | inline bool isTerrain() const { return (_flags & TERRAIN_BIT)!=0; } |
|---|
| 207 | inline bool isFootprint() const { return (_flags & FOOTPRINT_BIT)!=0; } |
|---|
| 208 | inline bool isRoofline() const { return (_flags & ROOFLINE_BIT)!=0; } |
|---|
| 209 | inline bool packedColorMode() const { return (_flags & PACKED_COLOR_BIT)!=0; } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | enum TemplateMode |
|---|
| 213 | { |
|---|
| 214 | FIXED_NO_ALPHA_BLENDING = 0, |
|---|
| 215 | FIXED_ALPHA_BLENDING = 1, |
|---|
| 216 | AXIAL_ROTATE_WITH_ALPHA_BLENDING = 2, |
|---|
| 217 | POINT_ROTATE_WITH_ALPHA_BLENDING = 4 |
|---|
| 218 | }; |
|---|
| 219 | |
|---|
| 220 | inline TemplateMode getTemplateMode() const { return (TemplateMode)_template; } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | inline bool isAlphaBlend() const |
|---|
| 224 | { |
|---|
| 225 | return (_template==FIXED_ALPHA_BLENDING) || |
|---|
| 226 | (_template==AXIAL_ROTATE_WITH_ALPHA_BLENDING) || |
|---|
| 227 | (_template==POINT_ROTATE_WITH_ALPHA_BLENDING); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | inline osg::Vec4 getPrimaryColor() const { return _primaryColor; } |
|---|
| 231 | inline float getTransparency() const { return (float)_transparency / 65535.0f; } |
|---|
| 232 | inline bool isTransparent() const { return _transparency > 0; } |
|---|
| 233 | |
|---|
| 234 | virtual void addChild(osg::Node& child) |
|---|
| 235 | { |
|---|
| 236 | |
|---|
| 237 | if (_parent.valid()) |
|---|
| 238 | _parent->addChild(child); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | virtual void addVertex(Vertex& vertex) |
|---|
| 242 | { |
|---|
| 243 | osg::Vec3Array* vertices = getOrCreateVertexArray(*_geometry); |
|---|
| 244 | vertices->push_back(vertex._coord); |
|---|
| 245 | |
|---|
| 246 | if (isGouraud()) |
|---|
| 247 | { |
|---|
| 248 | osg::Vec4Array* colors = getOrCreateColorArray(*_geometry); |
|---|
| 249 | if (vertex.validColor()) |
|---|
| 250 | { |
|---|
| 251 | colors->push_back(vertex._color); |
|---|
| 252 | } |
|---|
| 253 | else |
|---|
| 254 | { |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | colors->push_back(_primaryColor); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | bool strict = false; |
|---|
| 262 | if (strict) |
|---|
| 263 | { |
|---|
| 264 | if (vertex.validNormal()) |
|---|
| 265 | { |
|---|
| 266 | osg::Vec3Array* normals = getOrCreateNormalArray(*_geometry); |
|---|
| 267 | normals->push_back(vertex._normal); |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | else |
|---|
| 271 | { |
|---|
| 272 | |
|---|
| 273 | if (isLit()) |
|---|
| 274 | { |
|---|
| 275 | osg::Vec3Array* normals = getOrCreateNormalArray(*_geometry); |
|---|
| 276 | |
|---|
| 277 | if (vertex.validNormal()) |
|---|
| 278 | normals->push_back(vertex._normal); |
|---|
| 279 | else |
|---|
| 280 | { |
|---|
| 281 | |
|---|
| 282 | if (normals->empty()) |
|---|
| 283 | normals->push_back(osg::Vec3(0,0,1)); |
|---|
| 284 | else |
|---|
| 285 | normals->push_back(normals->back()); |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | for (int layer=0; layer<Vertex::MAX_LAYERS; layer++) |
|---|
| 291 | { |
|---|
| 292 | if (vertex.validUV(layer)) |
|---|
| 293 | { |
|---|
| 294 | osg::Vec2Array* UVs = getOrCreateTextureArray(*_geometry,layer); |
|---|
| 295 | UVs->push_back(vertex._uv[layer]); |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | virtual void addVertexUV(int unit, const osg::Vec2& uv) |
|---|
| 301 | { |
|---|
| 302 | osg::Vec2Array* UVs = getOrCreateTextureArray(*_geometry,unit); |
|---|
| 303 | UVs->push_back(uv); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | virtual void addMorphVertex(Vertex& vertex0, Vertex& ) |
|---|
| 307 | { |
|---|
| 308 | osg::Vec3Array* vertices = getOrCreateVertexArray(*_geometry); |
|---|
| 309 | vertices->push_back(vertex0._coord); |
|---|
| 310 | |
|---|
| 311 | if (isGouraud()) |
|---|
| 312 | { |
|---|
| 313 | osg::Vec4Array* colors = getOrCreateColorArray(*_geometry); |
|---|
| 314 | if (vertex0.validColor()) |
|---|
| 315 | { |
|---|
| 316 | colors->push_back(vertex0._color); |
|---|
| 317 | } |
|---|
| 318 | else |
|---|
| 319 | { |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | colors->push_back(_primaryColor); |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | if (vertex0.validNormal()) |
|---|
| 327 | { |
|---|
| 328 | osg::Vec3Array* normals = getOrCreateNormalArray(*_geometry); |
|---|
| 329 | normals->push_back(vertex0._normal); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | for (int layer=0; layer<Vertex::MAX_LAYERS; layer++) |
|---|
| 333 | { |
|---|
| 334 | if (vertex0.validUV(layer)) |
|---|
| 335 | { |
|---|
| 336 | osg::Vec2Array* UVs = getOrCreateTextureArray(*_geometry,layer); |
|---|
| 337 | UVs->push_back(vertex0._uv[layer]); |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | protected: |
|---|
| 343 | |
|---|
| 344 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 345 | { |
|---|
| 346 | std::string id = in.readString(8); |
|---|
| 347 | in.readInt32(); |
|---|
| 348 | in.readInt16(); |
|---|
| 349 | _drawFlag = in.readUInt8(SOLID_NO_BACKFACE); |
|---|
| 350 | uint8 texturedWhite = in.readUInt8(); |
|---|
| 351 | int16 primaryNameIndex = in.readInt16(-1); |
|---|
| 352 | in.readInt16(-1); |
|---|
| 353 | in.forward(1); |
|---|
| 354 | _template = in.readUInt8(FIXED_NO_ALPHA_BLENDING); |
|---|
| 355 | in.readInt16(-1); |
|---|
| 356 | int textureIndex = in.readInt16(-1); |
|---|
| 357 | int materialIndex = in.readInt16(-1); |
|---|
| 358 | in.readInt16(); |
|---|
| 359 | in.readInt16(); |
|---|
| 360 | in.readInt32(-1); |
|---|
| 361 | _transparency = in.readUInt16(0); |
|---|
| 362 | |
|---|
| 363 | in.readUInt8(); |
|---|
| 364 | in.readUInt8(); |
|---|
| 365 | _flags = in.readUInt32(0); |
|---|
| 366 | _lightMode = in.readUInt8(FACE_COLOR); |
|---|
| 367 | in.forward(7); |
|---|
| 368 | osg::Vec4 primaryPackedColor = in.readColor32(); |
|---|
| 369 | osg::Vec4 secondaryPackedColor = in.readColor32(); |
|---|
| 370 | |
|---|
| 371 | in.readInt16(-1); |
|---|
| 372 | in.forward(2); |
|---|
| 373 | int primaryColorIndex = in.readInt32(-1); |
|---|
| 374 | in.readInt32(-1); |
|---|
| 375 | |
|---|
| 376 | in.forward(2); |
|---|
| 377 | int shaderIndex = in.readInt16(-1); |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | switch (_template) |
|---|
| 381 | { |
|---|
| 382 | case AXIAL_ROTATE_WITH_ALPHA_BLENDING: |
|---|
| 383 | { |
|---|
| 384 | osg::Billboard* billboard = new osg::Billboard; |
|---|
| 385 | billboard->setMode(osg::Billboard::AXIAL_ROT); |
|---|
| 386 | _geode = billboard; |
|---|
| 387 | } |
|---|
| 388 | break; |
|---|
| 389 | case POINT_ROTATE_WITH_ALPHA_BLENDING: |
|---|
| 390 | { |
|---|
| 391 | osg::Billboard* billboard = new osg::Billboard; |
|---|
| 392 | billboard->setMode(osg::Billboard::POINT_ROT_WORLD); |
|---|
| 393 | _geode = billboard; |
|---|
| 394 | } |
|---|
| 395 | break; |
|---|
| 396 | default: |
|---|
| 397 | _geode = new osg::Geode; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | _geode->setDataVariance(osg::Object::STATIC); |
|---|
| 401 | _geode->setName(id); |
|---|
| 402 | |
|---|
| 403 | _geometry = new osg::Geometry; |
|---|
| 404 | _geometry->setDataVariance(osg::Object::STATIC); |
|---|
| 405 | _geode->addDrawable(_geometry.get()); |
|---|
| 406 | |
|---|
| 407 | |
|---|
| 408 | osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet; |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | if (isHidden()) |
|---|
| 412 | _geode->setNodeMask(0); |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | if (texturedWhite!=0 && textureIndex>=0) |
|---|
| 416 | { |
|---|
| 417 | _primaryColor = osg::Vec4(1,1,1,1); |
|---|
| 418 | } |
|---|
| 419 | else |
|---|
| 420 | { |
|---|
| 421 | if (packedColorMode()) |
|---|
| 422 | { |
|---|
| 423 | _primaryColor = primaryPackedColor; |
|---|
| 424 | } |
|---|
| 425 | else |
|---|
| 426 | { |
|---|
| 427 | if (document.version() < VERSION_15_1) |
|---|
| 428 | _primaryColor = document.getColorPool()->getColor(primaryNameIndex); |
|---|
| 429 | |
|---|
| 430 | else |
|---|
| 431 | _primaryColor = document.getColorPool()->getColor(primaryColorIndex); |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | stateset->setMode(GL_LIGHTING, isLit() ? osg::StateAttribute::ON : osg::StateAttribute::OFF); |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | if (isLit() || materialIndex>=0) |
|---|
| 440 | { |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | osg::Vec4 col = _primaryColor; |
|---|
| 444 | col.a() = 1.0f - getTransparency(); |
|---|
| 445 | osg::Material* material = document.getOrCreateMaterialPool()->getOrCreateMaterial(materialIndex,col); |
|---|
| 446 | stateset->setAttribute(material); |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | if (shaderIndex >= 0) |
|---|
| 451 | { |
|---|
| 452 | ShaderPool* sp = document.getOrCreateShaderPool(); |
|---|
| 453 | osg::Program* program = sp->get(shaderIndex); |
|---|
| 454 | if (program) |
|---|
| 455 | stateset->setAttributeAndModes(program, osg::StateAttribute::ON); |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | TexturePool* tp = document.getOrCreateTexturePool(); |
|---|
| 460 | osg::StateSet* textureStateSet = tp->get(textureIndex); |
|---|
| 461 | if (textureStateSet) |
|---|
| 462 | { |
|---|
| 463 | |
|---|
| 464 | stateset->merge(*textureStateSet); |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | |
|---|
| 468 | switch(_drawFlag) |
|---|
| 469 | { |
|---|
| 470 | case SOLID_BACKFACED: |
|---|
| 471 | { |
|---|
| 472 | static osg::ref_ptr<osg::CullFace> cullFace = new osg::CullFace(osg::CullFace::BACK); |
|---|
| 473 | stateset->setAttributeAndModes(cullFace.get(), osg::StateAttribute::ON); |
|---|
| 474 | break; |
|---|
| 475 | } |
|---|
| 476 | case SOLID_NO_BACKFACE: |
|---|
| 477 | if( document.getReplaceDoubleSidedPolys( ) ) |
|---|
| 478 | { |
|---|
| 479 | static osg::ref_ptr<osg::CullFace> cullFace = new osg::CullFace(osg::CullFace::BACK); |
|---|
| 480 | stateset->setAttributeAndModes(cullFace.get(), osg::StateAttribute::ON); |
|---|
| 481 | } |
|---|
| 482 | else |
|---|
| 483 | { |
|---|
| 484 | stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
|---|
| 485 | } |
|---|
| 486 | break; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | if (document.subfaceLevel() > 0) |
|---|
| 491 | { |
|---|
| 492 | stateset->setAttributeAndModes(document.getSubSurfacePolygonOffset(document.subfaceLevel()), osg::StateAttribute::ON); |
|---|
| 493 | stateset->setAttribute(document.getSubSurfaceDepth()); |
|---|
| 494 | |
|---|
| 495 | stateset->setRenderBinDetails(document.subfaceLevel(),"RenderBin"); |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | _geode->setStateSet(stateset.get()); |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | if (_parent.valid()) |
|---|
| 502 | _parent->addChild(*_geode); |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | osg::PrimitiveSet::Mode getPrimitiveSetMode(int numVertices) |
|---|
| 506 | { |
|---|
| 507 | switch(getDrawMode()) |
|---|
| 508 | { |
|---|
| 509 | case WIREFRAME_NOT_CLOSED: |
|---|
| 510 | return osg::PrimitiveSet::LINE_STRIP; |
|---|
| 511 | case WIREFRAME_CLOSED: |
|---|
| 512 | return osg::PrimitiveSet::LINE_LOOP; |
|---|
| 513 | case OMNIDIRECTIONAL_LIGHT: |
|---|
| 514 | case UNIDIRECTIONAL_LIGHT: |
|---|
| 515 | case BIDIRECTIONAL_LIGHT: |
|---|
| 516 | return osg::PrimitiveSet::POINTS; |
|---|
| 517 | default: break; |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | switch (numVertices) |
|---|
| 521 | { |
|---|
| 522 | case 1: return osg::PrimitiveSet::POINTS; |
|---|
| 523 | case 2: return osg::PrimitiveSet::LINES; |
|---|
| 524 | case 3: return osg::PrimitiveSet::TRIANGLES; |
|---|
| 525 | case 4: return osg::PrimitiveSet::QUADS; |
|---|
| 526 | default: break; |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | return osg::PrimitiveSet::POLYGON; |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | virtual void dispose(Document& document) |
|---|
| 533 | { |
|---|
| 534 | if (_geode.valid()) |
|---|
| 535 | { |
|---|
| 536 | |
|---|
| 537 | if (_matrix.valid()) |
|---|
| 538 | { |
|---|
| 539 | insertMatrixTransform(*_geode,*_matrix,_numberOfReplications); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | for (unsigned int i=0; i<_geode->getNumDrawables(); ++i) |
|---|
| 544 | { |
|---|
| 545 | osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(_geode->getDrawable(i)); |
|---|
| 546 | if (geometry) |
|---|
| 547 | { |
|---|
| 548 | osg::Array* vertices = geometry->getVertexArray(); |
|---|
| 549 | if (vertices) |
|---|
| 550 | { |
|---|
| 551 | GLint first = 0; |
|---|
| 552 | GLsizei count = vertices->getNumElements(); |
|---|
| 553 | osg::PrimitiveSet::Mode mode = getPrimitiveSetMode(count); |
|---|
| 554 | geometry->addPrimitiveSet(new osg::DrawArrays(mode,first,count)); |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | if (isGouraud()) |
|---|
| 559 | { |
|---|
| 560 | |
|---|
| 561 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 562 | } |
|---|
| 563 | else |
|---|
| 564 | { |
|---|
| 565 | |
|---|
| 566 | osg::Vec4 col = getPrimaryColor(); |
|---|
| 567 | col[3] = 1.0f - getTransparency(); |
|---|
| 568 | |
|---|
| 569 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 570 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 571 | (*colors)[0] = col; |
|---|
| 572 | geometry->setColorArray(colors); |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | if (isLit()) |
|---|
| 577 | { |
|---|
| 578 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX ); |
|---|
| 579 | } |
|---|
| 580 | else |
|---|
| 581 | { |
|---|
| 582 | geometry->setNormalBinding(osg::Geometry::BIND_OFF); |
|---|
| 583 | geometry->setNormalArray(NULL); |
|---|
| 584 | } |
|---|
| 585 | } |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | if( getDrawMode( ) == SOLID_NO_BACKFACE && document.getReplaceDoubleSidedPolys( ) ) |
|---|
| 589 | { |
|---|
| 590 | addDrawableAndReverseWindingOrder( _geode.get() ); |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | osg::StateSet* stateset = _geode->getOrCreateStateSet(); |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | bool isImageTranslucent = false; |
|---|
| 597 | if (document.getUseTextureAlphaForTransparancyBinning()) |
|---|
| 598 | { |
|---|
| 599 | for (unsigned int i=0; i<stateset->getTextureAttributeList().size(); ++i) |
|---|
| 600 | { |
|---|
| 601 | osg::StateAttribute* sa = stateset->getTextureAttribute(i,osg::StateAttribute::TEXTURE); |
|---|
| 602 | osg::Texture2D* texture = dynamic_cast<osg::Texture2D*>(sa); |
|---|
| 603 | if (texture) |
|---|
| 604 | { |
|---|
| 605 | osg::Image* image = texture->getImage(); |
|---|
| 606 | if (image && image->isImageTranslucent()) |
|---|
| 607 | isImageTranslucent = true; |
|---|
| 608 | } |
|---|
| 609 | } |
|---|
| 610 | } |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | bool isMaterialTransparent = false; |
|---|
| 614 | osg::Material* material = dynamic_cast<osg::Material*>(stateset->getAttribute(osg::StateAttribute::MATERIAL)); |
|---|
| 615 | if (material) |
|---|
| 616 | { |
|---|
| 617 | isMaterialTransparent = material->getDiffuse(osg::Material::FRONT).a() < 0.99f; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | if (isAlphaBlend() || isTransparent() || isImageTranslucent || isMaterialTransparent) |
|---|
| 622 | { |
|---|
| 623 | static osg::ref_ptr<osg::BlendFunc> blendFunc = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); |
|---|
| 624 | stateset->setAttributeAndModes(blendFunc.get(), osg::StateAttribute::ON); |
|---|
| 625 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | if (document.getUseBillboardCenter()) |
|---|
| 629 | { |
|---|
| 630 | |
|---|
| 631 | osg::Billboard* billboard = dynamic_cast<osg::Billboard*>(_geode.get()); |
|---|
| 632 | if (billboard) |
|---|
| 633 | { |
|---|
| 634 | for (unsigned int i=0; i<billboard->getNumDrawables(); ++i) |
|---|
| 635 | { |
|---|
| 636 | osg::BoundingBox bb = billboard->getDrawable(i)->getBound(); |
|---|
| 637 | billboard->setPosition(i,bb.center()); |
|---|
| 638 | |
|---|
| 639 | osgUtil::TransformAttributeFunctor tf(osg::Matrix::translate(-bb.center())); |
|---|
| 640 | billboard->getDrawable(i)->accept(tf); |
|---|
| 641 | |
|---|
| 642 | billboard->getDrawable(i)->dirtyBound(); |
|---|
| 643 | } |
|---|
| 644 | |
|---|
| 645 | billboard->dirtyBound(); |
|---|
| 646 | } |
|---|
| 647 | } |
|---|
| 648 | } |
|---|
| 649 | } |
|---|
| 650 | }; |
|---|
| 651 | |
|---|
| 652 | REGISTER_FLTRECORD(Face, FACE_OP) |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | class VertexListRecord : public PrimaryRecord |
|---|
| 661 | { |
|---|
| 662 | public: |
|---|
| 663 | |
|---|
| 664 | VertexListRecord() {} |
|---|
| 665 | |
|---|
| 666 | META_Record(VertexListRecord) |
|---|
| 667 | |
|---|
| 668 | virtual void addVertex(Vertex& vertex) |
|---|
| 669 | { |
|---|
| 670 | |
|---|
| 671 | if (_parent.valid()) |
|---|
| 672 | _parent->addVertex(vertex); |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | virtual void addVertexUV(int layer,const osg::Vec2& uv) |
|---|
| 676 | { |
|---|
| 677 | |
|---|
| 678 | if (_parent.valid()) |
|---|
| 679 | _parent->addVertexUV(layer,uv); |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | protected: |
|---|
| 683 | |
|---|
| 684 | virtual ~VertexListRecord() {} |
|---|
| 685 | |
|---|
| 686 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 687 | { |
|---|
| 688 | VertexPool* vp = document.getVertexPool(); |
|---|
| 689 | if (vp) |
|---|
| 690 | { |
|---|
| 691 | int vertices = (in.getRecordSize()-4) / 4; |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | RecordInputStream inVP(vp->rdbuf()); |
|---|
| 695 | for (int n=0; n<vertices; n++) |
|---|
| 696 | { |
|---|
| 697 | |
|---|
| 698 | uint32 pos = in.readUInt32(); |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | inVP.seekg((std::istream::pos_type)pos); |
|---|
| 702 | inVP.readRecord(document); |
|---|
| 703 | } |
|---|
| 704 | } |
|---|
| 705 | } |
|---|
| 706 | }; |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | REGISTER_FLTRECORD(VertexListRecord, VERTEX_LIST_OP) |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | class MorphVertexList : public PrimaryRecord |
|---|
| 717 | { |
|---|
| 718 | enum Mode |
|---|
| 719 | { |
|---|
| 720 | UNDEFINED, |
|---|
| 721 | MORPH_0, |
|---|
| 722 | MORPH_100 |
|---|
| 723 | }; |
|---|
| 724 | |
|---|
| 725 | Mode _mode; |
|---|
| 726 | Vertex _vertex0; |
|---|
| 727 | Vertex _vertex100; |
|---|
| 728 | |
|---|
| 729 | public: |
|---|
| 730 | |
|---|
| 731 | MorphVertexList(): |
|---|
| 732 | _mode(UNDEFINED) |
|---|
| 733 | { |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | META_Record(MorphVertexList) |
|---|
| 737 | |
|---|
| 738 | virtual void addVertex(Vertex& vertex) |
|---|
| 739 | { |
|---|
| 740 | switch (_mode) |
|---|
| 741 | { |
|---|
| 742 | case MORPH_0: |
|---|
| 743 | _vertex0 = vertex; |
|---|
| 744 | break; |
|---|
| 745 | case MORPH_100: |
|---|
| 746 | _vertex100 = vertex; |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | if (_parent.valid()) |
|---|
| 750 | _parent->addMorphVertex(_vertex0, _vertex100); |
|---|
| 751 | break; |
|---|
| 752 | case UNDEFINED: |
|---|
| 753 | break; |
|---|
| 754 | } |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | protected: |
|---|
| 765 | |
|---|
| 766 | virtual ~MorphVertexList() {} |
|---|
| 767 | |
|---|
| 768 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 769 | { |
|---|
| 770 | VertexPool* vp = document.getVertexPool(); |
|---|
| 771 | if (vp) |
|---|
| 772 | { |
|---|
| 773 | int vertices = (in.getRecordSize()-4) / 8; |
|---|
| 774 | |
|---|
| 775 | |
|---|
| 776 | RecordInputStream inVP(vp->rdbuf()); |
|---|
| 777 | for (int n=0; n<vertices; n++) |
|---|
| 778 | { |
|---|
| 779 | |
|---|
| 780 | uint32 offset0 = in.readUInt32(); |
|---|
| 781 | uint32 offset100 = in.readUInt32(); |
|---|
| 782 | |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | |
|---|
| 786 | _mode = MORPH_0; |
|---|
| 787 | inVP.seekg((std::istream::pos_type)offset0); |
|---|
| 788 | inVP.readRecord(document); |
|---|
| 789 | |
|---|
| 790 | |
|---|
| 791 | _mode = MORPH_100; |
|---|
| 792 | inVP.seekg((std::istream::pos_type)offset100); |
|---|
| 793 | inVP.readRecord(document); |
|---|
| 794 | } |
|---|
| 795 | } |
|---|
| 796 | } |
|---|
| 797 | }; |
|---|
| 798 | |
|---|
| 799 | REGISTER_FLTRECORD(MorphVertexList, MORPH_VERTEX_LIST_OP) |
|---|
| 800 | |
|---|
| 801 | |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | class Mesh : public PrimaryRecord |
|---|
| 806 | { |
|---|
| 807 | |
|---|
| 808 | static const unsigned int TERRAIN_BIT = 0x80000000u >> 0; |
|---|
| 809 | static const unsigned int NO_COLOR_BIT = 0x80000000u >> 1; |
|---|
| 810 | static const unsigned int NO_ALT_COLOR_BIT = 0x80000000u >> 2; |
|---|
| 811 | static const unsigned int PACKED_COLOR_BIT = 0x80000000u >> 3; |
|---|
| 812 | static const unsigned int FOOTPRINT_BIT = 0x80000000u >> 4; |
|---|
| 813 | static const unsigned int HIDDEN_BIT = 0x80000000u >> 5; |
|---|
| 814 | static const unsigned int ROOFLINE_BIT = 0x80000000u >> 6; |
|---|
| 815 | |
|---|
| 816 | osg::Vec4 _primaryColor; |
|---|
| 817 | uint8 _drawFlag; |
|---|
| 818 | uint8 _template; |
|---|
| 819 | uint16 _transparency; |
|---|
| 820 | uint32 _flags; |
|---|
| 821 | uint8 _lightMode; |
|---|
| 822 | |
|---|
| 823 | osg::ref_ptr<osg::Geode> _geode; |
|---|
| 824 | |
|---|
| 825 | public: |
|---|
| 826 | |
|---|
| 827 | Mesh() : |
|---|
| 828 | _primaryColor(1,1,1,1), |
|---|
| 829 | _drawFlag(SOLID_NO_BACKFACE), |
|---|
| 830 | _template(FIXED_NO_ALPHA_BLENDING), |
|---|
| 831 | _transparency(0), |
|---|
| 832 | _flags(0), |
|---|
| 833 | _lightMode(FACE_COLOR) |
|---|
| 834 | { |
|---|
| 835 | } |
|---|
| 836 | |
|---|
| 837 | META_Record(Mesh) |
|---|
| 838 | |
|---|
| 839 | META_setID(_geode) |
|---|
| 840 | META_setComment(_geode) |
|---|
| 841 | META_setMultitexture(_geode) |
|---|
| 842 | |
|---|
| 843 | |
|---|
| 844 | enum DrawMode |
|---|
| 845 | { |
|---|
| 846 | SOLID_BACKFACED = 0, |
|---|
| 847 | SOLID_NO_BACKFACE = 1, |
|---|
| 848 | WIREFRAME_CLOSED = 2, |
|---|
| 849 | WIREFRAME_NOT_CLOSED = 3, |
|---|
| 850 | SURROUND_ALTERNATE_COLOR = 4, |
|---|
| 851 | OMNIDIRECTIONAL_LIGHT = 8, |
|---|
| 852 | UNIDIRECTIONAL_LIGHT = 9, |
|---|
| 853 | BIDIRECTIONAL_LIGHT = 10 |
|---|
| 854 | }; |
|---|
| 855 | |
|---|
| 856 | inline DrawMode getDrawMode() const { return (DrawMode)_drawFlag; } |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | enum LightMode |
|---|
| 860 | { |
|---|
| 861 | FACE_COLOR = 0, |
|---|
| 862 | VERTEX_COLOR = 1, |
|---|
| 863 | FACE_COLOR_LIGHTING = 2, |
|---|
| 864 | VERTEX_COLOR_LIGHTING = 3 |
|---|
| 865 | }; |
|---|
| 866 | |
|---|
| 867 | inline LightMode getLightMode() const { return (LightMode)_lightMode; } |
|---|
| 868 | inline bool isLit() const { return (_lightMode==FACE_COLOR_LIGHTING) || (_lightMode==VERTEX_COLOR_LIGHTING); } |
|---|
| 869 | inline bool isGouraud() const { return (_lightMode==VERTEX_COLOR) || (_lightMode==VERTEX_COLOR_LIGHTING); } |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | inline bool noColor() const { return (_flags & NO_COLOR_BIT)!=0; } |
|---|
| 873 | inline bool isHidden() const { return (_flags & HIDDEN_BIT)!=0; } |
|---|
| 874 | inline bool isTerrain() const { return (_flags & TERRAIN_BIT)!=0; } |
|---|
| 875 | inline bool isFootprint() const { return (_flags & FOOTPRINT_BIT)!=0; } |
|---|
| 876 | inline bool isRoofline() const { return (_flags & ROOFLINE_BIT)!=0; } |
|---|
| 877 | inline bool packedColorMode() const { return (_flags & PACKED_COLOR_BIT)!=0; } |
|---|
| 878 | |
|---|
| 879 | |
|---|
| 880 | enum TemplateMode |
|---|
| 881 | { |
|---|
| 882 | FIXED_NO_ALPHA_BLENDING = 0, |
|---|
| 883 | FIXED_ALPHA_BLENDING = 1, |
|---|
| 884 | AXIAL_ROTATE_WITH_ALPHA_BLENDING = 2, |
|---|
| 885 | POINT_ROTATE_WITH_ALPHA_BLENDING = 4 |
|---|
| 886 | }; |
|---|
| 887 | |
|---|
| 888 | inline TemplateMode getTemplateMode() const { return (TemplateMode)_template; } |
|---|
| 889 | |
|---|
| 890 | |
|---|
| 891 | inline bool isAlphaBlend() const |
|---|
| 892 | { |
|---|
| 893 | return (_template==FIXED_ALPHA_BLENDING) || |
|---|
| 894 | (_template==AXIAL_ROTATE_WITH_ALPHA_BLENDING) || |
|---|
| 895 | (_template==POINT_ROTATE_WITH_ALPHA_BLENDING); |
|---|
| 896 | } |
|---|
| 897 | |
|---|
| 898 | inline osg::Vec4 getPrimaryColor() const { return _primaryColor; } |
|---|
| 899 | inline float getTransparency() const { return (float)_transparency / 65535.0f; } |
|---|
| 900 | inline bool isTransparent() const { return _transparency > 0; } |
|---|
| 901 | |
|---|
| 902 | virtual void addChild(osg::Node& child) |
|---|
| 903 | { |
|---|
| 904 | |
|---|
| 905 | if (_parent.valid()) |
|---|
| 906 | _parent->addChild(child); |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | virtual void addGeometry(osg::Geometry& geometry) |
|---|
| 910 | { |
|---|
| 911 | _geode->addDrawable(&geometry); |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | protected: |
|---|
| 915 | |
|---|
| 916 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 917 | { |
|---|
| 918 | std::string id = in.readString(8); |
|---|
| 919 | in.forward(4); |
|---|
| 920 | in.readInt32(); |
|---|
| 921 | in.readInt16(); |
|---|
| 922 | _drawFlag = in.readUInt8(SOLID_NO_BACKFACE); |
|---|
| 923 | uint8 texturedWhite = in.readUInt8(); |
|---|
| 924 | int16 primaryNameIndex = in.readInt16(-1); |
|---|
| 925 | in.readInt16(-1); |
|---|
| 926 | in.forward(1); |
|---|
| 927 | _template = in.readUInt8(FIXED_NO_ALPHA_BLENDING); |
|---|
| 928 | in.readInt16(-1); |
|---|
| 929 | int textureIndex = in.readInt16(-1); |
|---|
| 930 | int materialIndex = in.readInt16(-1); |
|---|
| 931 | in.readInt16(); |
|---|
| 932 | in.readInt16(); |
|---|
| 933 | in.readInt32(-1); |
|---|
| 934 | _transparency = in.readUInt16(0); |
|---|
| 935 | |
|---|
| 936 | in.readUInt8(); |
|---|
| 937 | in.readUInt8(); |
|---|
| 938 | _flags = in.readUInt32(0); |
|---|
| 939 | _lightMode = in.readUInt8(FACE_COLOR); |
|---|
| 940 | in.forward(7); |
|---|
| 941 | osg::Vec4 primaryPackedColor = in.readColor32(); |
|---|
| 942 | osg::Vec4 secondaryPackedColor = in.readColor32(); |
|---|
| 943 | |
|---|
| 944 | in.readInt16(-1); |
|---|
| 945 | in.forward(2); |
|---|
| 946 | int primaryColorIndex = in.readInt32(-1); |
|---|
| 947 | in.readInt32(-1); |
|---|
| 948 | |
|---|
| 949 | in.forward(2); |
|---|
| 950 | int shaderIndex = in.readInt16(-1); |
|---|
| 951 | |
|---|
| 952 | |
|---|
| 953 | switch (_template) |
|---|
| 954 | { |
|---|
| 955 | case AXIAL_ROTATE_WITH_ALPHA_BLENDING: |
|---|
| 956 | { |
|---|
| 957 | osg::Billboard* billboard = new osg::Billboard; |
|---|
| 958 | billboard->setMode(osg::Billboard::AXIAL_ROT); |
|---|
| 959 | _geode = billboard; |
|---|
| 960 | } |
|---|
| 961 | break; |
|---|
| 962 | case POINT_ROTATE_WITH_ALPHA_BLENDING: |
|---|
| 963 | { |
|---|
| 964 | osg::Billboard* billboard = new osg::Billboard; |
|---|
| 965 | billboard->setMode(osg::Billboard::POINT_ROT_WORLD); |
|---|
| 966 | _geode = billboard; |
|---|
| 967 | } |
|---|
| 968 | break; |
|---|
| 969 | default: |
|---|
| 970 | _geode = new osg::Geode; |
|---|
| 971 | } |
|---|
| 972 | |
|---|
| 973 | _geode->setDataVariance(osg::Object::STATIC); |
|---|
| 974 | _geode->setName(id); |
|---|
| 975 | |
|---|
| 976 | |
|---|
| 977 | osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet; |
|---|
| 978 | |
|---|
| 979 | |
|---|
| 980 | if (isHidden()) |
|---|
| 981 | _geode->setNodeMask(0); |
|---|
| 982 | |
|---|
| 983 | |
|---|
| 984 | if (texturedWhite!=0 && textureIndex>=0) |
|---|
| 985 | { |
|---|
| 986 | _primaryColor = osg::Vec4(1,1,1,1); |
|---|
| 987 | } |
|---|
| 988 | else |
|---|
| 989 | { |
|---|
| 990 | if (packedColorMode()) |
|---|
| 991 | { |
|---|
| 992 | _primaryColor = primaryPackedColor; |
|---|
| 993 | } |
|---|
| 994 | else |
|---|
| 995 | { |
|---|
| 996 | if (document.version() < VERSION_15_1) |
|---|
| 997 | _primaryColor = document.getColorPool()->getColor(primaryNameIndex); |
|---|
| 998 | |
|---|
| 999 | else |
|---|
| 1000 | _primaryColor = document.getColorPool()->getColor(primaryColorIndex); |
|---|
| 1001 | } |
|---|
| 1002 | } |
|---|
| 1003 | |
|---|
| 1004 | |
|---|
| 1005 | stateset->setMode(GL_LIGHTING, isLit() ? osg::StateAttribute::ON : osg::StateAttribute::OFF); |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | if (isLit() || materialIndex>=0) |
|---|
| 1009 | { |
|---|
| 1010 | |
|---|
| 1011 | |
|---|
| 1012 | osg::Vec4 col = _primaryColor; |
|---|
| 1013 | col.a() = 1.0f - getTransparency(); |
|---|
| 1014 | osg::Material* material = document.getOrCreateMaterialPool()->getOrCreateMaterial(materialIndex,col); |
|---|
| 1015 | stateset->setAttribute(material); |
|---|
| 1016 | } |
|---|
| 1017 | |
|---|
| 1018 | |
|---|
| 1019 | if (shaderIndex >= 0) |
|---|
| 1020 | { |
|---|
| 1021 | ShaderPool* sp = document.getOrCreateShaderPool(); |
|---|
| 1022 | osg::Program* program = sp->get(shaderIndex); |
|---|
| 1023 | if (program) |
|---|
| 1024 | stateset->setAttributeAndModes(program, osg::StateAttribute::ON); |
|---|
| 1025 | } |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | TexturePool* tp = document.getOrCreateTexturePool(); |
|---|
| 1029 | osg::StateSet* textureStateSet = tp->get(textureIndex); |
|---|
| 1030 | if (textureStateSet) |
|---|
| 1031 | { |
|---|
| 1032 | |
|---|
| 1033 | stateset->merge(*textureStateSet); |
|---|
| 1034 | } |
|---|
| 1035 | |
|---|
| 1036 | |
|---|
| 1037 | switch(_drawFlag) |
|---|
| 1038 | { |
|---|
| 1039 | case SOLID_BACKFACED: |
|---|
| 1040 | { |
|---|
| 1041 | static osg::ref_ptr<osg::CullFace> cullFace = new osg::CullFace(osg::CullFace::BACK); |
|---|
| 1042 | stateset->setAttributeAndModes(cullFace.get(), osg::StateAttribute::ON); |
|---|
| 1043 | break; |
|---|
| 1044 | } |
|---|
| 1045 | case SOLID_NO_BACKFACE: |
|---|
| 1046 | if( document.getReplaceDoubleSidedPolys( ) ) |
|---|
| 1047 | { |
|---|
| 1048 | static osg::ref_ptr<osg::CullFace> cullFace = new osg::CullFace(osg::CullFace::BACK); |
|---|
| 1049 | stateset->setAttributeAndModes(cullFace.get(), osg::StateAttribute::ON); |
|---|
| 1050 | } |
|---|
| 1051 | else |
|---|
| 1052 | { |
|---|
| 1053 | stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
|---|
| 1054 | } |
|---|
| 1055 | break; |
|---|
| 1056 | } |
|---|
| 1057 | |
|---|
| 1058 | |
|---|
| 1059 | if (document.subfaceLevel() > 0) |
|---|
| 1060 | { |
|---|
| 1061 | stateset->setAttributeAndModes(document.getSubSurfacePolygonOffset(document.subfaceLevel()), osg::StateAttribute::ON); |
|---|
| 1062 | stateset->setAttribute(document.getSubSurfaceDepth()); |
|---|
| 1063 | |
|---|
| 1064 | stateset->setRenderBinDetails(document.subfaceLevel(),"RenderBin"); |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | _geode->setStateSet(stateset.get()); |
|---|
| 1068 | |
|---|
| 1069 | |
|---|
| 1070 | if (_parent.valid()) |
|---|
| 1071 | _parent->addChild(*_geode); |
|---|
| 1072 | } |
|---|
| 1073 | |
|---|
| 1074 | virtual void dispose(Document& document) |
|---|
| 1075 | { |
|---|
| 1076 | if (_geode.valid()) |
|---|
| 1077 | { |
|---|
| 1078 | |
|---|
| 1079 | if (_matrix.valid()) |
|---|
| 1080 | { |
|---|
| 1081 | insertMatrixTransform(*_geode,*_matrix,_numberOfReplications); |
|---|
| 1082 | } |
|---|
| 1083 | |
|---|
| 1084 | if( getDrawMode( ) == SOLID_NO_BACKFACE && document.getReplaceDoubleSidedPolys( ) ) |
|---|
| 1085 | { |
|---|
| 1086 | addDrawableAndReverseWindingOrder( _geode.get() ); |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | osg::StateSet* stateset = _geode->getOrCreateStateSet(); |
|---|
| 1090 | |
|---|
| 1091 | |
|---|
| 1092 | bool isImageTranslucent = false; |
|---|
| 1093 | if (document.getUseTextureAlphaForTransparancyBinning()) |
|---|
| 1094 | { |
|---|
| 1095 | for (unsigned int i=0; i<stateset->getTextureAttributeList().size(); ++i) |
|---|
| 1096 | { |
|---|
| 1097 | osg::StateAttribute* sa = stateset->getTextureAttribute(i,osg::StateAttribute::TEXTURE); |
|---|
| 1098 | osg::Texture2D* texture = dynamic_cast<osg::Texture2D*>(sa); |
|---|
| 1099 | if (texture) |
|---|
| 1100 | { |
|---|
| 1101 | osg::Image* image = texture->getImage(); |
|---|
| 1102 | if (image && image->isImageTranslucent()) |
|---|
| 1103 | isImageTranslucent = true; |
|---|
| 1104 | } |
|---|
| 1105 | } |
|---|
| 1106 | } |
|---|
| 1107 | |
|---|
| 1108 | |
|---|
| 1109 | bool isMaterialTransparent = false; |
|---|
| 1110 | osg::Material* material = dynamic_cast<osg::Material*>(stateset->getAttribute(osg::StateAttribute::MATERIAL)); |
|---|
| 1111 | if (material) |
|---|
| 1112 | { |
|---|
| 1113 | isMaterialTransparent = material->getDiffuse(osg::Material::FRONT).a() < 0.99f; |
|---|
| 1114 | } |
|---|
| 1115 | |
|---|
| 1116 | |
|---|
| 1117 | if (isAlphaBlend() || isTransparent() || isImageTranslucent || isMaterialTransparent) |
|---|
| 1118 | { |
|---|
| 1119 | static osg::ref_ptr<osg::BlendFunc> blendFunc = new osg::BlendFunc(osg::BlendFunc::SRC_ALPHA, osg::BlendFunc::ONE_MINUS_SRC_ALPHA); |
|---|
| 1120 | stateset->setAttributeAndModes(blendFunc.get(), osg::StateAttribute::ON); |
|---|
| 1121 | stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 1122 | } |
|---|
| 1123 | |
|---|
| 1124 | if (document.getUseBillboardCenter()) |
|---|
| 1125 | { |
|---|
| 1126 | |
|---|
| 1127 | osg::Billboard* billboard = dynamic_cast<osg::Billboard*>(_geode.get()); |
|---|
| 1128 | if (billboard) |
|---|
| 1129 | { |
|---|
| 1130 | for (unsigned int i=0; i<billboard->getNumDrawables(); ++i) |
|---|
| 1131 | { |
|---|
| 1132 | osg::BoundingBox bb = billboard->getDrawable(i)->getBound(); |
|---|
| 1133 | billboard->setPosition(i,bb.center()); |
|---|
| 1134 | |
|---|
| 1135 | osgUtil::TransformAttributeFunctor tf(osg::Matrix::translate(-bb.center())); |
|---|
| 1136 | billboard->getDrawable(i)->accept(tf); |
|---|
| 1137 | |
|---|
| 1138 | billboard->getDrawable(i)->dirtyBound(); |
|---|
| 1139 | } |
|---|
| 1140 | |
|---|
| 1141 | billboard->dirtyBound(); |
|---|
| 1142 | } |
|---|
| 1143 | } |
|---|
| 1144 | } |
|---|
| 1145 | } |
|---|
| 1146 | }; |
|---|
| 1147 | |
|---|
| 1148 | REGISTER_FLTRECORD(Mesh, MESH_OP) |
|---|
| 1149 | |
|---|
| 1150 | |
|---|
| 1151 | |
|---|
| 1152 | |
|---|
| 1153 | |
|---|
| 1154 | |
|---|
| 1155 | class LocalVertexPool : public Record |
|---|
| 1156 | { |
|---|
| 1157 | |
|---|
| 1158 | static const unsigned int HAS_POSITION = 0x80000000u >> 0; |
|---|
| 1159 | static const unsigned int HAS_COLOR_INDEX = 0x80000000u >> 1; |
|---|
| 1160 | static const unsigned int HAS_RGBA_COLOR = 0x80000000u >> 2; |
|---|
| 1161 | static const unsigned int HAS_NORMAL = 0x80000000u >> 3; |
|---|
| 1162 | static const unsigned int HAS_BASE_UV = 0x80000000u >> 4; |
|---|
| 1163 | static const unsigned int HAS_UV_LAYER1 = 0x80000000u >> 5; |
|---|
| 1164 | static const unsigned int HAS_UV_LAYER2 = 0x80000000u >> 6; |
|---|
| 1165 | static const unsigned int HAS_UV_LAYER3 = 0x80000000u >> 7; |
|---|
| 1166 | static const unsigned int HAS_UV_LAYER4 = 0x80000000u >> 8; |
|---|
| 1167 | static const unsigned int HAS_UV_LAYER5 = 0x80000000u >> 9; |
|---|
| 1168 | static const unsigned int HAS_UV_LAYER6 = 0x80000000u >> 10; |
|---|
| 1169 | static const unsigned int HAS_UV_LAYER7 = 0x80000000u >> 11; |
|---|
| 1170 | |
|---|
| 1171 | public: |
|---|
| 1172 | |
|---|
| 1173 | LocalVertexPool() {} |
|---|
| 1174 | |
|---|
| 1175 | META_Record(LocalVertexPool) |
|---|
| 1176 | |
|---|
| 1177 | protected: |
|---|
| 1178 | |
|---|
| 1179 | virtual ~LocalVertexPool() {} |
|---|
| 1180 | |
|---|
| 1181 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 1182 | { |
|---|
| 1183 | |
|---|
| 1184 | uint32 vertices = in.readUInt32(); |
|---|
| 1185 | uint32 mask = in.readUInt32(); |
|---|
| 1186 | |
|---|
| 1187 | osg::ref_ptr<VertexList> _vertexList = new VertexList(vertices); |
|---|
| 1188 | |
|---|
| 1189 | |
|---|
| 1190 | for (unsigned int n=0; n<vertices; n++) |
|---|
| 1191 | { |
|---|
| 1192 | Vertex vertex; |
|---|
| 1193 | |
|---|
| 1194 | if (mask & HAS_POSITION) |
|---|
| 1195 | { |
|---|
| 1196 | osg::Vec3d coord = in.readVec3d(); |
|---|
| 1197 | vertex.setCoord(coord*document.unitScale()); |
|---|
| 1198 | |
|---|
| 1199 | if (!coord.valid()) |
|---|
| 1200 | { |
|---|
| 1201 | OSG_NOTICE<<"Warning: data error detected in LocalVertexPool::readRecord coord="<<coord.x()<<" "<<coord.y()<<" "<<coord.z()<<std::endl; |
|---|
| 1202 | } |
|---|
| 1203 | } |
|---|
| 1204 | |
|---|
| 1205 | if (mask & HAS_COLOR_INDEX) |
|---|
| 1206 | { |
|---|
| 1207 | uint32 alphaIndex = in.readUInt32(); |
|---|
| 1208 | int index = alphaIndex & 0x00ffffff; |
|---|
| 1209 | uint8 alpha = alphaIndex >> 24; |
|---|
| 1210 | osg::Vec4 color = document.getColorPool()->getColor(index); |
|---|
| 1211 | color.a() = (float)alpha/255; |
|---|
| 1212 | vertex.setColor(color); |
|---|
| 1213 | |
|---|
| 1214 | if (!color.valid()) |
|---|
| 1215 | { |
|---|
| 1216 | OSG_NOTICE<<"Warning: data error detected in LocalVertexPool::readRecord color="<<color.r()<<" "<<color.g()<<" "<<color.b()<<" "<<color.a()<<std::endl; |
|---|
| 1217 | } |
|---|
| 1218 | } |
|---|
| 1219 | |
|---|
| 1220 | if (mask & HAS_RGBA_COLOR) |
|---|
| 1221 | { |
|---|
| 1222 | osg::Vec4f color = in.readColor32(); |
|---|
| 1223 | vertex.setColor(color); |
|---|
| 1224 | |
|---|
| 1225 | if (!color.valid()) |
|---|
| 1226 | { |
|---|
| 1227 | OSG_NOTICE<<"Warning: data error detected in LocalVertexPool::readRecord color="<<color.r()<<" "<<color.g()<<" "<<color.b()<<" "<<color.a()<<std::endl; |
|---|
| 1228 | } |
|---|
| 1229 | } |
|---|
| 1230 | |
|---|
| 1231 | if (mask & HAS_NORMAL) |
|---|
| 1232 | { |
|---|
| 1233 | osg::Vec3f normal = in.readVec3f(); |
|---|
| 1234 | vertex.setNormal(normal); |
|---|
| 1235 | |
|---|
| 1236 | if (!normal.valid()) |
|---|
| 1237 | { |
|---|
| 1238 | OSG_NOTICE<<"Warning: data error detected in LocalVertexPool::readRecord normal="<<normal.x()<<" "<<normal.y()<<" "<<normal.z()<<std::endl; |
|---|
| 1239 | } |
|---|
| 1240 | } |
|---|
| 1241 | |
|---|
| 1242 | for (unsigned int layer=0; layer<8; layer++) |
|---|
| 1243 | { |
|---|
| 1244 | if (mask & (HAS_BASE_UV >> layer)) |
|---|
| 1245 | { |
|---|
| 1246 | osg::Vec2f uv = in.readVec2f(); |
|---|
| 1247 | vertex.setUV(layer,uv); |
|---|
| 1248 | |
|---|
| 1249 | if (!uv.valid()) |
|---|
| 1250 | { |
|---|
| 1251 | OSG_NOTICE<<"Warning: data error detected in LocalVertexPool::readRecord uv="<<uv.x()<<" "<<uv.y()<<std::endl; |
|---|
| 1252 | } |
|---|
| 1253 | |
|---|
| 1254 | } |
|---|
| 1255 | } |
|---|
| 1256 | |
|---|
| 1257 | (*_vertexList)[n] = vertex; |
|---|
| 1258 | } |
|---|
| 1259 | |
|---|
| 1260 | if (_parent.valid()) |
|---|
| 1261 | _parent->setLocalVertexPool(_vertexList.get()); |
|---|
| 1262 | |
|---|
| 1263 | } |
|---|
| 1264 | }; |
|---|
| 1265 | |
|---|
| 1266 | REGISTER_FLTRECORD(LocalVertexPool, LOCAL_VERTEX_POOL_OP) |
|---|
| 1267 | |
|---|
| 1268 | |
|---|
| 1269 | |
|---|
| 1270 | |
|---|
| 1271 | |
|---|
| 1272 | |
|---|
| 1273 | class MeshPrimitive : public PrimaryRecord |
|---|
| 1274 | { |
|---|
| 1275 | enum PrimitiveType |
|---|
| 1276 | { |
|---|
| 1277 | TRIANGLE_STRIP = 1, |
|---|
| 1278 | TRIANGLE_FAN = 2, |
|---|
| 1279 | QUADRILATERAL_STRIP = 3, |
|---|
| 1280 | INDEXED_POLYGON = 4 |
|---|
| 1281 | }; |
|---|
| 1282 | |
|---|
| 1283 | public: |
|---|
| 1284 | |
|---|
| 1285 | MeshPrimitive() {} |
|---|
| 1286 | |
|---|
| 1287 | META_Record(MeshPrimitive) |
|---|
| 1288 | |
|---|
| 1289 | protected: |
|---|
| 1290 | |
|---|
| 1291 | virtual ~MeshPrimitive() {} |
|---|
| 1292 | |
|---|
| 1293 | virtual void readRecord(RecordInputStream& in, Document& ) |
|---|
| 1294 | { |
|---|
| 1295 | Mesh* mesh = dynamic_cast<Mesh*>(_parent.get()); |
|---|
| 1296 | if (!mesh) return; |
|---|
| 1297 | |
|---|
| 1298 | VertexList* vertexList = mesh->getLocalVertexPool(); |
|---|
| 1299 | if (!vertexList) return; |
|---|
| 1300 | |
|---|
| 1301 | int16 type = in.readInt16(); |
|---|
| 1302 | uint16 indexSize = in.readUInt16(); |
|---|
| 1303 | uint32 vertexCount = in.readUInt32(); |
|---|
| 1304 | |
|---|
| 1305 | GLenum mode = 0; |
|---|
| 1306 | switch(type) |
|---|
| 1307 | { |
|---|
| 1308 | case TRIANGLE_STRIP: |
|---|
| 1309 | mode = osg::PrimitiveSet::TRIANGLE_STRIP; |
|---|
| 1310 | break; |
|---|
| 1311 | case TRIANGLE_FAN: |
|---|
| 1312 | mode = osg::PrimitiveSet::TRIANGLE_FAN; |
|---|
| 1313 | break; |
|---|
| 1314 | case QUADRILATERAL_STRIP: |
|---|
| 1315 | mode = osg::PrimitiveSet::QUAD_STRIP; |
|---|
| 1316 | break; |
|---|
| 1317 | case INDEXED_POLYGON: |
|---|
| 1318 | mode = osg::PrimitiveSet::POLYGON; |
|---|
| 1319 | break; |
|---|
| 1320 | } |
|---|
| 1321 | |
|---|
| 1322 | osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry; |
|---|
| 1323 | geometry->addPrimitiveSet(new osg::DrawArrays(mode,0,vertexCount)); |
|---|
| 1324 | |
|---|
| 1325 | for (unsigned int n=0; n<vertexCount; n++) |
|---|
| 1326 | { |
|---|
| 1327 | unsigned int index = 0; |
|---|
| 1328 | switch (indexSize) |
|---|
| 1329 | { |
|---|
| 1330 | case 1: |
|---|
| 1331 | index = in.readUInt8(); |
|---|
| 1332 | break; |
|---|
| 1333 | case 2: |
|---|
| 1334 | index = in.readUInt16(); |
|---|
| 1335 | break; |
|---|
| 1336 | case 4: |
|---|
| 1337 | index = in.readUInt32(); |
|---|
| 1338 | break; |
|---|
| 1339 | } |
|---|
| 1340 | |
|---|
| 1341 | if (index < vertexList->size()) |
|---|
| 1342 | { |
|---|
| 1343 | Vertex& vertex = (*vertexList)[index]; |
|---|
| 1344 | |
|---|
| 1345 | osg::Vec3Array* vertices = getOrCreateVertexArray(*geometry); |
|---|
| 1346 | vertices->push_back(vertex._coord); |
|---|
| 1347 | |
|---|
| 1348 | if (vertex.validColor()) |
|---|
| 1349 | { |
|---|
| 1350 | osg::Vec4Array* colors = getOrCreateColorArray(*geometry); |
|---|
| 1351 | colors->push_back(vertex._color); |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | if (vertex.validNormal()) |
|---|
| 1355 | { |
|---|
| 1356 | osg::Vec3Array* normals = getOrCreateNormalArray(*geometry); |
|---|
| 1357 | normals->push_back(vertex._normal); |
|---|
| 1358 | } |
|---|
| 1359 | |
|---|
| 1360 | for (int layer=0; layer<Vertex::MAX_LAYERS; layer++) |
|---|
| 1361 | { |
|---|
| 1362 | if (vertex.validUV(layer)) |
|---|
| 1363 | { |
|---|
| 1364 | osg::Vec2Array* UVs = getOrCreateTextureArray(*geometry,layer); |
|---|
| 1365 | UVs->push_back(vertex._uv[layer]); |
|---|
| 1366 | } |
|---|
| 1367 | } |
|---|
| 1368 | } |
|---|
| 1369 | } |
|---|
| 1370 | |
|---|
| 1371 | |
|---|
| 1372 | if (mesh->isGouraud()) |
|---|
| 1373 | { |
|---|
| 1374 | |
|---|
| 1375 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 1376 | } |
|---|
| 1377 | else |
|---|
| 1378 | { |
|---|
| 1379 | |
|---|
| 1380 | osg::Vec4 col = mesh->getPrimaryColor(); |
|---|
| 1381 | col[3] = 1.0f - mesh->getTransparency(); |
|---|
| 1382 | |
|---|
| 1383 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 1384 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 1385 | (*colors)[0] = col; |
|---|
| 1386 | geometry->setColorArray(colors); |
|---|
| 1387 | } |
|---|
| 1388 | |
|---|
| 1389 | |
|---|
| 1390 | if (mesh->isLit()) |
|---|
| 1391 | { |
|---|
| 1392 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX ); |
|---|
| 1393 | } |
|---|
| 1394 | else |
|---|
| 1395 | { |
|---|
| 1396 | geometry->setNormalBinding(osg::Geometry::BIND_OFF); |
|---|
| 1397 | geometry->setNormalArray(NULL); |
|---|
| 1398 | } |
|---|
| 1399 | |
|---|
| 1400 | mesh->addGeometry(*geometry); |
|---|
| 1401 | |
|---|
| 1402 | } |
|---|
| 1403 | }; |
|---|
| 1404 | |
|---|
| 1405 | REGISTER_FLTRECORD(MeshPrimitive, MESH_PRIMITIVE_OP) |
|---|
| 1406 | |
|---|
| 1407 | |
|---|
| 1408 | |
|---|
| 1409 | } |
|---|
| 1410 | |
|---|
| 1411 | |
|---|
| 1412 | |
|---|