Panda3D

lightAttrib.h

00001 // Filename: lightAttrib.h
00002 // Created by:  drose (26Mar02)
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 LIGHTATTRIB_H
00016 #define LIGHTATTRIB_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "light.h"
00021 #include "renderAttrib.h"
00022 #include "nodePath.h"
00023 #include "ordered_vector.h"
00024 #include "pmap.h"
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //       Class : LightAttrib
00028 // Description : Indicates which set of lights should be considered
00029 //               "on" to illuminate geometry at this level and below.
00030 //               A LightAttrib can either add lights or remove lights
00031 //               from the total set of "on" lights.
00032 ////////////////////////////////////////////////////////////////////
00033 class EXPCL_PANDA_PGRAPH LightAttrib : public RenderAttrib {
00034 protected:
00035   INLINE LightAttrib();
00036   INLINE LightAttrib(const LightAttrib &copy);
00037 
00038 PUBLISHED:
00039 
00040   // This is the old, deprecated interface to LightAttrib.  Do not use
00041   // any of these methods for new code; these methods will be removed
00042   // soon.
00043   enum Operation {
00044     O_set,
00045     O_add,
00046     O_remove
00047   };
00048   static CPT(RenderAttrib) make(Operation op, 
00049                                 Light *light);
00050   static CPT(RenderAttrib) make(Operation op, 
00051                                 Light *light1, Light *light2);
00052   static CPT(RenderAttrib) make(Operation op, 
00053                                 Light *light1, Light *light2,
00054                                 Light *light3);
00055   static CPT(RenderAttrib) make(Operation op, 
00056                                 Light *light1, Light *light2,
00057                                 Light *light3, Light *light4);
00058   static CPT(RenderAttrib) make_default();
00059 
00060   Operation get_operation() const;
00061 
00062   int get_num_lights() const;
00063   Light *get_light(int n) const;
00064   bool has_light(Light *light) const;
00065 
00066   CPT(RenderAttrib) add_light(Light *light) const;
00067   CPT(RenderAttrib) remove_light(Light *light) const;
00068 
00069 
00070   // The following is the new, more general interface to the
00071   // LightAttrib.
00072   static CPT(RenderAttrib) make();
00073   static CPT(RenderAttrib) make_all_off();
00074 
00075   INLINE int get_num_on_lights() const;
00076   INLINE NodePath get_on_light(int n) const;
00077   MAKE_SEQ(get_on_lights, get_num_on_lights, get_on_light);
00078   INLINE bool has_on_light(const NodePath &light) const;
00079 
00080   INLINE int get_num_off_lights() const;
00081   INLINE NodePath get_off_light(int n) const;
00082   MAKE_SEQ(get_off_lights, get_num_off_lights, get_off_light);
00083   INLINE bool has_off_light(const NodePath &light) const;
00084   INLINE bool has_all_off() const;
00085 
00086   INLINE bool is_identity() const;
00087 
00088   CPT(RenderAttrib) add_on_light(const NodePath &light) const;
00089   CPT(RenderAttrib) remove_on_light(const NodePath &light) const;
00090   CPT(RenderAttrib) add_off_light(const NodePath &light) const;
00091   CPT(RenderAttrib) remove_off_light(const NodePath &light) const;
00092 
00093   CPT(LightAttrib) filter_to_max(int max_lights) const;
00094   NodePath get_most_important_light() const;
00095 
00096 public:
00097   virtual void output(ostream &out) const;
00098   virtual void write(ostream &out, int indent_level) const;
00099 
00100 protected:
00101   virtual int compare_to_impl(const RenderAttrib *other) const;
00102   virtual size_t get_hash_impl() const;
00103   virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
00104   virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
00105   virtual CPT(RenderAttrib) get_auto_shader_attrib_impl(const RenderState *state) const;
00106 
00107 private:
00108   INLINE void check_filtered() const;
00109   void sort_on_lights();
00110 
00111 private:
00112   typedef ov_set<NodePath> Lights;
00113   Lights _on_lights, _off_lights;
00114   bool _off_all_lights;
00115 
00116   typedef pmap< int, CPT(LightAttrib) > Filtered;
00117   Filtered _filtered;
00118 
00119   UpdateSeq _sort_seq;
00120 
00121   static CPT(RenderAttrib) _empty_attrib;
00122   static CPT(RenderAttrib) _all_off_attrib;
00123 
00124 PUBLISHED:
00125   static int get_class_slot() {
00126     return _attrib_slot;
00127   }
00128   virtual int get_slot() const {
00129     return get_class_slot();
00130   }
00131 
00132 public:
00133   // This data is only needed when reading from a bam file.
00134   typedef pvector<PT(PandaNode) > NodeList;
00135   class BamAuxData : public BamReader::AuxData {
00136   public:
00137     // We hold a pointer to each of the PandaNodes on the on_list and
00138     // off_list.  We will later convert these to NodePaths in
00139     // finalize().
00140     int _num_off_lights;
00141     int _num_on_lights;
00142     NodeList _off_list;
00143     NodeList _on_list;
00144   };
00145 
00146 public:
00147   static void register_with_read_factory();
00148   virtual void write_datagram(BamWriter *manager, Datagram &dg);
00149   virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
00150 
00151   virtual void finalize(BamReader *manager);
00152 
00153 protected:
00154   static TypedWritable *make_from_bam(const FactoryParams &params);
00155   void fillin(DatagramIterator &scan, BamReader *manager);
00156   
00157 public:
00158   static TypeHandle get_class_type() {
00159     return _type_handle;
00160   }
00161   static void init_type() {
00162     RenderAttrib::init_type();
00163     register_type(_type_handle, "LightAttrib",
00164                   RenderAttrib::get_class_type());
00165     _attrib_slot = register_slot(_type_handle, 20, make_default);
00166   }
00167   virtual TypeHandle get_type() const {
00168     return get_class_type();
00169   }
00170   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00171 
00172 private:
00173   static TypeHandle _type_handle;
00174   static int _attrib_slot;
00175 };
00176 
00177 #include "lightAttrib.I"
00178 
00179 #endif
00180 
 All Classes Functions Variables Enumerations