1 // newmat.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
28 #include <simgear/compiler.h>
30 #ifdef SG_MATH_EXCEPTION_CLASH
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/sg_path.hxx>
36 #include <simgear/misc/sgstream.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
45 ////////////////////////////////////////////////////////////////////////
46 // Local static functions.
47 ////////////////////////////////////////////////////////////////////////
50 * Internal method to test whether a file exists.
52 * TODO: this should be moved to a SimGear library of local file
56 local_file_exists( const string& path ) {
57 sg_gzifstream in( path );
58 if ( ! in.is_open() ) {
67 ////////////////////////////////////////////////////////////////////////
68 // Constructors and destructor.
69 ////////////////////////////////////////////////////////////////////////
72 FGNewMat::FGNewMat (const SGPropertyNode * props)
75 read_properties(props);
76 build_ssg_state(false);
79 FGNewMat::FGNewMat (const string &texpath)
82 texture_path = texpath;
83 build_ssg_state(true);
86 FGNewMat::FGNewMat (ssgSimpleState * s)
92 FGNewMat::~FGNewMat (void)
98 ////////////////////////////////////////////////////////////////////////
100 ////////////////////////////////////////////////////////////////////////
103 FGNewMat::read_properties (const SGPropertyNode * props)
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");
110 if (!local_file_exists(tpath.str())) {
111 tpath = SGPath(globals->get_fg_root());
112 tpath.append("Textures");
115 texture_path = tpath.str();
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");
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);
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);
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);
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);
147 ////////////////////////////////////////////////////////////////////////
149 ////////////////////////////////////////////////////////////////////////
163 texture_loaded = false;
165 for (int i = 0; i < 4; i++)
166 ambient[i] = diffuse[i] = specular[i] = emission[i] = 0.0;
170 FGNewMat::load_texture ()
172 if (texture_loaded) {
175 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture " << texture_path );
176 textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv, mipmap );
177 texture_loaded = true;
184 FGNewMat::build_ssg_state (bool defer_tex_load)
187 (fgGetBool("/sim/rendering/shading") ? GL_SMOOTH : GL_FLAT);
188 bool texture_default = fgGetBool("/sim/rendering/textures");
190 state = new ssgStateSelector(2);
193 textured = new ssgSimpleState();
196 nontextured = new ssgSimpleState();
199 // Set up the textured state
200 textured->setShadeModel( shade_model );
201 textured->enable( GL_LIGHTING );
202 textured->enable ( GL_CULL_FACE ) ;
203 textured->enable( GL_TEXTURE_2D );
204 textured->disable( GL_BLEND );
205 textured->disable( GL_ALPHA_TEST );
207 # ifdef GL_EXT_texture_filter_anisotropic
208 float max_anisotropy;
209 glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy );
210 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
212 cout << "Max anisotropy = " << max_anisotropy << endl;
215 if ( !defer_tex_load ) {
216 textured->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
217 texture_loaded = true;
219 texture_loaded = false;
221 textured->enable( GL_COLOR_MATERIAL );
222 textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
223 textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
224 textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
226 // Set up the coloured state
227 nontextured->enable( GL_LIGHTING );
228 nontextured->setShadeModel( shade_model );
229 nontextured->enable ( GL_CULL_FACE ) ;
230 nontextured->disable( GL_TEXTURE_2D );
231 nontextured->disable( GL_BLEND );
232 nontextured->disable( GL_ALPHA_TEST );
233 nontextured->disable( GL_COLOR_MATERIAL );
235 nontextured->setMaterial ( GL_AMBIENT,
236 ambient[0], ambient[1],
237 ambient[2], ambient[3] ) ;
238 nontextured->setMaterial ( GL_DIFFUSE,
239 diffuse[0], diffuse[1],
240 diffuse[2], diffuse[3] ) ;
241 nontextured->setMaterial ( GL_SPECULAR,
242 specular[0], specular[1],
243 specular[2], specular[3] ) ;
244 nontextured->setMaterial ( GL_EMISSION,
245 emission[0], emission[1],
246 emission[2], emission[3] ) ;
248 state->setStep( 0, textured ); // textured
249 state->setStep( 1, nontextured ); // untextured
251 // Choose the appropriate starting state.
252 if ( texture_default ) {
253 state->selectStep(0);
255 state->selectStep(1);
260 void FGNewMat::set_ssg_state( ssgSimpleState *s )
262 state = new ssgStateSelector(2);
267 nontextured = new ssgSimpleState();
270 // Set up the coloured state
271 nontextured->enable( GL_LIGHTING );
272 nontextured->setShadeModel( GL_FLAT );
273 nontextured->enable ( GL_CULL_FACE ) ;
274 nontextured->disable( GL_TEXTURE_2D );
275 nontextured->disable( GL_BLEND );
276 nontextured->disable( GL_ALPHA_TEST );
277 nontextured->disable( GL_COLOR_MATERIAL );
279 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
280 << "," << ambient[2] << endl; */
281 nontextured->setMaterial ( GL_AMBIENT,
282 ambient[0], ambient[1],
283 ambient[2], ambient[3] ) ;
284 nontextured->setMaterial ( GL_DIFFUSE,
285 diffuse[0], diffuse[1],
286 diffuse[2], diffuse[3] ) ;
287 nontextured->setMaterial ( GL_SPECULAR,
288 specular[0], specular[1],
289 specular[2], specular[3] ) ;
290 nontextured->setMaterial ( GL_EMISSION,
291 emission[0], emission[1],
292 emission[2], emission[3] ) ;
294 state->setStep( 0, textured ); // textured
295 state->setStep( 1, nontextured ); // untextured
297 // Choose the appropriate starting state.
298 state->selectStep(0);