| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include "TexturePaletteManager.h" |
|---|
| 18 | #include "FltExportVisitor.h" |
|---|
| 19 | #include "ExportOptions.h" |
|---|
| 20 | #include "DataOutputStream.h" |
|---|
| 21 | #include "Opcodes.h" |
|---|
| 22 | #include <osg/Notify> |
|---|
| 23 | #include <osg/Texture2D> |
|---|
| 24 | #include <osgDB/FileNameUtils> |
|---|
| 25 | #include <map> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | namespace flt |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | TexturePaletteManager::TexturePaletteManager( const FltExportVisitor& nv, const ExportOptions& fltOpt ) |
|---|
| 33 | : _currIndex( 0 ), |
|---|
| 34 | _nv( nv ), |
|---|
| 35 | _fltOpt( fltOpt ) |
|---|
| 36 | { |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | int |
|---|
| 40 | TexturePaletteManager::add( int unit, const osg::Texture2D* texture ) |
|---|
| 41 | { |
|---|
| 42 | if( (!texture) || |
|---|
| 43 | (!texture->getImage()) ) |
|---|
| 44 | return -1; |
|---|
| 45 | |
|---|
| 46 | int index( -1 ); |
|---|
| 47 | TextureIndexMap::const_iterator it = _indexMap.find( texture ); |
|---|
| 48 | if (it != _indexMap.end()) |
|---|
| 49 | index = it->second; |
|---|
| 50 | else |
|---|
| 51 | { |
|---|
| 52 | index = _currIndex++; |
|---|
| 53 | _indexMap[ texture ] = index; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | _nv.writeATTRFile( unit, texture ); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | return index; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void |
|---|
| 63 | TexturePaletteManager::write( DataOutputStream& dos ) const |
|---|
| 64 | { |
|---|
| 65 | int x( 0 ), y( 0 ), height( 0 ); |
|---|
| 66 | TextureIndexMap::const_iterator it = _indexMap.begin(); |
|---|
| 67 | while (it != _indexMap.end()) |
|---|
| 68 | { |
|---|
| 69 | const osg::Texture2D* texture = it->first; |
|---|
| 70 | int index = it->second; |
|---|
| 71 | |
|---|
| 72 | std::string fileName; |
|---|
| 73 | if ( _fltOpt.getStripTextureFilePath() ) |
|---|
| 74 | fileName = osgDB::getSimpleFileName( texture->getImage()->getFileName() ); |
|---|
| 75 | else |
|---|
| 76 | fileName = texture->getImage()->getFileName(); |
|---|
| 77 | |
|---|
| 78 | dos.writeInt16( (int16) TEXTURE_PALETTE_OP ); |
|---|
| 79 | dos.writeUInt16( 216 ); |
|---|
| 80 | dos.writeString( fileName, 200 ); |
|---|
| 81 | dos.writeInt32( index ); |
|---|
| 82 | dos.writeInt32( x ); |
|---|
| 83 | dos.writeInt32( y ); |
|---|
| 84 | it++; |
|---|
| 85 | |
|---|
| 86 | x += texture->getImage()->s(); |
|---|
| 87 | if (texture->getImage()->t() > height) |
|---|
| 88 | height = texture->getImage()->t(); |
|---|
| 89 | if (x > 1024) |
|---|
| 90 | { |
|---|
| 91 | x = 0; |
|---|
| 92 | y += height; |
|---|
| 93 | height = 0; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | } |
|---|