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