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);
78 textured = new ssgSimpleState();
79 nontextured = new ssgSimpleState();
81 // Set up the textured state
82 textured->setShadeModel( shade_model );
83 textured->enable ( GL_CULL_FACE ) ;
84 textured->enable( GL_TEXTURE_2D );
85 textured->disable( GL_BLEND );
86 textured->disable( GL_ALPHA_TEST );
87 textured->setTexture( (char *)tex_file.c_str(), wrapu, wrapv );
88 // cout << "wrap u = " << wrapu << " wrapv = " << wrapv << endl;
89 textured->enable( GL_COLOR_MATERIAL );
90 textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
91 textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
92 textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
94 // Set up the coloured state
95 nontextured->enable( GL_LIGHTING );
96 nontextured->setShadeModel( shade_model );
97 nontextured->enable ( GL_CULL_FACE ) ;
98 nontextured->disable( GL_TEXTURE_2D );
99 nontextured->disable( GL_BLEND );
100 nontextured->disable( GL_ALPHA_TEST );
101 nontextured->disable( GL_COLOR_MATERIAL );
103 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
104 << "," << ambient[2] << endl; */
105 nontextured->setMaterial ( GL_AMBIENT,
106 ambient[0], ambient[1],
107 ambient[2], ambient[3] ) ;
108 nontextured->setMaterial ( GL_DIFFUSE,
109 diffuse[0], diffuse[1],
110 diffuse[2], diffuse[3] ) ;
111 nontextured->setMaterial ( GL_SPECULAR,
112 specular[0], specular[1],
113 specular[2], specular[3] ) ;
114 nontextured->setMaterial ( GL_EMISSION,
115 emission[0], emission[1],
116 emission[2], emission[3] ) ;
118 state->setStep( 0, textured ); // textured
119 state->setStep( 1, nontextured ); // untextured
121 // Choose the appropriate starting state.
122 if ( texture_default ) {
123 state->selectStep(0);
125 state->selectStep(1);
130 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
131 state = new ssgStateSelector(2);
133 nontextured = new ssgSimpleState();
135 // Set up the coloured state
136 nontextured->enable( GL_LIGHTING );
137 nontextured->setShadeModel( GL_FLAT );
138 nontextured->enable ( GL_CULL_FACE ) ;
139 nontextured->disable( GL_TEXTURE_2D );
140 nontextured->disable( GL_BLEND );
141 nontextured->disable( GL_ALPHA_TEST );
142 nontextured->disable( GL_COLOR_MATERIAL );
144 /* cout << "ambient = " << ambient[0] << "," << ambient[1]
145 << "," << ambient[2] << endl; */
146 nontextured->setMaterial ( GL_AMBIENT,
147 ambient[0], ambient[1],
148 ambient[2], ambient[3] ) ;
149 nontextured->setMaterial ( GL_DIFFUSE,
150 diffuse[0], diffuse[1],
151 diffuse[2], diffuse[3] ) ;
152 nontextured->setMaterial ( GL_SPECULAR,
153 specular[0], specular[1],
154 specular[2], specular[3] ) ;
155 nontextured->setMaterial ( GL_EMISSION,
156 emission[0], emission[1],
157 emission[2], emission[3] ) ;
159 state->setStep( 0, textured ); // textured
160 state->setStep( 1, nontextured ); // untextured
162 // Choose the appropriate starting state.
163 state->selectStep(0);
167 void FGNewMat::dump_info () {
168 FG_LOG( FG_TERRAIN, FG_INFO, "{" << endl << " texture = "
170 FG_LOG( FG_TERRAIN, FG_INFO, " xsize = " << xsize );
171 FG_LOG( FG_TERRAIN, FG_INFO, " ysize = " << ysize );
172 FG_LOG( FG_TERRAIN, FG_INFO, " ambient = " << ambient[0] << " "
173 << ambient[1] <<" "<< ambient[2] <<" "<< ambient[3] );
174 FG_LOG( FG_TERRAIN, FG_INFO, " diffuse = " << diffuse[0] << " "
175 << diffuse[1] << " " << diffuse[2] << " " << diffuse[3] );
176 FG_LOG( FG_TERRAIN, FG_INFO, " specular = " << specular[0] << " "
177 << specular[1] << " " << specular[2] << " " << specular[3]);
178 FG_LOG( FG_TERRAIN, FG_INFO, " emission = " << emission[0] << " "
179 << emission[1] << " " << emission[2] << " " << emission[3]);
180 FG_LOG( FG_TERRAIN, FG_INFO, " alpha = " << alpha << endl <<"}" );
186 FGNewMat::~FGNewMat ( void ) {
191 operator >> ( istream& in, FGNewMat& m )
197 if ( token == "texture" ) {
198 in >> token >> m.texture_name;
199 } else if ( token == "xsize" ) {
200 in >> token >> m.xsize;
201 } else if ( token == "ysize" ) {
202 in >> token >> m.ysize;
203 } else if ( token == "wrapu" ) {
204 in >> token >> m.wrapu;
205 } else if ( token == "wrapv" ) {
206 in >> token >> m.wrapv;
207 } else if ( token == "mipmap" ) {
208 in >> token >> m.mipmap;
209 } else if ( token == "ambient" ) {
210 in >> token >> m.ambient[0] >> m.ambient[1]
211 >> m.ambient[2] >> m.ambient[3];
212 } else if ( token == "diffuse" ) {
213 in >> token >> m.diffuse[0] >> m.diffuse[1]
214 >> m.diffuse[2] >> m.diffuse[3];
215 } else if ( token == "specular" ) {
216 in >> token >> m.specular[0] >> m.specular[1]
217 >> m.specular[2] >> m.specular[3];
218 } else if ( token == "emission" ) {
219 in >> token >> m.emission[0] >> m.emission[1]
220 >> m.emission[2] >> m.emission[3];
221 } else if ( token == "alpha" ) {
222 in >> token >> token;
223 if ( token == "yes" ) {
225 } else if ( token == "no" ) {
228 FG_LOG( FG_TERRAIN, FG_INFO, "Bad alpha value " << token );
230 } else if ( token[0] == '}' ) {