]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.cxx
Initialization fixes.
[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 <Main/globals.hxx>
39 #include <Main/fg_props.hxx>
40
41 #include "newmat.hxx"
42
43
44 static bool
45 local_file_exists( const string& path ) {
46     sg_gzifstream in( path );
47     if ( ! in.is_open() ) {
48         return false;
49     } else {
50         return true;
51     }
52 }
53
54
55 void 
56 FGNewMat::init ()
57 {
58   texture_path = "";
59   state = 0;
60   textured = 0;
61   nontextured = 0;
62   alpha = false;
63   xsize = 0;
64   ysize = 0;
65   wrapu = true;
66   wrapv = true;
67   mipmap = true;
68   texture_loaded = false;
69   refcount = 0;
70   for (int i = 0; i < 4; i++)
71     ambient[i] = diffuse[i] = specular[i] = emission[i] = 0.0;
72 }
73
74 FGNewMat::FGNewMat (const SGPropertyNode * props)
75 {
76     init();
77     read_properties(props);
78     build_ssg_state(false);
79 }
80
81 FGNewMat::FGNewMat (const string &texture_path)
82 {
83     init();
84     build_ssg_state(true);
85 }
86
87 FGNewMat::FGNewMat (ssgSimpleState * s)
88 {
89     init();
90     set_ssg_state(s);
91 }
92
93
94 bool
95 FGNewMat::load_texture ()
96 {
97   if (texture_loaded) {
98     return false;
99   } else {
100     SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture " << texture_path );
101 #ifdef PLIB_1_2_X
102     textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv );
103 #else
104     textured->setTexture((char *)texture_path.c_str(), wrapu, wrapv, mipmap );
105 #endif
106     texture_loaded = true;
107     return true;
108   }
109 }
110
111
112 void FGNewMat::build_ssg_state( bool defer_tex_load )
113 {
114     GLenum shade_model =
115       (fgGetBool("/sim/rendering/shading") ? GL_SMOOTH : GL_FLAT);
116     bool texture_default = fgGetBool("/sim/rendering/textures");
117
118     state = new ssgStateSelector(2);
119     state->ref();
120
121     textured = new ssgSimpleState();
122     textured->ref();
123
124     nontextured = new ssgSimpleState();
125     nontextured->ref();
126
127     // Set up the textured state
128     textured->setShadeModel( shade_model );
129     textured->enable( GL_LIGHTING );
130     textured->enable ( GL_CULL_FACE ) ;
131     textured->enable( GL_TEXTURE_2D );
132     textured->disable( GL_BLEND );
133     textured->disable( GL_ALPHA_TEST );
134     if ( !defer_tex_load ) {
135         textured->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
136         texture_loaded = true;
137     } else {
138         texture_loaded = false;
139     }
140     textured->enable( GL_COLOR_MATERIAL );
141     textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
142     textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
143     textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
144
145     // Set up the coloured state
146     nontextured->enable( GL_LIGHTING );
147     nontextured->setShadeModel( shade_model );
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     nontextured->setMaterial ( GL_AMBIENT, 
155                                ambient[0], ambient[1], 
156                                ambient[2], ambient[3] ) ;
157     nontextured->setMaterial ( GL_DIFFUSE, 
158                                diffuse[0], diffuse[1], 
159                                diffuse[2], diffuse[3] ) ;
160     nontextured->setMaterial ( GL_SPECULAR, 
161                                specular[0], specular[1], 
162                                specular[2], specular[3] ) ;
163     nontextured->setMaterial ( GL_EMISSION, 
164                                emission[0], emission[1], 
165                                emission[2], emission[3] ) ;
166
167     state->setStep( 0, textured );    // textured
168     state->setStep( 1, nontextured ); // untextured
169
170     // Choose the appropriate starting state.
171     if ( texture_default ) {
172         state->selectStep(0);
173     } else {
174         state->selectStep(1);
175     }
176 }
177
178
179 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
180     state = new ssgStateSelector(2);
181     state->ref();
182
183     textured = s;
184
185     nontextured = new ssgSimpleState();
186     nontextured->ref();
187
188     // Set up the coloured state
189     nontextured->enable( GL_LIGHTING );
190     nontextured->setShadeModel( GL_FLAT );
191     nontextured->enable ( GL_CULL_FACE      ) ;
192     nontextured->disable( GL_TEXTURE_2D );
193     nontextured->disable( GL_BLEND );
194     nontextured->disable( GL_ALPHA_TEST );
195     nontextured->disable( GL_COLOR_MATERIAL );
196
197     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
198        << "," << ambient[2] << endl; */
199     nontextured->setMaterial ( GL_AMBIENT, 
200                                ambient[0], ambient[1], 
201                                ambient[2], ambient[3] ) ;
202     nontextured->setMaterial ( GL_DIFFUSE, 
203                                diffuse[0], diffuse[1], 
204                                diffuse[2], diffuse[3] ) ;
205     nontextured->setMaterial ( GL_SPECULAR, 
206                                specular[0], specular[1], 
207                                specular[2], specular[3] ) ;
208     nontextured->setMaterial ( GL_EMISSION, 
209                                emission[0], emission[1], 
210                                emission[2], emission[3] ) ;
211
212     state->setStep( 0, textured );    // textured
213     state->setStep( 1, nontextured ); // untextured
214
215     // Choose the appropriate starting state.
216     state->selectStep(0);
217 }
218
219
220 // Destructor
221 FGNewMat::~FGNewMat ( void ) {
222 }
223
224
225 void
226 FGNewMat::read_properties (const SGPropertyNode * props)
227 {
228                                 // Get the path to the texture
229   string tname = props->getStringValue("texture", "unknown.rgb");
230   SGPath tpath(globals->get_fg_root());
231   tpath.append("Textures.high");
232   tpath.append(tname);
233   if (!local_file_exists(tpath.str())) {
234     tpath = SGPath(globals->get_fg_root());
235     tpath.append("Textures");
236     tpath.append(tname);
237   }
238   texture_path = tpath.str();
239
240   xsize = props->getDoubleValue("xsize", 0.0);
241   ysize = props->getDoubleValue("ysize", 0.0);
242   wrapu = props->getBoolValue("wrapu", true);
243   wrapv = props->getBoolValue("wrapv", true);
244   mipmap = props->getBoolValue("mipmap", true);
245   light_coverage = props->getDoubleValue("light-coverage");
246
247   ambient[0] = props->getDoubleValue("ambient/r", 0.0);
248   ambient[1] = props->getDoubleValue("ambient/g", 0.0);
249   ambient[2] = props->getDoubleValue("ambient/b", 0.0);
250   ambient[3] = props->getDoubleValue("ambient/a", 0.0);
251
252   diffuse[0] = props->getDoubleValue("diffuse/r", 0.0);
253   diffuse[1] = props->getDoubleValue("diffuse/g", 0.0);
254   diffuse[2] = props->getDoubleValue("diffuse/b", 0.0);
255   diffuse[3] = props->getDoubleValue("diffuse/a", 0.0);
256
257   specular[0] = props->getDoubleValue("specular/r", 0.0);
258   specular[1] = props->getDoubleValue("specular/g", 0.0);
259   specular[2] = props->getDoubleValue("specular/b", 0.0);
260   specular[3] = props->getDoubleValue("specular/a", 0.0);
261
262   emission[0] = props->getDoubleValue("emissive/r", 0.0);
263   emission[1] = props->getDoubleValue("emissive/g", 0.0);
264   emission[2] = props->getDoubleValue("emissive/b", 0.0);
265   emission[3] = props->getDoubleValue("emissive/a", 0.0);
266 }