Panda3D
 All Classes Functions Variables Enumerations
materialCollection.cxx
1 // Filename: materialCollection.cxx
2 // Created by: drose (16Mar02)
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 "materialCollection.h"
16 
17 #include "indent.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: MaterialCollection::Constructor
21 // Access: Published
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 MaterialCollection::
25 MaterialCollection() {
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: MaterialCollection::Copy Constructor
30 // Access: Published
31 // Description:
32 ////////////////////////////////////////////////////////////////////
33 MaterialCollection::
34 MaterialCollection(const MaterialCollection &copy) :
35  _materials(copy._materials)
36 {
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: MaterialCollection::Copy Assignment Operator
41 // Access: Published
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 void MaterialCollection::
45 operator = (const MaterialCollection &copy) {
46  _materials = copy._materials;
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: MaterialCollection::add_material
51 // Access: Published
52 // Description: Adds a new Material to the collection.
53 ////////////////////////////////////////////////////////////////////
55 add_material(Material *node_material) {
56  // If the pointer to our internal array is shared by any other
57  // MaterialCollections, we have to copy the array now so we won't
58  // inadvertently modify any of our brethren MaterialCollection
59  // objects.
60 
61  if (_materials.get_ref_count() > 1) {
62  Materials old_materials = _materials;
63  _materials = Materials::empty_array(0);
64  _materials.v() = old_materials.v();
65  }
66 
67  _materials.push_back(node_material);
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: MaterialCollection::remove_material
72 // Access: Published
73 // Description: Removes the indicated Material from the collection.
74 // Returns true if the material was removed, false if it was
75 // not a member of the collection.
76 ////////////////////////////////////////////////////////////////////
78 remove_material(Material *node_material) {
79  int material_index = -1;
80  for (int i = 0; material_index == -1 && i < (int)_materials.size(); i++) {
81  if (_materials[i] == node_material) {
82  material_index = i;
83  }
84  }
85 
86  if (material_index == -1) {
87  // The indicated material was not a member of the collection.
88  return false;
89  }
90 
91  // If the pointer to our internal array is shared by any other
92  // MaterialCollections, we have to copy the array now so we won't
93  // inadvertently modify any of our brethren MaterialCollection
94  // objects.
95 
96  if (_materials.get_ref_count() > 1) {
97  Materials old_materials = _materials;
98  _materials = Materials::empty_array(0);
99  _materials.v() = old_materials.v();
100  }
101 
102  _materials.erase(_materials.begin() + material_index);
103  return true;
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: MaterialCollection::add_materials_from
108 // Access: Published
109 // Description: Adds all the Materials indicated in the other
110 // collection to this material. The other materials are simply
111 // appended to the end of the materials in this list;
112 // duplicates are not automatically removed.
113 ////////////////////////////////////////////////////////////////////
116  int other_num_materials = other.get_num_materials();
117  for (int i = 0; i < other_num_materials; i++) {
118  add_material(other.get_material(i));
119  }
120 }
121 
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: MaterialCollection::remove_materials_from
125 // Access: Published
126 // Description: Removes from this collection all of the Materials
127 // listed in the other collection.
128 ////////////////////////////////////////////////////////////////////
131  Materials new_materials;
132  int num_materials = get_num_materials();
133  for (int i = 0; i < num_materials; i++) {
134  PT(Material) material = get_material(i);
135  if (!other.has_material(material)) {
136  new_materials.push_back(material);
137  }
138  }
139  _materials = new_materials;
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: MaterialCollection::remove_duplicate_materials
144 // Access: Published
145 // Description: Removes any duplicate entries of the same Materials
146 // on this collection. If a Material appears multiple
147 // times, the first appearance is retained; subsequent
148 // appearances are removed.
149 ////////////////////////////////////////////////////////////////////
152  Materials new_materials;
153 
154  int num_materials = get_num_materials();
155  for (int i = 0; i < num_materials; i++) {
156  PT(Material) material = get_material(i);
157  bool duplicated = false;
158 
159  for (int j = 0; j < i && !duplicated; j++) {
160  duplicated = (material == get_material(j));
161  }
162 
163  if (!duplicated) {
164  new_materials.push_back(material);
165  }
166  }
167 
168  _materials = new_materials;
169 }
170 
171 ////////////////////////////////////////////////////////////////////
172 // Function: MaterialCollection::has_material
173 // Access: Published
174 // Description: Returns true if the indicated Material appears in
175 // this collection, false otherwise.
176 ////////////////////////////////////////////////////////////////////
178 has_material(Material *material) const {
179  for (int i = 0; i < get_num_materials(); i++) {
180  if (material == get_material(i)) {
181  return true;
182  }
183  }
184  return false;
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: MaterialCollection::clear
189 // Access: Published
190 // Description: Removes all Materials from the collection.
191 ////////////////////////////////////////////////////////////////////
193 clear() {
194  _materials.clear();
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: MaterialCollection::find_material
199 // Access: Published
200 // Description: Returns the material in the collection with the
201 // indicated name, if any, or NULL if no material has
202 // that name.
203 ////////////////////////////////////////////////////////////////////
205 find_material(const string &name) const {
206  int num_materials = get_num_materials();
207  for (int i = 0; i < num_materials; i++) {
208  Material *material = get_material(i);
209  if (material->get_name() == name) {
210  return material;
211  }
212  }
213  return NULL;
214 }
215 
216 ////////////////////////////////////////////////////////////////////
217 // Function: MaterialCollection::get_num_materials
218 // Access: Published
219 // Description: Returns the number of Materials in the collection.
220 ////////////////////////////////////////////////////////////////////
223  return _materials.size();
224 }
225 
226 ////////////////////////////////////////////////////////////////////
227 // Function: MaterialCollection::get_material
228 // Access: Published
229 // Description: Returns the nth Material in the collection.
230 ////////////////////////////////////////////////////////////////////
232 get_material(int index) const {
233  nassertr(index >= 0 && index < (int)_materials.size(), NULL);
234 
235  return _materials[index];
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: MaterialCollection::operator []
240 // Access: Published
241 // Description: Returns the nth Material in the collection. This is
242 // the same as get_material(), but it may be a more
243 // convenient way to access it.
244 ////////////////////////////////////////////////////////////////////
246 operator [] (int index) const {
247  nassertr(index >= 0 && index < (int)_materials.size(), NULL);
248 
249  return _materials[index];
250 }
251 
252 ////////////////////////////////////////////////////////////////////
253 // Function: MaterialCollection::size
254 // Access: Published
255 // Description: Returns the number of materials in the collection. This
256 // is the same thing as get_num_materials().
257 ////////////////////////////////////////////////////////////////////
259 size() const {
260  return _materials.size();
261 }
262 
263 ////////////////////////////////////////////////////////////////////
264 // Function: MaterialCollection::output
265 // Access: Published
266 // Description: Writes a brief one-line description of the
267 // MaterialCollection to the indicated output stream.
268 ////////////////////////////////////////////////////////////////////
270 output(ostream &out) const {
271  if (get_num_materials() == 1) {
272  out << "1 Material";
273  } else {
274  out << get_num_materials() << " Materials";
275  }
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: MaterialCollection::write
280 // Access: Published
281 // Description: Writes a complete multi-line description of the
282 // MaterialCollection to the indicated output stream.
283 ////////////////////////////////////////////////////////////////////
285 write(ostream &out, int indent_level) const {
286  for (int i = 0; i < get_num_materials(); i++) {
287  indent(out, indent_level) << *get_material(i) << "\n";
288  }
289 }
bool has_material(Material *material) const
Returns true if the indicated Material appears in this collection, false otherwise.
void add_material(Material *node_material)
Adds a new Material to the collection.
void write(ostream &out, int indent_level=0) const
Writes a complete multi-line description of the MaterialCollection to the indicated output stream...
int size() const
Returns the number of materials in the collection.
void clear()
Removes all Materials from the collection.
Material * get_material(int index) const
Returns the nth Material in the collection.
Defines the way an object appears in the presence of lighting.
Definition: material.h:34
void output(ostream &out) const
Writes a brief one-line description of the MaterialCollection to the indicated output stream...
Material * find_material(const string &name) const
Returns the material in the collection with the indicated name, if any, or NULL if no material has th...
void remove_duplicate_materials()
Removes any duplicate entries of the same Materials on this collection.
int get_num_materials() const
Returns the number of Materials in the collection.
void remove_materials_from(const MaterialCollection &other)
Removes from this collection all of the Materials listed in the other collection. ...
void add_materials_from(const MaterialCollection &other)
Adds all the Materials indicated in the other collection to this material.
Material * operator[](int index) const
Returns the nth Material in the collection.
bool remove_material(Material *node_material)
Removes the indicated Material from the collection.