Panda3D
renderAttrib.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 renderAttrib.h
10  * @author drose
11  * @date 2002-02-21
12  */
13 
14 #ifndef RENDERATTRIB_H
15 #define RENDERATTRIB_H
16 
17 #include "pandabase.h"
18 
20 #include "renderAttribRegistry.h"
21 #include "pointerTo.h"
22 #include "simpleHashMap.h"
23 #include "lightReMutex.h"
24 #include "pStatCollector.h"
25 
26 class AttribSlots;
28 class CullTraverser;
29 class CullTraverserData;
30 class RenderState;
31 
32 /**
33  * This is the base class for a number of render attributes (other than
34  * transform) that may be set on scene graph nodes to control the appearance
35  * of geometry. This includes TextureAttrib, ColorAttrib, etc.
36  *
37  * RenderAttrib represents render attributes that always propagate down to the
38  * leaves without regard to the particular node they are assigned to. A
39  * RenderAttrib will have the same effect on a leaf node whether it is
40  * assigned to the graph at the leaf or several nodes above. This is
41  * different from RenderEffect, which represents a particular render property
42  * that is applied immediately to the node on which it is encountered, like
43  * billboarding or decaling.
44  *
45  * You should not attempt to create or modify a RenderAttrib directly;
46  * instead, use the make() method of the appropriate kind of attrib you want.
47  * This will allocate and return a new RenderAttrib of the appropriate type,
48  * and it may share pointers if possible. Do not modify the new RenderAttrib
49  * if you wish to change its properties; instead, create a new one.
50  */
51 class EXPCL_PANDA_PGRAPH RenderAttrib : public TypedWritableReferenceCount {
52 protected:
53  RenderAttrib();
54 
55 public:
56  RenderAttrib(const RenderAttrib &copy) = delete;
57  virtual ~RenderAttrib();
58 
59  RenderAttrib &operator = (const RenderAttrib &copy) = delete;
60 
61 PUBLISHED:
62  INLINE CPT(RenderAttrib) compose(const RenderAttrib *other) const;
63  INLINE CPT(RenderAttrib) invert_compose(const RenderAttrib *other) const;
64  virtual bool lower_attrib_can_override() const;
65 
66 public:
67  virtual bool has_cull_callback() const;
68  virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
69 
70 PUBLISHED:
71  INLINE int compare_to(const RenderAttrib &other) const;
72  INLINE size_t get_hash() const;
73  INLINE CPT(RenderAttrib) get_unique() const;
74 
75  virtual bool unref() const final;
76 
77  virtual void output(std::ostream &out) const;
78  virtual void write(std::ostream &out, int indent_level) const;
79 
80  static int get_num_attribs();
81  static void list_attribs(std::ostream &out);
82  static int garbage_collect();
83  static bool validate_attribs();
84 
85  virtual int get_slot() const=0;
86  MAKE_PROPERTY(slot, get_slot);
87 
88  enum PandaCompareFunc { // intentionally defined to match D3DCMPFUNC
89  M_none=0, // alpha-test disabled (always-draw)
90  M_never, // Never draw.
91  M_less, // incoming < reference_alpha
92  M_equal, // incoming == reference_alpha
93  M_less_equal, // incoming <= reference_alpha
94  M_greater, // incoming > reference_alpha
95  M_not_equal, // incoming != reference_alpha
96  M_greater_equal, // incoming >= reference_alpha
97  M_always // Always draw.
98  };
99 
100  // This is the enumerated type for TexGenAttrib. It is inherited into
101  // TexGenAttrib. It is defined up at this level only to avoid circular
102  // dependencies in the header files.
103  enum TexGenMode {
104  M_off,
105 
106  // In the types below, "eye" means the coordinate space of the observing
107  // camera, and "world" means world coordinates, e.g. the coordinate space
108  // of the root of the graph.
109 
110  // Sphere maps are classic static reflection maps. They are supported on
111  // just about any hardware, and require a precomputed 360-degree fisheye
112  // image. Sphere maps only make sense in eye coordinate space.
113  M_eye_sphere_map,
114 
115 /*
116  * Cube maps are a modern improvement on the sphere map; they don't suffer
117  * from any polar singularities, but they require six texture images. They
118  * can also be generated dynamically for real-time reflections (see
119  * GraphicsOutput::make_cube_map()). Typically, a statically-generated cube
120  * map will be in eye space, while a dynamically-generated map will be in
121  * world space. Cube mapping is not supported on all hardware.
122  */
123  M_world_cube_map,
124  M_eye_cube_map,
125 
126  // Normal maps are most useful for applying diffuse lighting effects via a
127  // pregenerated cube map.
128  M_world_normal,
129  M_eye_normal,
130 
131  // Position maps convert XYZ coordinates directly to texture coordinates.
132  // This is particularly useful for implementing projective texturing (see
133  // NodePath::project_texture()).
134  M_world_position,
135  M_unused, // formerly M_object_position, now deprecated.
136  M_eye_position,
137 
138 /*
139  * With M_point_sprite, texture coordinates will be generated for large points
140  * in the range (0,0) - (1,1) from upper-left to lower-right across the
141  * point's face. Without this, each point will have just a single uniform
142  * texture coordinate value across its face. Unfortunately, the generated
143  * texture coordinates are inverted (upside-down) from Panda's usual
144  * convention, but this is what the graphics card manufacturers decided to
145  * use. You could use a texture matrix to re-invert the texture, but that
146  * will probably force the sprites' vertices to be computed in the CPU. You'll
147  * have to paint your textures upside-down if you want true hardware sprites.
148  */
149  M_point_sprite,
150 
151  // M_light_vector generated special 3-d texture coordinates that
152  // represented the vector to a particular Light in the scene graph,
153  // expressed in each vertex's tangent space. This has now been removed.
154  // We need to reserve the slot in the enum, though, to make sure the
155  // following enum value still has the same value.
156  M_unused2,
157 
158  // M_constant generates the same fixed texture coordinates at each vertex.
159  // Not terribly useful, of course, except for certain special effects
160  // involving moving a flat color over an object.
161  M_constant,
162  };
163 
164 protected:
165  INLINE void calc_hash();
166 
167  static CPT(RenderAttrib) return_new(RenderAttrib *attrib);
168  static CPT(RenderAttrib) return_unique(RenderAttrib *attrib);
169  virtual int compare_to_impl(const RenderAttrib *other) const;
170  virtual size_t get_hash_impl() const;
171  virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
172  virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
173  void output_comparefunc(std::ostream &out, PandaCompareFunc fn) const;
174 
175 public:
176  INLINE static int register_slot(TypeHandle type_handle, int sort,
177  RenderAttrib *default_attrib);
178 
179 private:
180  void release_new();
181 
182 public:
183  static void init_attribs();
184 
185 private:
186  // This mutex protects _attribs.
187  static LightReMutex *_attribs_lock;
189  static Attribs *_attribs;
190 
191  int _saved_entry;
192  size_t _hash;
193 
194  // This keeps track of our current position through the garbage collection
195  // cycle.
196  static size_t _garbage_index;
197 
198  static PStatCollector _garbage_collect_pcollector;
199 
200  friend class RenderAttribRegistry;
201 
202 public:
203  virtual void write_datagram(BamWriter *manager, Datagram &dg);
204  static TypedWritable *change_this(TypedWritable *old_ptr, BamReader *manager);
205  virtual void finalize(BamReader *manager);
206 
207 protected:
208  static TypedWritable *new_from_bam(RenderAttrib *attrib, BamReader *manager);
209  void fillin(DatagramIterator &scan, BamReader *manager);
210 
211 public:
212  static TypeHandle get_class_type() {
213  return _type_handle;
214  }
215  static void init_type() {
216  TypedWritableReferenceCount::init_type();
217  register_type(_type_handle, "RenderAttrib",
218  TypedWritableReferenceCount::get_class_type());
219  }
220  virtual TypeHandle get_type() const {
221  return get_class_type();
222  }
223  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
224 
225 private:
226  static TypeHandle _type_handle;
227 };
228 
229 INLINE std::ostream &operator << (std::ostream &out, const RenderAttrib &attrib) {
230  attrib.output(out);
231  return out;
232 }
233 
234 #include "renderAttrib.I"
235 
236 #endif
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
virtual void finalize(BamReader *manager)
Called by the BamReader to perform any final actions needed for setting up the object after all objec...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
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.
A lightweight reentrant mutex.
Definition: lightReMutex.h:30
This collects together the pieces of data that are accumulated for each node while walking the scene ...
This template class implements an unordered map of keys to data, implemented as a hashtable.
Definition: simpleHashMap.h:81
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A lightweight class that represents a single element that may be timed and/or counted via stats.
This class is used to associate each RenderAttrib with a different slot index at runtime,...
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:73
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition: cullTraverser.h:45
virtual bool unref() const
Explicitly decrements the reference count.