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

Revision 13041, 3.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/*
2 * This library is open source and may be redistributed and/or modified under
3 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or (at
4 * your option) any later version. The full license is in the LICENSE file
5 * included with this distribution, and on the openscenegraph.org website.
6 *
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * OpenSceneGraph Public License for more details.
11*/
12
13//
14// Copyright(c) 2008 Skew Matrix Software LLC.
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
26namespace flt
27{
28
29
30MaterialPaletteManager::MaterialPaletteManager( ExportOptions& fltOpt )
31  : _currIndex( -1 ),
32    _fltOpt( fltOpt )
33{
34    // TODO: Pay attention to the version here(?)
35}
36
37
38int MaterialPaletteManager::add(osg::Material const* material)
39{
40    int index = -1;
41    if (material == NULL) return -1;
42
43
44    // If this material has already been cached, set 'index' to the cached value
45    MaterialPalette::const_iterator it = _materialPalette.find(material);
46    if ( it != _materialPalette.end() )
47    {
48        index = it->second.Index;
49    }
50
51    // New material? Add it to the cache...
52    else
53    {
54        index = ++_currIndex;
55        _materialPalette.insert(std::make_pair(material,
56                                               MaterialRecord(material, index) ) );
57    }
58
59    return index;
60}
61
62void
63MaterialPaletteManager::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 );            // Length - FIXME: hard-code/FLT version?
79        dos.writeInt32( m.Index );
80        dos.writeString( m.Material->getName(), 12 );
81        dos.writeInt32( 0 );             // Flags
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() ); // alpha
96        dos.writeFloat32(1.0f);       // 'Reserved' - unused
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// End namespace fltexp
Note: See TracBrowser for help on using the browser.