]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
b02aac76dd51dce5af0c3ddd3a57faea667a16cb
[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 SG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #if defined ( __CYGWIN__ )
37 #include <ieeefp.h>
38 #endif
39
40 #include <simgear/compiler.h>
41 #include <simgear/constants.h>
42 #include <simgear/structure/exception.hxx>
43
44 #include SG_GL_H
45
46 #include <string.h>
47 #include STL_STRING
48
49 #include <osg/AlphaFunc>
50 #include <osg/BlendFunc>
51 #include <osg/CullFace>
52 #include <osg/Material>
53 #include <osg/Point>
54 #include <osg/PointSprite>
55 #include <osg/PolygonMode>
56 #include <osg/PolygonOffset>
57 #include <osg/StateSet>
58 #include <osg/TexEnv>
59 #include <osg/TexGen>
60 #include <osg/Texture2D>
61
62 #include <simgear/debug/logstream.hxx>
63 #include <simgear/misc/sg_path.hxx>
64 #include <simgear/misc/sgstream.hxx>
65 #include <simgear/props/props_io.hxx>
66 #include <simgear/props/condition.hxx>
67 #include <simgear/scene/tgdb/userdata.hxx>
68
69 #include "mat.hxx"
70
71 #include "matlib.hxx"
72
73 SG_USING_NAMESPACE(std);
74 SG_USING_STD(string);
75
76 // Constructor
77 SGMaterialLib::SGMaterialLib ( void ) {
78 }
79
80 // Load a library of material properties
81 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
82         SGPropertyNode *prop_root )
83 {
84     SGPropertyNode materials;
85
86     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
87     try {
88         readProperties( mpath, &materials );
89     } catch (const sg_exception &ex) {
90         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
91                 << ex.getMessage() );
92         throw;
93     }
94
95     int nMaterials = materials.nChildren();
96     for (int i = 0; i < nMaterials; i++) {
97         const SGPropertyNode *node = materials.getChild(i);
98         if (!strcmp(node->getName(), "material")) {
99             const SGPropertyNode *conditionNode = node->getChild("condition");
100             if (conditionNode) {
101                 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
102                 if (!condition->test()) {
103                     SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
104                         << i << " (condition false)");
105                     continue;
106                 }
107             }
108
109             SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
110
111             vector<SGPropertyNode_ptr>names = node->getChildren("name");
112             for ( unsigned int j = 0; j < names.size(); j++ ) {
113                 string name = names[j]->getStringValue();
114                 // cerr << "Material " << name << endl;
115                 matlib[name] = m;
116                 m->add_name(name);
117                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
118                         << names[j]->getStringValue() );
119             }
120         } else {
121             SG_LOG(SG_INPUT, SG_WARN,
122                    "Skipping bad material entry " << node->getName());
123         }
124     }
125
126     return true;
127 }
128
129
130 // Load a library of material properties
131 bool SGMaterialLib::add_item ( const string &tex_path )
132 {
133     string material_name = tex_path;
134     int pos = tex_path.rfind( "/" );
135     material_name = material_name.substr( pos + 1 );
136
137     return add_item( material_name, tex_path );
138 }
139
140
141 // Load a library of material properties
142 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
143 {
144     int pos = full_path.rfind( "/" );
145     string tex_name = full_path.substr( pos + 1 );
146     string tex_path = full_path.substr( 0, pos );
147
148     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
149             << mat_name << " (" << full_path << ")");
150
151     matlib[mat_name] = new SGMaterial( full_path );
152     matlib[mat_name]->add_name(mat_name);
153
154     return true;
155 }
156
157
158 // Load a library of material properties
159 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
160 {
161     matlib[mat_name] = new SGMaterial( state );
162     matlib[mat_name]->add_name(mat_name);
163
164     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
165             << "osg::StateSet = " << mat_name );
166
167     return true;
168 }
169
170
171 // find a material record by material name
172 SGMaterial *SGMaterialLib::find( const string& material ) {
173     SGMaterial *result = NULL;
174     material_map_iterator it = matlib.find( material );
175     if ( it != end() ) {
176         result = it->second;
177         return result;
178     }
179
180     return NULL;
181 }
182
183 // Destructor
184 SGMaterialLib::~SGMaterialLib ( void ) {
185     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
186 }
187
188 const SGMaterial*
189 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
190 {
191   if (!stateSet)
192     return 0;
193   
194   const osg::Referenced* base = stateSet->getUserData();
195   if (!base)
196     return 0;
197
198   const SGMaterialUserData* matUserData
199     = dynamic_cast<const SGMaterialUserData*>(base);
200   if (!matUserData)
201     return 0;
202
203   return matUserData->getMaterial();
204 }