Panda3D

textureAttrib.I

00001 // Filename: textureAttrib.I
00002 // Created by:  drose (21Feb02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: TextureAttrib::Constructor
00018 //       Access: Protected
00019 //  Description: Use TextureAttrib::make() to construct a new
00020 //               TextureAttrib object.
00021 ////////////////////////////////////////////////////////////////////
00022 INLINE TextureAttrib::
00023 TextureAttrib() {
00024   _next_implicit_sort = 0;
00025   _off_all_stages = false;
00026   _sort_seq = UpdateSeq::old();
00027   _filtered_seq = UpdateSeq::old();
00028 }
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: TextureAttrib::Copy Constructor
00032 //       Access: Protected
00033 //  Description: Use TextureAttrib::make() to construct a new
00034 //               TextureAttrib object.  The copy constructor is only
00035 //               defined to facilitate methods like add_on_stage().
00036 ////////////////////////////////////////////////////////////////////
00037 INLINE TextureAttrib::
00038 TextureAttrib(const TextureAttrib &copy) :
00039   _on_stages(copy._on_stages),
00040   _render_stages(copy._render_stages),
00041   _render_ff_stages(copy._render_ff_stages),
00042   _next_implicit_sort(copy._next_implicit_sort),
00043   _off_stages(copy._off_stages),
00044   _off_all_stages(copy._off_all_stages),
00045   _sort_seq(copy._sort_seq),
00046   _filtered_seq(UpdateSeq::old())
00047 {
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: TextureAttrib::is_off
00052 //       Access: Published
00053 //  Description: Returns true if the TextureAttrib is an 'off'
00054 //               TextureAttrib, indicating that it should disable
00055 //               texturing.
00056 //
00057 //               If multitexture is in effect, a TextureAttrib may not
00058 //               be strictly "on" or "off"; therefore, to get a more
00059 //               precise answer to this question, you should consider
00060 //               using has_all_off() or get_num_off_stages() or
00061 //               has_off_stage() instead.
00062 ////////////////////////////////////////////////////////////////////
00063 INLINE bool TextureAttrib::
00064 is_off() const {
00065   return (_on_stages.empty());
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: TextureAttrib::get_texture
00070 //       Access: Published
00071 //  Description: If the TextureAttrib is not an 'off' TextureAttrib,
00072 //               returns the base-level texture that is associated.
00073 //               Otherwise, return NULL.
00074 ////////////////////////////////////////////////////////////////////
00075 INLINE Texture *TextureAttrib::
00076 get_texture() const {
00077   if (_on_stages.empty()) {
00078     return NULL;
00079   }
00080   check_sorted();
00081   return get_on_texture(filter_to_max(1)->get_on_stage(0));
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: TextureAttrib::get_num_on_stages
00086 //       Access: Published
00087 //  Description: Returns the number of stages that are turned on by
00088 //               the attribute.
00089 ////////////////////////////////////////////////////////////////////
00090 INLINE int TextureAttrib::
00091 get_num_on_stages() const {
00092   check_sorted();
00093   return _render_stages.size();
00094 }
00095 
00096 ////////////////////////////////////////////////////////////////////
00097 //     Function: TextureAttrib::get_on_stage
00098 //       Access: Published
00099 //  Description: Returns the nth stage turned on by the attribute,
00100 //               sorted in render order.
00101 ////////////////////////////////////////////////////////////////////
00102 INLINE TextureStage *TextureAttrib::
00103 get_on_stage(int n) const {
00104   nassertr(n >= 0 && n < (int)_render_stages.size(), (TextureStage *)NULL);
00105   return _render_stages[n]->_stage;
00106 }
00107 
00108 ////////////////////////////////////////////////////////////////////
00109 //     Function: TextureAttrib::get_num_on_ff_stages
00110 //       Access: Published
00111 //  Description: Returns the number of on-stages that are relevant
00112 //               to the classic fixed function pipeline.  This excludes 
00113 //               texture stages such as normal maps.
00114 ////////////////////////////////////////////////////////////////////
00115 INLINE int TextureAttrib::
00116 get_num_on_ff_stages() const {
00117   check_sorted();
00118   return _render_ff_stages.size();
00119 }
00120 
00121 ////////////////////////////////////////////////////////////////////
00122 //     Function: TextureAttrib::get_render_ff_stage
00123 //       Access: Published
00124 //  Description: Returns the nth stage turned on by the attribute,
00125 //               sorted in render order, including only those relevant
00126 //               to the classic fixed function pipeline.  This excludes 
00127 //               texture stages such as normal maps.
00128 ////////////////////////////////////////////////////////////////////
00129 INLINE TextureStage *TextureAttrib::
00130 get_on_ff_stage(int n) const {
00131   nassertr(n >= 0 && n < (int)_render_ff_stages.size(), (TextureStage *)NULL);
00132   return _render_ff_stages[n]->_stage;
00133 }
00134 
00135 ////////////////////////////////////////////////////////////////////
00136 //     Function: TextureAttrib::get_ff_tc_index
00137 //       Access: Published
00138 //  Description: For each TextureStage listed in get_on_ff_stage(),
00139 //               this returns a unique index number for the texture
00140 //               coordinate name used by that TextureStage.  It is
00141 //               guaranteed to remain the same index number for each
00142 //               texcoord name (for a given set of TextureStages),
00143 //               even if the texture render order changes.
00144 ////////////////////////////////////////////////////////////////////
00145 INLINE int TextureAttrib::
00146 get_ff_tc_index(int n) const {
00147   nassertr(n >= 0 && n < (int)_render_ff_stages.size(), -1);
00148   return _render_ff_stages[n]->_ff_tc_index;
00149 }
00150 
00151 ////////////////////////////////////////////////////////////////////
00152 //     Function: TextureAttrib::has_on_stage
00153 //       Access: Published
00154 //  Description: Returns true if the indicated stage is turned on by
00155 //               the attrib, false otherwise.
00156 ////////////////////////////////////////////////////////////////////
00157 INLINE bool TextureAttrib::
00158 has_on_stage(TextureStage *stage) const {
00159   return _on_stages.find(StageNode(stage)) != _on_stages.end();
00160 }
00161 
00162 ////////////////////////////////////////////////////////////////////
00163 //     Function: TextureAttrib::get_on_texture
00164 //       Access: Published
00165 //  Description: Returns the texture associated with the indicated
00166 //               stage, or NULL if no texture is associated.
00167 ////////////////////////////////////////////////////////////////////
00168 INLINE Texture *TextureAttrib::
00169 get_on_texture(TextureStage *stage) const {
00170   Stages::const_iterator si;
00171   si = _on_stages.find(StageNode(stage));
00172   if (si != _on_stages.end()) {
00173     return (*si)._texture;
00174   }
00175   return NULL;
00176 }
00177 
00178 ////////////////////////////////////////////////////////////////////
00179 //     Function: TextureAttrib::get_on_stage_override
00180 //       Access: Published
00181 //  Description: Returns the override value associated with the
00182 //               indicated stage.
00183 ////////////////////////////////////////////////////////////////////
00184 INLINE int TextureAttrib::
00185 get_on_stage_override(TextureStage *stage) const {
00186   Stages::const_iterator si;
00187   si = _on_stages.find(StageNode(stage));
00188   if (si != _on_stages.end()) {
00189     return (*si)._override;
00190   }
00191   nassert_raise("Specified TextureStage not included in attrib");
00192   return 0;
00193 }
00194 
00195 ////////////////////////////////////////////////////////////////////
00196 //     Function: TextureAttrib::get_num_off_stages
00197 //       Access: Published
00198 //  Description: Returns the number of stages that are turned off by
00199 //               the attribute.
00200 ////////////////////////////////////////////////////////////////////
00201 INLINE int TextureAttrib::
00202 get_num_off_stages() const {
00203   return _off_stages.size();
00204 }
00205 
00206 ////////////////////////////////////////////////////////////////////
00207 //     Function: TextureAttrib::get_off_stage
00208 //       Access: Published
00209 //  Description: Returns the nth stage turned off by the attribute,
00210 //               sorted in arbitrary (pointer) order.
00211 ////////////////////////////////////////////////////////////////////
00212 INLINE TextureStage *TextureAttrib::
00213 get_off_stage(int n) const {
00214   nassertr(n >= 0 && n < (int)_off_stages.size(), (TextureStage *)NULL);
00215   return _off_stages[n]._stage;
00216 }
00217 
00218 ////////////////////////////////////////////////////////////////////
00219 //     Function: TextureAttrib::has_off_stage
00220 //       Access: Published
00221 //  Description: Returns true if the indicated stage is turned off by
00222 //               the attrib, false otherwise.
00223 ////////////////////////////////////////////////////////////////////
00224 INLINE bool TextureAttrib::
00225 has_off_stage(TextureStage *stage) const {
00226   return _off_stages.find(StageNode(stage)) != _off_stages.end() ||
00227     (_off_all_stages && !has_on_stage(stage));
00228 }
00229 
00230 ////////////////////////////////////////////////////////////////////
00231 //     Function: TextureAttrib::has_all_off
00232 //       Access: Published
00233 //  Description: Returns true if this attrib turns off all stages
00234 //               (although it may also turn some on).
00235 ////////////////////////////////////////////////////////////////////
00236 INLINE bool TextureAttrib::
00237 has_all_off() const {
00238   return _off_all_stages;
00239 }
00240 
00241 ////////////////////////////////////////////////////////////////////
00242 //     Function: TextureAttrib::is_identity
00243 //       Access: Published
00244 //  Description: Returns true if this is an identity attrib: it does
00245 //               not change the set of stages in use.
00246 ////////////////////////////////////////////////////////////////////
00247 INLINE bool TextureAttrib::
00248 is_identity() const {
00249   return _on_stages.empty() && _off_stages.empty() && !_off_all_stages;
00250 }
00251 
00252 ////////////////////////////////////////////////////////////////////
00253 //     Function: TextureAttrib::check_sorted
00254 //       Access: Private
00255 //  Description: Confirms whether the _on_stages list is still sorted.
00256 //               It will become unsorted if someone calls
00257 //               TextureStage::set_sort().
00258 //
00259 //               If the list requires sorting, transparently sorts it
00260 //               before returning.
00261 ////////////////////////////////////////////////////////////////////
00262 INLINE void TextureAttrib::
00263 check_sorted() const {
00264   if (_sort_seq != TextureStage::get_sort_seq()) {
00265     ((TextureAttrib *)this)->sort_on_stages();
00266   }
00267 }
00268 
00269 ////////////////////////////////////////////////////////////////////
00270 //     Function: TextureAttrib::StageNode::Constructor
00271 //       Access: Public
00272 //  Description: 
00273 ////////////////////////////////////////////////////////////////////
00274 INLINE TextureAttrib::StageNode::
00275 StageNode(const TextureStage *stage, unsigned int implicit_sort, int override) :
00276   // Yeah, we cast away the constness here.  Just too much trouble to
00277   // deal with it properly.
00278   _stage((TextureStage *)stage),
00279   _implicit_sort(implicit_sort),
00280   _override(override)
00281 {
00282 }
00283 
00284 
00285 ////////////////////////////////////////////////////////////////////
00286 //     Function: TextureAttrib::CompareTextureStagePriorities::operator ()
00287 //       Access: Public
00288 //  Description: This STL function object is used to sort a list of
00289 //               texture stages in reverse order by priority, and
00290 //               within priority, within order by sort.
00291 ////////////////////////////////////////////////////////////////////
00292 INLINE bool TextureAttrib::CompareTextureStagePriorities::
00293 operator () (const TextureAttrib::StageNode *a, 
00294              const TextureAttrib::StageNode *b) const {
00295   if (a->_stage->get_priority() != b->_stage->get_priority()) {
00296     return a->_stage->get_priority() > b->_stage->get_priority();
00297   }
00298   if (a->_stage->get_sort() != b->_stage->get_sort()) {
00299     return a->_stage->get_sort() < b->_stage->get_sort();
00300   }
00301   return a->_implicit_sort < b->_implicit_sort;
00302 }
00303 
00304 ////////////////////////////////////////////////////////////////////
00305 //     Function: TextureAttrib::CompareTextureStageSort::operator ()
00306 //       Access: Public
00307 //  Description: This STL function object is used to sort a list of
00308 //               texture stages in order by sort.
00309 ////////////////////////////////////////////////////////////////////
00310 INLINE bool TextureAttrib::CompareTextureStageSort::
00311 operator () (const TextureAttrib::StageNode *a, 
00312              const TextureAttrib::StageNode *b) const {
00313   if (a->_stage->get_sort() != b->_stage->get_sort()) {
00314     return a->_stage->get_sort() < b->_stage->get_sort();
00315   }
00316   return a->_implicit_sort < b->_implicit_sort;
00317 }
00318 
00319 ////////////////////////////////////////////////////////////////////
00320 //     Function: TextureAttrib::CompareTextureStagePointer::operator ()
00321 //       Access: Public
00322 //  Description: This STL function object is used to sort a list of
00323 //               texture stages in order by pointer.
00324 ////////////////////////////////////////////////////////////////////
00325 INLINE bool TextureAttrib::CompareTextureStagePointer::
00326 operator () (const TextureAttrib::StageNode &a, 
00327              const TextureAttrib::StageNode &b) const {
00328   return a._stage < b._stage;
00329 }
 All Classes Functions Variables Enumerations