Panda3D
textureAttrib.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 textureAttrib.h
10  * @author drose
11  * @date 2002-02-21
12  */
13 
14 #ifndef TEXTUREATTRIB_H
15 #define TEXTUREATTRIB_H
16 
17 #include "pandabase.h"
18 
19 #include "renderAttrib.h"
20 #include "texture.h"
21 #include "textureStage.h"
22 #include "updateSeq.h"
23 #include "ordered_vector.h"
24 #include "vector_int.h"
25 #include "epvector.h"
26 
27 /**
28  * Indicates the set of TextureStages and their associated Textures that
29  * should be applied to (or removed from) a node.
30  */
31 class EXPCL_PANDA_PGRAPH TextureAttrib : public RenderAttrib {
32 protected:
33  INLINE TextureAttrib();
34  INLINE TextureAttrib(const TextureAttrib &copy);
35 
36 PUBLISHED:
37  // These methods are used to create a simple, single-textured layer. For
38  // multitexture, use the multitexture interfaces, further below.
39  static CPT(RenderAttrib) make(Texture *tex);
40  static CPT(RenderAttrib) make_off();
41  static CPT(RenderAttrib) make_default();
42 
43  INLINE bool is_off() const;
44  INLINE Texture *get_texture() const;
45 
46  // The following methods define the new multitexture mode for TextureAttrib.
47  // Each TextureAttrib can add or remove individual texture stages from the
48  // complete set of textures that are to be applied; this is similar to the
49  // mechanism of LightAttrib.
50  static CPT(RenderAttrib) make();
51  static CPT(RenderAttrib) make_all_off();
52 
53  INLINE int get_num_on_stages() const;
54  INLINE TextureStage *get_on_stage(int n) const;
55  MAKE_SEQ(get_on_stages, get_num_on_stages, get_on_stage);
56  INLINE int get_num_on_ff_stages() const;
57  INLINE TextureStage *get_on_ff_stage(int n) const;
58  MAKE_SEQ(get_on_ff_stages, get_num_on_ff_stages, get_on_ff_stage);
59  INLINE int get_ff_tc_index(int n) const;
60  INLINE bool has_on_stage(TextureStage *stage) const;
61  INLINE Texture *get_on_texture(TextureStage *stage) const;
62  INLINE const SamplerState &get_on_sampler(TextureStage *stage) const;
63  INLINE int get_on_stage_override(TextureStage *stage) const;
64 
65  int find_on_stage(const TextureStage *stage) const;
66 
67  MAKE_SEQ_PROPERTY(on_stages, get_num_on_stages, get_on_stage);
68 
69  MAKE_MAP_PROPERTY(textures, has_on_stage, get_on_texture);
70  MAKE_MAP_KEYS_SEQ(textures, get_num_on_stages, get_on_stage);
71 
72  MAKE_MAP_PROPERTY(samplers, has_on_stage, get_on_sampler);
73  MAKE_MAP_KEYS_SEQ(samplers, get_num_on_stages, get_on_stage);
74 
75  INLINE int get_num_off_stages() const;
76  INLINE TextureStage *get_off_stage(int n) const;
77  MAKE_SEQ(get_off_stages, get_num_off_stages, get_off_stage);
78  INLINE bool has_off_stage(TextureStage *stage) const;
79  INLINE bool has_all_off() const;
80 
81  MAKE_SEQ_PROPERTY(off_stages, get_num_off_stages, get_off_stage);
82 
83  INLINE bool is_identity() const;
84 
85  CPT(RenderAttrib) add_on_stage(TextureStage *stage, Texture *tex, int override = 0) const;
86  CPT(RenderAttrib) add_on_stage(TextureStage *stage, Texture *tex,
87  const SamplerState &sampler, int override = 0) const;
88  CPT(RenderAttrib) remove_on_stage(TextureStage *stage) const;
89  CPT(RenderAttrib) add_off_stage(TextureStage *stage, int override = 0) const;
90  CPT(RenderAttrib) remove_off_stage(TextureStage *stage) const;
91  CPT(RenderAttrib) unify_texture_stages(TextureStage *stage) const;
92 
93 public:
94  CPT(TextureAttrib) filter_to_max(int max_texture_stages) const;
95 
96  virtual bool lower_attrib_can_override() const;
97  virtual void output(std::ostream &out) const;
98 
99  virtual bool has_cull_callback() const;
100  virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
101 
102 protected:
103  virtual int compare_to_impl(const RenderAttrib *other) const;
104  virtual size_t get_hash_impl() const;
105  virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
106  virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
107 
108 private:
109  INLINE void check_sorted() const;
110  void sort_on_stages();
111 
112 private:
113  class StageNode {
114  public:
115  INLINE StageNode(const TextureStage *stage,
116  unsigned int implicit_sort = 0,
117  int override = 0);
118 
119  SamplerState _sampler;
120  PT(TextureStage) _stage;
121  PT(Texture) _texture;
122  bool _has_sampler;
123  int _ff_tc_index;
124  unsigned int _implicit_sort;
125  int _override;
126  };
127 
128  class CompareTextureStagePriorities {
129  public:
130  INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
131  };
132 
133  class CompareTextureStageSort {
134  public:
135  INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
136  };
137 
138  class CompareTextureStagePointer {
139  public:
140  INLINE bool operator () (const TextureAttrib::StageNode &a, const TextureAttrib::StageNode &b) const;
141  };
142 
144  Stages _on_stages; // set of all "on" stages, indexed by pointer.
145 
146  typedef pvector<StageNode *> RenderStages;
147  RenderStages _render_stages; // all "on" stages, sorted in render order.
148  RenderStages _render_ff_stages; // fixed-function stages only, in render order.
149  unsigned int _next_implicit_sort;
150 
151  Stages _off_stages;
152  bool _off_all_stages;
153 
154  typedef pmap< int, CPT(TextureAttrib) > Filtered;
155  Filtered _filtered;
156 
157  UpdateSeq _sort_seq;
158  UpdateSeq _filtered_seq;
159 
160  static CPT(RenderAttrib) _empty_attrib;
161  static CPT(RenderAttrib) _all_off_attrib;
162 
163 PUBLISHED:
164  static int get_class_slot() {
165  return _attrib_slot;
166  }
167  virtual int get_slot() const {
168  return get_class_slot();
169  }
170  MAKE_PROPERTY(class_slot, get_class_slot);
171 
172 public:
173  static void register_with_read_factory();
174  virtual void write_datagram(BamWriter *manager, Datagram &dg);
175  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
176 
177 protected:
178  static TypedWritable *make_from_bam(const FactoryParams &params);
179  void fillin(DatagramIterator &scan, BamReader *manager);
180 
181 public:
182  static TypeHandle get_class_type() {
183  return _type_handle;
184  }
185  static void init_type() {
186  RenderAttrib::init_type();
187  register_type(_type_handle, "TextureAttrib",
188  RenderAttrib::get_class_type());
189  _attrib_slot = register_slot(_type_handle, 30, new TextureAttrib);
190  }
191  virtual TypeHandle get_type() const {
192  return get_class_type();
193  }
194  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
195 
196 private:
197  static TypeHandle _type_handle;
198  static int _attrib_slot;
199 };
200 
201 #include "textureAttrib.I"
202 
203 #endif
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
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
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:71
virtual bool has_cull_callback() const
Should be overridden by derived classes to return true if cull_callback() has been defined.
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 collects together the pieces of data that are accumulated for each node while walking the scene ...
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.
A specialization of ordered_vector that emulates a standard STL set: one copy of each element is allo...
Indicates the set of TextureStages and their associated Textures that should be applied to (or remove...
Definition: textureAttrib.h:31
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 bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const
If has_cull_callback() returns true, this function will be called during the cull traversal to perfor...
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:36
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
This is a sequence number that increments monotonically.
Definition: updateSeq.h:37
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:35
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool lower_attrib_can_override() const
Intended to be overridden by derived RenderAttrib types to specify how two consecutive RenderAttrib o...
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition: cullTraverser.h:45