]> git.mxchange.org Git - flightgear.git/blob - src/Objects/materialmgr.cxx
Additional tweaks to fix the ssg texture (state) problems. Tweaked near
[flightgear.git] / src / Objects / materialmgr.cxx
1 // materialmgr.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
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 #ifdef FG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #include <GL/glut.h>
37 #include <XGL/xgl.h>
38
39 #include <Include/compiler.h>
40
41 #include <string.h>
42 #include STL_STRING
43
44 #include <Debug/logstream.hxx>
45 #include <Misc/fgpath.hxx>
46 #include <Misc/fgstream.hxx>
47 #include <Main/options.hxx>
48 #include <Main/views.hxx>
49 #include <Scenery/tileentry.hxx>
50
51 #include "materialmgr.hxx"
52 #include "fragment.hxx"
53
54 FG_USING_STD(string);
55
56
57 // global material management class
58 fgMATERIAL_MGR material_mgr;
59
60
61 // Constructor
62 FGMaterialSlot::FGMaterialSlot ( void ) { }
63
64
65 // Destructor
66 FGMaterialSlot::~FGMaterialSlot ( void ) {
67 }
68
69
70 // Constructor
71 fgMATERIAL_MGR::fgMATERIAL_MGR ( void ) {
72     materials_loaded = false;
73 }
74
75
76 void
77 FGMaterialSlot::render_fragments()
78 {
79     int tris_rendered = current_view.get_tris_rendered();
80
81     // cout << "rendering " + texture_name + " = " << list_size << "\n";
82
83     if ( empty() ) {
84         return;
85     }
86
87     if ( current_options.get_textures() ) {
88
89         if ( !m.is_loaded() ) {
90             m.load_texture( current_options.get_fg_root() );
91         }
92
93 #ifdef GL_VERSION_1_1
94         xglBindTexture( GL_TEXTURE_2D, m.get_texture_id() );
95 #elif GL_EXT_texture_object
96         xglBindTextureEXT( GL_TEXTURE_2D, m.get_texture_id() );
97 #else
98 #  error port me
99 #endif
100     } else {
101         xglMaterialfv (GL_FRONT, GL_AMBIENT, m.get_ambient() );
102         xglMaterialfv (GL_FRONT, GL_DIFFUSE, m.get_diffuse() );
103     }
104
105     FGTileEntry* last_tile_ptr = NULL;
106     frag_list_iterator current = list.begin();
107     frag_list_iterator last = list.end();
108
109     for ( ; current != last; ++current ) {
110         fgFRAGMENT* frag_ptr = *current;
111         tris_rendered += frag_ptr->num_faces();
112         if ( frag_ptr->tile_ptr != last_tile_ptr ) {
113             // new tile, new translate
114             last_tile_ptr = frag_ptr->tile_ptr;
115             xglLoadMatrixf( frag_ptr->tile_ptr->model_view );
116         }
117
118         // Woohoo!!!  We finally get to draw something!
119         // printf("  display_list = %d\n", frag_ptr->display_list);
120         xglCallList( frag_ptr->display_list );
121     }
122
123     current_view.set_tris_rendered( tris_rendered );
124 }
125
126
127 // Load a library of material properties
128 int
129 fgMATERIAL_MGR::load_lib ( void )
130 {
131     string material_name;
132
133     // build the path name to the material db
134     FGPath mpath( current_options.get_fg_root() );
135     mpath.append( "materials" );
136
137     fg_gzifstream in( mpath.str() );
138     if ( ! in ) {
139         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath.str() );
140         exit(-1);
141     }
142
143     while ( ! in.eof() ) {
144         // printf("%s", line);
145
146         // strip leading white space and comments
147         in >> skipcomment;
148
149         // set to zero to prevent its value accidently being '{'
150         // after a failed >> operation.
151         char token = 0;
152
153         in >> material_name >> token;
154
155         if ( token == '{' ) {
156             FG_LOG( FG_TERRAIN, FG_INFO,
157                     "  Loading material " << material_name );
158             FGMaterial m;
159             in >> m;
160
161             FGMaterialSlot m_slot;
162             m_slot.set_m( m );
163
164             // build the ssgSimpleState
165             FGPath tex_file( current_options.get_fg_root() );
166             tex_file.append( "Textures" );
167             tex_file.append( m.get_texture_name() );
168             tex_file.concat( ".rgb" );
169
170             ssgSimpleState *state = new ssgSimpleState;
171             state->setTexture( tex_file.c_str() );
172             state->enable( GL_TEXTURE_2D );
173             state->enable( GL_LIGHTING );
174             state->setShadeModel( GL_SMOOTH );
175             state->enable ( GL_CULL_FACE      ) ;
176             state->setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
177             m_slot.set_state( state );
178
179             material_mgr.material_map[material_name] = m_slot;
180         }
181     }
182
183     if ( current_options.get_textures() ) {
184         materials_loaded = true;
185     }
186
187     return(1);
188 }
189
190
191 // Initialize the transient list of fragments for each material property
192 void
193 fgMATERIAL_MGR::init_transient_material_lists( void )
194 {
195     iterator last = end();
196     for ( iterator it = begin(); it != last; ++it ) {
197         (*it).second.init_sort_list();
198     }
199 }
200
201
202 bool
203 fgMATERIAL_MGR::find( const string& material, FGMaterialSlot*& mtl_ptr )
204 {
205     iterator it = material_map.find( material );
206     if ( it != end() ) {
207         mtl_ptr = &((*it).second);
208         return true;
209     }
210
211     return false;
212 }
213
214
215 // Destructor
216 fgMATERIAL_MGR::~fgMATERIAL_MGR ( void ) {
217 }
218
219
220 void
221 fgMATERIAL_MGR::render_fragments()
222 {
223     current_view.set_tris_rendered( 0 );
224
225     iterator last = end();
226     for ( iterator current = begin(); current != last; ++current ) {
227         (*current).second.render_fragments();
228     }
229 }
230
231