Panda3D

materialPool.h

00001 // Filename: materialPool.h
00002 // Created by:  drose (30Apr01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef MATERIALPOOL_H
00016 #define MATERIALPOOL_H
00017 
00018 #include "pandabase.h"
00019 #include "material.h"
00020 #include "pointerTo.h"
00021 #include "lightMutex.h"
00022 #include "pset.h"
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //       Class : MaterialPool
00026 // Description : The MaterialPool (there is only one in the universe)
00027 //               serves to unify different pointers to the same
00028 //               Material, so we do not (a) waste memory with many
00029 //               different Material objects that are all equivalent,
00030 //               and (b) waste time switching the graphics engine
00031 //               between different Material states that are really the
00032 //               same thing.
00033 //
00034 //               The idea is to create a temporary Material
00035 //               representing the lighting state you want to apply,
00036 //               then call get_material(), passing in your temporary
00037 //               Material.  The return value will either be a new
00038 //               Material object, or it may be the the same object you
00039 //               supplied; in either case, it will have the same
00040 //               value.
00041 ////////////////////////////////////////////////////////////////////
00042 class EXPCL_PANDA_GOBJ MaterialPool {
00043 PUBLISHED:
00044   INLINE static Material *get_material(Material *temp);
00045   INLINE static void release_material(Material *temp);
00046   INLINE static void release_all_materials();
00047 
00048   INLINE static int garbage_collect();
00049   INLINE static void list_contents(ostream &out);
00050 
00051   static void write(ostream &out);
00052 
00053 private:
00054   INLINE MaterialPool();
00055 
00056   Material *ns_get_material(Material *temp);
00057   void ns_release_material(Material *temp);
00058   void ns_release_all_materials();
00059 
00060   int ns_garbage_collect();
00061   void ns_list_contents(ostream &out) const;
00062 
00063   static MaterialPool *get_global_ptr();
00064 
00065   static MaterialPool *_global_ptr;
00066 
00067   LightMutex _lock;
00068 
00069   // We store a map of CPT(Material) to PT(Material).  These are two
00070   // equivalent structures, but different pointers.  The first pointer
00071   // never leaves this class.  If the second pointer changes value,
00072   // we'll notice it and return a new one.
00073   typedef pmap< CPT(Material), PT(Material), indirect_compare_to<const Material *> > Materials;
00074   Materials _materials;
00075 };
00076 
00077 #include "materialPool.I"
00078 
00079 #endif
00080 
00081 
 All Classes Functions Variables Enumerations