Panda3D
textureStageCollection.cxx
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 textureStageCollection.cxx
10  * @author drose
11  * @date 2004-07-23
12  */
13 
14 #include "textureStageCollection.h"
15 
16 #include "indent.h"
17 #include "indirectLess.h"
18 #include <algorithm>
19 
20 /**
21  *
22  */
23 TextureStageCollection::
24 TextureStageCollection() {
25 }
26 
27 /**
28  *
29  */
30 TextureStageCollection::
31 TextureStageCollection(const TextureStageCollection &copy) :
32  _texture_stages(copy._texture_stages)
33 {
34 }
35 
36 /**
37  *
38  */
39 void TextureStageCollection::
40 operator = (const TextureStageCollection &copy) {
41  _texture_stages = copy._texture_stages;
42 }
43 
44 /**
45  * Adds a new TextureStage to the collection.
46  */
48 add_texture_stage(TextureStage *node_texture_stage) {
49  // If the pointer to our internal array is shared by any other
50  // TextureStageCollections, we have to copy the array now so we won't
51  // inadvertently modify any of our brethren TextureStageCollection objects.
52 
53  if (_texture_stages.get_ref_count() > 1) {
54  TextureStages old_texture_stages = _texture_stages;
55  _texture_stages = TextureStages::empty_array(0);
56  _texture_stages.v() = old_texture_stages.v();
57  }
58 
59  _texture_stages.push_back(node_texture_stage);
60 }
61 
62 /**
63  * Removes the indicated TextureStage from the collection. Returns true if
64  * the texture_stage was removed, false if it was not a member of the
65  * collection.
66  */
68 remove_texture_stage(TextureStage *node_texture_stage) {
69  int texture_stage_index = -1;
70  for (int i = 0; texture_stage_index == -1 && i < (int)_texture_stages.size(); i++) {
71  if (_texture_stages[i] == node_texture_stage) {
72  texture_stage_index = i;
73  }
74  }
75 
76  if (texture_stage_index == -1) {
77  // The indicated texture_stage was not a member of the collection.
78  return false;
79  }
80 
81  // If the pointer to our internal array is shared by any other
82  // TextureStageCollections, we have to copy the array now so we won't
83  // inadvertently modify any of our brethren TextureStageCollection objects.
84 
85  if (_texture_stages.get_ref_count() > 1) {
86  TextureStages old_texture_stages = _texture_stages;
87  _texture_stages = TextureStages::empty_array(0);
88  _texture_stages.v() = old_texture_stages.v();
89  }
90 
91  _texture_stages.erase(_texture_stages.begin() + texture_stage_index);
92  return true;
93 }
94 
95 /**
96  * Adds all the TextureStages indicated in the other collection to this
97  * texture_stage. The other texture_stages are simply appended to the end of
98  * the texture_stages in this list; duplicates are not automatically removed.
99  */
102  int other_num_texture_stages = other.get_num_texture_stages();
103  for (int i = 0; i < other_num_texture_stages; i++) {
105  }
106 }
107 
108 
109 /**
110  * Removes from this collection all of the TextureStages listed in the other
111  * collection.
112  */
115  TextureStages new_texture_stages;
116  int num_texture_stages = get_num_texture_stages();
117  for (int i = 0; i < num_texture_stages; i++) {
118  PT(TextureStage) texture_stage = get_texture_stage(i);
119  if (!other.has_texture_stage(texture_stage)) {
120  new_texture_stages.push_back(texture_stage);
121  }
122  }
123  _texture_stages = new_texture_stages;
124 }
125 
126 /**
127  * Removes any duplicate entries of the same TextureStages on this collection.
128  * If a TextureStage appears multiple times, the first appearance is retained;
129  * subsequent appearances are removed.
130  */
133  TextureStages new_texture_stages;
134 
135  int num_texture_stages = get_num_texture_stages();
136  for (int i = 0; i < num_texture_stages; i++) {
137  PT(TextureStage) texture_stage = get_texture_stage(i);
138  bool duplicated = false;
139 
140  for (int j = 0; j < i && !duplicated; j++) {
141  duplicated = (texture_stage == get_texture_stage(j));
142  }
143 
144  if (!duplicated) {
145  new_texture_stages.push_back(texture_stage);
146  }
147  }
148 
149  _texture_stages = new_texture_stages;
150 }
151 
152 /**
153  * Returns true if the indicated TextureStage appears in this collection,
154  * false otherwise.
155  */
157 has_texture_stage(TextureStage *texture_stage) const {
158  for (int i = 0; i < get_num_texture_stages(); i++) {
159  if (texture_stage == get_texture_stage(i)) {
160  return true;
161  }
162  }
163  return false;
164 }
165 
166 /**
167  * Removes all TextureStages from the collection.
168  */
170 clear() {
171  _texture_stages.clear();
172 }
173 
174 /**
175  * Returns the texture_stage in the collection with the indicated name, if
176  * any, or NULL if no texture_stage has that name.
177  */
179 find_texture_stage(const std::string &name) const {
180  int num_texture_stages = get_num_texture_stages();
181  for (int i = 0; i < num_texture_stages; i++) {
182  TextureStage *texture_stage = get_texture_stage(i);
183  if (texture_stage->get_name() == name) {
184  return texture_stage;
185  }
186  }
187  return nullptr;
188 }
189 
190 /**
191  * Returns the number of TextureStages in the collection.
192  */
193 int TextureStageCollection::
194 get_num_texture_stages() const {
195  return _texture_stages.size();
196 }
197 
198 /**
199  * Returns the nth TextureStage in the collection.
200  */
202 get_texture_stage(int index) const {
203  nassertr(index >= 0 && index < (int)_texture_stages.size(), nullptr);
204 
205  return _texture_stages[index];
206 }
207 
208 /**
209  * Returns the nth TextureStage in the collection. This is the same as
210  * get_texture_stage(), but it may be a more convenient way to access it.
211  */
213 operator [] (int index) const {
214  nassertr(index >= 0 && index < (int)_texture_stages.size(), nullptr);
215 
216  return _texture_stages[index];
217 }
218 
219 /**
220  * Returns the number of texture stages in the collection. This is the same
221  * thing as get_num_texture_stages().
222  */
224 size() const {
225  return _texture_stages.size();
226 }
227 
228 /**
229  * Sorts the TextureStages in this collection into order by
230  * TextureStage::sort(), from lowest to highest.
231  */
233 sort() {
234  std::sort(_texture_stages.begin(), _texture_stages.end(),
235  CompareTextureStageSort());
236 }
237 
238 /**
239  * Writes a brief one-line description of the TextureStageCollection to the
240  * indicated output stream.
241  */
243 output(std::ostream &out) const {
244  if (get_num_texture_stages() == 1) {
245  out << "1 TextureStage";
246  } else {
247  out << get_num_texture_stages() << " TextureStages";
248  }
249 }
250 
251 /**
252  * Writes a complete multi-line description of the TextureStageCollection to
253  * the indicated output stream.
254  */
256 write(std::ostream &out, int indent_level) const {
257  for (int i = 0; i < get_num_texture_stages(); i++) {
258  indent(out, indent_level) << *get_texture_stage(i) << "\n";
259  }
260 }
void add_texture_stage(TextureStage *node_texture_stage)
Adds a new TextureStage to the collection.
void clear()
Removes all TextureStages from the collection.
get_name
Returns the name of this texture stage.
Definition: textureStage.h:188
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void remove_duplicate_texture_stages()
Removes any duplicate entries of the same TextureStages on this collection.
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.
TextureStage * find_texture_stage(const std::string &name) const
Returns the texture_stage in the collection with the indicated name, if any, or NULL if no texture_st...
get_num_texture_stages
Returns the number of TextureStages in the collection.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
void output(std::ostream &out) const
Writes a brief one-line description of the TextureStageCollection to the indicated output stream.
void write(std::ostream &out, int indent_level=0) const
Writes a complete multi-line description of the TextureStageCollection to the indicated output stream...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TextureStage * operator [](int index) const
Returns the nth TextureStage in the collection.
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.
get_texture_stage
Returns the nth TextureStage in the collection.
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int size() const
Returns the number of texture stages in the collection.