]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.cxx
Get rid of non-portable fmax call.
[flightgear.git] / src / Objects / newmat.cxx
1 // newmat.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2000  Curtis L. Olson  - curt@flightgear.org
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 #include <simgear/compiler.h>
29
30 #ifdef SG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/misc/sgstream.hxx>
37
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
40
41 #include "newmat.hxx"
42
43
44 \f
45 ////////////////////////////////////////////////////////////////////////
46 // Local static functions.
47 ////////////////////////////////////////////////////////////////////////
48
49 /**
50  * Internal method to test whether a file exists.
51  *
52  * TODO: this should be moved to a SimGear library of local file
53  * functions.
54  */
55 static inline bool
56 local_file_exists( const string& path ) {
57     sg_gzifstream in( path );
58     if ( ! in.is_open() ) {
59         return false;
60     } else {
61         return true;
62     }
63 }
64
65
66 \f
67 ////////////////////////////////////////////////////////////////////////
68 // Constructors and destructor.
69 ////////////////////////////////////////////////////////////////////////
70
71
72 FGNewMat::FGNewMat (const SGPropertyNode * props)
73 {
74     init();
75     read_properties(props);
76     build_ssg_state(false);
77 }
78
79 FGNewMat::FGNewMat (const string &texpath)
80 {
81     init();
82     texture_path = texpath;
83     build_ssg_state(true);
84 }
85
86 FGNewMat::FGNewMat (ssgSimpleState * s)
87 {
88     init();
89     set_ssg_state(s);
90 }
91
92 FGNewMat::~FGNewMat (void)
93 {
94 }
95
96
97 \f
98 ////////////////////////////////////////////////////////////////////////
99 // Public methods.
100 ////////////////////////////////////////////////////////////////////////
101
102 void
103 FGNewMat::read_properties (const SGPropertyNode * props)
104 {
105                                 // Get the path to the texture
106   string tname = props->getStringValue("texture", "unknown.rgb");
107   SGPath tpath(globals->get_fg_root());
108   tpath.append("Textures.high");
109   tpath.append(tname);
110   if (!local_file_exists(tpath.str())) {
111     tpath = SGPath(globals->get_fg_root());
112     tpath.append("Textures");
113     tpath.append(tname);
114   }
115   texture_path = tpath.str();
116
117   xsize = props->getDoubleValue("xsize", 0.0);
118   ysize = props->getDoubleValue("ysize", 0.0);
119   wrapu = props->getBoolValue("wrapu", true);
120   wrapv = props->getBoolValue("wrapv", true);
121   mipmap = props->getBoolValue("mipmap", true);
122   light_coverage = props->getDoubleValue("light-coverage", 0.0);
123
124   ambient[0] = props->getDoubleValue("ambient/r", 0.0);
125   ambient[1] = props->getDoubleValue("ambient/g", 0.0);
126   ambient[2] = props->getDoubleValue("ambient/b", 0.0);
127   ambient[3] = props->getDoubleValue("ambient/a", 0.0);
128
129   diffuse[0] = props->getDoubleValue("diffuse/r", 0.0);
130   diffuse[1] = props->getDoubleValue("diffuse/g", 0.0);
131   diffuse[2] = props->getDoubleValue("diffuse/b", 0.0);
132   diffuse[3] = props->getDoubleValue("diffuse/a", 0.0);
133
134   specular[0] = props->getDoubleValue("specular/r", 0.0);
135   specular[1] = props->getDoubleValue("specular/g", 0.0);
136   specular[2] = props->getDoubleValue("specular/b", 0.0);
137   specular[3] = props->getDoubleValue("specular/a", 0.0);
138
139   emission[0] = props->getDoubleValue("emissive/r", 0.0);
140   emission[1] = props->getDoubleValue("emissive/g", 0.0);
141   emission[2] = props->getDoubleValue("emissive/b", 0.0);
142   emission[3] = props->getDoubleValue("emissive/a", 0.0);
143 }
144
145
146 \f
147 ////////////////////////////////////////////////////////////////////////
148 // Private methods.
149 ////////////////////////////////////////////////////////////////////////
150
151 void 
152 FGNewMat::init ()
153 {
154   texture_path = "";
155   state = 0;
156   textured = 0;
157   nontextured = 0;
158   xsize = 0;
159   ysize = 0;
160   wrapu = true;
161   wrapv = true;
162   mipmap = true;
163   light_coverage = 0.0;
164   texture_loaded = false;
165   refcount = 0;
166   for (int i = 0; i < 4; i++)
167     ambient[i] = diffuse[i] = specular[i] = emission[i] = 0.0;
168 }
169
170 bool
171 FGNewMat::load_texture ()
172 {
173   if (texture_loaded) {
174     return false;
175   } else {
176     SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture " << texture_path );
177     textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv, mipmap );
178     texture_loaded = true;
179     return true;
180   }
181 }
182
183
184 void 
185 FGNewMat::build_ssg_state (bool defer_tex_load)
186 {
187     GLenum shade_model =
188       (fgGetBool("/sim/rendering/shading") ? GL_SMOOTH : GL_FLAT);
189     bool texture_default = fgGetBool("/sim/rendering/textures");
190
191     state = new ssgStateSelector(2);
192     state->ref();
193
194     textured = new ssgSimpleState();
195     textured->ref();
196
197     nontextured = new ssgSimpleState();
198     nontextured->ref();
199
200     // Set up the textured state
201     textured->setShadeModel( shade_model );
202     textured->enable( GL_LIGHTING );
203     textured->enable ( GL_CULL_FACE ) ;
204     textured->enable( GL_TEXTURE_2D );
205     textured->disable( GL_BLEND );
206     textured->disable( GL_ALPHA_TEST );
207 #if 0
208 #  ifdef GL_EXT_texture_filter_anisotropic
209     float max_anisotropy;
210     glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy );
211     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
212                      max_anisotropy );
213     cout << "Max anisotropy = " << max_anisotropy << endl;
214 #  endif
215 #endif
216     if ( !defer_tex_load ) {
217         textured->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
218         texture_loaded = true;
219     } else {
220         texture_loaded = false;
221     }
222     textured->enable( GL_COLOR_MATERIAL );
223     textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
224     textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
225     textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
226
227     // Set up the coloured state
228     nontextured->enable( GL_LIGHTING );
229     nontextured->setShadeModel( shade_model );
230     nontextured->enable ( GL_CULL_FACE      ) ;
231     nontextured->disable( GL_TEXTURE_2D );
232     nontextured->disable( GL_BLEND );
233     nontextured->disable( GL_ALPHA_TEST );
234     nontextured->disable( GL_COLOR_MATERIAL );
235
236     nontextured->setMaterial ( GL_AMBIENT, 
237                                ambient[0], ambient[1], 
238                                ambient[2], ambient[3] ) ;
239     nontextured->setMaterial ( GL_DIFFUSE, 
240                                diffuse[0], diffuse[1], 
241                                diffuse[2], diffuse[3] ) ;
242     nontextured->setMaterial ( GL_SPECULAR, 
243                                specular[0], specular[1], 
244                                specular[2], specular[3] ) ;
245     nontextured->setMaterial ( GL_EMISSION, 
246                                emission[0], emission[1], 
247                                emission[2], emission[3] ) ;
248
249     state->setStep( 0, textured );    // textured
250     state->setStep( 1, nontextured ); // untextured
251
252     // Choose the appropriate starting state.
253     if ( texture_default ) {
254         state->selectStep(0);
255     } else {
256         state->selectStep(1);
257     }
258 }
259
260
261 void FGNewMat::set_ssg_state( ssgSimpleState *s )
262 {
263     state = new ssgStateSelector(2);
264     state->ref();
265
266     textured = s;
267
268     nontextured = new ssgSimpleState();
269     nontextured->ref();
270
271     // Set up the coloured state
272     nontextured->enable( GL_LIGHTING );
273     nontextured->setShadeModel( GL_FLAT );
274     nontextured->enable ( GL_CULL_FACE      ) ;
275     nontextured->disable( GL_TEXTURE_2D );
276     nontextured->disable( GL_BLEND );
277     nontextured->disable( GL_ALPHA_TEST );
278     nontextured->disable( GL_COLOR_MATERIAL );
279
280     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
281        << "," << ambient[2] << endl; */
282     nontextured->setMaterial ( GL_AMBIENT, 
283                                ambient[0], ambient[1], 
284                                ambient[2], ambient[3] ) ;
285     nontextured->setMaterial ( GL_DIFFUSE, 
286                                diffuse[0], diffuse[1], 
287                                diffuse[2], diffuse[3] ) ;
288     nontextured->setMaterial ( GL_SPECULAR, 
289                                specular[0], specular[1], 
290                                specular[2], specular[3] ) ;
291     nontextured->setMaterial ( GL_EMISSION, 
292                                emission[0], emission[1], 
293                                emission[2], emission[3] ) ;
294
295     state->setStep( 0, textured );    // textured
296     state->setStep( 1, nontextured ); // untextured
297
298     // Choose the appropriate starting state.
299     state->selectStep(0);
300 }
301
302 // end of newmat.cxx