]> git.mxchange.org Git - flightgear.git/blobdiff - Objects/material.cxx
Changes from NHV to make the code more dynamic with fewer hard coded limits.
[flightgear.git] / Objects / material.cxx
index 6625a334b3d24a5552a602c152fd5fe8dc48fc09..d5210dc0085ae4f15efe8b7f4fea63c3616357dc 100644 (file)
 #include <XGL/xgl.h>
 
 #include <string.h>
+#include <string>
 
+#include "Include/fg_stl_config.h"
 #include <Debug/fg_debug.h>
 #include <Main/options.hxx>
 #include <Misc/fgstream.hxx>
+#include <Main/views.hxx>
+#include <Scenery/tile.hxx>
 
 #include "material.hxx"
+#include "fragment.hxx"
 #include "texload.h"
 
+
 // global material management class
 fgMATERIAL_MGR material_mgr;
 
 
 // Constructor
-fgMATERIAL::fgMATERIAL ( void ) {
-    alpha = 0;
+fgMATERIAL::fgMATERIAL ( void )
+    : texture_name(""),
+      alpha(0)
+      // , list_size(0)
+{
     ambient[0]  = ambient[1]  = ambient[2]  = ambient[3]  = 0.0;
     diffuse[0]  = diffuse[1]  = diffuse[2]  = diffuse[3]  = 0.0;
     specular[0] = specular[1] = specular[2] = specular[3] = 0.0;
     emissive[0] = emissive[1] = emissive[2] = emissive[3] = 0.0;
 }
 
-
-int fgMATERIAL::append_sort_list( fgFRAGMENT *object ) {
-    if ( list_size < FG_MAX_MATERIAL_FRAGS )
-    {
+/*
+int
+fgMATERIAL::append_sort_list( fgFRAGMENT *object )
+{
+    if ( list_size < FG_MAX_MATERIAL_FRAGS ) {
        list[ list_size++ ] = object;
-       return 0;
-    }
-    else
-    {
        return 1;
+    } else {
+       return 0;
     }
 }
+*/
 
 istream&
 operator >> ( istream& in, fgMATERIAL& m )
 {
     string token;
 
-    for (;;)
-    {
+    for (;;) {
        in >> token;
        if ( token == "texture" )
        {
@@ -125,8 +133,6 @@ operator >> ( istream& in, fgMATERIAL& m )
 void
 fgMATERIAL::load_texture()
 {
-    if ( current_options.get_textures() )
-    {
        GLubyte *texbuf;
        int width, height;
 
@@ -206,7 +212,6 @@ fgMATERIAL::load_texture()
            xglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
                          GL_RGBA, GL_UNSIGNED_BYTE, texbuf);
        }
-    }
 }
 
 
@@ -217,11 +222,57 @@ fgMATERIAL::~fgMATERIAL ( void ) {
 
 // Constructor
 fgMATERIAL_MGR::fgMATERIAL_MGR ( void ) {
+    textures_loaded = false;
+}
+
+
+void
+fgMATERIAL::render_fragments()
+{
+    // cout << "rendering " + texture_name + " = " << list_size << "\n";
+
+    if ( empty() )
+       return;
+
+    if ( current_options.get_textures() )
+    {
+#ifdef GL_VERSION_1_1
+       xglBindTexture(GL_TEXTURE_2D, texture_id);
+#elif GL_EXT_texture_object
+       xglBindTextureEXT(GL_TEXTURE_2D, texture_id);
+#else
+#  error port me
+#endif
+    } else {
+       xglMaterialfv (GL_FRONT, GL_AMBIENT, ambient);
+       xglMaterialfv (GL_FRONT, GL_DIFFUSE, diffuse);
+    }
+
+    fgTILE* last_tile_ptr = NULL;
+    frag_list_iterator current = list.begin();
+    frag_list_iterator last = list.end();
+
+    for ( ; current != last; ++current ) {
+       fgFRAGMENT* frag_ptr = *current;
+       current_view.tris_rendered += frag_ptr->num_faces();
+       if ( frag_ptr->tile_ptr != last_tile_ptr )
+       {
+           // new tile, new translate
+           last_tile_ptr = frag_ptr->tile_ptr;
+           xglLoadMatrixd( frag_ptr->tile_ptr->model_view );
+       }
+
+       // Woohoo!!!  We finally get to draw something!
+       // printf("  display_list = %d\n", frag_ptr->display_list);
+       xglCallList( frag_ptr->display_list );
+    }
 }
 
 
 // Load a library of material properties
-int fgMATERIAL_MGR::load_lib ( void ) {
+int
+fgMATERIAL_MGR::load_lib ( void )
+{
     string material_name;
 
     // build the path name to the material db
@@ -244,17 +295,20 @@ int fgMATERIAL_MGR::load_lib ( void ) {
        in.stream() >> material_name >> token;
 
        if ( token == '{' ) {
-           cout << "Loading material " << material_name << endl;
+           printf( "  Loading material %s\n", material_name.c_str() );
            fgMATERIAL m;
            in.stream() >> m;
+
+           if ( current_options.get_textures() ) {
+               m.load_texture();
+           }
+
            material_mgr.material_map[material_name] = m;
        }
     }
 
-    for ( iterator it = material_map.begin();
-         it != material_map.end(); ++it )
-    {
-       it->second.load_texture();
+    if ( current_options.get_textures() ) {
+       textures_loaded = true;
     }
 
     return(1);
@@ -262,21 +316,24 @@ int fgMATERIAL_MGR::load_lib ( void ) {
 
 
 // Initialize the transient list of fragments for each material property
-void fgMATERIAL_MGR::init_transient_material_lists( void ) {
-    for ( iterator it = material_map.begin();
-         it != material_map.end(); ++it )
+void
+fgMATERIAL_MGR::init_transient_material_lists( void )
+{
+    iterator last = end();
+    for ( iterator it = begin(); it != last; ++it )
     {
-       it->second.init_sort_list();
+       (*it).second.init_sort_list();
     }
 }
 
+
 bool
 fgMATERIAL_MGR::find( const string& material, fgMATERIAL*& mtl_ptr )
 {
     iterator it = material_map.find( material );
-    if ( it != material_map.end() )
+    if ( it != end() )
     {
-       mtl_ptr = &(it->second);
+       mtl_ptr = &((*it).second);
        return true;
     }
 
@@ -289,7 +346,56 @@ fgMATERIAL_MGR::~fgMATERIAL_MGR ( void ) {
 }
 
 
+void
+fgMATERIAL_MGR::render_fragments()
+{
+    current_view.tris_rendered = 0;
+    iterator last = end();
+    for ( iterator current = begin(); current != last; ++current )
+       (*current).second.render_fragments();
+}
+
+
 // $Log$
+// Revision 1.8  1998/10/12 23:49:17  curt
+// Changes from NHV to make the code more dynamic with fewer hard coded limits.
+//
+// Revision 1.7  1998/09/17 18:35:52  curt
+// Tweaks and optimizations by Norman Vine.
+//
+// Revision 1.6  1998/09/15 01:35:05  curt
+// cleaned up my fragment.num_faces hack :-) to use the STL (no need in
+// duplicating work.)
+// Tweaked fgTileMgrRender() do not calc tile matrix unless necessary.
+// removed some unneeded stuff from fgTileMgrCurElev()
+//
+// Revision 1.5  1998/09/10 19:07:11  curt
+// /Simulator/Objects/fragment.hxx
+//   Nested fgFACE inside fgFRAGMENT since its not used anywhere else.
+//
+// ./Simulator/Objects/material.cxx
+// ./Simulator/Objects/material.hxx
+//   Made fgMATERIAL and fgMATERIAL_MGR bona fide classes with private
+//   data members - that should keep the rabble happy :)
+//
+// ./Simulator/Scenery/tilemgr.cxx
+//   In viewable() delay evaluation of eye[0] and eye[1] in until they're
+//   actually needed.
+//   Change to fgTileMgrRender() to call fgMATERIAL_MGR::render_fragments()
+//   method.
+//
+// ./Include/fg_stl_config.h
+// ./Include/auto_ptr.hxx
+//   Added support for g++ 2.7.
+//   Further changes to other files are forthcoming.
+//
+// Brief summary of changes required for g++ 2.7.
+//   operator->() not supported by iterators: use (*i).x instead of i->x
+//   default template arguments not supported,
+//   <functional> doesn't have mem_fun_ref() needed by callbacks.
+//   some std include files have different names.
+//   template member functions not supported.
+//
 // Revision 1.4  1998/09/01 19:03:08  curt
 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
 //  - The new classes in libmisc.tgz define a stream interface into zlib.