root/OpenSceneGraph/trunk/src/osgPlugins/OpenFlight/PaletteRecords.cpp @ 13041

Revision 13041, 28.3 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

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