]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.cxx
8606e61666d93be63181b11b227dd65281331c77
[simgear.git] / simgear / scene / material / mat.cxx
1 // mat.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2000  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 #include <simgear/compiler.h>
29
30 #include <string.h>
31 #include <map>
32 SG_USING_STD(map);
33
34 #include <plib/ul.h>
35
36 #ifdef SG_MATH_EXCEPTION_CLASH
37 #  include <math.h>
38 #endif
39
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
43
44 #include "mat.hxx"
45
46 \f
47 ////////////////////////////////////////////////////////////////////////
48 // Constructors and destructor.
49 ////////////////////////////////////////////////////////////////////////
50
51
52 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season )
53 {
54     init();
55     read_properties( fg_root, props, season );
56     build_ssg_state( false );
57 }
58
59 SGMaterial::SGMaterial( const string &texpath )
60 {
61     init();
62
63     _internal_state st( NULL, texpath, false );
64     _status.push_back( st );
65
66     build_ssg_state( true );
67 }
68
69 SGMaterial::SGMaterial( ssgSimpleState *s )
70 {
71     init();
72     set_ssg_state( s );
73 }
74
75 SGMaterial::~SGMaterial (void)
76 {
77 }
78
79
80 \f
81 ////////////////////////////////////////////////////////////////////////
82 // Public methods.
83 ////////////////////////////////////////////////////////////////////////
84
85 void
86 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season )
87 {
88                                 // Gather the path(s) to the texture(s)
89   vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
90   for (unsigned int i = 0; i < textures.size(); i++)
91   {
92     string tname = textures[i]->getStringValue();
93     string otname = tname;
94     if (season && strncmp(season, "summer", 6))
95     {
96         if (tname.substr(0,7) == "Terrain")
97             tname.insert(7,"."+string(season));
98     }
99
100     if (tname == "") {
101         tname = "unknown.rgb";
102     }
103
104     SGPath tpath( fg_root );
105     tpath.append("Textures.high");
106     tpath.append(tname);
107     if ( !ulFileExists(tpath.c_str()) ) {
108       tpath = SGPath( fg_root );
109       tpath.append("Textures");
110       tpath.append(tname);
111     }
112
113     if ( ulFileExists(tpath.c_str()) ) {
114       _internal_state st( NULL, tpath.str(), false );
115       _status.push_back( st );
116     }
117   }
118
119   if (textures.size() == 0) {
120     string tname = "unknown.rgb";
121     SGPath tpath( fg_root );
122     tpath.append("Textures");
123     tpath.append(tname);
124     _internal_state st( NULL, tpath.str(), true );
125     _status.push_back( st );
126   }
127
128   xsize = props->getDoubleValue("xsize", 0.0);
129   ysize = props->getDoubleValue("ysize", 0.0);
130   wrapu = props->getBoolValue("wrapu", true);
131   wrapv = props->getBoolValue("wrapv", true);
132   mipmap = props->getBoolValue("mipmap", true);
133   light_coverage = props->getDoubleValue("light-coverage", 0.0);
134
135   // Taken from default values as used in ac3d
136   ambient[0] = props->getDoubleValue("ambient/r", 0.2);
137   ambient[1] = props->getDoubleValue("ambient/g", 0.2);
138   ambient[2] = props->getDoubleValue("ambient/b", 0.2);
139   ambient[3] = props->getDoubleValue("ambient/a", 1.0);
140
141   diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
142   diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
143   diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
144   diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
145
146   specular[0] = props->getDoubleValue("specular/r", 0.0);
147   specular[1] = props->getDoubleValue("specular/g", 0.0);
148   specular[2] = props->getDoubleValue("specular/b", 0.0);
149   specular[3] = props->getDoubleValue("specular/a", 1.0);
150
151   emission[0] = props->getDoubleValue("emissive/r", 0.0);
152   emission[1] = props->getDoubleValue("emissive/g", 0.0);
153   emission[2] = props->getDoubleValue("emissive/b", 0.0);
154   emission[3] = props->getDoubleValue("emissive/a", 1.0);
155
156   shininess = props->getDoubleValue("shininess", 1.0);
157
158   vector<SGPropertyNode_ptr> object_group_nodes =
159     ((SGPropertyNode *)props)->getChildren("object-group");
160   for (unsigned int i = 0; i < object_group_nodes.size(); i++)
161     object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
162
163   // read glyph table for taxi-/runway-signs
164   vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
165   for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
166     const char *name = glyph_nodes[i]->getStringValue("name");
167     if (name)
168       glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
169   }
170 }
171
172
173 \f
174 ////////////////////////////////////////////////////////////////////////
175 // Private methods.
176 ////////////////////////////////////////////////////////////////////////
177
178 void 
179 SGMaterial::init ()
180 {
181     _status.clear();
182     _current_ptr = 0;
183     xsize = 0;
184     ysize = 0;
185     wrapu = true;
186     wrapv = true;
187
188     mipmap = true;
189     light_coverage = 0.0;
190     shininess = 1.0;
191     for (int i = 0; i < 4; i++) {
192         ambient[i]  = (i < 3) ? 0.2 : 1.0;
193         specular[i] = (i < 3) ? 0.0 : 1.0;
194         diffuse[i]  = (i < 3) ? 0.8 : 1.0;
195         emission[i] = (i < 3) ? 0.0 : 1.0;
196     }
197 }
198
199 bool
200 SGMaterial::load_texture ( int n )
201 {
202     int i   = (n >= 0) ? n   : 0 ;
203     int end = (n >= 0) ? n+1 : _status.size();
204
205     for (; i < end; i++)
206     {
207         if ( !_status[i].texture_loaded ) {
208             SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
209                                           << _status[i].texture_path );
210             _status[i].state->setTexture(
211                    (char *)_status[i].texture_path.c_str(),
212                    wrapu, wrapv, mipmap );
213             _status[i].texture_loaded = true;
214        }
215     }
216     return true;
217 }
218
219 ssgSimpleState *
220 SGMaterial::get_state (int n) const
221 {
222     if (_status.size() == 0) {
223         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
224         return NULL;
225     }
226
227     ssgSimpleState *st = (n >= 0) ? _status[n].state
228                                   : _status[_current_ptr].state;
229     ((SGMaterial *)this)->_current_ptr += 1;
230     if (_current_ptr >= _status.size())
231         ((SGMaterial *)this)->_current_ptr = 0;
232
233     return st;
234 }
235
236
237 void 
238 SGMaterial::build_ssg_state( bool defer_tex_load )
239 {
240     GLenum shade_model = GL_SMOOTH;
241     
242     for (unsigned int i = 0; i < _status.size(); i++)
243     {
244         ssgSimpleState *state = new ssgSimpleState();
245
246         // Set up the textured state
247         state->setShadeModel( shade_model );
248         state->enable( GL_LIGHTING );
249         state->enable ( GL_CULL_FACE ) ;
250         state->enable( GL_TEXTURE_2D );
251         state->disable( GL_BLEND );
252         state->disable( GL_ALPHA_TEST );
253
254         if ( !defer_tex_load ) {
255             SG_LOG(SG_INPUT, SG_INFO, "    " << _status[i].texture_path );
256             state->setTexture( (char *)_status[i].texture_path.c_str(),
257                                 wrapu, wrapv );
258             _status[i].texture_loaded = true;
259         } else {
260             _status[i].texture_loaded = false;
261         }
262
263         state->enable( GL_COLOR_MATERIAL );
264         state->setMaterial ( GL_AMBIENT,
265                              ambient[0], ambient[1],
266                              ambient[2], ambient[3] ) ;
267         state->setMaterial ( GL_DIFFUSE,
268                             diffuse[0], diffuse[1],
269                             diffuse[2], diffuse[3] ) ;
270         state->setMaterial ( GL_SPECULAR,
271                             specular[0], specular[1],
272                             specular[2], specular[3] ) ;
273         state->setMaterial ( GL_EMISSION,
274                             emission[0], emission[1],
275                             emission[2], emission[3] ) ;
276         state->setShininess ( shininess );
277
278         _status[i].state = state;
279     }
280 }
281
282
283 void SGMaterial::set_ssg_state( ssgSimpleState *s )
284 {
285     _status.push_back( _internal_state( s, "", true ) );
286 }
287
288
289 \f
290 ////////////////////////////////////////////////////////////////////////
291 // SGMaterialGlyph.
292 ////////////////////////////////////////////////////////////////////////
293
294 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
295     _left(p->getDoubleValue("left", 0.0)),
296     _right(p->getDoubleValue("right", 1.0))
297 {
298 }
299
300
301 // end of mat.cxx