| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include "MaterialPaletteManager.h" |
|---|
| 18 | #include "DataOutputStream.h" |
|---|
| 19 | #include "Opcodes.h" |
|---|
| 20 | #include <osg/Notify> |
|---|
| 21 | #include <osg/Material> |
|---|
| 22 | #include <osg/Vec4f> |
|---|
| 23 | #include <cassert> |
|---|
| 24 | #include <sstream> |
|---|
| 25 | |
|---|
| 26 | namespace flt |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | MaterialPaletteManager::MaterialPaletteManager( ExportOptions& fltOpt ) |
|---|
| 31 | : _currIndex( -1 ), |
|---|
| 32 | _fltOpt( fltOpt ) |
|---|
| 33 | { |
|---|
| 34 | |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | int MaterialPaletteManager::add(osg::Material const* material) |
|---|
| 39 | { |
|---|
| 40 | int index = -1; |
|---|
| 41 | if (material == NULL) return -1; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | MaterialPalette::const_iterator it = _materialPalette.find(material); |
|---|
| 46 | if ( it != _materialPalette.end() ) |
|---|
| 47 | { |
|---|
| 48 | index = it->second.Index; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | else |
|---|
| 53 | { |
|---|
| 54 | index = ++_currIndex; |
|---|
| 55 | _materialPalette.insert(std::make_pair(material, |
|---|
| 56 | MaterialRecord(material, index) ) ); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | return index; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void |
|---|
| 63 | MaterialPaletteManager::write( DataOutputStream& dos ) const |
|---|
| 64 | { |
|---|
| 65 | using osg::Vec4f; |
|---|
| 66 | |
|---|
| 67 | MaterialPalette::const_iterator it = _materialPalette.begin(); |
|---|
| 68 | for ( ; it != _materialPalette.end(); ++it) |
|---|
| 69 | { |
|---|
| 70 | MaterialRecord m = it->second; |
|---|
| 71 | Vec4f const& ambient = m.Material->getAmbient(osg::Material::FRONT); |
|---|
| 72 | Vec4f const& diffuse = m.Material->getDiffuse(osg::Material::FRONT); |
|---|
| 73 | Vec4f const& specular = m.Material->getSpecular(osg::Material::FRONT); |
|---|
| 74 | Vec4f const& emissive = m.Material->getEmission(osg::Material::FRONT); |
|---|
| 75 | float shininess = m.Material->getShininess(osg::Material::FRONT); |
|---|
| 76 | |
|---|
| 77 | dos.writeInt16( (int16) MATERIAL_PALETTE_OP ); |
|---|
| 78 | dos.writeInt16( 84 ); |
|---|
| 79 | dos.writeInt32( m.Index ); |
|---|
| 80 | dos.writeString( m.Material->getName(), 12 ); |
|---|
| 81 | dos.writeInt32( 0 ); |
|---|
| 82 | dos.writeFloat32(ambient.r() ); |
|---|
| 83 | dos.writeFloat32(ambient.g() ); |
|---|
| 84 | dos.writeFloat32(ambient.b() ); |
|---|
| 85 | dos.writeFloat32(diffuse.r() ); |
|---|
| 86 | dos.writeFloat32(diffuse.g() ); |
|---|
| 87 | dos.writeFloat32(diffuse.b() ); |
|---|
| 88 | dos.writeFloat32(specular.r() ); |
|---|
| 89 | dos.writeFloat32(specular.g() ); |
|---|
| 90 | dos.writeFloat32(specular.b() ); |
|---|
| 91 | dos.writeFloat32(emissive.r() ); |
|---|
| 92 | dos.writeFloat32(emissive.g() ); |
|---|
| 93 | dos.writeFloat32(emissive.b() ); |
|---|
| 94 | dos.writeFloat32(shininess); |
|---|
| 95 | dos.writeFloat32( diffuse.a() ); |
|---|
| 96 | dos.writeFloat32(1.0f); |
|---|
| 97 | |
|---|
| 98 | if (m.Material->getAmbientFrontAndBack() == false || |
|---|
| 99 | m.Material->getDiffuseFrontAndBack() == false || |
|---|
| 100 | m.Material->getSpecularFrontAndBack() == false || |
|---|
| 101 | m.Material->getEmissionFrontAndBack() == false || |
|---|
| 102 | m.Material->getShininessFrontAndBack() == false ) |
|---|
| 103 | |
|---|
| 104 | { |
|---|
| 105 | std::string warning( "fltexp: No support for different front and back material properties." ); |
|---|
| 106 | OSG_WARN << warning << std::endl; |
|---|
| 107 | _fltOpt.getWriteResult().warn( warning ); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | } |
|---|