]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.cxx
a886f33e0484b87d8d71f94d615fe9dcb2f78508
[simgear.git] / simgear / scene / material / mat.cxx
1 // e 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  - http://www.flightgear.org/~curt
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 <string.h>
31 #include <map>
32 SG_USING_STD(map);
33
34 #include <plib/ul.h>
35
36 #ifdef SG_MATH_EXCEPTION_CLASH
37 #  include <math.h>
38 #endif
39
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/misc/sg_path.hxx>
42 #include <simgear/misc/sgstream.hxx>
43
44 #include "mat.hxx"
45
46 \f
47 ////////////////////////////////////////////////////////////////////////
48 // Constructors and destructor.
49 ////////////////////////////////////////////////////////////////////////
50
51
52 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season )
53 {
54     init();
55     read_properties( fg_root, props, season );
56     build_ssg_state( false );
57 }
58
59 SGMaterial::SGMaterial( const string &texpath )
60 {
61     init();
62
63     _internal_state st( NULL, texpath, false );
64     _status.push_back( st );
65
66     build_ssg_state( true );
67 }
68
69 SGMaterial::SGMaterial( ssgSimpleState *s )
70 {
71     init();
72     set_ssg_state( s );
73 }
74
75 SGMaterial::~SGMaterial (void)
76 {
77   for (unsigned int i = 0; i < object_groups.size(); i++) {
78     delete object_groups[i];
79     object_groups[i] = 0;
80   }
81 }
82
83
84 \f
85 ////////////////////////////////////////////////////////////////////////
86 // Public methods.
87 ////////////////////////////////////////////////////////////////////////
88
89 void
90 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season )
91 {
92                                 // Gather the path(s) to the texture(s)
93   vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
94   for (unsigned int i = 0; i < textures.size(); i++)
95   {
96     string tname = textures[i]->getStringValue();
97     string otname = tname;
98     if (season && strncmp(season, "summer", 6))
99     {
100         if (tname.substr(0,7) == "Terrain")
101             tname.insert(7,"."+string(season));
102     }
103
104     if (tname == "") {
105         tname = "unknown.rgb";
106     }
107
108     SGPath tpath( fg_root );
109     tpath.append("Textures.high");
110     tpath.append(tname);
111     if ( !ulFileExists(tpath.c_str()) ) {
112       tpath = SGPath( fg_root );
113       tpath.append("Textures");
114       tpath.append(tname);
115     }
116
117     if ( ulFileExists(tpath.c_str()) ) {
118       _internal_state st( NULL, tpath.str(), false );
119       _status.push_back( st );
120     }
121   }
122
123   if (textures.size() == 0) {
124     string tname = "unknown.rgb";
125     SGPath tpath( fg_root );
126     tpath.append("Textures");
127     tpath.append(tname);
128     _internal_state st( NULL, tpath.str(), true );
129     _status.push_back( st );
130   }
131
132   xsize = props->getDoubleValue("xsize", 0.0);
133   ysize = props->getDoubleValue("ysize", 0.0);
134   wrapu = props->getBoolValue("wrapu", true);
135   wrapv = props->getBoolValue("wrapv", true);
136   mipmap = props->getBoolValue("mipmap", true);
137   light_coverage = props->getDoubleValue("light-coverage", 0.0);
138
139   // Taken from default values as used in ac3d
140   ambient[0] = props->getDoubleValue("ambient/r", 0.2);
141   ambient[1] = props->getDoubleValue("ambient/g", 0.2);
142   ambient[2] = props->getDoubleValue("ambient/b", 0.2);
143   ambient[3] = props->getDoubleValue("ambient/a", 1.0);
144
145   diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
146   diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
147   diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
148   diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
149
150   specular[0] = props->getDoubleValue("specular/r", 0.0);
151   specular[1] = props->getDoubleValue("specular/g", 0.0);
152   specular[2] = props->getDoubleValue("specular/b", 0.0);
153   specular[3] = props->getDoubleValue("specular/a", 1.0);
154
155   emission[0] = props->getDoubleValue("emissive/r", 0.0);
156   emission[1] = props->getDoubleValue("emissive/g", 0.0);
157   emission[2] = props->getDoubleValue("emissive/b", 0.0);
158   emission[3] = props->getDoubleValue("emissive/a", 1.0);
159
160   shininess = props->getDoubleValue("shininess", 1.0);
161
162   vector<SGPropertyNode_ptr> object_group_nodes =
163     ((SGPropertyNode *)props)->getChildren("object-group");
164   for (unsigned int i = 0; i < object_group_nodes.size(); i++)
165     object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
166 }
167
168
169 \f
170 ////////////////////////////////////////////////////////////////////////
171 // Private methods.
172 ////////////////////////////////////////////////////////////////////////
173
174 void 
175 SGMaterial::init ()
176 {
177     _status.clear();
178     _current_ptr = 0;
179     xsize = 0;
180     ysize = 0;
181     wrapu = true;
182     wrapv = true;
183     mipmap = true;
184     light_coverage = 0.0;
185     refcount = 0;
186     shininess = 1.0;
187     for (int i = 0; i < 4; i++) {
188         ambient[i]  = (i < 3) ? 0.2 : 1.0;
189         specular[i] = (i < 3) ? 0.0 : 1.0;
190         diffuse[i]  = (i < 3) ? 0.8 : 1.0;
191         emission[i] = (i < 3) ? 0.0 : 1.0;
192     }
193 }
194
195 bool
196 SGMaterial::load_texture ( int n )
197 {
198     int i   = (n >= 0) ? n   : 0 ;
199     int end = (n >= 0) ? n+1 : _status.size();
200
201     for (; i < end; i++)
202     {
203         if ( !_status[i].texture_loaded ) {
204             SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
205                                           << _status[i].texture_path );
206             _status[i].state->setTexture(
207                    (char *)_status[i].texture_path.c_str(),
208                    wrapu, wrapv, mipmap );
209             _status[i].texture_loaded = true;
210        }
211     }
212     return true;
213 }
214
215 ssgSimpleState *
216 SGMaterial::get_state (int n) const
217 {
218     if (_status.size() == 0) {
219         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
220         return NULL;
221     }
222
223     ssgSimpleState *st = (n >= 0) ? _status[n].state
224                                   : _status[_current_ptr].state;
225     ((SGMaterial *)this)->_current_ptr += 1;
226     if (_current_ptr >= _status.size())
227         ((SGMaterial *)this)->_current_ptr = 0;
228
229     return st;
230 }
231
232
233 void 
234 SGMaterial::build_ssg_state( bool defer_tex_load )
235 {
236     GLenum shade_model = GL_SMOOTH;
237     
238     for (unsigned int i = 0; i < _status.size(); i++)
239     {
240         ssgSimpleState *state = new ssgSimpleState();
241         state->ref();
242
243         // Set up the textured state
244         state->setShadeModel( shade_model );
245         state->enable( GL_LIGHTING );
246         state->enable ( GL_CULL_FACE ) ;
247         state->enable( GL_TEXTURE_2D );
248         state->disable( GL_BLEND );
249         state->disable( GL_ALPHA_TEST );
250
251         if ( !defer_tex_load ) {
252             SG_LOG(SG_INPUT, SG_INFO, "    " << _status[i].texture_path );
253             state->setTexture( (char *)_status[i].texture_path.c_str(),
254                                 wrapu, wrapv );
255             _status[i].texture_loaded = true;
256         } else {
257             _status[i].texture_loaded = false;
258         }
259
260         state->enable( GL_COLOR_MATERIAL );
261         state->setMaterial ( GL_AMBIENT,
262                              ambient[0], ambient[1],
263                              ambient[2], ambient[3] ) ;
264         state->setMaterial ( GL_DIFFUSE,
265                             diffuse[0], diffuse[1],
266                             diffuse[2], diffuse[3] ) ;
267         state->setMaterial ( GL_SPECULAR,
268                             specular[0], specular[1],
269                             specular[2], specular[3] ) ;
270         state->setMaterial ( GL_EMISSION,
271                             emission[0], emission[1],
272                             emission[2], emission[3] ) ;
273         state->setShininess ( shininess );
274
275         _status[i].state = state;
276     }
277 }
278
279
280 void SGMaterial::set_ssg_state( ssgSimpleState *s )
281 {
282     _internal_state st( s, "", true );
283     st.state->ref();
284     _status.push_back( st );
285 }
286
287 // end of mat.cxx