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