1 // materialmgr.cxx -- class to handle material properties
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
28 #ifdef FG_MATH_EXCEPTION_CLASH
39 #include <Include/compiler.h>
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>
51 #include "materialmgr.hxx"
52 #include "fragment.hxx"
57 // global material management class
58 fgMATERIAL_MGR material_mgr;
62 FGMaterialSlot::FGMaterialSlot ( void ) { }
66 FGMaterialSlot::~FGMaterialSlot ( void ) {
71 fgMATERIAL_MGR::fgMATERIAL_MGR ( void ) {
72 materials_loaded = false;
77 FGMaterialSlot::render_fragments()
79 int tris_rendered = current_view.get_tris_rendered();
81 // cout << "rendering " + texture_name + " = " << list_size << "\n";
87 if ( current_options.get_textures() ) {
89 if ( !m.is_loaded() ) {
90 m.load_texture( current_options.get_fg_root() );
92 // build the ssgSimpleState
93 GLuint tex_id = m.get_texture_id();
94 state.setTexture( tex_id );
95 state.enable( GL_TEXTURE_2D );
96 state.enable( GL_LIGHTING );
97 state.setShadeModel( GL_SMOOTH );
98 state.enable ( GL_CULL_FACE ) ;
99 state.setMaterial ( GL_AMBIENT_AND_DIFFUSE, 1, 1, 1, 1 ) ;
102 #ifdef GL_VERSION_1_1
103 xglBindTexture( GL_TEXTURE_2D, m.get_texture_id() );
104 #elif GL_EXT_texture_object
105 xglBindTextureEXT( GL_TEXTURE_2D, m.get_texture_id() );
110 xglMaterialfv (GL_FRONT, GL_AMBIENT, m.get_ambient() );
111 xglMaterialfv (GL_FRONT, GL_DIFFUSE, m.get_diffuse() );
114 FGTileEntry* last_tile_ptr = NULL;
115 frag_list_iterator current = list.begin();
116 frag_list_iterator last = list.end();
118 for ( ; current != last; ++current ) {
119 fgFRAGMENT* frag_ptr = *current;
120 tris_rendered += frag_ptr->num_faces();
121 if ( frag_ptr->tile_ptr != last_tile_ptr ) {
122 // new tile, new translate
123 last_tile_ptr = frag_ptr->tile_ptr;
124 xglLoadMatrixf( frag_ptr->tile_ptr->model_view );
127 // Woohoo!!! We finally get to draw something!
128 // printf(" display_list = %d\n", frag_ptr->display_list);
129 xglCallList( frag_ptr->display_list );
132 current_view.set_tris_rendered( tris_rendered );
136 // Load a library of material properties
138 fgMATERIAL_MGR::load_lib ( void )
140 string material_name;
142 // build the path name to the material db
143 FGPath mpath( current_options.get_fg_root() );
144 mpath.append( "materials" );
146 fg_gzifstream in( mpath.str() );
148 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << mpath.str() );
152 while ( ! in.eof() ) {
153 // printf("%s", line);
155 // strip leading white space and comments
158 // set to zero to prevent its value accidently being '{'
159 // after a failed >> operation.
162 in >> material_name >> token;
164 if ( token == '{' ) {
165 FG_LOG( FG_TERRAIN, FG_INFO,
166 " Loading material " << material_name );
170 // if ( current_options.get_textures() ) {
171 // m.load_texture( current_options.get_fg_root() );
174 FGMaterialSlot m_slot;
176 material_mgr.material_map[material_name] = m_slot;
180 if ( current_options.get_textures() ) {
181 materials_loaded = true;
188 // Initialize the transient list of fragments for each material property
190 fgMATERIAL_MGR::init_transient_material_lists( void )
192 iterator last = end();
193 for ( iterator it = begin(); it != last; ++it ) {
194 (*it).second.init_sort_list();
200 fgMATERIAL_MGR::find( const string& material, FGMaterialSlot*& mtl_ptr )
202 iterator it = material_map.find( material );
204 mtl_ptr = &((*it).second);
213 fgMATERIAL_MGR::~fgMATERIAL_MGR ( void ) {
218 fgMATERIAL_MGR::render_fragments()
220 current_view.set_tris_rendered( 0 );
222 iterator last = end();
223 for ( iterator current = begin(); current != last; ++current ) {
224 (*current).second.render_fragments();