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