]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/matlib.cxx
Attached patches remove BORLANDC, and hence SG_MATH_EXCEPTION_CLASH and SG_INCOM
[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 STL_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_NAMESPACE(std);
70 SG_USING_STD(string);
71
72 // Constructor
73 SGMaterialLib::SGMaterialLib ( void ) {
74 }
75
76 // Load a library of material properties
77 bool SGMaterialLib::load( const string &fg_root, const string& mpath, const char *season,
78         SGPropertyNode *prop_root )
79 {
80     SGPropertyNode materials;
81
82     SG_LOG( SG_INPUT, SG_INFO, "Reading materials from " << mpath );
83     try {
84         readProperties( mpath, &materials );
85     } catch (const sg_exception &ex) {
86         SG_LOG( SG_INPUT, SG_ALERT, "Error reading materials: "
87                 << ex.getMessage() );
88         throw;
89     }
90
91     int nMaterials = materials.nChildren();
92     for (int i = 0; i < nMaterials; i++) {
93         const SGPropertyNode *node = materials.getChild(i);
94         if (!strcmp(node->getName(), "material")) {
95             const SGPropertyNode *conditionNode = node->getChild("condition");
96             if (conditionNode) {
97                 SGSharedPtr<const SGCondition> condition = sgReadCondition(prop_root, conditionNode);
98                 if (!condition->test()) {
99                     SG_LOG(SG_INPUT, SG_DEBUG, "Skipping material entry #"
100                         << i << " (condition false)");
101                     continue;
102                 }
103             }
104
105             SGSharedPtr<SGMaterial> m = new SGMaterial(fg_root, node, season);
106
107             vector<SGPropertyNode_ptr>names = node->getChildren("name");
108             for ( unsigned int j = 0; j < names.size(); j++ ) {
109                 string name = names[j]->getStringValue();
110                 // cerr << "Material " << name << endl;
111                 matlib[name] = m;
112                 m->add_name(name);
113                 SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
114                         << names[j]->getStringValue() );
115             }
116         } else {
117             SG_LOG(SG_INPUT, SG_WARN,
118                    "Skipping bad material entry " << node->getName());
119         }
120     }
121
122     return true;
123 }
124
125
126 // Load a library of material properties
127 bool SGMaterialLib::add_item ( const string &tex_path )
128 {
129     string material_name = tex_path;
130     int pos = tex_path.rfind( "/" );
131     material_name = material_name.substr( pos + 1 );
132
133     return add_item( material_name, tex_path );
134 }
135
136
137 // Load a library of material properties
138 bool SGMaterialLib::add_item ( const string &mat_name, const string &full_path )
139 {
140     int pos = full_path.rfind( "/" );
141     string tex_name = full_path.substr( pos + 1 );
142     string tex_path = full_path.substr( 0, pos );
143
144     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
145             << mat_name << " (" << full_path << ")");
146
147     matlib[mat_name] = new SGMaterial( full_path );
148     matlib[mat_name]->add_name(mat_name);
149
150     return true;
151 }
152
153
154 // Load a library of material properties
155 bool SGMaterialLib::add_item ( const string &mat_name, osg::StateSet *state )
156 {
157     matlib[mat_name] = new SGMaterial( state );
158     matlib[mat_name]->add_name(mat_name);
159
160     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
161             << "osg::StateSet = " << mat_name );
162
163     return true;
164 }
165
166
167 // find a material record by material name
168 SGMaterial *SGMaterialLib::find( const string& material ) {
169     SGMaterial *result = NULL;
170     material_map_iterator it = matlib.find( material );
171     if ( it != end() ) {
172         result = it->second;
173         return result;
174     }
175
176     return NULL;
177 }
178
179 // Destructor
180 SGMaterialLib::~SGMaterialLib ( void ) {
181     SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
182 }
183
184 const SGMaterial*
185 SGMaterialLib::findMaterial(const osg::StateSet* stateSet) const
186 {
187   if (!stateSet)
188     return 0;
189   
190   const osg::Referenced* base = stateSet->getUserData();
191   if (!base)
192     return 0;
193
194   const SGMaterialUserData* matUserData
195     = dynamic_cast<const SGMaterialUserData*>(base);
196   if (!matUserData)
197     return 0;
198
199   return matUserData->getMaterial();
200 }