]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.cxx
Updates to cloud code to add different basic cloud types. This isn't the
[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 FG_MATH_EXCEPTION_CLASH
31 #  include <math.h>
32 #endif
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/misc/fgpath.hxx>
36 #include <simgear/misc/fgstream.hxx>
37
38 #include "newmat.hxx"
39
40
41 // Constructor
42 FGNewMat::FGNewMat ( void ) {
43 }
44
45
46 // Constructor
47 FGNewMat::FGNewMat ( const string &name )
48 {
49     FGNewMat( name, name );
50 }
51
52
53 // Constructor
54 FGNewMat::FGNewMat ( const string &mat_name, const string &tex_name )
55 {
56     material_name = mat_name;
57     texture_name = tex_name;
58     xsize = ysize = 0;
59     alpha = 0; 
60     ambient[0]  = ambient[1]  = ambient[2]  = ambient[3]  = 1.0;
61     diffuse[0]  = diffuse[1]  = diffuse[2]  = diffuse[3]  = 1.0;
62     specular[0] = specular[1] = specular[2] = specular[3] = 1.0;
63     emission[0] = emission[1] = emission[2] = emission[3] = 1.0;
64 }
65
66
67 void FGNewMat::build_ssg_state( const string& path,
68                                 GLenum shade_model, bool texture_default )
69 {
70     FGPath tex_file( path );
71     tex_file.append( texture_name );
72
73     state = new ssgStateSelector(2);
74     textured = new ssgSimpleState();
75     nontextured = new ssgSimpleState();
76
77     // Set up the textured state
78     textured->setShadeModel( shade_model );
79     textured->enable ( GL_CULL_FACE ) ;
80     textured->enable( GL_TEXTURE_2D );
81     textured->disable( GL_BLEND );
82     textured->disable( GL_ALPHA_TEST );
83     textured->setTexture( (char *)tex_file.c_str() );
84     textured->enable( GL_COLOR_MATERIAL );
85     textured->setColourMaterial( GL_AMBIENT_AND_DIFFUSE );
86     textured->setMaterial( GL_EMISSION, 0, 0, 0, 1 );
87     textured->setMaterial( GL_SPECULAR, 0, 0, 0, 1 );
88
89     // Set up the coloured state
90     nontextured->enable( GL_LIGHTING );
91     nontextured->setShadeModel( shade_model );
92     nontextured->enable ( GL_CULL_FACE      ) ;
93     nontextured->disable( GL_TEXTURE_2D );
94     nontextured->disable( GL_BLEND );
95     nontextured->disable( GL_ALPHA_TEST );
96     nontextured->disable( GL_COLOR_MATERIAL );
97
98     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
99        << "," << ambient[2] << endl; */
100     nontextured->setMaterial ( GL_AMBIENT, 
101                                ambient[0], ambient[1], 
102                                ambient[2], ambient[3] ) ;
103     nontextured->setMaterial ( GL_DIFFUSE, 
104                                diffuse[0], diffuse[1], 
105                                diffuse[2], diffuse[3] ) ;
106     nontextured->setMaterial ( GL_SPECULAR, 
107                                specular[0], specular[1], 
108                                specular[2], specular[3] ) ;
109     nontextured->setMaterial ( GL_EMISSION, 
110                                emission[0], emission[1], 
111                                emission[2], emission[3] ) ;
112
113     state->setStep( 0, textured );    // textured
114     state->setStep( 1, nontextured ); // untextured
115
116     // Choose the appropriate starting state.
117     if ( texture_default ) {
118         state->selectStep(0);
119     } else {
120         state->selectStep(1);
121     }
122 }
123
124
125 void FGNewMat::set_ssg_state( ssgSimpleState *s ) {
126     state = new ssgStateSelector(2);
127     textured = s;
128     nontextured = new ssgSimpleState();
129
130     // Set up the coloured state
131     nontextured->enable( GL_LIGHTING );
132     nontextured->setShadeModel( GL_FLAT );
133     nontextured->enable ( GL_CULL_FACE      ) ;
134     nontextured->disable( GL_TEXTURE_2D );
135     nontextured->disable( GL_BLEND );
136     nontextured->disable( GL_ALPHA_TEST );
137     nontextured->disable( GL_COLOR_MATERIAL );
138
139     /* cout << "ambient = " << ambient[0] << "," << ambient[1] 
140        << "," << ambient[2] << endl; */
141     nontextured->setMaterial ( GL_AMBIENT, 
142                                ambient[0], ambient[1], 
143                                ambient[2], ambient[3] ) ;
144     nontextured->setMaterial ( GL_DIFFUSE, 
145                                diffuse[0], diffuse[1], 
146                                diffuse[2], diffuse[3] ) ;
147     nontextured->setMaterial ( GL_SPECULAR, 
148                                specular[0], specular[1], 
149                                specular[2], specular[3] ) ;
150     nontextured->setMaterial ( GL_EMISSION, 
151                                emission[0], emission[1], 
152                                emission[2], emission[3] ) ;
153
154     state->setStep( 0, textured );    // textured
155     state->setStep( 1, nontextured ); // untextured
156
157     // Choose the appropriate starting state.
158     state->selectStep(0);
159 }
160
161
162 void FGNewMat::dump_info () {
163     FG_LOG( FG_TERRAIN, FG_INFO, "{" << endl << "  texture = " 
164             << texture_name );
165     FG_LOG( FG_TERRAIN, FG_INFO, "  xsize = " << xsize );
166     FG_LOG( FG_TERRAIN, FG_INFO, "  ysize = " << ysize );
167     FG_LOG( FG_TERRAIN, FG_INFO, "  ambient = " << ambient[0] << " "
168             << ambient[1] <<" "<< ambient[2] <<" "<< ambient[3] );
169     FG_LOG( FG_TERRAIN, FG_INFO, "  diffuse = " << diffuse[0] << " " 
170             << diffuse[1] << " " << diffuse[2] << " " << diffuse[3] );
171     FG_LOG( FG_TERRAIN, FG_INFO, "  specular = " << specular[0] << " " 
172             << specular[1] << " " << specular[2] << " " << specular[3]);
173     FG_LOG( FG_TERRAIN, FG_INFO, "  emission = " << emission[0] << " " 
174             << emission[1] << " " << emission[2] << " " << emission[3]);
175     FG_LOG( FG_TERRAIN, FG_INFO, "  alpha = " << alpha << endl <<"}" );
176             
177 }
178
179
180 // Destructor
181 FGNewMat::~FGNewMat ( void ) {
182 }
183
184
185 istream&
186 operator >> ( istream& in, FGNewMat& m )
187 {
188     string token;
189
190     for (;;) {
191         in >> token;
192         if ( token == "texture" ) {
193             in >> token >> m.texture_name;
194         } else if ( token == "xsize" ) {
195             in >> token >> m.xsize;
196         } else if ( token == "ysize" ) {
197             in >> token >> m.ysize;
198         } else if ( token == "ambient" ) {
199             in >> token >> m.ambient[0] >> m.ambient[1]
200                >> m.ambient[2] >> m.ambient[3];
201         } else if ( token == "diffuse" ) {
202             in >> token >> m.diffuse[0] >> m.diffuse[1]
203                >> m.diffuse[2] >> m.diffuse[3];
204         } else if ( token == "specular" ) {
205             in >> token >> m.specular[0] >> m.specular[1]
206                >> m.specular[2] >> m.specular[3];
207         } else if ( token == "emission" ) {
208             in >> token >> m.emission[0] >> m.emission[1]
209                >> m.emission[2] >> m.emission[3];
210         } else if ( token == "alpha" ) {
211             in >> token >> token;
212             if ( token == "yes" ) {
213                 m.alpha = 1;
214             } else if ( token == "no" ) {
215                 m.alpha = 0;
216             } else {
217                 FG_LOG( FG_TERRAIN, FG_INFO, "Bad alpha value " << token );
218             }
219         } else if ( token[0] == '}' ) {
220             break;
221         }
222     }
223
224     return in;
225 }