Panda3D
lightAttrib.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file lightAttrib.h
10  * @author drose
11  * @date 2002-03-26
12  */
13 
14 #ifndef LIGHTATTRIB_H
15 #define LIGHTATTRIB_H
16 
17 #include "pandabase.h"
18 
19 #include "light.h"
20 #include "renderAttrib.h"
21 #include "nodePath.h"
22 #include "ordered_vector.h"
23 #include "pmap.h"
24 
25 /**
26  * Indicates which set of lights should be considered "on" to illuminate
27  * geometry at this level and below. A LightAttrib can either add lights or
28  * remove lights from the total set of "on" lights.
29  */
30 class EXPCL_PANDA_PGRAPH LightAttrib : public RenderAttrib {
31 protected:
32  INLINE LightAttrib();
33  LightAttrib(const LightAttrib &copy);
34 
35 PUBLISHED:
36  virtual ~LightAttrib();
37 
38  // This is the old, deprecated interface to LightAttrib. Do not use any of
39  // these methods for new code; these methods will be removed soon.
40  enum Operation {
41  O_set,
42  O_add,
43  O_remove
44  };
45  static CPT(RenderAttrib) make(Operation op,
46  Light *light);
47  static CPT(RenderAttrib) make(Operation op,
48  Light *light1, Light *light2);
49  static CPT(RenderAttrib) make(Operation op,
50  Light *light1, Light *light2,
51  Light *light3);
52  static CPT(RenderAttrib) make(Operation op,
53  Light *light1, Light *light2,
54  Light *light3, Light *light4);
55  static CPT(RenderAttrib) make_default();
56 
57  Operation get_operation() const;
58 
59  int get_num_lights() const;
60  Light *get_light(int n) const;
61  bool has_light(Light *light) const;
62 
63  CPT(RenderAttrib) add_light(Light *light) const;
64  CPT(RenderAttrib) remove_light(Light *light) const;
65 
66 
67  // The following is the new, more general interface to the LightAttrib.
68  static CPT(RenderAttrib) make();
69  static CPT(RenderAttrib) make_all_off();
70 
71  INLINE size_t get_num_on_lights() const;
72  INLINE size_t get_num_non_ambient_lights() const;
73  INLINE NodePath get_on_light(size_t n) const;
74  MAKE_SEQ(get_on_lights, get_num_on_lights, get_on_light);
75  INLINE bool has_on_light(const NodePath &light) const;
76  INLINE bool has_any_on_light() const;
77 
78  INLINE size_t get_num_off_lights() const;
79  INLINE NodePath get_off_light(size_t n) const;
80  MAKE_SEQ(get_off_lights, get_num_off_lights, get_off_light);
81  INLINE bool has_off_light(const NodePath &light) const;
82  INLINE bool has_all_off() const;
83 
84  INLINE bool is_identity() const;
85 
86  CPT(RenderAttrib) add_on_light(const NodePath &light) const;
87  CPT(RenderAttrib) remove_on_light(const NodePath &light) const;
88  CPT(RenderAttrib) add_off_light(const NodePath &light) const;
89  CPT(RenderAttrib) remove_off_light(const NodePath &light) const;
90 
91  NodePath get_most_important_light() const;
92  LColor get_ambient_contribution() const;
93 
94  MAKE_SEQ_PROPERTY(on_lights, get_num_on_lights, get_on_light);
95  MAKE_SEQ_PROPERTY(off_lights, get_num_off_lights, get_off_light);
96 
97 public:
98  virtual void output(std::ostream &out) const;
99  virtual void write(std::ostream &out, int indent_level) const;
100 
101 protected:
102  virtual int compare_to_impl(const RenderAttrib *other) const;
103  virtual size_t get_hash_impl() const;
104  virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
105  virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
106 
107 private:
108  INLINE void check_sorted() const;
109  void sort_on_lights();
110 
111 private:
112  typedef ov_set<NodePath> Lights;
113  Lights _on_lights, _off_lights;
114  bool _off_all_lights;
115 
116  // These are sorted in descending order of priority, with the ambient lights
117  // sorted last.
119  OrderedLights _sorted_on_lights;
120  size_t _num_non_ambient_lights;
121 
122  UpdateSeq _sort_seq;
123 
124  static CPT(RenderAttrib) _empty_attrib;
125  static CPT(RenderAttrib) _all_off_attrib;
126 
127 PUBLISHED:
128  static int get_class_slot() {
129  return _attrib_slot;
130  }
131  virtual int get_slot() const {
132  return get_class_slot();
133  }
134  MAKE_PROPERTY(class_slot, get_class_slot);
135 
136 public:
137  // This data is only needed when reading from a bam file.
138  typedef pvector<PT(PandaNode) > NodeList;
140  public:
141  // We hold a pointer to each of the PandaNodes on the on_list and
142  // off_list. We will later convert these to NodePaths in finalize().
143  int _num_off_lights;
144  int _num_on_lights;
145  NodeList _off_list;
146  NodeList _on_list;
147  };
148 
149 public:
150  static void register_with_read_factory();
151  virtual void write_datagram(BamWriter *manager, Datagram &dg);
152  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
153 
154  virtual void finalize(BamReader *manager);
155 
156 protected:
157  static TypedWritable *make_from_bam(const FactoryParams &params);
158  void fillin(DatagramIterator &scan, BamReader *manager);
159 
160 public:
161  static TypeHandle get_class_type() {
162  return _type_handle;
163  }
164  static void init_type() {
165  RenderAttrib::init_type();
166  register_type(_type_handle, "LightAttrib",
167  RenderAttrib::get_class_type());
168  _attrib_slot = register_slot(_type_handle, 20, new LightAttrib);
169  }
170  virtual TypeHandle get_type() const {
171  return get_class_type();
172  }
173  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
174 
175 private:
176  static TypeHandle _type_handle;
177  static int _attrib_slot;
178 };
179 
180 #include "lightAttrib.I"
181 
182 #endif
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
The abstract interface to all kinds of lights.
Definition: light.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
This is our own Panda specialization on the default STL list.
Definition: plist.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
static int register_slot(TypeHandle type_handle, int sort, RenderAttrib *default_attrib)
Adds the indicated TypeHandle to the registry, if it is not there already, and returns a unique slot ...
Definition: renderAttrib.I:101
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
virtual void finalize(BamReader *manager)
Called by the BamReader to perform any final actions needed for setting up the object after all objec...
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
This is a sequence number that increments monotonically.
Definition: updateSeq.h:37
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
Indicates which set of lights should be considered "on" to illuminate geometry at this level and belo...
Definition: lightAttrib.h:30