| 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/Light> |
|---|
| 22 | #include <osg/Texture2D> |
|---|
| 23 | #include <osg/TexEnv> |
|---|
| 24 | #include <osg/BlendFunc> |
|---|
| 25 | #include <osgSim/LightPointNode> |
|---|
| 26 | #include <osgDB/ReadFile> |
|---|
| 27 | #include <osgDB/FileUtils> |
|---|
| 28 | #include "Registry.h" |
|---|
| 29 | #include "Document.h" |
|---|
| 30 | #include "AttrData.h" |
|---|
| 31 | #include "RecordInputStream.h" |
|---|
| 32 | |
|---|
| 33 | namespace flt { |
|---|
| 34 | |
|---|
| 35 | class VertexPalette : public Record |
|---|
| 36 | { |
|---|
| 37 | public: |
|---|
| 38 | |
|---|
| 39 | VertexPalette() {} |
|---|
| 40 | |
|---|
| 41 | META_Record(VertexPalette) |
|---|
| 42 | |
|---|
| 43 | protected: |
|---|
| 44 | |
|---|
| 45 | virtual ~VertexPalette() {} |
|---|
| 46 | |
|---|
| 47 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 48 | { |
|---|
| 49 | uint32 paletteSize = in.readUInt32(); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | const int RECORD_HEADER_SIZE = 4; |
|---|
| 53 | const int OFFSET = RECORD_HEADER_SIZE+sizeof(paletteSize); |
|---|
| 54 | |
|---|
| 55 | std::string buffer(paletteSize,'\0'); |
|---|
| 56 | in.read(&buffer[OFFSET], paletteSize-OFFSET); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | document.setVertexPool(new VertexPool(buffer)); |
|---|
| 60 | } |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | REGISTER_FLTRECORD(VertexPalette, VERTEX_PALETTE_OP) |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | class ColorPalette : public Record |
|---|
| 68 | { |
|---|
| 69 | public: |
|---|
| 70 | |
|---|
| 71 | ColorPalette() {} |
|---|
| 72 | |
|---|
| 73 | META_Record(ColorPalette) |
|---|
| 74 | |
|---|
| 75 | protected: |
|---|
| 76 | |
|---|
| 77 | virtual ~ColorPalette() {} |
|---|
| 78 | |
|---|
| 79 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 80 | { |
|---|
| 81 | if (document.getColorPoolParent()) |
|---|
| 82 | |
|---|
| 83 | return; |
|---|
| 84 | |
|---|
| 85 | if (document.version() > VERSION_13) |
|---|
| 86 | { |
|---|
| 87 | bool oldVersion = false; |
|---|
| 88 | bool colorNameSection = in.getRecordSize() > 4228; |
|---|
| 89 | int maxColors = (document.version()>=VERSION_15_1) ? 1024 : 512; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | if (!colorNameSection) |
|---|
| 93 | { |
|---|
| 94 | |
|---|
| 95 | int maxColorsByRecordSize = (in.getRecordBodySize()-128) / 4; |
|---|
| 96 | if (maxColorsByRecordSize < maxColors) |
|---|
| 97 | maxColors = maxColorsByRecordSize; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | ColorPool* cp = new ColorPool(oldVersion,maxColors); |
|---|
| 101 | document.setColorPool(cp); |
|---|
| 102 | |
|---|
| 103 | in.forward(128); |
|---|
| 104 | for (int i=0; i<maxColors; i++) |
|---|
| 105 | { |
|---|
| 106 | uint8 alpha = in.readUInt8(1); |
|---|
| 107 | uint8 blue = in.readUInt8(1); |
|---|
| 108 | uint8 green = in.readUInt8(1); |
|---|
| 109 | uint8 red = in.readUInt8(1); |
|---|
| 110 | |
|---|
| 111 | (*cp)[i] = osg::Vec4((float)red/255,(float)green/255,(float)blue/255,(float)alpha/255); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | else |
|---|
| 115 | { |
|---|
| 116 | bool oldVersion = true; |
|---|
| 117 | int maxColors = 32+56; |
|---|
| 118 | |
|---|
| 119 | ColorPool* cp = new ColorPool(oldVersion,maxColors); |
|---|
| 120 | document.setColorPool(cp); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | for (int i=0; i < 32; i++) |
|---|
| 124 | { |
|---|
| 125 | uint16 red = in.readUInt16(1); |
|---|
| 126 | uint16 green = in.readUInt16(1); |
|---|
| 127 | uint16 blue = in.readUInt16(1); |
|---|
| 128 | (*cp)[i] = osg::Vec4((float)red/255,(float)green/255,(float)blue/255,1); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | for (int i=0; i < 56; i++) |
|---|
| 133 | { |
|---|
| 134 | uint16 red = in.readUInt16(1); |
|---|
| 135 | uint16 green = in.readUInt16(1); |
|---|
| 136 | uint16 blue = in.readUInt16(1); |
|---|
| 137 | (*cp)[i+32] = osg::Vec4((float)red/255,(float)green/255,(float)blue/255,1); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | REGISTER_FLTRECORD(ColorPalette, COLOR_PALETTE_OP) |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | class NameTable : public Record |
|---|
| 149 | { |
|---|
| 150 | public: |
|---|
| 151 | |
|---|
| 152 | NameTable() {} |
|---|
| 153 | |
|---|
| 154 | META_Record(NameTable) |
|---|
| 155 | |
|---|
| 156 | protected: |
|---|
| 157 | |
|---|
| 158 | virtual ~NameTable() {} |
|---|
| 159 | |
|---|
| 160 | virtual void readRecord(RecordInputStream& , Document& ) |
|---|
| 161 | { |
|---|
| 162 | } |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | REGISTER_FLTRECORD(NameTable, NAME_TABLE_OP) |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | class MaterialPalette : public Record |
|---|
| 170 | { |
|---|
| 171 | public: |
|---|
| 172 | |
|---|
| 173 | MaterialPalette() {} |
|---|
| 174 | |
|---|
| 175 | META_Record(MaterialPalette) |
|---|
| 176 | |
|---|
| 177 | protected: |
|---|
| 178 | |
|---|
| 179 | virtual ~MaterialPalette() {} |
|---|
| 180 | |
|---|
| 181 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 182 | { |
|---|
| 183 | if (document.getMaterialPoolParent()) |
|---|
| 184 | |
|---|
| 185 | return; |
|---|
| 186 | |
|---|
| 187 | int32 index = in.readInt32(); |
|---|
| 188 | std::string name = in.readString(12); |
|---|
| 189 | in.readUInt32(); |
|---|
| 190 | osg::Vec3f ambient = in.readVec3f(); |
|---|
| 191 | osg::Vec3f diffuse = in.readVec3f(); |
|---|
| 192 | osg::Vec3f specular = in.readVec3f(); |
|---|
| 193 | osg::Vec3f emissive = in.readVec3f(); |
|---|
| 194 | float32 shininess = in.readFloat32(); |
|---|
| 195 | float32 alpha = in.readFloat32(); |
|---|
| 196 | |
|---|
| 197 | osg::Material* material = new osg::Material; |
|---|
| 198 | material->setName(name); |
|---|
| 199 | material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(ambient,alpha)); |
|---|
| 200 | material->setDiffuse (osg::Material::FRONT_AND_BACK,osg::Vec4(diffuse,alpha)); |
|---|
| 201 | material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(specular,alpha)); |
|---|
| 202 | material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(emissive,alpha)); |
|---|
| 203 | material->setShininess(osg::Material::FRONT_AND_BACK,shininess); |
|---|
| 204 | |
|---|
| 205 | MaterialPool* mp = document.getOrCreateMaterialPool(); |
|---|
| 206 | (*mp)[index] = material; |
|---|
| 207 | } |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | REGISTER_FLTRECORD(MaterialPalette, MATERIAL_PALETTE_OP) |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | class OldMaterialPalette : public Record |
|---|
| 215 | { |
|---|
| 216 | public: |
|---|
| 217 | |
|---|
| 218 | OldMaterialPalette() {} |
|---|
| 219 | |
|---|
| 220 | META_Record(OldMaterialPalette) |
|---|
| 221 | |
|---|
| 222 | protected: |
|---|
| 223 | |
|---|
| 224 | virtual ~OldMaterialPalette() {} |
|---|
| 225 | |
|---|
| 226 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 227 | { |
|---|
| 228 | if (document.getMaterialPoolParent()) |
|---|
| 229 | |
|---|
| 230 | return; |
|---|
| 231 | |
|---|
| 232 | for (int i=0; i < 64; i++) |
|---|
| 233 | { |
|---|
| 234 | osg::Vec3f ambient = in.readVec3f(); |
|---|
| 235 | osg::Vec3f diffuse = in.readVec3f(); |
|---|
| 236 | osg::Vec3f specular = in.readVec3f(); |
|---|
| 237 | osg::Vec3f emissive = in.readVec3f(); |
|---|
| 238 | float32 shininess = in.readFloat32(); |
|---|
| 239 | float32 alpha = in.readFloat32(); |
|---|
| 240 | in.readUInt32(); |
|---|
| 241 | std::string name = in.readString(12); |
|---|
| 242 | in.forward(4*28); |
|---|
| 243 | |
|---|
| 244 | osg::Material* material = new osg::Material; |
|---|
| 245 | material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(ambient,alpha)); |
|---|
| 246 | material->setDiffuse (osg::Material::FRONT_AND_BACK,osg::Vec4(diffuse,alpha)); |
|---|
| 247 | material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(specular,alpha)); |
|---|
| 248 | material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(emissive,alpha)); |
|---|
| 249 | material->setShininess(osg::Material::FRONT_AND_BACK,shininess); |
|---|
| 250 | |
|---|
| 251 | MaterialPool* mp = document.getOrCreateMaterialPool(); |
|---|
| 252 | (*mp)[i] = material; |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | }; |
|---|
| 257 | |
|---|
| 258 | REGISTER_FLTRECORD(OldMaterialPalette, OLD_MATERIAL_PALETTE_OP) |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | class TexturePalette : public Record |
|---|
| 263 | { |
|---|
| 264 | public: |
|---|
| 265 | |
|---|
| 266 | TexturePalette() {} |
|---|
| 267 | |
|---|
| 268 | META_Record(TexturePalette) |
|---|
| 269 | |
|---|
| 270 | protected: |
|---|
| 271 | |
|---|
| 272 | virtual ~TexturePalette() {} |
|---|
| 273 | |
|---|
| 274 | osg::Texture2D::WrapMode convertWrapMode(int32 attrWrapMode, const Document& document) const |
|---|
| 275 | { |
|---|
| 276 | osg::Texture2D::WrapMode osgWrapMode = osg::Texture2D::REPEAT; |
|---|
| 277 | switch (attrWrapMode) |
|---|
| 278 | { |
|---|
| 279 | case AttrData::WRAP_CLAMP: |
|---|
| 280 | if (document.getReplaceClampWithClampToEdge()) |
|---|
| 281 | osgWrapMode = osg::Texture2D::CLAMP_TO_EDGE; |
|---|
| 282 | else |
|---|
| 283 | osgWrapMode = osg::Texture2D::CLAMP; |
|---|
| 284 | break; |
|---|
| 285 | case AttrData::WRAP_MIRRORED_REPEAT: |
|---|
| 286 | osgWrapMode = osg::Texture2D::MIRROR; |
|---|
| 287 | break; |
|---|
| 288 | case AttrData::WRAP_REPEAT: |
|---|
| 289 | osgWrapMode = osg::Texture2D::REPEAT; |
|---|
| 290 | break; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | return osgWrapMode; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | osg::StateSet* readTexture(const std::string& filename, const Document& document) const |
|---|
| 297 | { |
|---|
| 298 | osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(filename,document.getOptions()); |
|---|
| 299 | if (!image) return NULL; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 303 | |
|---|
| 304 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 305 | texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); |
|---|
| 306 | texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); |
|---|
| 307 | texture->setResizeNonPowerOfTwoHint(true); |
|---|
| 308 | texture->setImage(image.get()); |
|---|
| 309 | stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | std::string attrname = filename + ".attr"; |
|---|
| 313 | osg::ref_ptr<AttrData> attr = dynamic_cast<AttrData*>(osgDB::readObjectFile(attrname,document.getOptions())); |
|---|
| 314 | if (attr.valid()) |
|---|
| 315 | { |
|---|
| 316 | |
|---|
| 317 | osg::Texture2D::WrapMode wrap_s = convertWrapMode(attr->wrapMode_u,document); |
|---|
| 318 | texture->setWrap(osg::Texture2D::WRAP_S,wrap_s); |
|---|
| 319 | |
|---|
| 320 | osg::Texture2D::WrapMode wrap_t = convertWrapMode(attr->wrapMode_v,document); |
|---|
| 321 | texture->setWrap(osg::Texture2D::WRAP_T,wrap_t); |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | switch (attr->minFilterMode) |
|---|
| 325 | { |
|---|
| 326 | case AttrData::MIN_FILTER_POINT: |
|---|
| 327 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST); |
|---|
| 328 | break; |
|---|
| 329 | case AttrData::MIN_FILTER_BILINEAR: |
|---|
| 330 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR); |
|---|
| 331 | break; |
|---|
| 332 | case AttrData::MIN_FILTER_MIPMAP_POINT: |
|---|
| 333 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST_MIPMAP_NEAREST); |
|---|
| 334 | break; |
|---|
| 335 | case AttrData::MIN_FILTER_MIPMAP_LINEAR: |
|---|
| 336 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST_MIPMAP_LINEAR); |
|---|
| 337 | break; |
|---|
| 338 | case AttrData::MIN_FILTER_MIPMAP_BILINEAR: |
|---|
| 339 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_NEAREST); |
|---|
| 340 | break; |
|---|
| 341 | case AttrData::MIN_FILTER_MIPMAP_TRILINEAR: |
|---|
| 342 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR); |
|---|
| 343 | break; |
|---|
| 344 | case AttrData::MIN_FILTER_BICUBIC: |
|---|
| 345 | case AttrData::MIN_FILTER_BILINEAR_GEQUAL: |
|---|
| 346 | case AttrData::MIN_FILTER_BILINEAR_LEQUAL: |
|---|
| 347 | case AttrData::MIN_FILTER_BICUBIC_GEQUAL: |
|---|
| 348 | case AttrData::MIN_FILTER_BICUBIC_LEQUAL: |
|---|
| 349 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_NEAREST); |
|---|
| 350 | break; |
|---|
| 351 | default: |
|---|
| 352 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR); |
|---|
| 353 | break; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | switch (attr->magFilterMode) |
|---|
| 358 | { |
|---|
| 359 | case AttrData::MAG_FILTER_POINT: |
|---|
| 360 | texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST); |
|---|
| 361 | break; |
|---|
| 362 | case AttrData::MAG_FILTER_BILINEAR: |
|---|
| 363 | case AttrData::MAG_FILTER_BILINEAR_GEQUAL: |
|---|
| 364 | case AttrData::MAG_FILTER_BILINEAR_LEQUAL: |
|---|
| 365 | case AttrData::MAG_FILTER_SHARPEN: |
|---|
| 366 | case AttrData::MAG_FILTER_BICUBIC: |
|---|
| 367 | case AttrData::MAG_FILTER_BICUBIC_GEQUAL: |
|---|
| 368 | case AttrData::MAG_FILTER_BICUBIC_LEQUAL: |
|---|
| 369 | case AttrData::MAG_FILTER_ADD_DETAIL: |
|---|
| 370 | case AttrData::MAG_FILTER_MODULATE_DETAIL: |
|---|
| 371 | texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR); |
|---|
| 372 | break; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | switch(attr->intFormat) |
|---|
| 377 | { |
|---|
| 378 | case AttrData::INTERNAL_FORMAT_TX_I_12A_4: |
|---|
| 379 | texture->setInternalFormat(GL_LUMINANCE12_ALPHA4); |
|---|
| 380 | break; |
|---|
| 381 | case AttrData::INTERNAL_FORMAT_TX_IA_8: |
|---|
| 382 | texture->setInternalFormat(GL_LUMINANCE_ALPHA); |
|---|
| 383 | break; |
|---|
| 384 | case AttrData::INTERNAL_FORMAT_TX_RGB_5: |
|---|
| 385 | texture->setInternalFormat(GL_RGB5); |
|---|
| 386 | break; |
|---|
| 387 | case AttrData::INTERNAL_FORMAT_TX_RGBA_4: |
|---|
| 388 | texture->setInternalFormat(GL_RGBA4); |
|---|
| 389 | break; |
|---|
| 390 | case AttrData::INTERNAL_FORMAT_TX_IA_12: |
|---|
| 391 | texture->setInternalFormat(GL_LUMINANCE12_ALPHA12); |
|---|
| 392 | break; |
|---|
| 393 | case AttrData::INTERNAL_FORMAT_TX_RGBA_8: |
|---|
| 394 | texture->setInternalFormat(GL_RGBA8); |
|---|
| 395 | break; |
|---|
| 396 | case AttrData::INTERNAL_FORMAT_TX_RGBA_12: |
|---|
| 397 | texture->setInternalFormat(GL_RGBA12); |
|---|
| 398 | break; |
|---|
| 399 | case AttrData::INTERNAL_FORMAT_TX_I_16: |
|---|
| 400 | texture->setInternalFormat(GL_INTENSITY16); |
|---|
| 401 | break; |
|---|
| 402 | case AttrData::INTERNAL_FORMAT_TX_RGB_12: |
|---|
| 403 | texture->setInternalFormat(GL_RGB12); |
|---|
| 404 | break; |
|---|
| 405 | case AttrData::INTERNAL_FORMAT_DEFAULT: |
|---|
| 406 | default: |
|---|
| 407 | |
|---|
| 408 | break; |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | osg::TexEnv* texenv = new osg::TexEnv; |
|---|
| 412 | switch (attr->texEnvMode) |
|---|
| 413 | { |
|---|
| 414 | case AttrData::TEXENV_MODULATE: |
|---|
| 415 | texenv->setMode(osg::TexEnv::MODULATE); |
|---|
| 416 | break; |
|---|
| 417 | case AttrData::TEXENV_BLEND: |
|---|
| 418 | texenv->setMode(osg::TexEnv::BLEND); |
|---|
| 419 | break; |
|---|
| 420 | case AttrData::TEXENV_DECAL: |
|---|
| 421 | texenv->setMode(osg::TexEnv::DECAL); |
|---|
| 422 | break; |
|---|
| 423 | case AttrData::TEXENV_COLOR: |
|---|
| 424 | texenv->setMode(osg::TexEnv::REPLACE); |
|---|
| 425 | break; |
|---|
| 426 | case AttrData::TEXENV_ADD: |
|---|
| 427 | texenv->setMode(osg::TexEnv::ADD); |
|---|
| 428 | break; |
|---|
| 429 | } |
|---|
| 430 | stateset->setTextureAttribute(0, texenv); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | return stateset; |
|---|
| 434 | } |
|---|
| 435 | |
|---|
| 436 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 437 | { |
|---|
| 438 | if (document.getTexturePoolParent()) |
|---|
| 439 | |
|---|
| 440 | return; |
|---|
| 441 | |
|---|
| 442 | int maxLength = (document.version() < VERSION_14) ? 80 : 200; |
|---|
| 443 | std::string filename = in.readString(maxLength); |
|---|
| 444 | int32 index = in.readInt32(-1); |
|---|
| 445 | in.readInt32(); |
|---|
| 446 | in.readInt32(); |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | std::string pathname = osgDB::findDataFile(filename,document.getOptions()); |
|---|
| 450 | if (pathname.empty()) |
|---|
| 451 | { |
|---|
| 452 | osg::notify(osg::WARN) << "Can't find texture (" << index << ") " << filename << std::endl; |
|---|
| 453 | return; |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | osg::StateSet* stateset = flt::Registry::instance()->getTextureFromLocalCache(pathname); |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | if (!stateset) |
|---|
| 461 | { |
|---|
| 462 | stateset = readTexture(pathname,document); |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | flt::Registry::instance()->addTextureToLocalCache(pathname,stateset); |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | |
|---|
| 469 | TexturePool* tp = document.getOrCreateTexturePool(); |
|---|
| 470 | (*tp)[index] = stateset; |
|---|
| 471 | } |
|---|
| 472 | }; |
|---|
| 473 | |
|---|
| 474 | REGISTER_FLTRECORD(TexturePalette, TEXTURE_PALETTE_OP) |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | class EyepointAndTrackplanePalette : public Record |
|---|
| 479 | { |
|---|
| 480 | public: |
|---|
| 481 | |
|---|
| 482 | EyepointAndTrackplanePalette() {} |
|---|
| 483 | |
|---|
| 484 | META_Record(EyepointAndTrackplanePalette) |
|---|
| 485 | |
|---|
| 486 | protected: |
|---|
| 487 | |
|---|
| 488 | virtual ~EyepointAndTrackplanePalette() {} |
|---|
| 489 | |
|---|
| 490 | virtual void readRecord(RecordInputStream& , Document& ) {} |
|---|
| 491 | }; |
|---|
| 492 | |
|---|
| 493 | REGISTER_FLTRECORD(EyepointAndTrackplanePalette, EYEPOINT_AND_TRACKPLANE_PALETTE_OP) |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | class LinkagePalette : public Record |
|---|
| 498 | { |
|---|
| 499 | public: |
|---|
| 500 | |
|---|
| 501 | LinkagePalette() {} |
|---|
| 502 | |
|---|
| 503 | META_Record(LinkagePalette) |
|---|
| 504 | |
|---|
| 505 | protected: |
|---|
| 506 | |
|---|
| 507 | virtual ~LinkagePalette() {} |
|---|
| 508 | |
|---|
| 509 | virtual void readRecord(RecordInputStream& , Document& ) {} |
|---|
| 510 | }; |
|---|
| 511 | |
|---|
| 512 | REGISTER_FLTRECORD(LinkagePalette, LINKAGE_PALETTE_OP) |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | |
|---|
| 516 | class SoundPalette : public Record |
|---|
| 517 | { |
|---|
| 518 | public: |
|---|
| 519 | |
|---|
| 520 | SoundPalette() {} |
|---|
| 521 | |
|---|
| 522 | META_Record(SoundPalette) |
|---|
| 523 | |
|---|
| 524 | protected: |
|---|
| 525 | |
|---|
| 526 | virtual ~SoundPalette() {} |
|---|
| 527 | |
|---|
| 528 | virtual void readRecord(RecordInputStream& , Document& ) {} |
|---|
| 529 | }; |
|---|
| 530 | |
|---|
| 531 | REGISTER_FLTRECORD(SoundPalette, SOUND_PALETTE_OP) |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | class LightSourcePalette : public Record |
|---|
| 536 | { |
|---|
| 537 | public: |
|---|
| 538 | |
|---|
| 539 | LightSourcePalette() {} |
|---|
| 540 | |
|---|
| 541 | META_Record(LightSourcePalette) |
|---|
| 542 | |
|---|
| 543 | enum LightType |
|---|
| 544 | { |
|---|
| 545 | INFINITE_LIGHT = 0, |
|---|
| 546 | LOCAL_LIGHT = 1, |
|---|
| 547 | SPOT_LIGHT = 2 |
|---|
| 548 | }; |
|---|
| 549 | |
|---|
| 550 | protected: |
|---|
| 551 | |
|---|
| 552 | virtual ~LightSourcePalette() {} |
|---|
| 553 | |
|---|
| 554 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 555 | { |
|---|
| 556 | if (document.getLightSourcePoolParent()) |
|---|
| 557 | |
|---|
| 558 | return; |
|---|
| 559 | |
|---|
| 560 | int32 index = in.readInt32(-1); |
|---|
| 561 | in.forward(2*4); |
|---|
| 562 | std::string name = in.readString(20); |
|---|
| 563 | in.forward(4); |
|---|
| 564 | osg::Vec4f ambient = in.readVec4f(); |
|---|
| 565 | osg::Vec4f diffuse = in.readVec4f(); |
|---|
| 566 | osg::Vec4f specular = in.readVec4f(); |
|---|
| 567 | int32 type = in.readInt32(); |
|---|
| 568 | in.forward(4*10); |
|---|
| 569 | float32 spotExponent = in.readFloat32(); |
|---|
| 570 | float32 spotCutoff = in.readFloat32(); |
|---|
| 571 | in.readFloat32(); |
|---|
| 572 | in.readFloat32(); |
|---|
| 573 | float32 constantAttenuation = in.readFloat32(); |
|---|
| 574 | float32 linearAttenuation = in.readFloat32(); |
|---|
| 575 | float32 quadraticAttenuation = in.readFloat32(); |
|---|
| 576 | in.readInt32(); |
|---|
| 577 | |
|---|
| 578 | osg::ref_ptr<osg::Light> light = new osg::Light; |
|---|
| 579 | light->setAmbient(ambient); |
|---|
| 580 | light->setDiffuse(diffuse); |
|---|
| 581 | light->setSpecular(specular); |
|---|
| 582 | |
|---|
| 583 | switch (type) |
|---|
| 584 | { |
|---|
| 585 | case INFINITE_LIGHT: |
|---|
| 586 | light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); |
|---|
| 587 | break; |
|---|
| 588 | case LOCAL_LIGHT: |
|---|
| 589 | light->setPosition(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 590 | light->setConstantAttenuation(constantAttenuation); |
|---|
| 591 | light->setLinearAttenuation(linearAttenuation); |
|---|
| 592 | light->setQuadraticAttenuation(quadraticAttenuation); |
|---|
| 593 | break; |
|---|
| 594 | case SPOT_LIGHT: |
|---|
| 595 | light->setPosition(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); |
|---|
| 596 | light->setDirection(osg::Vec3(0.0f,1.0f,0.0f)); |
|---|
| 597 | light->setConstantAttenuation(constantAttenuation); |
|---|
| 598 | light->setLinearAttenuation(linearAttenuation); |
|---|
| 599 | light->setQuadraticAttenuation(quadraticAttenuation); |
|---|
| 600 | light->setSpotExponent(spotExponent); |
|---|
| 601 | light->setSpotCutoff(spotCutoff); |
|---|
| 602 | break; |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | |
|---|
| 606 | LightSourcePool* pool = document.getOrCreateLightSourcePool(); |
|---|
| 607 | (*pool)[index] = light.get(); |
|---|
| 608 | } |
|---|
| 609 | }; |
|---|
| 610 | |
|---|
| 611 | REGISTER_FLTRECORD(LightSourcePalette, LIGHT_SOURCE_PALETTE_OP) |
|---|
| 612 | |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | class LightPointAppearancePalette : public Record |
|---|
| 616 | { |
|---|
| 617 | public: |
|---|
| 618 | |
|---|
| 619 | LightPointAppearancePalette() {} |
|---|
| 620 | |
|---|
| 621 | META_Record(LightPointAppearancePalette) |
|---|
| 622 | |
|---|
| 623 | protected: |
|---|
| 624 | |
|---|
| 625 | virtual ~LightPointAppearancePalette() {} |
|---|
| 626 | |
|---|
| 627 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 628 | { |
|---|
| 629 | if (document.getLightPointAppearancePoolParent()) |
|---|
| 630 | |
|---|
| 631 | return; |
|---|
| 632 | |
|---|
| 633 | osg::ref_ptr<LPAppearance> appearance = new LPAppearance; |
|---|
| 634 | |
|---|
| 635 | in.forward(4); |
|---|
| 636 | appearance->name = in.readString(256); |
|---|
| 637 | appearance->index = in.readInt32(-1); |
|---|
| 638 | appearance->materialCode = in.readInt16(); |
|---|
| 639 | appearance->featureID = in.readInt16(); |
|---|
| 640 | |
|---|
| 641 | int32 backColorIndex = in.readInt32(); |
|---|
| 642 | appearance->backColor = document.getColorPool() ? |
|---|
| 643 | document.getColorPool()->getColor(backColorIndex) : |
|---|
| 644 | osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f); |
|---|
| 645 | |
|---|
| 646 | appearance->displayMode = in.readInt32(); |
|---|
| 647 | appearance->intensityFront = in.readFloat32(); |
|---|
| 648 | appearance->intensityBack = in.readFloat32(); |
|---|
| 649 | appearance->minDefocus = in.readFloat32(); |
|---|
| 650 | appearance->maxDefocus = in.readFloat32(); |
|---|
| 651 | appearance->fadingMode = in.readInt32(); |
|---|
| 652 | appearance->fogPunchMode = in.readInt32(); |
|---|
| 653 | appearance->directionalMode = in.readInt32(); |
|---|
| 654 | appearance->rangeMode = in.readInt32(); |
|---|
| 655 | appearance->minPixelSize = in.readFloat32(); |
|---|
| 656 | appearance->maxPixelSize = in.readFloat32(); |
|---|
| 657 | appearance->actualPixelSize = in.readFloat32(); |
|---|
| 658 | appearance->transparentFalloffPixelSize = in.readFloat32(); |
|---|
| 659 | appearance->transparentFalloffExponent = in.readFloat32(); |
|---|
| 660 | appearance->transparentFalloffScalar = in.readFloat32(); |
|---|
| 661 | appearance->transparentFalloffClamp = in.readFloat32(); |
|---|
| 662 | appearance->fogScalar = in.readFloat32(); |
|---|
| 663 | appearance->fogIntensity = in.readFloat32(); |
|---|
| 664 | appearance->sizeDifferenceThreshold = in.readFloat32(); |
|---|
| 665 | appearance->directionality = in.readInt32(); |
|---|
| 666 | appearance->horizontalLobeAngle = in.readFloat32(); |
|---|
| 667 | appearance->verticalLobeAngle = in.readFloat32(); |
|---|
| 668 | appearance->lobeRollAngle = in.readFloat32(); |
|---|
| 669 | appearance->directionalFalloffExponent = in.readFloat32(); |
|---|
| 670 | appearance->directionalAmbientIntensity = in.readFloat32(); |
|---|
| 671 | appearance->significance = in.readFloat32(); |
|---|
| 672 | appearance->flags = in.readUInt32(); |
|---|
| 673 | appearance->visibilityRange = in.readFloat32(); |
|---|
| 674 | appearance->fadeRangeRatio = in.readFloat32(); |
|---|
| 675 | appearance->fadeInDuration = in.readFloat32(); |
|---|
| 676 | appearance->fadeOutDuration = in.readFloat32(); |
|---|
| 677 | appearance->LODRangeRatio = in.readFloat32(); |
|---|
| 678 | appearance->LODScale = in.readFloat32(); |
|---|
| 679 | |
|---|
| 680 | if(document.version() > VERSION_15_8) |
|---|
| 681 | appearance->texturePatternIndex = in.readInt16(-1); |
|---|
| 682 | else |
|---|
| 683 | appearance->texturePatternIndex = -1; |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | |
|---|
| 688 | LightPointAppearancePool* lpaPool = document.getOrCreateLightPointAppearancePool(); |
|---|
| 689 | (*lpaPool)[appearance->index] = appearance.get(); |
|---|
| 690 | } |
|---|
| 691 | |
|---|
| 692 | }; |
|---|
| 693 | |
|---|
| 694 | REGISTER_FLTRECORD(LightPointAppearancePalette, LIGHT_POINT_APPEARANCE_PALETTE_OP) |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | |
|---|
| 698 | class LightPointAnimationPalette : public Record |
|---|
| 699 | { |
|---|
| 700 | public: |
|---|
| 701 | |
|---|
| 702 | LightPointAnimationPalette() {} |
|---|
| 703 | |
|---|
| 704 | META_Record(LightPointAnimationPalette) |
|---|
| 705 | |
|---|
| 706 | protected: |
|---|
| 707 | |
|---|
| 708 | virtual ~LightPointAnimationPalette() {} |
|---|
| 709 | |
|---|
| 710 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 711 | { |
|---|
| 712 | if (document.getLightPointAnimationPoolParent()) |
|---|
| 713 | |
|---|
| 714 | return; |
|---|
| 715 | |
|---|
| 716 | osg::ref_ptr<LPAnimation> animation = new LPAnimation; |
|---|
| 717 | |
|---|
| 718 | in.forward(4); |
|---|
| 719 | animation->name = in.readString(256); |
|---|
| 720 | animation->index = in.readInt32(-1); |
|---|
| 721 | |
|---|
| 722 | animation->animationPeriod = in.readFloat32(); |
|---|
| 723 | animation->animationPhaseDelay = in.readFloat32(); |
|---|
| 724 | animation->animationEnabledPeriod = in.readFloat32(); |
|---|
| 725 | animation->axisOfRotation = in.readVec3f(); |
|---|
| 726 | animation->flags = in.readUInt32(); |
|---|
| 727 | animation->animationType = in.readInt32(); |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | animation->morseCodeTiming = in.readInt32(); |
|---|
| 731 | animation->wordRate = in.readInt32(); |
|---|
| 732 | animation->characterRate = in.readInt32(); |
|---|
| 733 | animation->morseCodeString = in.readString(1024); |
|---|
| 734 | |
|---|
| 735 | |
|---|
| 736 | int32 numberOfSequences = in.readInt32(); |
|---|
| 737 | for (int n=0; n<numberOfSequences; ++n) |
|---|
| 738 | { |
|---|
| 739 | LPAnimation::Pulse pulse; |
|---|
| 740 | pulse.state = in.readUInt32(); |
|---|
| 741 | pulse.duration = in.readFloat32(); |
|---|
| 742 | pulse.color = in.readColor32(); |
|---|
| 743 | |
|---|
| 744 | animation->sequence.push_back(pulse); |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | LightPointAnimationPool* lpaPool = document.getOrCreateLightPointAnimationPool(); |
|---|
| 749 | (*lpaPool)[animation->index] = animation.get(); |
|---|
| 750 | } |
|---|
| 751 | }; |
|---|
| 752 | |
|---|
| 753 | REGISTER_FLTRECORD(LightPointAnimationPalette, LIGHT_POINT_ANIMATION_PALETTE_OP) |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | class LineStylePalette : public Record |
|---|
| 758 | { |
|---|
| 759 | public: |
|---|
| 760 | |
|---|
| 761 | LineStylePalette() {} |
|---|
| 762 | |
|---|
| 763 | META_Record(LineStylePalette) |
|---|
| 764 | |
|---|
| 765 | protected: |
|---|
| 766 | |
|---|
| 767 | virtual ~LineStylePalette() {} |
|---|
| 768 | |
|---|
| 769 | virtual void readRecord(RecordInputStream& , Document& ) |
|---|
| 770 | { |
|---|
| 771 | } |
|---|
| 772 | }; |
|---|
| 773 | |
|---|
| 774 | REGISTER_FLTRECORD(LineStylePalette, LINE_STYLE_PALETTE_OP) |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | |
|---|
| 778 | class TextureMappingPalette : public Record |
|---|
| 779 | { |
|---|
| 780 | public: |
|---|
| 781 | |
|---|
| 782 | TextureMappingPalette() {} |
|---|
| 783 | |
|---|
| 784 | META_Record(TextureMappingPalette) |
|---|
| 785 | |
|---|
| 786 | protected: |
|---|
| 787 | |
|---|
| 788 | virtual ~TextureMappingPalette() {} |
|---|
| 789 | |
|---|
| 790 | virtual void readRecord(RecordInputStream& , Document& ) |
|---|
| 791 | { |
|---|
| 792 | } |
|---|
| 793 | }; |
|---|
| 794 | |
|---|
| 795 | REGISTER_FLTRECORD(TextureMappingPalette, TEXTURE_MAPPING_PALETTE_OP) |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | class ShaderPalette : public Record |
|---|
| 800 | { |
|---|
| 801 | public: |
|---|
| 802 | |
|---|
| 803 | ShaderPalette() {} |
|---|
| 804 | |
|---|
| 805 | META_Record(ShaderPalette) |
|---|
| 806 | |
|---|
| 807 | enum ShaderType |
|---|
| 808 | { |
|---|
| 809 | CG=0, |
|---|
| 810 | CGFX=1, |
|---|
| 811 | GLSL=2 |
|---|
| 812 | }; |
|---|
| 813 | |
|---|
| 814 | protected: |
|---|
| 815 | |
|---|
| 816 | virtual ~ShaderPalette() {} |
|---|
| 817 | |
|---|
| 818 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 819 | { |
|---|
| 820 | if (document.getShaderPoolParent()) |
|---|
| 821 | |
|---|
| 822 | return; |
|---|
| 823 | |
|---|
| 824 | int32 index = in.readInt32(-1); |
|---|
| 825 | int32 type = in.readInt32(-1); |
|---|
| 826 | std::string name = in.readString(1024); |
|---|
| 827 | |
|---|
| 828 | if (type == CG) |
|---|
| 829 | { |
|---|
| 830 | |
|---|
| 831 | std::string vertexProgramFilename = in.readString(1024); |
|---|
| 832 | std::string fragmentProgramFilename = in.readString(1024); |
|---|
| 833 | in.readInt32(); |
|---|
| 834 | in.readInt32(); |
|---|
| 835 | std::string vertexProgramEntry = in.readString(256); |
|---|
| 836 | std::string fragmentProgramEntry = in.readString(256); |
|---|
| 837 | } |
|---|
| 838 | else if (type == GLSL) |
|---|
| 839 | { |
|---|
| 840 | int32 vertexProgramFileCount(1); |
|---|
| 841 | int32 fragmentProgramFileCount(1); |
|---|
| 842 | |
|---|
| 843 | if (document.version() >= VERSION_16_1) |
|---|
| 844 | { |
|---|
| 845 | |
|---|
| 846 | vertexProgramFileCount = in.readInt32(); |
|---|
| 847 | fragmentProgramFileCount = in.readInt32(); |
|---|
| 848 | } |
|---|
| 849 | |
|---|
| 850 | |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | |
|---|
| 854 | osg::Program* program = new osg::Program; |
|---|
| 855 | program->setName(name); |
|---|
| 856 | |
|---|
| 857 | |
|---|
| 858 | int idx; |
|---|
| 859 | for( idx=0; idx<vertexProgramFileCount; idx++) |
|---|
| 860 | { |
|---|
| 861 | std::string vertexProgramFilename = in.readString(1024); |
|---|
| 862 | |
|---|
| 863 | std::string vertexProgramFilePath = osgDB::findDataFile(vertexProgramFilename,document.getOptions()); |
|---|
| 864 | if (!vertexProgramFilePath.empty()) |
|---|
| 865 | { |
|---|
| 866 | osg::Shader* vertexShader = osg::Shader::readShaderFile(osg::Shader::VERTEX, vertexProgramFilePath); |
|---|
| 867 | if (vertexShader) |
|---|
| 868 | program->addShader( vertexShader ); |
|---|
| 869 | } |
|---|
| 870 | } |
|---|
| 871 | |
|---|
| 872 | |
|---|
| 873 | for( idx=0; idx<fragmentProgramFileCount; idx++) |
|---|
| 874 | { |
|---|
| 875 | std::string fragmentProgramFilename = in.readString(1024); |
|---|
| 876 | |
|---|
| 877 | std::string fragmentProgramFilePath = osgDB::findDataFile(fragmentProgramFilename,document.getOptions()); |
|---|
| 878 | if (!fragmentProgramFilePath.empty()) |
|---|
| 879 | { |
|---|
| 880 | osg::Shader* fragmentShader = osg::Shader::readShaderFile(osg::Shader::FRAGMENT, fragmentProgramFilePath); |
|---|
| 881 | if (fragmentShader) |
|---|
| 882 | program->addShader( fragmentShader ); |
|---|
| 883 | } |
|---|
| 884 | } |
|---|
| 885 | |
|---|
| 886 | |
|---|
| 887 | ShaderPool* shaderPool = document.getOrCreateShaderPool(); |
|---|
| 888 | (*shaderPool)[index] = program; |
|---|
| 889 | } |
|---|
| 890 | } |
|---|
| 891 | }; |
|---|
| 892 | |
|---|
| 893 | REGISTER_FLTRECORD(ShaderPalette, SHADER_PALETTE_OP) |
|---|
| 894 | |
|---|
| 895 | |
|---|
| 896 | } |
|---|
| 897 | |
|---|