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