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