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