Panda3D
textureAttrib.I
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.I
10  * @author drose
11  * @date 2002-02-21
12  */
13 
14 /**
15  * Use TextureAttrib::make() to construct a new TextureAttrib object.
16  */
17 INLINE TextureAttrib::
18 TextureAttrib() {
19  _next_implicit_sort = 0;
20  _off_all_stages = false;
21  _sort_seq = UpdateSeq::old();
22  _filtered_seq = UpdateSeq::old();
23 }
24 
25 /**
26  * Use TextureAttrib::make() to construct a new TextureAttrib object. The
27  * copy constructor is only defined to facilitate methods like add_on_stage().
28  */
29 INLINE TextureAttrib::
30 TextureAttrib(const TextureAttrib &copy) :
31  _on_stages(copy._on_stages),
32  _render_stages(copy._render_stages),
33  _render_ff_stages(copy._render_ff_stages),
34  _next_implicit_sort(copy._next_implicit_sort),
35  _off_stages(copy._off_stages),
36  _off_all_stages(copy._off_all_stages),
37  _sort_seq(copy._sort_seq),
38  _filtered_seq(UpdateSeq::old())
39 {
40 }
41 
42 /**
43  * Returns true if the TextureAttrib is an 'off' TextureAttrib, indicating
44  * that it should disable texturing.
45  *
46  * If multitexture is in effect, a TextureAttrib may not be strictly "on" or
47  * "off"; therefore, to get a more precise answer to this question, you should
48  * consider using has_all_off() or get_num_off_stages() or has_off_stage()
49  * instead.
50  */
51 INLINE bool TextureAttrib::
52 is_off() const {
53  return (_on_stages.empty());
54 }
55 
56 /**
57  * If the TextureAttrib is not an 'off' TextureAttrib, returns the base-level
58  * texture that is associated. Otherwise, return NULL.
59  */
61 get_texture() const {
62  if (_on_stages.empty()) {
63  return nullptr;
64  }
65  check_sorted();
66  return get_on_texture(filter_to_max(1)->get_on_stage(0));
67 }
68 
69 /**
70  * Returns the number of stages that are turned on by the attribute.
71  */
72 INLINE int TextureAttrib::
73 get_num_on_stages() const {
74  check_sorted();
75  return _render_stages.size();
76 }
77 
78 /**
79  * Returns the nth stage turned on by the attribute, sorted in render order.
80  */
82 get_on_stage(int n) const {
83  nassertr(n >= 0 && n < (int)_render_stages.size(), nullptr);
84  return _render_stages[n]->_stage;
85 }
86 
87 /**
88  * Returns the number of on-stages that are relevant to the classic fixed
89  * function pipeline. This excludes texture stages such as normal maps.
90  */
91 INLINE int TextureAttrib::
92 get_num_on_ff_stages() const {
93  check_sorted();
94  return _render_ff_stages.size();
95 }
96 
97 /**
98  * Returns the nth stage turned on by the attribute, sorted in render order,
99  * including only those relevant to the classic fixed function pipeline. This
100  * excludes texture stages such as normal maps.
101  */
103 get_on_ff_stage(int n) const {
104  nassertr(n >= 0 && n < (int)_render_ff_stages.size(), nullptr);
105  return _render_ff_stages[n]->_stage;
106 }
107 
108 /**
109  * For each TextureStage listed in get_on_ff_stage(), this returns a unique
110  * index number for the texture coordinate name used by that TextureStage. It
111  * is guaranteed to remain the same index number for each texcoord name (for a
112  * given set of TextureStages), even if the texture render order changes.
113  */
114 INLINE int TextureAttrib::
115 get_ff_tc_index(int n) const {
116  nassertr(n >= 0 && n < (int)_render_ff_stages.size(), -1);
117  return _render_ff_stages[n]->_ff_tc_index;
118 }
119 
120 /**
121  * Returns true if the indicated stage is turned on by the attrib, false
122  * otherwise.
123  */
124 INLINE bool TextureAttrib::
125 has_on_stage(TextureStage *stage) const {
126  return _on_stages.find(StageNode(stage)) != _on_stages.end();
127 }
128 
129 /**
130  * Returns the texture associated with the indicated stage, or NULL if no
131  * texture is associated.
132  */
133 INLINE Texture *TextureAttrib::
134 get_on_texture(TextureStage *stage) const {
135  Stages::const_iterator si;
136  si = _on_stages.find(StageNode(stage));
137  if (si != _on_stages.end()) {
138  return (*si)._texture;
139  }
140  return nullptr;
141 }
142 
143 /**
144  * Returns the sampler associated with the indicated stage, or the one
145  * associated with its texture if no custom stage has been specified. It is
146  * an error to call this if the stage does not exist.
147  */
148 INLINE const SamplerState &TextureAttrib::
149 get_on_sampler(TextureStage *stage) const {
150  Stages::const_iterator si;
151  si = _on_stages.find(StageNode(stage));
152  nassertr_always(si != _on_stages.end(), SamplerState::get_default());
153 
154  return si->_has_sampler ? si->_sampler
155  : si->_texture->get_default_sampler();
156 }
157 
158 /**
159  * Returns the override value associated with the indicated stage.
160  */
161 INLINE int TextureAttrib::
163  Stages::const_iterator si;
164  si = _on_stages.find(StageNode(stage));
165  if (si != _on_stages.end()) {
166  return (*si)._override;
167  }
168  nassert_raise("Specified TextureStage not included in attrib");
169  return 0;
170 }
171 
172 /**
173  * Returns the number of stages that are turned off by the attribute.
174  */
175 INLINE int TextureAttrib::
176 get_num_off_stages() const {
177  return _off_stages.size();
178 }
179 
180 /**
181  * Returns the nth stage turned off by the attribute, sorted in arbitrary
182  * (pointer) order.
183  */
185 get_off_stage(int n) const {
186  nassertr(n >= 0 && n < (int)_off_stages.size(), nullptr);
187  return _off_stages[n]._stage;
188 }
189 
190 /**
191  * Returns true if the indicated stage is turned off by the attrib, false
192  * otherwise.
193  */
194 INLINE bool TextureAttrib::
196  return _off_stages.find(StageNode(stage)) != _off_stages.end() ||
197  (_off_all_stages && !has_on_stage(stage));
198 }
199 
200 /**
201  * Returns true if this attrib turns off all stages (although it may also turn
202  * some on).
203  */
204 INLINE bool TextureAttrib::
205 has_all_off() const {
206  return _off_all_stages;
207 }
208 
209 /**
210  * Returns true if this is an identity attrib: it does not change the set of
211  * stages in use.
212  */
213 INLINE bool TextureAttrib::
214 is_identity() const {
215  return _on_stages.empty() && _off_stages.empty() && !_off_all_stages;
216 }
217 
218 /**
219  * Confirms whether the _on_stages list is still sorted. It will become
220  * unsorted if someone calls TextureStage::set_sort().
221  *
222  * If the list requires sorting, transparently sorts it before returning.
223  */
224 INLINE void TextureAttrib::
225 check_sorted() const {
226  if (_sort_seq != TextureStage::get_sort_seq()) {
227  ((TextureAttrib *)this)->sort_on_stages();
228  }
229 }
230 
231 /**
232  *
233  */
234 INLINE TextureAttrib::StageNode::
235 StageNode(const TextureStage *stage, unsigned int implicit_sort, int override) :
236  // Yeah, we cast away the constness here. Just too much trouble to deal
237  // with it properly.
238  _stage((TextureStage *)stage),
239  _implicit_sort(implicit_sort),
240  _override(override),
241  _has_sampler(false)
242 {
243 }
244 
245 /**
246  * This STL function object is used to sort a list of texture stages in
247  * reverse order by priority, and within priority, within order by sort.
248  */
249 INLINE bool TextureAttrib::CompareTextureStagePriorities::
250 operator () (const TextureAttrib::StageNode *a,
251  const TextureAttrib::StageNode *b) const {
252  if (a->_stage->get_priority() != b->_stage->get_priority()) {
253  return a->_stage->get_priority() > b->_stage->get_priority();
254  }
255  if (a->_stage->get_sort() != b->_stage->get_sort()) {
256  return a->_stage->get_sort() < b->_stage->get_sort();
257  }
258  return a->_implicit_sort < b->_implicit_sort;
259 }
260 
261 /**
262  * This STL function object is used to sort a list of texture stages in order
263  * by sort.
264  */
265 INLINE bool TextureAttrib::CompareTextureStageSort::
266 operator () (const TextureAttrib::StageNode *a,
267  const TextureAttrib::StageNode *b) const {
268  if (a->_stage->get_sort() != b->_stage->get_sort()) {
269  return a->_stage->get_sort() < b->_stage->get_sort();
270  }
271  return a->_implicit_sort < b->_implicit_sort;
272 }
273 
274 /**
275  * This STL function object is used to sort a list of texture stages in order
276  * by pointer.
277  */
278 INLINE bool TextureAttrib::CompareTextureStagePointer::
279 operator () (const TextureAttrib::StageNode &a,
280  const TextureAttrib::StageNode &b) const {
281  return a._stage < b._stage;
282 }
bool is_identity() const
Returns true if this is an identity attrib: it does not change the set of stages in use.
static UpdateSeq get_sort_seq()
Returns a global sequence number that is incremented any time any TextureStage in the world changes s...
Definition: textureStage.I:675
int get_ff_tc_index(int n) const
For each TextureStage listed in get_on_ff_stage(), this returns a unique index number for the texture...
size_type_0 size() const
Returns the number of elements in the ordered vector.
get_on_stage
Returns the nth stage turned on by the attribute, sorted in render order.
Definition: textureAttrib.h:55
get_off_stage
Returns the nth stage turned off by the attribute, sorted in arbitrary (pointer) order.
Definition: textureAttrib.h:77
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
get_on_sampler
Returns the sampler associated with the indicated stage, or the one associated with its texture if no...
Definition: textureAttrib.h:72
iterator_0 end()
Returns the iterator that marks the end of the ordered vector.
bool empty() const
Returns true if the ordered vector is empty, false otherwise.
Indicates the set of TextureStages and their associated Textures that should be applied to (or remove...
Definition: textureAttrib.h:31
has_on_stage
Returns true if the indicated stage is turned on by the attrib, false otherwise.
Definition: textureAttrib.h:69
Texture * get_texture() const
If the TextureAttrib is not an 'off' TextureAttrib, returns the base-level texture that is associated...
Definition: textureAttrib.I:61
bool has_all_off() const
Returns true if this attrib turns off all stages (although it may also turn some on).
get_on_ff_stage
Returns the nth stage turned on by the attribute, sorted in render order, including only those releva...
Definition: textureAttrib.h:58
bool is_off() const
Returns true if the TextureAttrib is an 'off' TextureAttrib, indicating that it should disable textur...
Definition: textureAttrib.I:52
Represents a set of settings that indicate how a texture is sampled.
Definition: samplerState.h:36
static const SamplerState & get_default()
Returns a reference to the global default immutable SamplerState object.
Definition: samplerState.I:36
bool has_off_stage(TextureStage *stage) const
Returns true if the indicated stage is turned off by the attrib, false otherwise.
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
get_on_texture
Returns the texture associated with the indicated stage, or NULL if no texture is associated.
Definition: textureAttrib.h:69
int get_on_stage_override(TextureStage *stage) const
Returns the override value associated with the indicated stage.