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