| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 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 | |
|---|
| 34 | using namespace osg; |
|---|
| 35 | using namespace osgDB; |
|---|
| 36 | using namespace flt; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class 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 | |
|---|
| 60 | ReaderWriter::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 | |
|---|
| 105 | |
|---|
| 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 | |
|---|
| 173 | |
|---|
| 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 | |
|---|
| 187 | ReaderWriter::WriteResult |
|---|
| 188 | ReaderWriterATTR::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 | |
|---|
| 307 | |
|---|
| 308 | REGISTER_OSGPLUGIN(attr, ReaderWriterATTR) |
|---|