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

Revision 13041, 4.3 kB (checked in by robert, 2 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® loader for OpenSceneGraph
16//
17//  Copyright (C) 2005-2007  Brede Johansen
18//
19
20#include <assert.h>
21#include "Pools.h"
22#include "Document.h"
23
24using namespace flt;
25
26
27osg::Vec4 ColorPool::getColor(int indexIntensity) const
28{
29    if (_old) // version <= 13
30    {
31        // bit 0-6:  intensity
32        // bit 7-11  color index
33        // bit 12    fixed intensity bit
34        bool fixedIntensity = (indexIntensity & 0x1000) ? true : false;
35        unsigned int index = (fixedIntensity) ? (indexIntensity & 0x0fff)+(4096>>7) : indexIntensity >> 7;
36
37        if (index>=size())
38        {
39            // color index not available.
40            return osg::Vec4(1,1,1,1);
41        }
42
43        osg::Vec4 col =  operator[](index);
44        if (!fixedIntensity)
45        {
46            float intensity = (float)(indexIntensity & 0x7f)/127.f;
47            col[0] *= intensity;
48            col[1] *= intensity;
49            col[2] *= intensity;
50        }
51        return col;
52    }
53    else // version > 13
54    {
55        // bit 0-6:  intensity
56        // bit 7-15  color index
57        unsigned int index = indexIntensity >> 7;
58
59        if (index>=size())
60        {
61            // color index not available.
62            return osg::Vec4(1,1,1,1);
63        }
64
65        osg::Vec4 col =  operator[](index);
66        float intensity = (float)(indexIntensity & 0x7f)/127.f;
67        col[0] *= intensity;
68        col[1] *= intensity;
69        col[2] *= intensity;
70        return col;
71    }
72}
73
74
75MaterialPool::MaterialPool()
76{
77    // Default material.
78    // http://www.multigen-paradigm.com/ubb/Forum1/HTML/000228.html
79    _defaultMaterial = new osg::Material;
80    _defaultMaterial->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(1,1,1,1));
81    _defaultMaterial->setDiffuse (osg::Material::FRONT_AND_BACK,osg::Vec4(1,1,1,1));
82    _defaultMaterial->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(0,0,0,1));
83    _defaultMaterial->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(0,0,0,1));
84    _defaultMaterial->setShininess(osg::Material::FRONT_AND_BACK,0);
85}
86
87
88
89osg::Material* MaterialPool::get(int index)
90{
91    iterator itr = find(index);
92    if (itr != end())
93        return (*itr).second.get();
94
95    // Use default material if not found in material palette.
96    return _defaultMaterial.get();
97}
98
99
100namespace {
101
102    osg::Vec4 finalColor(const osg::Vec4& materialColor, const osg::Vec4& faceColor)
103    {
104        return osg::Vec4(
105            materialColor.r() * faceColor.r(),
106            materialColor.g() * faceColor.g(),
107            materialColor.b() * faceColor.b(),
108            materialColor.a() * faceColor.a());
109    }
110
111} // end namespace
112
113osg::Material* MaterialPool::getOrCreateMaterial(int index, const osg::Vec4& faceColor)
114{
115    MaterialParameters materialParam(index,faceColor);
116
117    // Look for final material.
118    FinalMaterialMap::iterator itr = _finalMaterialMap.find(materialParam);
119    if (itr != _finalMaterialMap.end())
120    {
121        // Final material found.
122        return (*itr).second.get();
123    }
124
125    osg::Material* poolMaterial = get(index);
126
127    // Create final material.
128    osg::Material* material = dynamic_cast<osg::Material*>(poolMaterial->clone(osg::CopyOp::SHALLOW_COPY));
129    osg::Vec4 materialPaletteAmbient = poolMaterial->getAmbient(osg::Material::FRONT);
130    osg::Vec4 materialPaletteDiffuse = poolMaterial->getDiffuse(osg::Material::FRONT);
131
132    material->setAmbient(osg::Material::FRONT_AND_BACK, finalColor(materialPaletteAmbient, faceColor));
133    material->setDiffuse(osg::Material::FRONT_AND_BACK, finalColor(materialPaletteDiffuse, faceColor));
134    material->setAlpha(osg::Material::FRONT_AND_BACK, materialPaletteAmbient.a()*faceColor.a());
135
136    // Set final material so it can be reused.
137    _finalMaterialMap[materialParam] = material;
138
139    return material;
140}
Note: See TracBrowser for help on using the browser.