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

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