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>
42 FGNewMat::FGNewMat ( void ) {
45 light_coverage = -1.0;
50 FGNewMat::FGNewMat ( const string &name )
52 FGNewMat( name, name );
57 FGNewMat::FGNewMat ( const string &mat_name, const string &tex_name )
59 material_name = mat_name;
60 texture_name = tex_name;
65 ambient[0] = ambient[1] = ambient[2] = ambient[3] = 1.0;
66 diffuse[0] = diffuse[1] = diffuse[2] = diffuse[3] = 1.0;
67 specular[0] = specular[1] = specular[2] = specular[3] = 1.0;
68 emission[0] = emission[1] = emission[2] = emission[3] = 1.0;
69 light_coverage = -1.0;
73 void FGNewMat::build_ssg_state( GLenum shade_model, bool texture_default,
76 state = new ssgStateSelector(2);
79 textured = new ssgSimpleState();
82 nontextured = new ssgSimpleState();
85 // Set up the textured state
86 textured->setShadeModel( shade_model );
87 textured->enable( GL_LIGHTING );
88 textured->enable ( GL_CULL_FACE ) ;
89 textured->enable( GL_TEXTURE_2D );
90 textured->disable( GL_BLEND );
91 textured->disable( GL_ALPHA_TEST );
92 if ( !defer_tex_load ) {
93 textured->setTexture( (char *)texture_name.c_str(), wrapu, wrapv );
94 texture_loaded = true;
96 texture_loaded = false;
98 // cout << "wrap u = " << wrapu << " wrapv = " << wrapv << endl;
99 textured->enable( GL_COLOR_MATERIAL );
100 textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
101 textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
102 textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
104 // Set up the coloured state
105 nontextured->enable( GL_LIGHTING );
106 nontextured->setShadeModel( shade_model );
107 nontextured->enable ( GL_CULL_FACE ) ;
108 nontextured->disable( GL_TEXTURE_2D );
109 nontextured->disable( GL_BLEND );
110 nontextured->disable( GL_ALPHA_TEST );
111 nontextured->disable( GL_COLOR_MATERIAL );
113 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
114 << "," << ambient[2] << endl; */
115 nontextured->setMaterial ( GL_AMBIENT,
116 ambient[0], ambient[1],
117 ambient[2], ambient[3] ) ;
118 nontextured->setMaterial ( GL_DIFFUSE,
119 diffuse[0], diffuse[1],
120 diffuse[2], diffuse[3] ) ;
121 nontextured->setMaterial ( GL_SPECULAR,
122 specular[0], specular[1],
123 specular[2], specular[3] ) ;
124 nontextured->setMaterial ( GL_EMISSION,
125 emission[0], emission[1],
126 emission[2], emission[3] ) ;
128 state->setStep( 0, textured ); // textured
129 state->setStep( 1, nontextured ); // untextured
131 // Choose the appropriate starting state.
132 if ( texture_default ) {
133 state->selectStep(0);
135 state->selectStep(1);
140 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
141 state = new ssgStateSelector(2);
146 nontextured = new ssgSimpleState();
149 // Set up the coloured state
150 nontextured->enable( GL_LIGHTING );
151 nontextured->setShadeModel( GL_FLAT );
152 nontextured->enable ( GL_CULL_FACE ) ;
153 nontextured->disable( GL_TEXTURE_2D );
154 nontextured->disable( GL_BLEND );
155 nontextured->disable( GL_ALPHA_TEST );
156 nontextured->disable( GL_COLOR_MATERIAL );
158 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
159 << "," << ambient[2] << endl; */
160 nontextured->setMaterial ( GL_AMBIENT,
161 ambient[0], ambient[1],
162 ambient[2], ambient[3] ) ;
163 nontextured->setMaterial ( GL_DIFFUSE,
164 diffuse[0], diffuse[1],
165 diffuse[2], diffuse[3] ) ;
166 nontextured->setMaterial ( GL_SPECULAR,
167 specular[0], specular[1],
168 specular[2], specular[3] ) ;
169 nontextured->setMaterial ( GL_EMISSION,
170 emission[0], emission[1],
171 emission[2], emission[3] ) ;
173 state->setStep( 0, textured ); // textured
174 state->setStep( 1, nontextured ); // untextured
176 // Choose the appropriate starting state.
177 state->selectStep(0);
181 void FGNewMat::dump_info () {
182 SG_LOG( SG_TERRAIN, SG_INFO, "{" << endl << " texture = "
184 SG_LOG( SG_TERRAIN, SG_INFO, " xsize = " << xsize );
185 SG_LOG( SG_TERRAIN, SG_INFO, " ysize = " << ysize );
186 SG_LOG( SG_TERRAIN, SG_INFO, " ambient = " << ambient[0] << " "
187 << ambient[1] <<" "<< ambient[2] <<" "<< ambient[3] );
188 SG_LOG( SG_TERRAIN, SG_INFO, " diffuse = " << diffuse[0] << " "
189 << diffuse[1] << " " << diffuse[2] << " " << diffuse[3] );
190 SG_LOG( SG_TERRAIN, SG_INFO, " specular = " << specular[0] << " "
191 << specular[1] << " " << specular[2] << " " << specular[3]);
192 SG_LOG( SG_TERRAIN, SG_INFO, " emission = " << emission[0] << " "
193 << emission[1] << " " << emission[2] << " " << emission[3]);
194 SG_LOG( SG_TERRAIN, SG_INFO, " alpha = " << alpha << endl <<"}" );
200 FGNewMat::~FGNewMat ( void ) {
205 operator >> ( istream& in, FGNewMat& m )
211 if ( token == "texture" ) {
212 in >> token >> m.texture_name;
213 } else if ( token == "xsize" ) {
214 in >> token >> m.xsize;
215 } else if ( token == "ysize" ) {
216 in >> token >> m.ysize;
217 } else if ( token == "wrapu" ) {
218 in >> token >> m.wrapu;
219 } else if ( token == "wrapv" ) {
220 in >> token >> m.wrapv;
221 } else if ( token == "mipmap" ) {
222 in >> token >> m.mipmap;
223 } else if ( token == "ambient" ) {
224 in >> token >> m.ambient[0] >> m.ambient[1]
225 >> m.ambient[2] >> m.ambient[3];
226 } else if ( token == "diffuse" ) {
227 in >> token >> m.diffuse[0] >> m.diffuse[1]
228 >> m.diffuse[2] >> m.diffuse[3];
229 } else if ( token == "specular" ) {
230 in >> token >> m.specular[0] >> m.specular[1]
231 >> m.specular[2] >> m.specular[3];
232 } else if ( token == "emission" ) {
233 in >> token >> m.emission[0] >> m.emission[1]
234 >> m.emission[2] >> m.emission[3];
235 } else if ( token == "alpha" ) {
236 in >> token >> token;
237 if ( token == "yes" ) {
239 } else if ( token == "no" ) {
242 SG_LOG( SG_TERRAIN, SG_INFO, "Bad alpha value " << token );
244 } else if ( token == "light-coverage" ) {
245 in >> token >> m.light_coverage;
246 } else if ( token[0] == '}' ) {