]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
2d6fddb85db8261f4c70a2fd990f318e0820b53a
[simgear.git] / simgear / scene / material / matlib.cxx
1 // materialmgr.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #if defined ( __CYGWIN__ )
33 #include <ieeefp.h>
34 #endif
35
36 #include <simgear/compiler.h>
37 #include <simgear/constants.h>
38 #include <simgear/structure/exception.hxx>
39
40 #include SG_GL_H
41
42 #include <string.h>
43 #include <string>
44
45 #include <osg/AlphaFunc>
46 #include <osg/BlendFunc>
47 #include <osg/CullFace>
48 #include <osg/Material>
49 #include <osg/Point>
50 #include <osg/PointSprite>
51 #include <osg/PolygonMode>
52 #include <osg/PolygonOffset>
53 #include <osg/StateSet>
54 #include <osg/TexEnv>
55 #include <osg/TexGen>
56 #include <osg/Texture2D>
57
58 #include <simgear/debug/logstream.hxx>
59 #include <simgear/misc/sg_path.hxx>
60 #include <simgear/misc/sgstream.hxx>
61 #include <simgear/props/props_io.hxx>
62 #include <simgear/props/condition.hxx>
63 #include <simgear/scene/tgdb/userdata.hxx>
64
65 #include "mat.hxx"
66
67 #include "matlib.hxx"
68
69 SG_USING_STD(string);
70
71 // Constructor
72 SGMaterialLib::SGMaterialLib ( void ) {
73 }
74
75 // Load a library of material properties
76 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
77         SGPropertyNode *prop_root )
78 {
79     SGPropertyNode materials;
80
81     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
82     try {
83         readProperties( mpath, &materials );
84     } catch (const sg_exception &ex) {
85         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
86                 << ex.getMessage() );
87         throw;
88     }
89
90     int nMaterials = materials.nChildren();
91     for (int i = 0; i < nMaterials; i++) {
92         const SGPropertyNode *node = materials.getChild(i);
93         if (!strcmp(node->getName(), "material")) {
94             const SGPropertyNode *conditionNode = node->getChild("condition");
95             if (conditionNode) {
96                 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
97                 if (!condition->test()) {
98                     SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
99                         << i << " (condition false)");
100                     continue;
101                 }
102             }
103
104             SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
105
106             vector<SGPropertyNode_ptr>names = node->getChildren("name");
107             for ( unsigned int j = 0; j < names.size(); j++ ) {
108                 string name = names[j]->getStringValue();
109                 // cerr << "Material " << name << endl;
110                 matlib[name] = m;
111                 m->add_name(name);
112                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
113                         << names[j]->getStringValue() );
114             }
115         } else {
116             SG_LOG(SG_INPUT, SG_WARN,
117                    "Skipping bad material entry " << node->getName());
118         }
119     }
120
121     return true;
122 }
123
124
125 // Load a library of material properties
126 bool SGMaterialLib::add_item ( const string &tex_path )
127 {
128     string material_name = tex_path;
129     int pos = tex_path.rfind( "/" );
130     material_name = material_name.substr( pos + 1 );
131
132     return add_item( material_name, tex_path );
133 }
134
135
136 // Load a library of material properties
137 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
138 {
139     int pos = full_path.rfind( "/" );
140     string tex_name = full_path.substr( pos + 1 );
141     string tex_path = full_path.substr( 0, pos );
142
143     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
144             << mat_name << " (" << full_path << ")");
145
146     matlib[mat_name] = new SGMaterial( full_path );
147     matlib[mat_name]->add_name(mat_name);
148
149     return true;
150 }
151
152
153 // Load a library of material properties
154 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
155 {
156     matlib[mat_name] = new SGMaterial( state );
157     matlib[mat_name]->add_name(mat_name);
158
159     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
160             << "osg::StateSet = " << mat_name );
161
162     return true;
163 }
164
165
166 // find a material record by material name
167 SGMaterial *SGMaterialLib::find( const string& material ) {
168     SGMaterial *result = NULL;
169     material_map_iterator it = matlib.find( material );
170     if ( it != end() ) {
171         result = it->second;
172         return result;
173     }
174
175     return NULL;
176 }
177
178 // Destructor
179 SGMaterialLib::~SGMaterialLib ( void ) {
180     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
181 }
182
183 const SGMaterial*
184 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
185 {
186   if (!stateSet)
187     return 0;
188   
189   const osg::Referenced* base = stateSet->getUserData();
190   if (!base)
191     return 0;
192
193   const SGMaterialUserData* matUserData
194     = dynamic_cast<const SGMaterialUserData*>(base);
195   if (!matUserData)
196     return 0;
197
198   return matUserData->getMaterial();
199 }