Panda3D
Loading...
Searching...
No Matches
eggMaterial.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 eggMaterial.cxx
10 * @author drose
11 * @date 1999-01-29
12 */
13
14#include "eggMaterial.h"
15
16#include "indent.h"
17
18TypeHandle EggMaterial::_type_handle;
19
20
21/**
22 *
23 */
24EggMaterial::
25EggMaterial(const std::string &mref_name)
26 : EggNode(mref_name)
27{
28 _flags = 0;
29}
30
31/**
32 *
33 */
34EggMaterial::
35EggMaterial(const EggMaterial &copy)
36 : EggNode(copy),
37 _base(copy._base),
38 _diff(copy._diff),
39 _amb(copy._amb),
40 _emit(copy._emit),
41 _spec(copy._spec),
42 _shininess(copy._shininess),
43 _roughness(copy._roughness),
44 _metallic(copy._metallic),
45 _ior(copy._ior),
46 _local(copy._local),
47 _flags(copy._flags)
48{
49}
50
51
52/**
53 * Writes the material definition to the indicated output stream in Egg
54 * format.
55 */
57write(std::ostream &out, int indent_level) const {
58 write_header(out, indent_level, "<Material>");
59
60 if (has_base()) {
61 indent(out, indent_level + 2)
62 << "<Scalar> baser { " << get_base()[0] << " }\n";
63 indent(out, indent_level + 2)
64 << "<Scalar> baseg { " << get_base()[1] << " }\n";
65 indent(out, indent_level + 2)
66 << "<Scalar> baseb { " << get_base()[2] << " }\n";
67 if (get_base()[3] != 1.0) {
68 indent(out, indent_level + 2)
69 << "<Scalar> basea { " << get_base()[3] << " }\n";
70 }
71 }
72
73 if (has_diff()) {
74 indent(out, indent_level + 2)
75 << "<Scalar> diffr { " << get_diff()[0] << " }\n";
76 indent(out, indent_level + 2)
77 << "<Scalar> diffg { " << get_diff()[1] << " }\n";
78 indent(out, indent_level + 2)
79 << "<Scalar> diffb { " << get_diff()[2] << " }\n";
80 if (get_diff()[3] != 1.0) {
81 indent(out, indent_level + 2)
82 << "<Scalar> diffa { " << get_diff()[3] << " }\n";
83 }
84 }
85
86 if (has_amb()) {
87 indent(out, indent_level + 2)
88 << "<Scalar> ambr { " << get_amb()[0] << " }\n";
89 indent(out, indent_level + 2)
90 << "<Scalar> ambg { " << get_amb()[1] << " }\n";
91 indent(out, indent_level + 2)
92 << "<Scalar> ambb { " << get_amb()[2] << " }\n";
93 if (get_amb()[3] != 1.0) {
94 indent(out, indent_level + 2)
95 << "<Scalar> amba { " << get_amb()[3] << " }\n";
96 }
97 }
98
99 if (has_emit()) {
100 indent(out, indent_level + 2)
101 << "<Scalar> emitr { " << get_emit()[0] << " }\n";
102 indent(out, indent_level + 2)
103 << "<Scalar> emitg { " << get_emit()[1] << " }\n";
104 indent(out, indent_level + 2)
105 << "<Scalar> emitb { " << get_emit()[2] << " }\n";
106 if (get_emit()[3] != 1.0) {
107 indent(out, indent_level + 2)
108 << "<Scalar> emita { " << get_emit()[3] << " }\n";
109 }
110 }
111
112 if (has_spec()) {
113 indent(out, indent_level + 2)
114 << "<Scalar> specr { " << get_spec()[0] << " }\n";
115 indent(out, indent_level + 2)
116 << "<Scalar> specg { " << get_spec()[1] << " }\n";
117 indent(out, indent_level + 2)
118 << "<Scalar> specb { " << get_spec()[2] << " }\n";
119 if (get_spec()[3] != 1.0) {
120 indent(out, indent_level + 2)
121 << "<Scalar> speca { " << get_spec()[3] << " }\n";
122 }
123 }
124
125 if (has_shininess()) {
126 indent(out, indent_level + 2)
127 << "<Scalar> shininess { " << get_shininess() << " }\n";
128 }
129
130 if (has_roughness()) {
131 indent(out, indent_level + 2)
132 << "<Scalar> roughness { " << get_roughness() << " }\n";
133 }
134
135 if (has_metallic()) {
136 indent(out, indent_level + 2)
137 << "<Scalar> metallic { " << get_metallic() << " }\n";
138 }
139
140 if (has_ior()) {
141 indent(out, indent_level + 2)
142 << "<Scalar> ior { " << get_ior() << " }\n";
143 }
144
145 if (has_local()) {
146 indent(out, indent_level + 2)
147 << "<Scalar> local { " << get_local() << " }\n";
148 }
149
150 indent(out, indent_level) << "}\n";
151}
152
153/**
154 * Returns true if the two materials are equivalent in all relevant properties
155 * (according to eq), false otherwise.
156 *
157 * The Equivalence parameter, eq, should be set to the bitwise OR of the
158 * following properties, according to what you consider relevant:
159 *
160 * EggMaterial::E_attributes: All material attributes (diff, spec, etc.)
161 * except MRef name.
162 *
163 * EggMaterial::E_mref_name: The MRef name.
164 */
166is_equivalent_to(const EggMaterial &other, int eq) const {
167 if (eq & E_attributes) {
168 if (_flags != other._flags ||
169 (has_base() && get_base() != other.get_base()) ||
170 (has_diff() && get_diff() != other.get_diff()) ||
171 (has_amb() && get_amb() != other.get_amb()) ||
172 (has_emit() && get_emit() != other.get_emit()) ||
173 (has_spec() && get_spec() != other.get_spec()) ||
174 (has_shininess() && get_shininess() != other.get_shininess()) ||
175 (has_roughness() && get_roughness() != other.get_roughness()) ||
176 (has_metallic() && get_metallic() != other.get_metallic()) ||
177 (has_ior() && get_ior() != other.get_ior()) ||
178 (has_local() && get_local() != other.get_local())) {
179 return false;
180 }
181 }
182
183 if (eq & E_mref_name) {
184 if (get_name() != other.get_name()) {
185 return false;
186 }
187 }
188
189 return true;
190}
191
192/**
193 * An ordering operator to compare two materials for sorting order. This
194 * imposes an arbitrary ordering useful to identify unique materials,
195 * according to the indicated Equivalence factor. See is_equivalent_to().
196 */
198sorts_less_than(const EggMaterial &other, int eq) const {
199 if (eq & E_attributes) {
200 if (_flags != other._flags) {
201 return _flags < (int)other._flags;
202 }
203 if (has_diff() && get_diff() != other.get_diff()) {
204 return get_diff().compare_to(other.get_diff()) < 0;
205 }
206 if (has_amb() && get_amb() != other.get_amb()) {
207 return get_amb().compare_to(other.get_amb()) < 0;
208 }
209 if (has_emit() && get_emit() != other.get_emit()) {
210 return get_emit().compare_to(other.get_emit()) < 0;
211 }
212 if (has_spec() && get_spec() != other.get_spec()) {
213 return get_spec().compare_to(other.get_spec()) < 0;
214 }
215 if (has_shininess() && get_shininess() != other.get_shininess()) {
216 return get_shininess() < other.get_shininess();
217 }
218 if (has_roughness() && get_roughness() != other.get_roughness()) {
219 return get_roughness() < other.get_roughness();
220 }
221 if (has_metallic() && get_metallic() != other.get_metallic()) {
222 return get_metallic() < other.get_metallic();
223 }
224 if (has_ior() && get_ior() != other.get_ior()) {
225 return get_ior() < other.get_ior();
226 }
227 if (has_local() && get_local() != other.get_local()) {
228 return get_local() < other.get_local();
229 }
230 }
231
232 if (eq & E_mref_name) {
233 if (get_name() != other.get_name()) {
234 return get_name() < other.get_name();
235 }
236 }
237
238 return false;
239}
virtual void write(std::ostream &out, int indent_level) const
Writes the material definition to the indicated output stream in Egg format.
get_emit
It is legal to call this even if has_emit() returns false.
Definition eggMaterial.h:95
bool sorts_less_than(const EggMaterial &other, int eq) const
An ordering operator to compare two materials for sorting order.
get_spec
It is legal to call this even if has_spec() returns false.
Definition eggMaterial.h:96
get_diff
It is legal to call this even if has_diff() returns false.
Definition eggMaterial.h:93
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),...
get_amb
It is legal to call this even if has_amb() returns false.
Definition eggMaterial.h:94
get_base
It is legal to call this even if has_base() returns false.
Definition eggMaterial.h:92
void write_header(std::ostream &out, int indent_level, const char *egg_keyword) const
Writes the first line of the egg object, e.g.
A base class for things that may be directly added into the egg hierarchy.
Definition eggNode.h:36
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.