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  CPT(RenderAttrib) replace_texture(Texture *tex, Texture *new_tex) const;
93 
94 public:
95  CPT(TextureAttrib) filter_to_max(int max_texture_stages) const;
96 
97  virtual bool lower_attrib_can_override() const;
98  virtual void output(std::ostream &out) const;
99 
100  virtual bool has_cull_callback() const;
101  virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
102 
103 protected:
104  virtual int compare_to_impl(const RenderAttrib *other) const;
105  virtual size_t get_hash_impl() const;
106  virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
107  virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
108 
109 private:
110  INLINE void check_sorted() const;
111  void sort_on_stages();
112 
113 private:
114  class StageNode {
115  public:
116  INLINE StageNode(const TextureStage *stage,
117  unsigned int implicit_sort = 0,
118  int override = 0);
119 
120  SamplerState _sampler;
121  PT(TextureStage) _stage;
122  PT(Texture) _texture;
123  bool _has_sampler;
124  int _ff_tc_index;
125  unsigned int _implicit_sort;
126  int _override;
127  };
128 
129  class CompareTextureStagePriorities {
130  public:
131  INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
132  };
133 
134  class CompareTextureStageSort {
135  public:
136  INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
137  };
138 
139  class CompareTextureStagePointer {
140  public:
141  INLINE bool operator () (const TextureAttrib::StageNode &a, const TextureAttrib::StageNode &b) const;
142  };
143 
145  Stages _on_stages; // set of all "on" stages, indexed by pointer.
146 
147  typedef pvector<StageNode *> RenderStages;
148  RenderStages _render_stages; // all "on" stages, sorted in render order.
149  RenderStages _render_ff_stages; // fixed-function stages only, in render order.
150  unsigned int _next_implicit_sort;
151 
152  Stages _off_stages;
153  bool _off_all_stages;
154 
155  typedef pmap< int, CPT(TextureAttrib) > Filtered;
156  Filtered _filtered;
157 
158  UpdateSeq _sort_seq;
159  UpdateSeq _filtered_seq;
160 
161  static CPT(RenderAttrib) _empty_attrib;
162  static CPT(RenderAttrib) _all_off_attrib;
163 
164 PUBLISHED:
165  static int get_class_slot() {
166  return _attrib_slot;
167  }
168  virtual int get_slot() const {
169  return get_class_slot();
170  }
171  MAKE_PROPERTY(class_slot, get_class_slot);
172 
173 public:
174  static void register_with_read_factory();
175  virtual void write_datagram(BamWriter *manager, Datagram &dg);
176  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
177 
178 protected:
179  static TypedWritable *make_from_bam(const FactoryParams &params);
180  void fillin(DatagramIterator &scan, BamReader *manager);
181 
182 public:
183  static TypeHandle get_class_type() {
184  return _type_handle;
185  }
186  static void init_type() {
187  RenderAttrib::init_type();
188  register_type(_type_handle, "TextureAttrib",
189  RenderAttrib::get_class_type());
190  _attrib_slot = register_slot(_type_handle, 30, new TextureAttrib);
191  }
192  virtual TypeHandle get_type() const {
193  return get_class_type();
194  }
195  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
196 
197 private:
198  static TypeHandle _type_handle;
199  static int _attrib_slot;
200 };
201 
202 #include "textureAttrib.I"
203 
204 #endif
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
This collects together the pieces of data that are accumulated for each node while walking the scene ...
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition: cullTraverser.h:45
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
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 write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
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
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:36
Indicates the set of TextureStages and their associated Textures that should be applied to (or remove...
Definition: textureAttrib.h:31
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...
virtual bool has_cull_callback() const
Should be overridden by derived classes to return true if cull_callback() has been defined.
virtual bool lower_attrib_can_override() const
Intended to be overridden by derived RenderAttrib types to specify how two consecutive RenderAttrib o...
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:35
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
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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().
This is a sequence number that increments monotonically.
Definition: updateSeq.h:37
A specialization of ordered_vector that emulates a standard STL set: one copy of each element is allo...
This is our own Panda specialization on the default STL list.
Definition: plist.h:35
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.