]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.cxx
Integrated FGOptions with the property manager (by David Megginson)
[flightgear.git] / src / Objects / newmat.cxx
1 // newmat.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2000  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #ifdef FG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/fgpath.hxx>
36 #include <simgear/misc/fgstream.hxx>
37
38 #include "newmat.hxx"
39
40
41 // Constructor
42 FGNewMat::FGNewMat ( void ) {
43     wrapu = wrapv = 1;
44     mipmap = 1;
45     light_coverage = -1.0;
46 }
47
48
49 // Constructor
50 FGNewMat::FGNewMat ( const string &name )
51 {
52     FGNewMat( name, name );
53 }
54
55
56 // Constructor
57 FGNewMat::FGNewMat ( const string &mat_name, const string &tex_name )
58 {
59     material_name = mat_name;
60     texture_name = tex_name;
61     xsize = ysize = 0;
62     wrapu = wrapv = 1;
63     mipmap = 1;
64     alpha = 0; 
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;
70 }
71
72
73 void FGNewMat::build_ssg_state( const string& path,
74                                 GLenum shade_model, bool texture_default )
75 {
76     FGPath tex_file( path );
77     tex_file.append( texture_name );
78
79     state = new ssgStateSelector(2);
80     state->ref();
81
82     textured = new ssgSimpleState();
83     textured->ref();
84
85     nontextured = new ssgSimpleState();
86     nontextured->ref();
87
88     // Set up the textured state
89     textured->setShadeModel( shade_model );
90     textured->enable( GL_LIGHTING );
91     textured->enable ( GL_CULL_FACE ) ;
92     textured->enable( GL_TEXTURE_2D );
93     textured->disable( GL_BLEND );
94     textured->disable( GL_ALPHA_TEST );
95     textured->setTexture( (char *)tex_file.c_str(), wrapu, wrapv );
96     // cout << "wrap u = " << wrapu << " wrapv = " << wrapv << endl;
97     textured->enable( GL_COLOR_MATERIAL );
98     textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
99     textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
100     textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
101
102     // Set up the coloured state
103     nontextured->enable( GL_LIGHTING );
104     nontextured->setShadeModel( shade_model );
105     nontextured->enable ( GL_CULL_FACE      ) ;
106     nontextured->disable( GL_TEXTURE_2D );
107     nontextured->disable( GL_BLEND );
108     nontextured->disable( GL_ALPHA_TEST );
109     nontextured->disable( GL_COLOR_MATERIAL );
110
111     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
112        << "," << ambient[2] << endl; */
113     nontextured->setMaterial ( GL_AMBIENT, 
114                                ambient[0], ambient[1], 
115                                ambient[2], ambient[3] ) ;
116     nontextured->setMaterial ( GL_DIFFUSE, 
117                                diffuse[0], diffuse[1], 
118                                diffuse[2], diffuse[3] ) ;
119     nontextured->setMaterial ( GL_SPECULAR, 
120                                specular[0], specular[1], 
121                                specular[2], specular[3] ) ;
122     nontextured->setMaterial ( GL_EMISSION, 
123                                emission[0], emission[1], 
124                                emission[2], emission[3] ) ;
125
126     state->setStep( 0, textured );    // textured
127     state->setStep( 1, nontextured ); // untextured
128
129     // Choose the appropriate starting state.
130     if ( texture_default ) {
131         state->selectStep(0);
132     } else {
133         state->selectStep(1);
134     }
135 }
136
137
138 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
139     state = new ssgStateSelector(2);
140     state->ref();
141
142     textured = s;
143
144     nontextured = new ssgSimpleState();
145     nontextured->ref();
146
147     // Set up the coloured state
148     nontextured->enable( GL_LIGHTING );
149     nontextured->setShadeModel( GL_FLAT );
150     nontextured->enable ( GL_CULL_FACE      ) ;
151     nontextured->disable( GL_TEXTURE_2D );
152     nontextured->disable( GL_BLEND );
153     nontextured->disable( GL_ALPHA_TEST );
154     nontextured->disable( GL_COLOR_MATERIAL );
155
156     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
157        << "," << ambient[2] << endl; */
158     nontextured->setMaterial ( GL_AMBIENT, 
159                                ambient[0], ambient[1], 
160                                ambient[2], ambient[3] ) ;
161     nontextured->setMaterial ( GL_DIFFUSE, 
162                                diffuse[0], diffuse[1], 
163                                diffuse[2], diffuse[3] ) ;
164     nontextured->setMaterial ( GL_SPECULAR, 
165                                specular[0], specular[1], 
166                                specular[2], specular[3] ) ;
167     nontextured->setMaterial ( GL_EMISSION, 
168                                emission[0], emission[1], 
169                                emission[2], emission[3] ) ;
170
171     state->setStep( 0, textured );    // textured
172     state->setStep( 1, nontextured ); // untextured
173
174     // Choose the appropriate starting state.
175     state->selectStep(0);
176 }
177
178
179 void FGNewMat::dump_info () {
180     FG_LOG( FG_TERRAIN, FG_INFO, "{" << endl << "  texture = " 
181             << texture_name );
182     FG_LOG( FG_TERRAIN, FG_INFO, "  xsize = " << xsize );
183     FG_LOG( FG_TERRAIN, FG_INFO, "  ysize = " << ysize );
184     FG_LOG( FG_TERRAIN, FG_INFO, "  ambient = " << ambient[0] << " "
185             << ambient[1] <<" "<< ambient[2] <<" "<< ambient[3] );
186     FG_LOG( FG_TERRAIN, FG_INFO, "  diffuse = " << diffuse[0] << " " 
187             << diffuse[1] << " " << diffuse[2] << " " << diffuse[3] );
188     FG_LOG( FG_TERRAIN, FG_INFO, "  specular = " << specular[0] << " " 
189             << specular[1] << " " << specular[2] << " " << specular[3]);
190     FG_LOG( FG_TERRAIN, FG_INFO, "  emission = " << emission[0] << " " 
191             << emission[1] << " " << emission[2] << " " << emission[3]);
192     FG_LOG( FG_TERRAIN, FG_INFO, "  alpha = " << alpha << endl <<"}" );
193             
194 }
195
196
197 // Destructor
198 FGNewMat::~FGNewMat ( void ) {
199 }
200
201
202 istream&
203 operator >> ( istream& in, FGNewMat& m )
204 {
205     string token;
206
207     for (;;) {
208         in >> token;
209         if ( token == "texture" ) {
210             in >> token >> m.texture_name;
211         } else if ( token == "xsize" ) {
212             in >> token >> m.xsize;
213         } else if ( token == "ysize" ) {
214             in >> token >> m.ysize;
215         } else if ( token == "wrapu" ) {
216             in >> token >> m.wrapu;
217         } else if ( token == "wrapv" ) {
218             in >> token >> m.wrapv;
219         } else if ( token == "mipmap" ) {
220             in >> token >> m.mipmap;
221         } else if ( token == "ambient" ) {
222             in >> token >> m.ambient[0] >> m.ambient[1]
223                >> m.ambient[2] >> m.ambient[3];
224         } else if ( token == "diffuse" ) {
225             in >> token >> m.diffuse[0] >> m.diffuse[1]
226                >> m.diffuse[2] >> m.diffuse[3];
227         } else if ( token == "specular" ) {
228             in >> token >> m.specular[0] >> m.specular[1]
229                >> m.specular[2] >> m.specular[3];
230         } else if ( token == "emission" ) {
231             in >> token >> m.emission[0] >> m.emission[1]
232                >> m.emission[2] >> m.emission[3];
233         } else if ( token == "alpha" ) {
234             in >> token >> token;
235             if ( token == "yes" ) {
236                 m.alpha = 1;
237             } else if ( token == "no" ) {
238                 m.alpha = 0;
239             } else {
240                 FG_LOG( FG_TERRAIN, FG_INFO, "Bad alpha value " << token );
241             }
242         } else if ( token == "light-coverage" ) {
243             in >> token >> m.light_coverage;
244         } else if ( token[0] == '}' ) {
245             break;
246         }
247     }
248
249     return in;
250 }