1 // mat.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
36 #ifdef SG_MATH_EXCEPTION_CLASH
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
47 ////////////////////////////////////////////////////////////////////////
48 // Constructors and destructor.
49 ////////////////////////////////////////////////////////////////////////
52 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season )
55 read_properties( fg_root, props, season );
56 build_ssg_state( false );
59 SGMaterial::SGMaterial( const string &texpath )
63 _internal_state st( NULL, texpath, false );
64 _status.push_back( st );
66 build_ssg_state( true );
69 SGMaterial::SGMaterial( ssgSimpleState *s )
75 SGMaterial::~SGMaterial (void)
81 ////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////
86 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season )
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++)
92 string tname = textures[i]->getStringValue();
93 string otname = tname;
94 if (season && strncmp(season, "summer", 6))
96 if (tname.substr(0,7) == "Terrain")
97 tname.insert(7,"."+string(season));
101 tname = "unknown.rgb";
104 SGPath tpath( fg_root );
105 tpath.append("Textures.high");
107 if ( !ulFileExists(tpath.c_str()) ) {
108 tpath = SGPath( fg_root );
109 tpath.append("Textures");
113 if ( ulFileExists(tpath.c_str()) ) {
114 _internal_state st( NULL, tpath.str(), false );
115 _status.push_back( st );
119 if (textures.size() == 0) {
120 string tname = "unknown.rgb";
121 SGPath tpath( fg_root );
122 tpath.append("Textures");
124 _internal_state st( NULL, tpath.str(), true );
125 _status.push_back( st );
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);
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);
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);
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);
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);
156 shininess = props->getDoubleValue("shininess", 1.0);
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]));
166 ////////////////////////////////////////////////////////////////////////
168 ////////////////////////////////////////////////////////////////////////
180 light_coverage = 0.0;
182 for (int i = 0; i < 4; i++) {
183 ambient[i] = (i < 3) ? 0.2 : 1.0;
184 specular[i] = (i < 3) ? 0.0 : 1.0;
185 diffuse[i] = (i < 3) ? 0.8 : 1.0;
186 emission[i] = (i < 3) ? 0.0 : 1.0;
191 SGMaterial::load_texture ( int n )
193 int i = (n >= 0) ? n : 0 ;
194 int end = (n >= 0) ? n+1 : _status.size();
198 if ( !_status[i].texture_loaded ) {
199 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
200 << _status[i].texture_path );
201 _status[i].state->setTexture(
202 (char *)_status[i].texture_path.c_str(),
203 wrapu, wrapv, mipmap );
204 _status[i].texture_loaded = true;
211 SGMaterial::get_state (int n) const
213 if (_status.size() == 0) {
214 SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
218 ssgSimpleState *st = (n >= 0) ? _status[n].state
219 : _status[_current_ptr].state;
220 ((SGMaterial *)this)->_current_ptr += 1;
221 if (_current_ptr >= _status.size())
222 ((SGMaterial *)this)->_current_ptr = 0;
229 SGMaterial::build_ssg_state( bool defer_tex_load )
231 GLenum shade_model = GL_SMOOTH;
233 for (unsigned int i = 0; i < _status.size(); i++)
235 ssgSimpleState *state = new ssgSimpleState();
237 // Set up the textured state
238 state->setShadeModel( shade_model );
239 state->enable( GL_LIGHTING );
240 state->enable ( GL_CULL_FACE ) ;
241 state->enable( GL_TEXTURE_2D );
242 state->disable( GL_BLEND );
243 state->disable( GL_ALPHA_TEST );
245 if ( !defer_tex_load ) {
246 SG_LOG(SG_INPUT, SG_INFO, " " << _status[i].texture_path );
247 state->setTexture( (char *)_status[i].texture_path.c_str(),
249 _status[i].texture_loaded = true;
251 _status[i].texture_loaded = false;
254 state->enable( GL_COLOR_MATERIAL );
255 state->setMaterial ( GL_AMBIENT,
256 ambient[0], ambient[1],
257 ambient[2], ambient[3] ) ;
258 state->setMaterial ( GL_DIFFUSE,
259 diffuse[0], diffuse[1],
260 diffuse[2], diffuse[3] ) ;
261 state->setMaterial ( GL_SPECULAR,
262 specular[0], specular[1],
263 specular[2], specular[3] ) ;
264 state->setMaterial ( GL_EMISSION,
265 emission[0], emission[1],
266 emission[2], emission[3] ) ;
267 state->setShininess ( shininess );
269 _status[i].state = state;
274 void SGMaterial::set_ssg_state( ssgSimpleState *s )
276 _status.push_back( _internal_state( s, "", true ) );