]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.cxx
Fix some lighting values.
[simgear.git] / simgear / scene / material / mat.cxx
1 // mat.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 <simgear_config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <map>
31 SG_USING_STD(map);
32
33 #include <simgear/compiler.h>
34
35 #ifdef SG_MATH_EXCEPTION_CLASH
36 #  include <math.h>
37 #endif
38
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/math/sg_random.h>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
43
44 #include "mat.hxx"
45
46 \f
47 ////////////////////////////////////////////////////////////////////////
48 // Local static functions.
49 ////////////////////////////////////////////////////////////////////////
50
51 /**
52  * Internal method to test whether a file exists.
53  *
54  * TODO: this should be moved to a SimGear library of local file
55  * functions.
56  */
57 static inline bool
58 local_file_exists( const string& path ) {
59     sg_gzifstream in( path );
60     if ( ! in.is_open() ) {
61         return false;
62     } else {
63         return true;
64     }
65 }
66
67
68 \f
69 ////////////////////////////////////////////////////////////////////////
70 // Constructors and destructor.
71 ////////////////////////////////////////////////////////////////////////
72
73
74 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props )
75 {
76     init();
77     read_properties( fg_root, props );
78     build_ssg_state( false );
79 }
80
81 SGMaterial::SGMaterial( const string &texpath )
82 {
83     init();
84     texture_path = texpath;
85     build_ssg_state( true );
86 }
87
88 SGMaterial::SGMaterial( ssgSimpleState *s )
89 {
90     init();
91     set_ssg_state( s );
92 }
93
94 SGMaterial::~SGMaterial (void)
95 {
96   for (unsigned int i = 0; i < object_groups.size(); i++) {
97     delete object_groups[i];
98     object_groups[i] = 0;
99   }
100 }
101
102
103 \f
104 ////////////////////////////////////////////////////////////////////////
105 // Public methods.
106 ////////////////////////////////////////////////////////////////////////
107
108 void
109 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props )
110 {
111                                 // Get the path to the texture
112   string tname = props->getStringValue("texture", "unknown.rgb");
113   SGPath tpath( fg_root );
114   tpath.append("Textures.high");
115   tpath.append(tname);
116   if (!local_file_exists(tpath.str())) {
117     tpath = SGPath( fg_root );
118     tpath.append("Textures");
119     tpath.append(tname);
120   }
121   texture_path = tpath.str();
122
123   xsize = props->getDoubleValue("xsize", 0.0);
124   ysize = props->getDoubleValue("ysize", 0.0);
125   wrapu = props->getBoolValue("wrapu", true);
126   wrapv = props->getBoolValue("wrapv", true);
127   mipmap = props->getBoolValue("mipmap", true);
128   light_coverage = props->getDoubleValue("light-coverage", 0.0);
129
130   ambient[0] = props->getDoubleValue("ambient/r", 0.0);
131   ambient[1] = props->getDoubleValue("ambient/g", 0.0);
132   ambient[2] = props->getDoubleValue("ambient/b", 0.0);
133   ambient[3] = props->getDoubleValue("ambient/a", 0.0);
134
135   diffuse[0] = props->getDoubleValue("diffuse/r", 0.0);
136   diffuse[1] = props->getDoubleValue("diffuse/g", 0.0);
137   diffuse[2] = props->getDoubleValue("diffuse/b", 0.0);
138   diffuse[3] = props->getDoubleValue("diffuse/a", 0.0);
139
140   specular[0] = props->getDoubleValue("specular/r", 0.0);
141   specular[1] = props->getDoubleValue("specular/g", 0.0);
142   specular[2] = props->getDoubleValue("specular/b", 0.0);
143   specular[3] = props->getDoubleValue("specular/a", 0.0);
144
145   emission[0] = props->getDoubleValue("emissive/r", 0.0);
146   emission[1] = props->getDoubleValue("emissive/g", 0.0);
147   emission[2] = props->getDoubleValue("emissive/b", 0.0);
148   emission[3] = props->getDoubleValue("emissive/a", 0.0);
149
150   shininess = props->getDoubleValue("shininess", 0.0);
151
152   vector<SGPropertyNode_ptr> object_group_nodes =
153     ((SGPropertyNode *)props)->getChildren("object-group");
154   for (unsigned int i = 0; i < object_group_nodes.size(); i++)
155     object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
156 }
157
158
159 \f
160 ////////////////////////////////////////////////////////////////////////
161 // Private methods.
162 ////////////////////////////////////////////////////////////////////////
163
164 void 
165 SGMaterial::init ()
166 {
167     texture_path = "";
168     state = NULL;
169     xsize = 0;
170     ysize = 0;
171     wrapu = true;
172     wrapv = true;
173     mipmap = true;
174     light_coverage = 0.0;
175     texture_loaded = false;
176     refcount = 0;
177     shininess = 0.0;
178     for (int i = 0; i < 4; i++) {
179         ambient[i] = diffuse[i] = specular[i] = emission[i] = 0.0;
180     }
181 }
182
183 bool
184 SGMaterial::load_texture ()
185 {
186     if ( texture_loaded ) {
187         return false;
188     } else {
189         SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
190                 << texture_path );
191         state->setTexture( (char *)texture_path.c_str(), wrapu, wrapv, mipmap );
192         texture_loaded = true;
193         return true;
194     }
195 }
196
197
198 void 
199 SGMaterial::build_ssg_state( bool defer_tex_load )
200 {
201     GLenum shade_model = GL_SMOOTH;
202     
203     state = new ssgSimpleState();
204     state->ref();
205
206     // Set up the textured state
207     state->setShadeModel( shade_model );
208     state->enable( GL_LIGHTING );
209     state->enable ( GL_CULL_FACE ) ;
210     state->enable( GL_TEXTURE_2D );
211     state->disable( GL_BLEND );
212     state->disable( GL_ALPHA_TEST );
213     if ( !defer_tex_load ) {
214         SG_LOG(SG_INPUT, SG_INFO, "    " << texture_path );
215         state->setTexture( (char *)texture_path.c_str(), wrapu, wrapv );
216         texture_loaded = true;
217     } else {
218         texture_loaded = false;
219     }
220     state->enable( GL_COLOR_MATERIAL );
221 #if 0
222     state->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
223     state->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
224     state->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
225 #else
226     state->setMaterial ( GL_AMBIENT,
227                             ambient[0], ambient[1],
228                             ambient[2], ambient[3] ) ;
229     state->setMaterial ( GL_DIFFUSE,
230                             diffuse[0], diffuse[1],
231                             diffuse[2], diffuse[3] ) ;
232     state->setMaterial ( GL_SPECULAR,
233                             specular[0], specular[1],
234                             specular[2], specular[3] ) ;
235     state->setMaterial ( GL_EMISSION,
236                             emission[0], emission[1],
237                             emission[2], emission[3] ) ;
238     state->setShininess ( shininess );
239 #endif
240 }
241
242
243 void SGMaterial::set_ssg_state( ssgSimpleState *s )
244 {
245     state = s;
246     state->ref();
247     texture_loaded = true;
248 }
249
250 // end of mat.cxx