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>
33 #include <simgear/compiler.h>
35 #ifdef SG_MATH_EXCEPTION_CLASH
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/math/sg_random.h>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
47 ////////////////////////////////////////////////////////////////////////
48 // Local static functions.
49 ////////////////////////////////////////////////////////////////////////
52 * Internal method to test whether a file exists.
54 * TODO: this should be moved to a SimGear library of local file
58 local_file_exists( const string& path ) {
59 sg_gzifstream in( path );
60 if ( ! in.is_open() ) {
69 ////////////////////////////////////////////////////////////////////////
70 // Constructors and destructor.
71 ////////////////////////////////////////////////////////////////////////
74 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props )
77 read_properties( fg_root, props );
78 build_ssg_state( false );
81 SGMaterial::SGMaterial( const string &texpath )
84 texture_path = texpath;
85 build_ssg_state( true );
88 SGMaterial::SGMaterial( ssgSimpleState *s )
94 SGMaterial::~SGMaterial (void)
96 for (unsigned int i = 0; i < object_groups.size(); i++) {
97 delete object_groups[i];
104 ////////////////////////////////////////////////////////////////////////
106 ////////////////////////////////////////////////////////////////////////
109 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props )
111 // Get the path to the texture
112 string tname = props->getStringValue("texture", "unknown.rgb");
113 SGPath tpath( fg_root );
114 tpath.append("Textures.high");
116 if (!local_file_exists(tpath.str())) {
117 tpath = SGPath( fg_root );
118 tpath.append("Textures");
121 texture_path = tpath.str();
123 xsize = props->getDoubleValue("xsize", 0.0);
124 ysize = props->getDoubleValue("ysize", 0.0);
125 wrapu = props->getBoolValue("wrapu", true);
126 wrapv = props->getBoolValue("wrapv", true);
127 mipmap = props->getBoolValue("mipmap", true);
128 light_coverage = props->getDoubleValue("light-coverage", 0.0);
130 // Taken from default values as used in ac3d
131 ambient[0] = props->getDoubleValue("ambient/r", 0.2);
132 ambient[1] = props->getDoubleValue("ambient/g", 0.2);
133 ambient[2] = props->getDoubleValue("ambient/b", 0.2);
134 ambient[3] = props->getDoubleValue("ambient/a", 1.0);
136 diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
137 diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
138 diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
139 diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
141 specular[0] = props->getDoubleValue("specular/r", 0.0);
142 specular[1] = props->getDoubleValue("specular/g", 0.0);
143 specular[2] = props->getDoubleValue("specular/b", 0.0);
144 specular[3] = props->getDoubleValue("specular/a", 1.0);
146 emission[0] = props->getDoubleValue("emissive/r", 0.0);
147 emission[1] = props->getDoubleValue("emissive/g", 0.0);
148 emission[2] = props->getDoubleValue("emissive/b", 0.0);
149 emission[3] = props->getDoubleValue("emissive/a", 1.0);
151 shininess = props->getDoubleValue("shininess", 1.0);
153 vector<SGPropertyNode_ptr> object_group_nodes =
154 ((SGPropertyNode *)props)->getChildren("object-group");
155 for (unsigned int i = 0; i < object_group_nodes.size(); i++)
156 object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
161 ////////////////////////////////////////////////////////////////////////
163 ////////////////////////////////////////////////////////////////////////
175 light_coverage = 0.0;
176 texture_loaded = false;
179 for (int i = 0; i < 4; i++) {
180 ambient[i] = (i < 3) ? 0.2 : 1.0;
181 specular[i] = (i < 3) ? 0.0 : 1.0;
182 diffuse[i] = (i < 3) ? 0.8 : 1.0;
183 emission[i] = (i < 3) ? 0.0 : 1.0;
188 SGMaterial::load_texture ()
190 if ( texture_loaded ) {
193 SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
195 state->setTexture( (char *)texture_path.c_str(), wrapu, wrapv, mipmap );
196 texture_loaded = true;
203 SGMaterial::build_ssg_state( bool defer_tex_load )
205 GLenum shade_model = GL_SMOOTH;
207 state = new ssgSimpleState();
210 // Set up the textured state
211 state->setShadeModel( shade_model );
212 state->enable( GL_LIGHTING );
213 state->enable ( GL_CULL_FACE ) ;
214 state->enable( GL_TEXTURE_2D );
215 state->disable( GL_BLEND );
216 state->disable( GL_ALPHA_TEST );
217 if ( !defer_tex_load ) {
218 SG_LOG(SG_INPUT, SG_INFO, " " << texture_path );
219 state->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
220 texture_loaded = true;
222 texture_loaded = false;
224 state->enable( GL_COLOR_MATERIAL );
225 state->setMaterial ( GL_AMBIENT,
226 ambient[0], ambient[1],
227 ambient[2], ambient[3] ) ;
228 state->setMaterial ( GL_DIFFUSE,
229 diffuse[0], diffuse[1],
230 diffuse[2], diffuse[3] ) ;
231 state->setMaterial ( GL_SPECULAR,
232 specular[0], specular[1],
233 specular[2], specular[3] ) ;
234 state->setMaterial ( GL_EMISSION,
235 emission[0], emission[1],
236 emission[2], emission[3] ) ;
237 state->setShininess ( shininess );
241 void SGMaterial::set_ssg_state( ssgSimpleState *s )
245 texture_loaded = true;