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 local_file_exists( const string& path ) {
46 sg_gzifstream in( path );
47 if ( ! in.is_open() ) {
68 texture_loaded = false;
70 for (int i = 0; i < 4; i++)
71 ambient[i] = diffuse[i] = specular[i] = emission[i] = 0.0;
74 FGNewMat::FGNewMat (const SGPropertyNode * props)
77 read_properties(props);
78 build_ssg_state(false);
81 FGNewMat::FGNewMat (const string &texture_path)
84 build_ssg_state(true);
87 FGNewMat::FGNewMat (ssgSimpleState * s)
95 FGNewMat::load_texture ()
100 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture " << texture_path );
102 textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv );
104 textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv, mipmap );
106 texture_loaded = true;
112 void FGNewMat::build_ssg_state( bool defer_tex_load )
115 (fgGetBool("/sim/rendering/shading") ? GL_SMOOTH : GL_FLAT);
116 bool texture_default = fgGetBool("/sim/rendering/textures");
118 state = new ssgStateSelector(2);
121 textured = new ssgSimpleState();
124 nontextured = new ssgSimpleState();
127 // Set up the textured state
128 textured->setShadeModel( shade_model );
129 textured->enable( GL_LIGHTING );
130 textured->enable ( GL_CULL_FACE ) ;
131 textured->enable( GL_TEXTURE_2D );
132 textured->disable( GL_BLEND );
133 textured->disable( GL_ALPHA_TEST );
134 if ( !defer_tex_load ) {
135 textured->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
136 texture_loaded = true;
138 texture_loaded = false;
140 textured->enable( GL_COLOR_MATERIAL );
141 textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
142 textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
143 textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
145 // Set up the coloured state
146 nontextured->enable( GL_LIGHTING );
147 nontextured->setShadeModel( shade_model );
148 nontextured->enable ( GL_CULL_FACE ) ;
149 nontextured->disable( GL_TEXTURE_2D );
150 nontextured->disable( GL_BLEND );
151 nontextured->disable( GL_ALPHA_TEST );
152 nontextured->disable( GL_COLOR_MATERIAL );
154 nontextured->setMaterial ( GL_AMBIENT,
155 ambient[0], ambient[1],
156 ambient[2], ambient[3] ) ;
157 nontextured->setMaterial ( GL_DIFFUSE,
158 diffuse[0], diffuse[1],
159 diffuse[2], diffuse[3] ) ;
160 nontextured->setMaterial ( GL_SPECULAR,
161 specular[0], specular[1],
162 specular[2], specular[3] ) ;
163 nontextured->setMaterial ( GL_EMISSION,
164 emission[0], emission[1],
165 emission[2], emission[3] ) ;
167 state->setStep( 0, textured ); // textured
168 state->setStep( 1, nontextured ); // untextured
170 // Choose the appropriate starting state.
171 if ( texture_default ) {
172 state->selectStep(0);
174 state->selectStep(1);
179 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
180 state = new ssgStateSelector(2);
185 nontextured = new ssgSimpleState();
188 // Set up the coloured state
189 nontextured->enable( GL_LIGHTING );
190 nontextured->setShadeModel( GL_FLAT );
191 nontextured->enable ( GL_CULL_FACE ) ;
192 nontextured->disable( GL_TEXTURE_2D );
193 nontextured->disable( GL_BLEND );
194 nontextured->disable( GL_ALPHA_TEST );
195 nontextured->disable( GL_COLOR_MATERIAL );
197 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
198 << "," << ambient[2] << endl; */
199 nontextured->setMaterial ( GL_AMBIENT,
200 ambient[0], ambient[1],
201 ambient[2], ambient[3] ) ;
202 nontextured->setMaterial ( GL_DIFFUSE,
203 diffuse[0], diffuse[1],
204 diffuse[2], diffuse[3] ) ;
205 nontextured->setMaterial ( GL_SPECULAR,
206 specular[0], specular[1],
207 specular[2], specular[3] ) ;
208 nontextured->setMaterial ( GL_EMISSION,
209 emission[0], emission[1],
210 emission[2], emission[3] ) ;
212 state->setStep( 0, textured ); // textured
213 state->setStep( 1, nontextured ); // untextured
215 // Choose the appropriate starting state.
216 state->selectStep(0);
221 FGNewMat::~FGNewMat ( void ) {
226 FGNewMat::read_properties (const SGPropertyNode * props)
228 // Get the path to the texture
229 string tname = props->getStringValue("texture", "unknown.rgb");
230 SGPath tpath(globals->get_fg_root());
231 tpath.append("Textures.high");
233 if (!local_file_exists(tpath.str())) {
234 tpath = SGPath(globals->get_fg_root());
235 tpath.append("Textures");
238 texture_path = tpath.str();
240 xsize = props->getDoubleValue("xsize", 0.0);
241 ysize = props->getDoubleValue("ysize", 0.0);
242 wrapu = props->getBoolValue("wrapu", true);
243 wrapv = props->getBoolValue("wrapv", true);
244 mipmap = props->getBoolValue("mipmap", true);
245 light_coverage = props->getDoubleValue("light-coverage");
247 ambient[0] = props->getDoubleValue("ambient/r", 0.0);
248 ambient[1] = props->getDoubleValue("ambient/g", 0.0);
249 ambient[2] = props->getDoubleValue("ambient/b", 0.0);
250 ambient[3] = props->getDoubleValue("ambient/a", 0.0);
252 diffuse[0] = props->getDoubleValue("diffuse/r", 0.0);
253 diffuse[1] = props->getDoubleValue("diffuse/g", 0.0);
254 diffuse[2] = props->getDoubleValue("diffuse/b", 0.0);
255 diffuse[3] = props->getDoubleValue("diffuse/a", 0.0);
257 specular[0] = props->getDoubleValue("specular/r", 0.0);
258 specular[1] = props->getDoubleValue("specular/g", 0.0);
259 specular[2] = props->getDoubleValue("specular/b", 0.0);
260 specular[3] = props->getDoubleValue("specular/a", 0.0);
262 emission[0] = props->getDoubleValue("emissive/r", 0.0);
263 emission[1] = props->getDoubleValue("emissive/g", 0.0);
264 emission[2] = props->getDoubleValue("emissive/b", 0.0);
265 emission[3] = props->getDoubleValue("emissive/a", 0.0);