Panda3D
textureAttrib.h
1 // Filename: textureAttrib.h
2 // Created by: drose (21Feb02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef TEXTUREATTRIB_H
16 #define TEXTUREATTRIB_H
17 
18 #include "pandabase.h"
19 
20 #include "renderAttrib.h"
21 #include "texture.h"
22 #include "textureStage.h"
23 #include "updateSeq.h"
24 #include "ordered_vector.h"
25 #include "vector_int.h"
26 #include "epvector.h"
27 
28 ////////////////////////////////////////////////////////////////////
29 // Class : TextureAttrib
30 // Description : Indicates the set of TextureStages and their
31 // associated Textures that should be applied to (or
32 // removed from) a node.
33 ////////////////////////////////////////////////////////////////////
34 class EXPCL_PANDA_PGRAPH TextureAttrib : public RenderAttrib {
35 protected:
36  INLINE TextureAttrib();
37  INLINE TextureAttrib(const TextureAttrib &copy);
38 
39 PUBLISHED:
40  // These methods are used to create a simple, single-textured layer.
41  // For multitexture, use the multitexture interfaces, further below.
42  static CPT(RenderAttrib) make(Texture *tex);
43  static CPT(RenderAttrib) make_off();
44  static CPT(RenderAttrib) make_default();
45 
46  INLINE bool is_off() const;
47  INLINE Texture *get_texture() const;
48 
49  // The following methods define the new multitexture mode for
50  // TextureAttrib. Each TextureAttrib can add or remove individual
51  // texture stages from the complete set of textures that are to be
52  // applied; this is similar to the mechanism of LightAttrib.
53  static CPT(RenderAttrib) make();
54  static CPT(RenderAttrib) make_all_off();
55 
56  INLINE int get_num_on_stages() const;
57  INLINE TextureStage *get_on_stage(int n) const;
58  MAKE_SEQ(get_on_stages, get_num_on_stages, get_on_stage);
59  INLINE int get_num_on_ff_stages() const;
60  INLINE TextureStage *get_on_ff_stage(int n) const;
61  MAKE_SEQ(get_on_ff_stages, get_num_on_ff_stages, get_on_ff_stage);
62  INLINE int get_ff_tc_index(int n) const;
63  INLINE bool has_on_stage(TextureStage *stage) const;
64  INLINE Texture *get_on_texture(TextureStage *stage) const;
65  INLINE const SamplerState &get_on_sampler(TextureStage *stage) const;
66  INLINE int get_on_stage_override(TextureStage *stage) const;
67 
68  int find_on_stage(const TextureStage *stage) const;
69 
70  INLINE int get_num_off_stages() const;
71  INLINE TextureStage *get_off_stage(int n) const;
72  MAKE_SEQ(get_off_stages, get_num_off_stages, get_off_stage);
73  INLINE bool has_off_stage(TextureStage *stage) const;
74  INLINE bool has_all_off() const;
75 
76  INLINE bool is_identity() const;
77 
78  CPT(RenderAttrib) add_on_stage(TextureStage *stage, Texture *tex, int override = 0) const;
79  CPT(RenderAttrib) add_on_stage(TextureStage *stage, Texture *tex,
80  const SamplerState &sampler, int override = 0) const;
81  CPT(RenderAttrib) remove_on_stage(TextureStage *stage) const;
82  CPT(RenderAttrib) add_off_stage(TextureStage *stage, int override = 0) const;
83  CPT(RenderAttrib) remove_off_stage(TextureStage *stage) const;
84  CPT(RenderAttrib) unify_texture_stages(TextureStage *stage) const;
85 
86 public:
87  CPT(TextureAttrib) filter_to_max(int max_texture_stages) const;
88 
89  virtual bool lower_attrib_can_override() const;
90  virtual void output(ostream &out) const;
91 
92  virtual bool has_cull_callback() const;
93  virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const;
94 
95 protected:
96  virtual int compare_to_impl(const RenderAttrib *other) const;
97  virtual size_t get_hash_impl() const;
98  virtual CPT(RenderAttrib) compose_impl(const RenderAttrib *other) const;
99  virtual CPT(RenderAttrib) invert_compose_impl(const RenderAttrib *other) const;
100  virtual CPT(RenderAttrib) get_auto_shader_attrib_impl(const RenderState *state) const;
101 
102 private:
103  INLINE void check_sorted() const;
104  void sort_on_stages();
105 
106 private:
107  class StageNode {
108  public:
109  INLINE StageNode(const TextureStage *stage,
110  unsigned int implicit_sort = 0,
111  int override = 0);
112 
113  SamplerState _sampler;
114  PT(TextureStage) _stage;
115  PT(Texture) _texture;
116  bool _has_sampler;
117  int _ff_tc_index;
118  unsigned int _implicit_sort;
119  int _override;
120  };
121 
122  class CompareTextureStagePriorities {
123  public:
124  INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
125  };
126 
127  class CompareTextureStageSort {
128  public:
129  INLINE bool operator () (const TextureAttrib::StageNode *a, const TextureAttrib::StageNode *b) const;
130  };
131 
132  class CompareTextureStagePointer {
133  public:
134  INLINE bool operator () (const TextureAttrib::StageNode &a, const TextureAttrib::StageNode &b) const;
135  };
136 
138  Stages _on_stages; // set of all "on" stages, indexed by pointer.
139 
141  RenderStages _render_stages; // all "on" stages, sorted in render order.
142  RenderStages _render_ff_stages; // fixed-function stages only, in render order.
143  unsigned int _next_implicit_sort;
144 
145  Stages _off_stages;
146  bool _off_all_stages;
147 
149  Filtered _filtered;
150 
151  UpdateSeq _sort_seq;
152  UpdateSeq _filtered_seq;
153 
154  static CPT(RenderAttrib) _empty_attrib;
155  static CPT(RenderAttrib) _all_off_attrib;
156 
157 PUBLISHED:
158  static int get_class_slot() {
159  return _attrib_slot;
160  }
161  virtual int get_slot() const {
162  return get_class_slot();
163  }
164 
165 public:
166  static void register_with_read_factory();
167  virtual void write_datagram(BamWriter *manager, Datagram &dg);
168  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
169 
170 protected:
171  static TypedWritable *make_from_bam(const FactoryParams &params);
172  void fillin(DatagramIterator &scan, BamReader *manager);
173 
174 public:
175  static TypeHandle get_class_type() {
176  return _type_handle;
177  }
178  static void init_type() {
179  RenderAttrib::init_type();
180  register_type(_type_handle, "TextureAttrib",
181  RenderAttrib::get_class_type());
182  _attrib_slot = register_slot(_type_handle, 30, make_default);
183  }
184  virtual TypeHandle get_type() const {
185  return get_class_type();
186  }
187  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
188 
189 private:
190  static TypeHandle _type_handle;
191  static int _attrib_slot;
192 };
193 
194 #include "textureAttrib.I"
195 
196 #endif
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:60
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
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:37
This collects together the pieces of data that are accumulated for each node while walking the scene ...
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
This is our own Panda specialization on the default STL list.
Definition: plist.h:38
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:34
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
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...
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:39
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:85
This is a sequence number that increments monotonically.
Definition: updateSeq.h:43
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:38
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
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:48