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