Panda3D

Loading...
Searching...
No Matches
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 */
23TextureStageCollection::
24TextureStageCollection() {
25}
26
27/**
28 *
29 */
30TextureStageCollection::
31TextureStageCollection(const TextureStageCollection &copy) :
32 _texture_stages(copy._texture_stages)
33{
34}
35
36/**
37 *
38 */
39 void TextureStageCollection::
40operator = (const TextureStageCollection &copy) {
41 _texture_stages = copy._texture_stages;
42}
43
44/**
45 * Adds a new TextureStage to the collection.
46 */
47 void TextureStageCollection::
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 */
67 bool TextureStageCollection::
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 */
100 void TextureStageCollection::
101 add_texture_stages_from(const TextureStageCollection &other) {
102 int other_num_texture_stages = other.get_num_texture_stages();
103 for (int i = 0; i < other_num_texture_stages; i++) {
104 add_texture_stage(other.get_texture_stage(i));
105 }
106}
107
108
109/**
110 * Removes from this collection all of the TextureStages listed in the other
111 * collection.
112 */
113 void TextureStageCollection::
114 remove_texture_stages_from(const TextureStageCollection &other) {
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 */
131 void TextureStageCollection::
132 remove_duplicate_texture_stages() {
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 */
156 bool TextureStageCollection::
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 */
169 void TextureStageCollection::
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 */
178 TextureStage *TextureStageCollection::
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 */
201 TextureStage *TextureStageCollection::
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 */
212 TextureStage *TextureStageCollection::
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 */
223 int TextureStageCollection::
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 */
232 void TextureStageCollection::
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 */
242 void TextureStageCollection::
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 */
255 void TextureStageCollection::
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}
TextureStageCollection
Definition textureStageCollection.h:24
TextureStageCollection::remove_duplicate_texture_stages
void remove_duplicate_texture_stages()
Removes any duplicate entries of the same TextureStages on this collection.
Definition textureStageCollection.cxx:132
TextureStageCollection::sort
void sort()
Sorts the TextureStages in this collection into order by TextureStage::sort(), from lowest to highest...
Definition textureStageCollection.cxx:233
TextureStageCollection::remove_texture_stages_from
void remove_texture_stages_from(const TextureStageCollection &other)
Removes from this collection all of the TextureStages listed in the other collection.
Definition textureStageCollection.cxx:114
TextureStageCollection::size
int size() const
Returns the number of texture stages in the collection.
Definition textureStageCollection.cxx:224
TextureStageCollection::has_texture_stage
bool has_texture_stage(TextureStage *texture_stage) const
Returns true if the indicated TextureStage appears in this collection, false otherwise.
Definition textureStageCollection.cxx:157
TextureStageCollection::get_texture_stage
get_texture_stage
Returns the nth TextureStage in the collection.
Definition textureStageCollection.h:43
TextureStageCollection::write
void write(std::ostream &out, int indent_level=0) const
Writes a complete multi-line description of the TextureStageCollection to the indicated output stream...
Definition textureStageCollection.cxx:256
TextureStageCollection::add_texture_stages_from
void add_texture_stages_from(const TextureStageCollection &other)
Adds all the TextureStages indicated in the other collection to this texture_stage.
Definition textureStageCollection.cxx:101
TextureStageCollection::remove_texture_stage
bool remove_texture_stage(TextureStage *node_texture_stage)
Removes the indicated TextureStage from the collection.
Definition textureStageCollection.cxx:68
TextureStageCollection::output
void output(std::ostream &out) const
Writes a brief one-line description of the TextureStageCollection to the indicated output stream.
Definition textureStageCollection.cxx:243
TextureStageCollection::clear
void clear()
Removes all TextureStages from the collection.
Definition textureStageCollection.cxx:170
TextureStageCollection::find_texture_stage
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...
Definition textureStageCollection.cxx:179
TextureStageCollection::operator[]
TextureStage * operator[](int index) const
Returns the nth TextureStage in the collection.
Definition textureStageCollection.cxx:213
TextureStageCollection::get_num_texture_stages
get_num_texture_stages
Returns the number of TextureStages in the collection.
Definition textureStageCollection.h:43
TextureStageCollection::add_texture_stage
void add_texture_stage(TextureStage *node_texture_stage)
Adds a new TextureStage to the collection.
Definition textureStageCollection.cxx:48
TextureStage
Defines the properties of a named stage of the multitexture pipeline.
Definition textureStage.h:35
TextureStage::get_name
get_name
Returns the name of this texture stage.
Definition textureStage.h:194
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
indent.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
indirectLess.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
textureStageCollection.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.