]> git.mxchange.org Git - flightgear.git/blob - src/Objects/matlib.cxx
Fixed runway-altitude problem that prevented starts at airports higher
[flightgear.git] / src / Objects / 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  - curt@me.umn.edu
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <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 #include <GL/glut.h>
37 #include <GL/gl.h>
38
39 #include <simgear/compiler.h>
40 #include <simgear/misc/exception.hxx>
41
42 #include <string.h>
43 #include STL_STRING
44
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/misc/sg_path.hxx>
47 #include <simgear/misc/sgstream.hxx>
48
49 #include <Include/general.hxx>
50 #include <Main/globals.hxx>
51 #include <Main/fg_props.hxx>
52 #include <Scenery/tileentry.hxx>
53
54 #include "matlib.hxx"
55
56 SG_USING_STD(string);
57
58
59 // global material management class
60 FGMaterialLib material_lib;
61
62
63 // Constructor
64 FGMaterialLib::FGMaterialLib ( void ) {
65   set_step(0);
66 }
67
68
69 // Load a library of material properties
70 bool FGMaterialLib::load( const string& mpath ) {
71
72   SGPropertyNode materials;
73
74   cout << "Reading materials from " << mpath << endl;
75   try {
76     readProperties(mpath, &materials);
77   } catch (const sg_exception &ex) {
78     SG_LOG(SG_INPUT, SG_ALERT, "Error reading materials: " << ex.getMessage());
79     throw ex;
80   }
81
82   int nMaterials = materials.nChildren();
83   for (int i = 0; i < nMaterials; i++) {
84     const SGPropertyNode * node = materials.getChild(i);
85     if (node->getName() == "material") {
86       FGNewMat * m = new FGNewMat(node);
87
88       vector<const SGPropertyNode *>names = node->getChildren("name");
89       for (unsigned int j = 0; j < names.size(); j++) {
90         m->ref();
91         matlib[names[j]->getStringValue()] = m;
92         SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
93                 << names[j]->getStringValue());
94       }
95     } else {
96       SG_LOG(SG_INPUT, SG_ALERT,
97              "Skipping bad material entry " << node->getName());
98     }
99   }
100
101     // hard coded light state
102     ssgSimpleState *lights = new ssgSimpleState;
103     lights->ref();
104     lights->disable( GL_TEXTURE_2D );
105     lights->enable( GL_CULL_FACE );
106     lights->enable( GL_COLOR_MATERIAL );
107     lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
108     lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
109     lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
110     lights->enable( GL_BLEND );
111     lights->disable( GL_ALPHA_TEST );
112     lights->disable( GL_LIGHTING );
113
114     matlib["LIGHTS"] = new FGNewMat(lights);
115
116     return true;
117 }
118
119
120 // Load a library of material properties
121 bool FGMaterialLib::add_item ( const string &tex_path )
122 {
123     string material_name = tex_path;
124     int pos = tex_path.rfind( "/" );
125     material_name = material_name.substr( pos + 1 );
126
127     return add_item( material_name, tex_path );
128 }
129
130
131 // Load a library of material properties
132 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
133 {
134     int pos = full_path.rfind( "/" );
135     string tex_name = full_path.substr( pos + 1 );
136     string tex_path = full_path.substr( 0, pos );
137
138     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
139             << mat_name << " (" << full_path << ")");
140
141     material_lib.matlib[mat_name] = new FGNewMat(full_path);
142
143     return true;
144 }
145
146
147 // Load a library of material properties
148 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
149 {
150     FGNewMat *m = new FGNewMat(state);
151
152     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
153             << "ssgSimpleState = " << mat_name );
154
155     material_lib.matlib[mat_name] = m;
156
157     return true;
158 }
159
160
161 // find a material record by material name
162 FGNewMat *FGMaterialLib::find( const string& material ) {
163     FGNewMat *result = NULL;
164     material_map_iterator it = matlib.find( material );
165     if ( it != end() ) {
166         result = it->second;
167         return result;
168     }
169
170     return NULL;
171 }
172
173
174 // Destructor
175 FGMaterialLib::~FGMaterialLib ( void ) {
176     // Free up all the material entries first
177     for ( material_map_iterator it = begin(); it != end(); it++ ) {
178         FGNewMat *slot = it->second;
179         slot->deRef();
180         if ( slot->getRef() <= 0 ) {
181             delete slot;
182         }
183     }
184 }
185
186
187 // Set the step for all of the state selectors in the material slots
188 void FGMaterialLib::set_step ( int step )
189 {
190     // container::iterator it = begin();
191     for ( material_map_iterator it = begin(); it != end(); it++ ) {
192         const string &key = it->first;
193         SG_LOG( SG_GENERAL, SG_INFO,
194                 "Updating material " << key << " to step " << step );
195         FGNewMat *slot = it->second;
196         slot->get_state()->selectStep(step);
197     }
198 }
199
200
201 // Get the step for the state selectors
202 int FGMaterialLib::get_step ()
203 {
204   material_map_iterator it = begin();
205   return it->second->get_state()->getSelectStep();
206 }
207
208
209 // Load one pending "deferred" texture.  Return true if a texture
210 // loaded successfully, false if no pending, or error.
211 void FGMaterialLib::load_next_deferred() {
212     // container::iterator it = begin();
213     for ( material_map_iterator it = begin(); it != end(); it++ ) {
214         const string &key = it->first;
215         FGNewMat *slot = it->second;
216         if (slot->load_texture())
217           return;
218     }
219 }
220
221