Panda3D
textureStageCollection.cxx
1 // Filename: textureStageCollection.cxx
2 // Created by: drose (23Jul04)
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 #include "textureStageCollection.h"
16 
17 #include "indent.h"
18 #include "indirectLess.h"
19 #include <algorithm>
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: TextureStageCollection::Constructor
23 // Access: Published
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 TextureStageCollection::
27 TextureStageCollection() {
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: TextureStageCollection::Copy Constructor
32 // Access: Published
33 // Description:
34 ////////////////////////////////////////////////////////////////////
35 TextureStageCollection::
36 TextureStageCollection(const TextureStageCollection &copy) :
37  _texture_stages(copy._texture_stages)
38 {
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: TextureStageCollection::Copy Assignment Operator
43 // Access: Published
44 // Description:
45 ////////////////////////////////////////////////////////////////////
46 void TextureStageCollection::
47 operator = (const TextureStageCollection &copy) {
48  _texture_stages = copy._texture_stages;
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: TextureStageCollection::add_texture_stage
53 // Access: Published
54 // Description: Adds a new TextureStage to the collection.
55 ////////////////////////////////////////////////////////////////////
57 add_texture_stage(TextureStage *node_texture_stage) {
58  // If the pointer to our internal array is shared by any other
59  // TextureStageCollections, we have to copy the array now so we won't
60  // inadvertently modify any of our brethren TextureStageCollection
61  // objects.
62 
63  if (_texture_stages.get_ref_count() > 1) {
64  TextureStages old_texture_stages = _texture_stages;
65  _texture_stages = TextureStages::empty_array(0);
66  _texture_stages.v() = old_texture_stages.v();
67  }
68 
69  _texture_stages.push_back(node_texture_stage);
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: TextureStageCollection::remove_texture_stage
74 // Access: Published
75 // Description: Removes the indicated TextureStage from the collection.
76 // Returns true if the texture_stage was removed, false if it was
77 // not a member of the collection.
78 ////////////////////////////////////////////////////////////////////
80 remove_texture_stage(TextureStage *node_texture_stage) {
81  int texture_stage_index = -1;
82  for (int i = 0; texture_stage_index == -1 && i < (int)_texture_stages.size(); i++) {
83  if (_texture_stages[i] == node_texture_stage) {
84  texture_stage_index = i;
85  }
86  }
87 
88  if (texture_stage_index == -1) {
89  // The indicated texture_stage was not a member of the collection.
90  return false;
91  }
92 
93  // If the pointer to our internal array is shared by any other
94  // TextureStageCollections, we have to copy the array now so we won't
95  // inadvertently modify any of our brethren TextureStageCollection
96  // objects.
97 
98  if (_texture_stages.get_ref_count() > 1) {
99  TextureStages old_texture_stages = _texture_stages;
100  _texture_stages = TextureStages::empty_array(0);
101  _texture_stages.v() = old_texture_stages.v();
102  }
103 
104  _texture_stages.erase(_texture_stages.begin() + texture_stage_index);
105  return true;
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: TextureStageCollection::add_texture_stages_from
110 // Access: Published
111 // Description: Adds all the TextureStages indicated in the other
112 // collection to this texture_stage. The other texture_stages are simply
113 // appended to the end of the texture_stages in this list;
114 // duplicates are not automatically removed.
115 ////////////////////////////////////////////////////////////////////
118  int other_num_texture_stages = other.get_num_texture_stages();
119  for (int i = 0; i < other_num_texture_stages; i++) {
121  }
122 }
123 
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: TextureStageCollection::remove_texture_stages_from
127 // Access: Published
128 // Description: Removes from this collection all of the TextureStages
129 // listed in the other collection.
130 ////////////////////////////////////////////////////////////////////
133  TextureStages new_texture_stages;
134  int num_texture_stages = get_num_texture_stages();
135  for (int i = 0; i < num_texture_stages; i++) {
136  PT(TextureStage) texture_stage = get_texture_stage(i);
137  if (!other.has_texture_stage(texture_stage)) {
138  new_texture_stages.push_back(texture_stage);
139  }
140  }
141  _texture_stages = new_texture_stages;
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: TextureStageCollection::remove_duplicate_texture_stages
146 // Access: Published
147 // Description: Removes any duplicate entries of the same TextureStages
148 // on this collection. If a TextureStage appears multiple
149 // times, the first appearance is retained; subsequent
150 // appearances are removed.
151 ////////////////////////////////////////////////////////////////////
154  TextureStages new_texture_stages;
155 
156  int num_texture_stages = get_num_texture_stages();
157  for (int i = 0; i < num_texture_stages; i++) {
158  PT(TextureStage) texture_stage = get_texture_stage(i);
159  bool duplicated = false;
160 
161  for (int j = 0; j < i && !duplicated; j++) {
162  duplicated = (texture_stage == get_texture_stage(j));
163  }
164 
165  if (!duplicated) {
166  new_texture_stages.push_back(texture_stage);
167  }
168  }
169 
170  _texture_stages = new_texture_stages;
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function: TextureStageCollection::has_texture_stage
175 // Access: Published
176 // Description: Returns true if the indicated TextureStage appears in
177 // this collection, false otherwise.
178 ////////////////////////////////////////////////////////////////////
180 has_texture_stage(TextureStage *texture_stage) const {
181  for (int i = 0; i < get_num_texture_stages(); i++) {
182  if (texture_stage == get_texture_stage(i)) {
183  return true;
184  }
185  }
186  return false;
187 }
188 
189 ////////////////////////////////////////////////////////////////////
190 // Function: TextureStageCollection::clear
191 // Access: Published
192 // Description: Removes all TextureStages from the collection.
193 ////////////////////////////////////////////////////////////////////
195 clear() {
196  _texture_stages.clear();
197 }
198 
199 ////////////////////////////////////////////////////////////////////
200 // Function: TextureStageCollection::find_texture_stage
201 // Access: Published
202 // Description: Returns the texture_stage in the collection with the
203 // indicated name, if any, or NULL if no texture_stage has
204 // that name.
205 ////////////////////////////////////////////////////////////////////
207 find_texture_stage(const string &name) const {
208  int num_texture_stages = get_num_texture_stages();
209  for (int i = 0; i < num_texture_stages; i++) {
210  TextureStage *texture_stage = get_texture_stage(i);
211  if (texture_stage->get_name() == name) {
212  return texture_stage;
213  }
214  }
215  return NULL;
216 }
217 
218 ////////////////////////////////////////////////////////////////////
219 // Function: TextureStageCollection::get_num_texture_stages
220 // Access: Published
221 // Description: Returns the number of TextureStages in the collection.
222 ////////////////////////////////////////////////////////////////////
225  return _texture_stages.size();
226 }
227 
228 ////////////////////////////////////////////////////////////////////
229 // Function: TextureStageCollection::get_texture_stage
230 // Access: Published
231 // Description: Returns the nth TextureStage in the collection.
232 ////////////////////////////////////////////////////////////////////
234 get_texture_stage(int index) const {
235  nassertr(index >= 0 && index < (int)_texture_stages.size(), NULL);
236 
237  return _texture_stages[index];
238 }
239 
240 ////////////////////////////////////////////////////////////////////
241 // Function: TextureStageCollection::operator []
242 // Access: Published
243 // Description: Returns the nth TextureStage in the collection. This is
244 // the same as get_texture_stage(), but it may be a more
245 // convenient way to access it.
246 ////////////////////////////////////////////////////////////////////
248 operator [] (int index) const {
249  nassertr(index >= 0 && index < (int)_texture_stages.size(), NULL);
250 
251  return _texture_stages[index];
252 }
253 
254 ////////////////////////////////////////////////////////////////////
255 // Function: TextureStageCollection::size
256 // Access: Published
257 // Description: Returns the number of texture stages in the
258 // collection. This is the same thing as
259 // get_num_texture_stages().
260 ////////////////////////////////////////////////////////////////////
262 size() const {
263  return _texture_stages.size();
264 }
265 
266 ////////////////////////////////////////////////////////////////////
267 // Function: TextureStageCollection::sort
268 // Access: Published
269 // Description: Sorts the TextureStages in this collection into order
270 // by TextureStage::sort(), from lowest to highest.
271 ////////////////////////////////////////////////////////////////////
273 sort() {
274  ::sort(_texture_stages.begin(), _texture_stages.end(),
275  CompareTextureStageSort());
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: TextureStageCollection::output
280 // Access: Published
281 // Description: Writes a brief one-line description of the
282 // TextureStageCollection to the indicated output stream.
283 ////////////////////////////////////////////////////////////////////
285 output(ostream &out) const {
286  if (get_num_texture_stages() == 1) {
287  out << "1 TextureStage";
288  } else {
289  out << get_num_texture_stages() << " TextureStages";
290  }
291 }
292 
293 ////////////////////////////////////////////////////////////////////
294 // Function: TextureStageCollection::write
295 // Access: Published
296 // Description: Writes a complete multi-line description of the
297 // TextureStageCollection to the indicated output stream.
298 ////////////////////////////////////////////////////////////////////
300 write(ostream &out, int indent_level) const {
301  for (int i = 0; i < get_num_texture_stages(); i++) {
302  indent(out, indent_level) << *get_texture_stage(i) << "\n";
303  }
304 }
void add_texture_stage(TextureStage *node_texture_stage)
Adds a new TextureStage to the collection.
TextureStage * find_texture_stage(const string &name) const
Returns the texture_stage in the collection with the indicated name, if any, or NULL if no texture_st...
TextureStage * get_texture_stage(int index) const
Returns the nth TextureStage in the collection.
void clear()
Removes all TextureStages from the collection.
int get_num_texture_stages() const
Returns the number of TextureStages in the collection.
void remove_duplicate_texture_stages()
Removes any duplicate entries of the same TextureStages on this collection.
void output(ostream &out) const
Writes a brief one-line description of the TextureStageCollection to the indicated output stream...
void add_texture_stages_from(const TextureStageCollection &other)
Adds all the TextureStages indicated in the other collection to this texture_stage.
bool remove_texture_stage(TextureStage *node_texture_stage)
Removes the indicated TextureStage from the collection.
void remove_texture_stages_from(const TextureStageCollection &other)
Removes from this collection all of the TextureStages listed in the other collection.
void write(ostream &out, int indent_level=0) const
Writes a complete multi-line description of the TextureStageCollection to the indicated output stream...
TextureStage * operator[](int index) const
Returns the nth TextureStage in the collection.
const string & get_name() const
Returns the name of this texture stage.
Definition: textureStage.I:32
void sort()
Sorts the TextureStages in this collection into order by TextureStage::sort(), from lowest to highest...
bool has_texture_stage(TextureStage *texture_stage) const
Returns true if the indicated TextureStage appears in this collection, false otherwise.
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:38
int size() const
Returns the number of texture stages in the collection.