Panda3D
eggMaterial.cxx
1 // Filename: eggMaterial.cxx
2 // Created by: drose (29Jan99)
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 "eggMaterial.h"
16 
17 #include "indent.h"
18 
19 TypeHandle EggMaterial::_type_handle;
20 
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: EggMaterial::Constructor
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 EggMaterial::
28 EggMaterial(const string &mref_name)
29  : EggNode(mref_name)
30 {
31  _flags = 0;
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function: EggMaterial::Copy Constructor
36 // Access: Public
37 // Description:
38 ////////////////////////////////////////////////////////////////////
39 EggMaterial::
40 EggMaterial(const EggMaterial &copy)
41  : EggNode(copy),
42  _diff(copy._diff),
43  _amb(copy._amb),
44  _emit(copy._emit),
45  _spec(copy._spec),
46  _shininess(copy._shininess),
47  _local(copy._local),
48  _flags(copy._flags)
49 {
50 }
51 
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: EggMaterial::write
55 // Access: Public, Virtual
56 // Description: Writes the material definition to the indicated output
57 // stream in Egg format.
58 ////////////////////////////////////////////////////////////////////
59 void EggMaterial::
60 write(ostream &out, int indent_level) const {
61  write_header(out, indent_level, "<Material>");
62 
63  if (has_diff()) {
64  indent(out, indent_level + 2)
65  << "<Scalar> diffr { " << get_diff()[0] << " }\n";
66  indent(out, indent_level + 2)
67  << "<Scalar> diffg { " << get_diff()[1] << " }\n";
68  indent(out, indent_level + 2)
69  << "<Scalar> diffb { " << get_diff()[2] << " }\n";
70  if (get_diff()[3] != 1.0) {
71  indent(out, indent_level + 2)
72  << "<Scalar> diffa { " << get_diff()[3] << " }\n";
73  }
74  }
75 
76  if (has_amb()) {
77  indent(out, indent_level + 2)
78  << "<Scalar> ambr { " << get_amb()[0] << " }\n";
79  indent(out, indent_level + 2)
80  << "<Scalar> ambg { " << get_amb()[1] << " }\n";
81  indent(out, indent_level + 2)
82  << "<Scalar> ambb { " << get_amb()[2] << " }\n";
83  if (get_amb()[3] != 1.0) {
84  indent(out, indent_level + 2)
85  << "<Scalar> amba { " << get_amb()[3] << " }\n";
86  }
87  }
88 
89  if (has_emit()) {
90  indent(out, indent_level + 2)
91  << "<Scalar> emitr { " << get_emit()[0] << " }\n";
92  indent(out, indent_level + 2)
93  << "<Scalar> emitg { " << get_emit()[1] << " }\n";
94  indent(out, indent_level + 2)
95  << "<Scalar> emitb { " << get_emit()[2] << " }\n";
96  if (get_emit()[3] != 1.0) {
97  indent(out, indent_level + 2)
98  << "<Scalar> emita { " << get_emit()[3] << " }\n";
99  }
100  }
101 
102  if (has_spec()) {
103  indent(out, indent_level + 2)
104  << "<Scalar> specr { " << get_spec()[0] << " }\n";
105  indent(out, indent_level + 2)
106  << "<Scalar> specg { " << get_spec()[1] << " }\n";
107  indent(out, indent_level + 2)
108  << "<Scalar> specb { " << get_spec()[2] << " }\n";
109  if (get_spec()[3] != 1.0) {
110  indent(out, indent_level + 2)
111  << "<Scalar> speca { " << get_spec()[3] << " }\n";
112  }
113  }
114 
115  if (has_shininess()) {
116  indent(out, indent_level + 2)
117  << "<Scalar> shininess { " << get_shininess() << " }\n";
118  }
119 
120  if (has_local()) {
121  indent(out, indent_level + 2)
122  << "<Scalar> local { " << get_local() << " }\n";
123  }
124 
125  indent(out, indent_level) << "}\n";
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: EggMaterial::is_equivalent_to
130 // Access: Public
131 // Description: Returns true if the two materials are equivalent in
132 // all relevant properties (according to eq), false
133 // otherwise.
134 //
135 // The Equivalence parameter, eq, should be set to the
136 // bitwise OR of the following properties, according to
137 // what you consider relevant:
138 //
139 // EggMaterial::E_attributes:
140 // All material attributes (diff, spec,
141 // etc.) except MRef name.
142 //
143 // EggMaterial::E_mref_name:
144 // The MRef name.
145 ////////////////////////////////////////////////////////////////////
146 bool EggMaterial::
147 is_equivalent_to(const EggMaterial &other, int eq) const {
148  if (eq & E_attributes) {
149  if (_flags != other._flags ||
150  (has_diff() && get_diff() != other.get_diff()) ||
151  (has_amb() && get_amb() != other.get_amb()) ||
152  (has_emit() && get_emit() != other.get_emit()) ||
153  (has_spec() && get_spec() != other.get_spec()) ||
154  (has_shininess() && get_shininess() != other.get_shininess()) ||
155  (has_local() && get_local() != other.get_local())) {
156  return false;
157  }
158  }
159 
160  if (eq & E_mref_name) {
161  if (get_name() != other.get_name()) {
162  return false;
163  }
164  }
165 
166  return true;
167 }
168 
169 ////////////////////////////////////////////////////////////////////
170 // Function: EggMaterial::sorts_less_than
171 // Access: Public
172 // Description: An ordering operator to compare two materials for
173 // sorting order. This imposes an arbitrary ordering
174 // useful to identify unique materials, according to the
175 // indicated Equivalence factor. See
176 // is_equivalent_to().
177 ////////////////////////////////////////////////////////////////////
178 bool EggMaterial::
179 sorts_less_than(const EggMaterial &other, int eq) const {
180  if (eq & E_attributes) {
181  if (_flags != other._flags) {
182  return _flags < (int)other._flags;
183  }
184  if (has_diff() && get_diff() != other.get_diff()) {
185  return get_diff().compare_to(other.get_diff()) < 0;
186  }
187  if (has_amb() && get_amb() != other.get_amb()) {
188  return get_amb().compare_to(other.get_amb()) < 0;
189  }
190  if (has_emit() && get_emit() != other.get_emit()) {
191  return get_emit().compare_to(other.get_emit()) < 0;
192  }
193  if (has_spec() && get_spec() != other.get_spec()) {
194  return get_spec().compare_to(other.get_spec()) < 0;
195  }
196  if (has_shininess() && get_shininess() != other.get_shininess()) {
197  return get_shininess() < other.get_shininess();
198  }
199  if (has_local() && get_local() != other.get_local()) {
200  return get_local() < other.get_local();
201  }
202  }
203 
204  if (eq & E_mref_name) {
205  if (get_name() != other.get_name()) {
206  return get_name() < other.get_name();
207  }
208  }
209 
210  return false;
211 }
LColor get_amb() const
It is legal to call this even if has_amb() returns false.
Definition: eggMaterial.I:102
bool is_equivalent_to(const EggMaterial &other, int eq) const
Returns true if the two materials are equivalent in all relevant properties (according to eq)...
virtual void write(ostream &out, int indent_level) const
Writes the material definition to the indicated output stream in Egg format.
Definition: eggMaterial.cxx:60
int compare_to(const LVecBase4f &other) const
This flavor of compare_to uses a default threshold value based on the numeric type.
Definition: lvecBase4.h:988
void write_header(ostream &out, int indent_level, const char *egg_keyword) const
Writes the first line of the egg object, e.g.
bool sorts_less_than(const EggMaterial &other, int eq) const
An ordering operator to compare two materials for sorting order.
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:38
LColor get_spec() const
It is legal to call this even if has_spec() returns false.
Definition: eggMaterial.I:196
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
LColor get_emit() const
It is legal to call this even if has_emit() returns false.
Definition: eggMaterial.I:149
LColor get_diff() const
It is legal to call this even if has_diff() returns false.
Definition: eggMaterial.I:55