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 FG_MATH_EXCEPTION_CLASH
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/fgpath.hxx>
36 #include <simgear/misc/fgstream.hxx>
42 FGNewMat::FGNewMat ( void ) {
49 FGNewMat::FGNewMat ( const string &name )
51 FGNewMat( name, name );
56 FGNewMat::FGNewMat ( const string &mat_name, const string &tex_name )
58 material_name = mat_name;
59 texture_name = tex_name;
64 ambient[0] = ambient[1] = ambient[2] = ambient[3] = 1.0;
65 diffuse[0] = diffuse[1] = diffuse[2] = diffuse[3] = 1.0;
66 specular[0] = specular[1] = specular[2] = specular[3] = 1.0;
67 emission[0] = emission[1] = emission[2] = emission[3] = 1.0;
71 void FGNewMat::build_ssg_state( const string& path,
72 GLenum shade_model, bool texture_default )
74 FGPath tex_file( path );
75 tex_file.append( texture_name );
77 state = new ssgStateSelector(2);
80 textured = new ssgSimpleState();
83 nontextured = new ssgSimpleState();
86 // Set up the textured state
87 textured->setShadeModel( shade_model );
88 textured->enable ( GL_CULL_FACE ) ;
89 textured->enable( GL_TEXTURE_2D );
90 textured->disable( GL_BLEND );
91 textured->disable( GL_ALPHA_TEST );
92 textured->setTexture( (char *)tex_file.c_str(), wrapu, wrapv );
93 // cout << "wrap u = " << wrapu << " wrapv = " << wrapv << endl;
94 textured->enable( GL_COLOR_MATERIAL );
95 textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
96 textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
97 textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
99 // Set up the coloured state
100 nontextured->enable( GL_LIGHTING );
101 nontextured->setShadeModel( shade_model );
102 nontextured->enable ( GL_CULL_FACE ) ;
103 nontextured->disable( GL_TEXTURE_2D );
104 nontextured->disable( GL_BLEND );
105 nontextured->disable( GL_ALPHA_TEST );
106 nontextured->disable( GL_COLOR_MATERIAL );
108 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
109 << "," << ambient[2] << endl; */
110 nontextured->setMaterial ( GL_AMBIENT,
111 ambient[0], ambient[1],
112 ambient[2], ambient[3] ) ;
113 nontextured->setMaterial ( GL_DIFFUSE,
114 diffuse[0], diffuse[1],
115 diffuse[2], diffuse[3] ) ;
116 nontextured->setMaterial ( GL_SPECULAR,
117 specular[0], specular[1],
118 specular[2], specular[3] ) ;
119 nontextured->setMaterial ( GL_EMISSION,
120 emission[0], emission[1],
121 emission[2], emission[3] ) ;
123 state->setStep( 0, textured ); // textured
124 state->setStep( 1, nontextured ); // untextured
126 // Choose the appropriate starting state.
127 if ( texture_default ) {
128 state->selectStep(0);
130 state->selectStep(1);
135 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
136 state = new ssgStateSelector(2);
141 nontextured = new ssgSimpleState();
144 // Set up the coloured state
145 nontextured->enable( GL_LIGHTING );
146 nontextured->setShadeModel( GL_FLAT );
147 nontextured->enable ( GL_CULL_FACE ) ;
148 nontextured->disable( GL_TEXTURE_2D );
149 nontextured->disable( GL_BLEND );
150 nontextured->disable( GL_ALPHA_TEST );
151 nontextured->disable( GL_COLOR_MATERIAL );
153 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
154 << "," << ambient[2] << endl; */
155 nontextured->setMaterial ( GL_AMBIENT,
156 ambient[0], ambient[1],
157 ambient[2], ambient[3] ) ;
158 nontextured->setMaterial ( GL_DIFFUSE,
159 diffuse[0], diffuse[1],
160 diffuse[2], diffuse[3] ) ;
161 nontextured->setMaterial ( GL_SPECULAR,
162 specular[0], specular[1],
163 specular[2], specular[3] ) ;
164 nontextured->setMaterial ( GL_EMISSION,
165 emission[0], emission[1],
166 emission[2], emission[3] ) ;
168 state->setStep( 0, textured ); // textured
169 state->setStep( 1, nontextured ); // untextured
171 // Choose the appropriate starting state.
172 state->selectStep(0);
176 void FGNewMat::dump_info () {
177 FG_LOG( FG_TERRAIN, FG_INFO, "{" << endl << " texture = "
179 FG_LOG( FG_TERRAIN, FG_INFO, " xsize = " << xsize );
180 FG_LOG( FG_TERRAIN, FG_INFO, " ysize = " << ysize );
181 FG_LOG( FG_TERRAIN, FG_INFO, " ambient = " << ambient[0] << " "
182 << ambient[1] <<" "<< ambient[2] <<" "<< ambient[3] );
183 FG_LOG( FG_TERRAIN, FG_INFO, " diffuse = " << diffuse[0] << " "
184 << diffuse[1] << " " << diffuse[2] << " " << diffuse[3] );
185 FG_LOG( FG_TERRAIN, FG_INFO, " specular = " << specular[0] << " "
186 << specular[1] << " " << specular[2] << " " << specular[3]);
187 FG_LOG( FG_TERRAIN, FG_INFO, " emission = " << emission[0] << " "
188 << emission[1] << " " << emission[2] << " " << emission[3]);
189 FG_LOG( FG_TERRAIN, FG_INFO, " alpha = " << alpha << endl <<"}" );
195 FGNewMat::~FGNewMat ( void ) {
200 operator >> ( istream& in, FGNewMat& m )
206 if ( token == "texture" ) {
207 in >> token >> m.texture_name;
208 } else if ( token == "xsize" ) {
209 in >> token >> m.xsize;
210 } else if ( token == "ysize" ) {
211 in >> token >> m.ysize;
212 } else if ( token == "wrapu" ) {
213 in >> token >> m.wrapu;
214 } else if ( token == "wrapv" ) {
215 in >> token >> m.wrapv;
216 } else if ( token == "mipmap" ) {
217 in >> token >> m.mipmap;
218 } else if ( token == "ambient" ) {
219 in >> token >> m.ambient[0] >> m.ambient[1]
220 >> m.ambient[2] >> m.ambient[3];
221 } else if ( token == "diffuse" ) {
222 in >> token >> m.diffuse[0] >> m.diffuse[1]
223 >> m.diffuse[2] >> m.diffuse[3];
224 } else if ( token == "specular" ) {
225 in >> token >> m.specular[0] >> m.specular[1]
226 >> m.specular[2] >> m.specular[3];
227 } else if ( token == "emission" ) {
228 in >> token >> m.emission[0] >> m.emission[1]
229 >> m.emission[2] >> m.emission[3];
230 } else if ( token == "alpha" ) {
231 in >> token >> token;
232 if ( token == "yes" ) {
234 } else if ( token == "no" ) {
237 FG_LOG( FG_TERRAIN, FG_INFO, "Bad alpha value " << token );
239 } else if ( token[0] == '}' ) {