1 // mat.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.
25 # include <simgear_config.h>
28 #include <simgear/compiler.h>
35 #ifdef SG_MATH_EXCEPTION_CLASH
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/misc/sg_path.hxx>
41 #include <simgear/misc/sgstream.hxx>
46 ////////////////////////////////////////////////////////////////////////
47 // Constructors and destructor.
48 ////////////////////////////////////////////////////////////////////////
51 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props )
54 read_properties( fg_root, props );
55 build_ssg_state( false );
58 SGMaterial::SGMaterial( const string &texpath )
62 _internal_state st = { NULL, texpath, false };
63 _status.push_back( st );
65 build_ssg_state( true );
68 SGMaterial::SGMaterial( ssgSimpleState *s )
74 SGMaterial::~SGMaterial (void)
76 for (unsigned int i = 0; i < object_groups.size(); i++) {
77 delete object_groups[i];
84 ////////////////////////////////////////////////////////////////////////
86 ////////////////////////////////////////////////////////////////////////
89 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props )
91 // Gather the path(s) to the texture(s)
92 vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
93 for (int i = 0; i < textures.size(); i++)
95 string tname = textures[i]->getStringValue();
97 tname = "unknown.rgb";
99 SGPath tpath( fg_root );
100 tpath.append("Textures.high");
102 if (!ulFileExists(tpath.c_str())) {
103 tpath = SGPath( fg_root );
104 tpath.append("Textures");
107 _internal_state st = { NULL, tpath.str(), false };
108 _status.push_back( st );
111 if (textures.size() == 0) {
112 string tname = "unknown.rgb";
113 SGPath tpath( fg_root );
114 tpath.append("Textures");
116 _internal_state st = { NULL, tpath.str(), true };
117 _status.push_back( st );
120 xsize = props->getDoubleValue("xsize", 0.0);
121 ysize = props->getDoubleValue("ysize", 0.0);
122 wrapu = props->getBoolValue("wrapu", true);
123 wrapv = props->getBoolValue("wrapv", true);
124 mipmap = props->getBoolValue("mipmap", true);
125 light_coverage = props->getDoubleValue("light-coverage", 0.0);
127 // Taken from default values as used in ac3d
128 ambient[0] = props->getDoubleValue("ambient/r", 0.2);
129 ambient[1] = props->getDoubleValue("ambient/g", 0.2);
130 ambient[2] = props->getDoubleValue("ambient/b", 0.2);
131 ambient[3] = props->getDoubleValue("ambient/a", 1.0);
133 diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
134 diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
135 diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
136 diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
138 specular[0] = props->getDoubleValue("specular/r", 0.0);
139 specular[1] = props->getDoubleValue("specular/g", 0.0);
140 specular[2] = props->getDoubleValue("specular/b", 0.0);
141 specular[3] = props->getDoubleValue("specular/a", 1.0);
143 emission[0] = props->getDoubleValue("emissive/r", 0.0);
144 emission[1] = props->getDoubleValue("emissive/g", 0.0);
145 emission[2] = props->getDoubleValue("emissive/b", 0.0);
146 emission[3] = props->getDoubleValue("emissive/a", 1.0);
148 shininess = props->getDoubleValue("shininess", 1.0);
150 vector<SGPropertyNode_ptr> object_group_nodes =
151 ((SGPropertyNode *)props)->getChildren("object-group");
152 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
153 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
158 ////////////////////////////////////////////////////////////////////////
160 ////////////////////////////////////////////////////////////////////////
171 light_coverage = 0.0;
174 for (int i = 0; i < 4; i++) {
175 ambient[i] = (i < 3) ? 0.2 : 1.0;
176 specular[i] = (i < 3) ? 0.0 : 1.0;
177 diffuse[i] = (i < 3) ? 0.8 : 1.0;
178 emission[i] = (i < 3) ? 0.0 : 1.0;
183 SGMaterial::load_texture ( int n )
185 int i = (n >= 0) ? n : 0 ;
186 int end = (n >= 0) ? n+1 : _status.size();
190 if ( !_status[i].texture_loaded ) {
191 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
192 << _status[i].texture_path );
193 _status[i].state->setTexture(
194 (char *)_status[i].texture_path.c_str(),
195 wrapu, wrapv, mipmap );
196 _status[i].texture_loaded = true;
203 SGMaterial::get_state (int n) const
205 static unsigned current = 0;
207 if (_status.size() == 0) {
208 SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
212 if ( ++current >= _status.size())
215 return (n >= 0) ? _status[n].state : _status[current].state;
220 SGMaterial::build_ssg_state( bool defer_tex_load )
222 GLenum shade_model = GL_SMOOTH;
224 for (int i = 0; i < _status.size(); i++)
226 ssgSimpleState *state = new ssgSimpleState();
229 // Set up the textured state
230 state->setShadeModel( shade_model );
231 state->enable( GL_LIGHTING );
232 state->enable ( GL_CULL_FACE ) ;
233 state->enable( GL_TEXTURE_2D );
234 state->disable( GL_BLEND );
235 state->disable( GL_ALPHA_TEST );
237 if ( !defer_tex_load ) {
238 SG_LOG(SG_INPUT, SG_INFO, " " << _status[i].texture_path );
239 state->setTexture( (char *)_status[i].texture_path.c_str(),
241 _status[i].texture_loaded = true;
243 _status[i].texture_loaded = false;
246 state->enable( GL_COLOR_MATERIAL );
247 state->setMaterial ( GL_AMBIENT,
248 ambient[0], ambient[1],
249 ambient[2], ambient[3] ) ;
250 state->setMaterial ( GL_DIFFUSE,
251 diffuse[0], diffuse[1],
252 diffuse[2], diffuse[3] ) ;
253 state->setMaterial ( GL_SPECULAR,
254 specular[0], specular[1],
255 specular[2], specular[3] ) ;
256 state->setMaterial ( GL_EMISSION,
257 emission[0], emission[1],
258 emission[2], emission[3] ) ;
259 state->setShininess ( shininess );
261 _status[i].state = state;
266 void SGMaterial::set_ssg_state( ssgSimpleState *s )
269 _internal_state st = { s, "", true };
270 _status.push_back( st );