root/OpenSceneGraph/trunk/src/osgPlugins/OpenFlight/ReaderWriterATTR.cpp @ 9124

Revision 9124, 10.1 kB (checked in by robert, 5 years ago)

From Michael Platings, Converted std::fstream/ifstream/ofstream to osgDB::fstream/ifstream/ofstream and
fopen to osgDB::fopen to facilitate support for wide character filenames using UT8 encoding.

  • 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
20#include <osg/Notify>
21#include <osg/TexEnv>
22#include <osg/Texture2D>
23#include <osg/StateSet>
24#include <osg/GL>
25
26#include <osgDB/FileNameUtils>
27#include <osgDB/FileUtils>
28#include <osgDB/Registry>
29
30#include "AttrData.h"
31#include "DataInputStream.h"
[8003]32#include "DataOutputStream.h"
[5038]33
34using namespace osg;
35using namespace osgDB;
36using namespace flt;
37
38
39class ReaderWriterATTR : public osgDB::ReaderWriter
40{
41    public:
42   
[8578]43        ReaderWriterATTR()
44        {
45            supportsExtension("attr","OpenFlight texture attribute format");
46        }
47   
[5038]48        virtual const char* className() const { return "ATTR Image Attribute Reader/Writer"; }
49       
50        virtual bool acceptsExtension(const std::string& extension) const
51        {
52            return equalCaseInsensitive(extension,"attr");
53        }
54
55        virtual ReadResult readObject(const std::string& fileName, const ReaderWriter::Options*) const;
[8003]56        virtual ReaderWriter::WriteResult writeObject(const osg::Object& object, const std::string& fileName, const Options* options) const;
[5038]57};
58
59
60ReaderWriter::ReadResult ReaderWriterATTR::readObject(const std::string& file, const ReaderWriter::Options* options) const
61{
62    using std::ios;
63
64    std::string ext = osgDB::getLowerCaseFileExtension(file);
65    if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
66
67    std::string fileName = osgDB::findDataFile( file, options );
68    if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
69
[9124]70    osgDB::ifstream fin;
[5122]71    fin.imbue(std::locale::classic());
72    fin.open(fileName.c_str(), std::ios::in | std::ios::binary);
[5038]73
74    if ( fin.fail())
75        return ReadResult::ERROR_IN_READING_FILE;
76
[5205]77    flt::DataInputStream in(fin.rdbuf());
[5038]78
79    AttrData* attr = new AttrData;
80
81    try
82    {
83        attr->texels_u  = in.readInt32();
[8003]84        attr->texels_v  = in.readInt32();
[5038]85        attr->direction_u  = in.readInt32();
86        attr->direction_v  = in.readInt32();
87        attr->x_up  = in.readInt32();
88        attr->y_up  = in.readInt32();
89        attr->fileFormat  = in.readInt32();
90        attr->minFilterMode  = in.readInt32();
91        attr->magFilterMode  = in.readInt32();
[5794]92        attr->wrapMode  = in.readInt32(AttrData::WRAP_REPEAT);
[5038]93
94        attr->wrapMode_u  = in.readInt32();
[5794]95        if (attr->wrapMode_u == AttrData::WRAP_NONE)
[5038]96            attr->wrapMode_u = attr->wrapMode;
97
98        attr->wrapMode_v  = in.readInt32();
[5794]99        if (attr->wrapMode_v == AttrData::WRAP_NONE)
[5038]100            attr->wrapMode_v = attr->wrapMode;
101
102        attr->modifyFlag = in.readInt32();
103        attr->pivot_x  = in.readInt32();
104        attr->pivot_y  = in.readInt32();
105
106        // v11 ends here
107//      if (in.eof() || (_flt_version <= 11)) return true;
108#if 1
[5794]109        attr->texEnvMode = in.readInt32(AttrData::TEXENV_MODULATE);
[5038]110        attr->intensityAsAlpha = in.readInt32();
111        in.forward(4*8);
[7919]112        in.forward(4);
[5038]113        attr->size_u = in.readFloat64();
114        attr->size_v = in.readFloat64();
115        attr->originCode = in.readInt32();
116        attr->kernelVersion = in.readInt32();
117        attr->intFormat = in.readInt32();
118        attr->extFormat = in.readInt32();
119        attr->useMips = in.readInt32();
120        for (int n=0; n<8; n++)
[5585]121            attr->of_mips[n] = in.readFloat32();
[5038]122        attr->useLodScale = in.readInt32();
123        attr->lod0 = in.readFloat32();
124        attr->scale0 = in.readFloat32();
125        attr->lod1 = in.readFloat32();
126        attr->scale1 = in.readFloat32();
127        attr->lod2 = in.readFloat32();
128        attr->scale2 = in.readFloat32();
129        attr->lod3 = in.readFloat32();
130        attr->scale3 = in.readFloat32();
131        attr->lod4 = in.readFloat32();
132        attr->scale4 = in.readFloat32();
133        attr->lod5 = in.readFloat32();
134        attr->scale5 = in.readFloat32();
135        attr->lod6 = in.readFloat32();
136        attr->scale6 = in.readFloat32();
137        attr->lod7 = in.readFloat32();
138        attr->scale7 = in.readFloat32();
139        attr->clamp = in.readFloat32();
140        attr->magFilterAlpha = in.readInt32();
141        attr->magFilterColor = in.readInt32();
142        in.forward(4);
143        in.forward(4*8);
144        attr->lambertMeridian = in.readFloat64();
145        attr->lambertUpperLat = in.readFloat64();
146        attr->lambertlowerLat = in.readFloat64();
147        in.forward(8);
148        in.forward(4*5);
149        attr->useDetail = in.readInt32(  );
150        attr->txDetail_j = in.readInt32();
151        attr->txDetail_k = in.readInt32();
152        attr->txDetail_m = in.readInt32();
153        attr->txDetail_n = in.readInt32();
154        attr->txDetail_s = in.readInt32( );
155        attr->useTile = in.readInt32();
156        attr->txTile_ll_u= in.readFloat32();
157        attr->txTile_ll_v = in.readFloat32();
158        attr->txTile_ur_u = in.readFloat32();
159        attr->txTile_ur_v = in.readFloat32();
160        attr->projection = in.readInt32();
161        attr->earthModel = in.readInt32();
162        in.forward(4);
163        attr->utmZone = in.readInt32();
164        attr->imageOrigin = in.readInt32();
165        attr->geoUnits = in.readInt32();
166        in.forward(4);
167        in.forward(4);
168        attr->hemisphere = in.readInt32();
169        in.forward(4);
170        in.forward(4);
[7764]171        in.forward(149*4);
[5038]172        attr->comments = in.readString(512);
173
174        // v12 ends here
175//      if (in.eof() || (_flt_version <= 12)) return true;
176
[7919]177        in.forward(14*4);
[5038]178        attr->attrVersion = in.readInt32();
179        attr->controlPoints = in.readInt32();
[8003]180        attr->numSubtextures = in.readInt32();
[5038]181#endif
182    }
183    catch(...)
184    {
185        if (!fin.eof())
186        {
187            throw;
188        }
189    }
190
191    fin.close();
192
193    return attr;
194}
195
196
[8003]197ReaderWriter::WriteResult
198ReaderWriterATTR::writeObject(const osg::Object& object, const std::string& fileName, const Options* options) const
199{
200    using std::ios;
[5038]201
[8003]202    std::string ext = osgDB::getLowerCaseFileExtension( fileName );
203    if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
[5122]204
[8003]205    const AttrData* attr = dynamic_cast< const AttrData* >( &object );
206    if (attr == NULL)
207    {
208        osg::notify( osg::FATAL ) << "AttrWriter: Invalid Object." << std::endl;
209        return WriteResult::FILE_NOT_HANDLED;
210    }
[5122]211
[9124]212    osgDB::ofstream fOut;
[8003]213    fOut.open( fileName.c_str(), std::ios::out | std::ios::binary );
[5122]214
[8003]215    if ( fOut.fail())
216        return WriteResult::ERROR_IN_WRITING_FILE;
[5122]217
[8003]218    flt::DataOutputStream out( fOut.rdbuf() );
[5122]219
220
[8003]221    out.writeInt32( attr->texels_u );
222    out.writeInt32( attr->texels_v );
223    out.writeInt32( attr->direction_u );
224    out.writeInt32( attr->direction_v );
225    out.writeInt32( attr->x_up );
226    out.writeInt32( attr->y_up );
227    out.writeInt32( attr->fileFormat );
228    out.writeInt32( attr->minFilterMode );
229    out.writeInt32( attr->magFilterMode );
230    out.writeInt32( attr->wrapMode );
[5122]231
[8003]232    out.writeInt32( attr->wrapMode_u );
233    out.writeInt32( attr->wrapMode_v );
[5122]234
[8003]235    out.writeInt32( attr->modifyFlag );
236    out.writeInt32( attr->pivot_x );
237    out.writeInt32( attr->pivot_y );
[5122]238
[8003]239    out.writeInt32( attr->texEnvMode );
240    out.writeInt32( attr->intensityAsAlpha );
241    out.writeFill( 4*8 );
242    out.writeFloat64( attr->size_u );
243    out.writeFloat64( attr->size_v );
244    out.writeInt32( attr->originCode );
245    out.writeInt32( attr->kernelVersion );
246    out.writeInt32( attr->intFormat );
247    out.writeInt32( attr->extFormat );
248    out.writeInt32( attr->useMips );
249    for (int n=0; n<8; n++)
250        out.writeFloat32( attr->of_mips[n] );
251    out.writeInt32( attr->useLodScale );
252    out.writeFloat32( attr->lod0 );
253    out.writeFloat32( attr->scale0 );
254    out.writeFloat32( attr->lod1 );
255    out.writeFloat32( attr->scale1 );
256    out.writeFloat32( attr->lod2 );
257    out.writeFloat32( attr->scale2 );
258    out.writeFloat32( attr->lod3 );
259    out.writeFloat32( attr->scale3 );
260    out.writeFloat32( attr->lod4 );
261    out.writeFloat32( attr->scale4 );
262    out.writeFloat32( attr->lod5 );
263    out.writeFloat32( attr->scale5 );
264    out.writeFloat32( attr->lod6 );
265    out.writeFloat32( attr->scale6 );
266    out.writeFloat32( attr->lod7 );
267    out.writeFloat32( attr->scale7 );
268    out.writeFloat32( attr->clamp );
269    out.writeInt32( attr->magFilterAlpha );
270    out.writeInt32( attr->magFilterColor );
271    out.writeFill( 4 );
272    out.writeFill( 4*8 );
273    out.writeFloat64( attr->lambertMeridian );
274    out.writeFloat64( attr->lambertUpperLat );
275    out.writeFloat64( attr->lambertlowerLat );
276    out.writeFill( 8 );
277    out.writeFill( 4*5 );
278    out.writeInt32( attr->useDetail );
279    out.writeInt32( attr->txDetail_j );
280    out.writeInt32( attr->txDetail_k );
281    out.writeInt32( attr->txDetail_m );
282    out.writeInt32( attr->txDetail_n );
283    out.writeInt32( attr->txDetail_s );
284    out.writeInt32( attr->useTile );
285    out.writeFloat32( attr->txTile_ll_u );
286    out.writeFloat32( attr->txTile_ll_v );
287    out.writeFloat32( attr->txTile_ur_u );
288    out.writeFloat32( attr->txTile_ur_v );
289    out.writeInt32( attr->projection );
290    out.writeInt32( attr->earthModel );
291    out.writeFill( 4 );
292    out.writeInt32( attr->utmZone );
293    out.writeInt32( attr->imageOrigin );
294    out.writeInt32( attr->geoUnits );
295    out.writeFill( 4 );
296    out.writeFill( 4 );
297    out.writeInt32( attr->hemisphere );
298    out.writeFill( 4 );
299    out.writeFill( 4 );
300    out.writeFill( 149*4 );
301    out.writeString( attr->comments, 512 );
[5122]302
[8003]303    out.writeFill( 13*4 );
304    out.writeInt32( attr->attrVersion );
305    out.writeInt32( attr->controlPoints );
306    out.writeInt32( attr->numSubtextures );
[5122]307
308
[8003]309    fOut.close();
310
311    return WriteResult::FILE_SAVED;
312}
313
314
315
316// now register with Registry to instantiate the above
317// reader/writer.
318REGISTER_OSGPLUGIN(attr, ReaderWriterATTR)
Note: See TracBrowser for help on using the browser.