]> git.mxchange.org Git - flightgear.git/blob - src/Objects/matlib.cxx
Added interface to ATC xponder hardware.
[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_NAMESPACE(std);
57 SG_USING_STD(string);
58
59
60 // global material management class
61 FGMaterialLib material_lib;
62
63
64 // Constructor
65 FGMaterialLib::FGMaterialLib ( void ) {
66   set_step(0);
67 }
68
69
70 // Load a library of material properties
71 bool FGMaterialLib::load( const string& mpath ) {
72
73   SGPropertyNode materials;
74
75   SG_LOG(SG_INPUT, SG_INFO, "Reading materials from " << mpath);
76   try {
77     readProperties(mpath, &materials);
78   } catch (const sg_exception &ex) {
79     SG_LOG(SG_INPUT, SG_ALERT, "Error reading materials: " << ex.getMessage());
80     throw ex;
81   }
82
83   int nMaterials = materials.nChildren();
84   for (int i = 0; i < nMaterials; i++) {
85     const SGPropertyNode * node = materials.getChild(i);
86     if (!strcmp(node->getName(), "material")) {
87       FGNewMat * m = new FGNewMat(node);
88
89       vector<SGPropertyNode_ptr>names = node->getChildren("name");
90       for (unsigned int j = 0; j < names.size(); j++) {
91         string name = names[j]->getStringValue();
92         m->ref();
93         // cerr << "Material " << name << endl;
94         matlib[name] = m;
95         SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material "
96                 << names[j]->getStringValue());
97       }
98     } else {
99       SG_LOG(SG_INPUT, SG_ALERT,
100              "Skipping bad material entry " << node->getName());
101     }
102   }
103
104     // hard coded light state
105     ssgSimpleState *lights = new ssgSimpleState;
106     lights->ref();
107     lights->disable( GL_TEXTURE_2D );
108     lights->enable( GL_CULL_FACE );
109     lights->enable( GL_COLOR_MATERIAL );
110     lights->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
111     lights->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
112     lights->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
113     lights->enable( GL_BLEND );
114     lights->disable( GL_ALPHA_TEST );
115     lights->disable( GL_LIGHTING );
116
117     matlib["LIGHTS"] = new FGNewMat(lights);
118
119     return true;
120 }
121
122
123 // Load a library of material properties
124 bool FGMaterialLib::add_item ( const string &tex_path )
125 {
126     string material_name = tex_path;
127     int pos = tex_path.rfind( "/" );
128     material_name = material_name.substr( pos + 1 );
129
130     return add_item( material_name, tex_path );
131 }
132
133
134 // Load a library of material properties
135 bool FGMaterialLib::add_item ( const string &mat_name, const string &full_path )
136 {
137     int pos = full_path.rfind( "/" );
138     string tex_name = full_path.substr( pos + 1 );
139     string tex_path = full_path.substr( 0, pos );
140
141     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material " 
142             << mat_name << " (" << full_path << ")");
143
144     material_lib.matlib[mat_name] = new FGNewMat(full_path);
145
146     return true;
147 }
148
149
150 // Load a library of material properties
151 bool FGMaterialLib::add_item ( const string &mat_name, ssgSimpleState *state )
152 {
153     FGNewMat *m = new FGNewMat(state);
154
155     SG_LOG( SG_TERRAIN, SG_INFO, "  Loading material given a premade "
156             << "ssgSimpleState = " << mat_name );
157
158     material_lib.matlib[mat_name] = m;
159
160     return true;
161 }
162
163
164 // find a material record by material name
165 FGNewMat *FGMaterialLib::find( const string& material ) {
166     FGNewMat *result = NULL;
167     material_map_iterator it = matlib.find( material );
168     if ( it != end() ) {
169         result = it->second;
170         return result;
171     }
172
173     return NULL;
174 }
175
176
177 // Destructor
178 FGMaterialLib::~FGMaterialLib ( void ) {
179     // Free up all the material entries first
180     for ( material_map_iterator it = begin(); it != end(); it++ ) {
181         FGNewMat *slot = it->second;
182         slot->deRef();
183         if ( slot->getRef() <= 0 ) {
184             delete slot;
185         }
186     }
187 }
188
189
190 // Set the step for all of the state selectors in the material slots
191 void FGMaterialLib::set_step ( int step )
192 {
193     // container::iterator it = begin();
194     for ( material_map_iterator it = begin(); it != end(); it++ ) {
195         const string &key = it->first;
196         SG_LOG( SG_GENERAL, SG_INFO,
197                 "Updating material " << key << " to step " << step );
198         FGNewMat *slot = it->second;
199         slot->get_state()->selectStep(step);
200     }
201 }
202
203
204 // Get the step for the state selectors
205 int FGMaterialLib::get_step ()
206 {
207   material_map_iterator it = begin();
208   return it->second->get_state()->getSelectStep();
209 }
210
211
212 // Load one pending "deferred" texture.  Return true if a texture
213 // loaded successfully, false if no pending, or error.
214 void FGMaterialLib::load_next_deferred() {
215     // container::iterator it = begin();
216     for ( material_map_iterator it = begin(); it != end(); it++ ) {
217         /* we don't need the key, but here's how we'd get it if we wanted it. */
218         // const string &key = it->first;
219         FGNewMat *slot = it->second;
220         if (slot->load_texture())
221           return;
222     }
223 }