]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.cxx
696de0c30421dae58968a23bccd0e2eae7b67cc5
[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 }
46
47
48 // Constructor
49 FGNewMat::FGNewMat ( const string &name )
50 {
51     FGNewMat( name, name );
52 }
53
54
55 // Constructor
56 FGNewMat::FGNewMat ( const string &mat_name, const string &tex_name )
57 {
58     material_name = mat_name;
59     texture_name = tex_name;
60     xsize = ysize = 0;
61     wrapu = wrapv = 1;
62     mipmap = 1;
63     alpha = 0; 
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;
68 }
69
70
71 void FGNewMat::build_ssg_state( const string& path,
72                                 GLenum shade_model, bool texture_default )
73 {
74     FGPath tex_file( path );
75     tex_file.append( texture_name );
76
77     state = new ssgStateSelector(2);
78     state->ref();
79
80     textured = new ssgSimpleState();
81     textured->ref();
82
83     nontextured = new ssgSimpleState();
84     nontextured->ref();
85
86     // Set up the textured state
87     textured->setShadeModel( shade_model );
88     textured->enable( GL_LIGHTING );
89     textured->enable ( GL_CULL_FACE ) ;
90     textured->enable( GL_TEXTURE_2D );
91     textured->disable( GL_BLEND );
92     textured->disable( GL_ALPHA_TEST );
93     textured->setTexture( (char *)tex_file.c_str(), wrapu, wrapv );
94     // cout << "wrap u = " << wrapu << " wrapv = " << wrapv << endl;
95     textured->enable( GL_COLOR_MATERIAL );
96     textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
97     textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
98     textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
99
100     // Set up the coloured state
101     nontextured->enable( GL_LIGHTING );
102     nontextured->setShadeModel( shade_model );
103     nontextured->enable ( GL_CULL_FACE      ) ;
104     nontextured->disable( GL_TEXTURE_2D );
105     nontextured->disable( GL_BLEND );
106     nontextured->disable( GL_ALPHA_TEST );
107     nontextured->disable( GL_COLOR_MATERIAL );
108
109     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
110        << "," << ambient[2] << endl; */
111     nontextured->setMaterial ( GL_AMBIENT, 
112                                ambient[0], ambient[1], 
113                                ambient[2], ambient[3] ) ;
114     nontextured->setMaterial ( GL_DIFFUSE, 
115                                diffuse[0], diffuse[1], 
116                                diffuse[2], diffuse[3] ) ;
117     nontextured->setMaterial ( GL_SPECULAR, 
118                                specular[0], specular[1], 
119                                specular[2], specular[3] ) ;
120     nontextured->setMaterial ( GL_EMISSION, 
121                                emission[0], emission[1], 
122                                emission[2], emission[3] ) ;
123
124     state->setStep( 0, textured );    // textured
125     state->setStep( 1, nontextured ); // untextured
126
127     // Choose the appropriate starting state.
128     if ( texture_default ) {
129         state->selectStep(0);
130     } else {
131         state->selectStep(1);
132     }
133 }
134
135
136 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
137     state = new ssgStateSelector(2);
138     state->ref();
139
140     textured = s;
141
142     nontextured = new ssgSimpleState();
143     nontextured->ref();
144
145     // Set up the coloured state
146     nontextured->enable( GL_LIGHTING );
147     nontextured->setShadeModel( GL_FLAT );
148     nontextured->enable ( GL_CULL_FACE      ) ;
149     nontextured->disable( GL_TEXTURE_2D );
150     nontextured->disable( GL_BLEND );
151     nontextured->disable( GL_ALPHA_TEST );
152     nontextured->disable( GL_COLOR_MATERIAL );
153
154     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
155        << "," << ambient[2] << endl; */
156     nontextured->setMaterial ( GL_AMBIENT, 
157                                ambient[0], ambient[1], 
158                                ambient[2], ambient[3] ) ;
159     nontextured->setMaterial ( GL_DIFFUSE, 
160                                diffuse[0], diffuse[1], 
161                                diffuse[2], diffuse[3] ) ;
162     nontextured->setMaterial ( GL_SPECULAR, 
163                                specular[0], specular[1], 
164                                specular[2], specular[3] ) ;
165     nontextured->setMaterial ( GL_EMISSION, 
166                                emission[0], emission[1], 
167                                emission[2], emission[3] ) ;
168
169     state->setStep( 0, textured );    // textured
170     state->setStep( 1, nontextured ); // untextured
171
172     // Choose the appropriate starting state.
173     state->selectStep(0);
174 }
175
176
177 void FGNewMat::dump_info () {
178     FG_LOG( FG_TERRAIN, FG_INFO, "{" << endl << "  texture = " 
179             << texture_name );
180     FG_LOG( FG_TERRAIN, FG_INFO, "  xsize = " << xsize );
181     FG_LOG( FG_TERRAIN, FG_INFO, "  ysize = " << ysize );
182     FG_LOG( FG_TERRAIN, FG_INFO, "  ambient = " << ambient[0] << " "
183             << ambient[1] <<" "<< ambient[2] <<" "<< ambient[3] );
184     FG_LOG( FG_TERRAIN, FG_INFO, "  diffuse = " << diffuse[0] << " " 
185             << diffuse[1] << " " << diffuse[2] << " " << diffuse[3] );
186     FG_LOG( FG_TERRAIN, FG_INFO, "  specular = " << specular[0] << " " 
187             << specular[1] << " " << specular[2] << " " << specular[3]);
188     FG_LOG( FG_TERRAIN, FG_INFO, "  emission = " << emission[0] << " " 
189             << emission[1] << " " << emission[2] << " " << emission[3]);
190     FG_LOG( FG_TERRAIN, FG_INFO, "  alpha = " << alpha << endl <<"}" );
191             
192 }
193
194
195 // Destructor
196 FGNewMat::~FGNewMat ( void ) {
197 }
198
199
200 istream&
201 operator >> ( istream& in, FGNewMat& m )
202 {
203     string token;
204
205     for (;;) {
206         in >> token;
207         if ( token == "texture" ) {
208             in >> token >> m.texture_name;
209         } else if ( token == "xsize" ) {
210             in >> token >> m.xsize;
211         } else if ( token == "ysize" ) {
212             in >> token >> m.ysize;
213         } else if ( token == "wrapu" ) {
214             in >> token >> m.wrapu;
215         } else if ( token == "wrapv" ) {
216             in >> token >> m.wrapv;
217         } else if ( token == "mipmap" ) {
218             in >> token >> m.mipmap;
219         } else if ( token == "ambient" ) {
220             in >> token >> m.ambient[0] >> m.ambient[1]
221                >> m.ambient[2] >> m.ambient[3];
222         } else if ( token == "diffuse" ) {
223             in >> token >> m.diffuse[0] >> m.diffuse[1]
224                >> m.diffuse[2] >> m.diffuse[3];
225         } else if ( token == "specular" ) {
226             in >> token >> m.specular[0] >> m.specular[1]
227                >> m.specular[2] >> m.specular[3];
228         } else if ( token == "emission" ) {
229             in >> token >> m.emission[0] >> m.emission[1]
230                >> m.emission[2] >> m.emission[3];
231         } else if ( token == "alpha" ) {
232             in >> token >> token;
233             if ( token == "yes" ) {
234                 m.alpha = 1;
235             } else if ( token == "no" ) {
236                 m.alpha = 0;
237             } else {
238                 FG_LOG( FG_TERRAIN, FG_INFO, "Bad alpha value " << token );
239             }
240         } else if ( token[0] == '}' ) {
241             break;
242         }
243     }
244
245     return in;
246 }