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

Revision 9527, 28.1 kB (checked in by robert, 5 years ago)

Replaced readImageFile() usage with readRefImageFile() to prevent threading issues with caching of imagery in the osgDB::Registry cache.

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