Panda3D

colorBlendAttrib.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 colorBlendAttrib.cxx
10 * @author drose
11 * @date 2002-03-29
12 */
13
14 #include "colorBlendAttrib.h"
15 #include "graphicsStateGuardianBase.h"
16 #include "dcast.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21
22 using std::ostream;
23
24 TypeHandle ColorBlendAttrib::_type_handle;
25 int ColorBlendAttrib::_attrib_slot;
26
27 /**
28 * Constructs a new ColorBlendAttrib object that disables special-effect
29 * blending, allowing normal transparency to be used instead.
30 */
31 CPT(RenderAttrib) ColorBlendAttrib::
32 make_off() {
33 ColorBlendAttrib *attrib = new ColorBlendAttrib;
34 return return_new(attrib);
35 }
36
37 /**
38 * Constructs a new ColorBlendAttrib object.
39 *
40 * @deprecated Use the three- or four-parameter constructor instead.
41 */
42 CPT(RenderAttrib) ColorBlendAttrib::
43 make(ColorBlendAttrib::Mode mode) {
44 ColorBlendAttrib *attrib = new ColorBlendAttrib(mode, O_one, O_one,
45 mode, O_one, O_one,
46 LColor::zero());
47 return return_new(attrib);
48 }
49
50 /**
51 * Constructs a new ColorBlendAttrib object that enables special-effect
52 * blending. This supercedes transparency. The given mode and operands are
53 * used for both the RGB and alpha channels.
54 */
55 CPT(RenderAttrib) ColorBlendAttrib::
56 make(ColorBlendAttrib::Mode mode,
57 ColorBlendAttrib::Operand a, ColorBlendAttrib::Operand b,
58 const LColor &color) {
59 ColorBlendAttrib *attrib = new ColorBlendAttrib(mode, a, b, mode, a, b, color);
60 return return_new(attrib);
61 }
62
63 /**
64 * Constructs a new ColorBlendAttrib object that enables special-effect
65 * blending. This supercedes transparency. This form is used to specify
66 * separate blending parameters for the RGB and alpha channels.
67 */
68 CPT(RenderAttrib) ColorBlendAttrib::
69 make(ColorBlendAttrib::Mode mode,
70 ColorBlendAttrib::Operand a, ColorBlendAttrib::Operand b,
71 ColorBlendAttrib::Mode alpha_mode,
72 ColorBlendAttrib::Operand alpha_a, ColorBlendAttrib::Operand alpha_b,
73 const LColor &color) {
74 ColorBlendAttrib *attrib = new ColorBlendAttrib(mode, a, b,
75 alpha_mode, alpha_a, alpha_b,
76 color);
77 return return_new(attrib);
78 }
79
80 /**
81 * Returns a RenderAttrib that corresponds to whatever the standard default
82 * properties for render attributes of this type ought to be.
83 */
84 CPT(RenderAttrib) ColorBlendAttrib::
85 make_default() {
86 return return_new(new ColorBlendAttrib);
87 }
88
89 /**
90 *
91 */
92 void ColorBlendAttrib::
93 output(ostream &out) const {
94 out << get_type() << ":" << get_mode();
95
96 if (get_mode() != M_none) {
97 out << "(" << get_operand_a()
98 << "," << get_operand_b();
99 if (involves_constant_color()) {
100 out << "," << get_color();
101 }
102 out << ")";
103 }
104 }
105
106 /**
107 * Intended to be overridden by derived ColorBlendAttrib types to return a
108 * unique number indicating whether this ColorBlendAttrib is equivalent to the
109 * other one.
110 *
111 * This should return 0 if the two ColorBlendAttrib objects are equivalent, a
112 * number less than zero if this one should be sorted before the other one,
113 * and a number greater than zero otherwise.
114 *
115 * This will only be called with two ColorBlendAttrib objects whose get_type()
116 * functions return the same.
117 */
118 int ColorBlendAttrib::
119 compare_to_impl(const RenderAttrib *other) const {
120 const ColorBlendAttrib *ta = (const ColorBlendAttrib *)other;
121
122 if (_mode != ta->_mode) {
123 return (int)_mode - (int)ta->_mode;
124 }
125
126 if (_a != ta->_a) {
127 return (int)_a - (int)ta->_a;
128 }
129
130 if (_b != ta->_b) {
131 return (int)_b - (int)ta->_b;
132 }
133
134 return _color.compare_to(ta->_color);
135 }
136
137 /**
138 * Intended to be overridden by derived RenderAttrib types to return a unique
139 * hash for these particular properties. RenderAttribs that compare the same
140 * with compare_to_impl(), above, should return the same hash; RenderAttribs
141 * that compare differently should return a different hash.
142 */
143 size_t ColorBlendAttrib::
144 get_hash_impl() const {
145 size_t hash = 0;
146 hash = int_hash::add_hash(hash, (int)_mode);
147 hash = int_hash::add_hash(hash, (int)_a);
148 hash = int_hash::add_hash(hash, (int)_b);
149 hash = _color.add_hash(hash);
150
151 return hash;
152 }
153
154 /**
155 * Tells the BamReader how to create objects of type ColorBlendAttrib.
156 */
157 void ColorBlendAttrib::
158 register_with_read_factory() {
159 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
160 }
161
162 /**
163 * Writes the contents of this object to the datagram for shipping out to a
164 * Bam file.
165 */
166 void ColorBlendAttrib::
167 write_datagram(BamWriter *manager, Datagram &dg) {
168 RenderAttrib::write_datagram(manager, dg);
169
170 dg.add_uint8(_mode);
171 dg.add_uint8(_a);
172 dg.add_uint8(_b);
173
174 if (manager->get_file_minor_ver() >= 42) {
175 dg.add_uint8(_alpha_mode);
176 dg.add_uint8(_alpha_a);
177 dg.add_uint8(_alpha_b);
178 }
179
180 _color.write_datagram(dg);
181 }
182
183 /**
184 * This function is called by the BamReader's factory when a new object of
185 * type ColorBlendAttrib is encountered in the Bam file. It should create the
186 * ColorBlendAttrib and extract its information from the file.
187 */
188 TypedWritable *ColorBlendAttrib::
189 make_from_bam(const FactoryParams &params) {
190 ColorBlendAttrib *attrib = new ColorBlendAttrib;
191 DatagramIterator scan;
192 BamReader *manager;
193
194 parse_params(params, scan, manager);
195 attrib->fillin(scan, manager);
196
197 return attrib;
198 }
199
200 /**
201 * This internal function is called by make_from_bam to read in all of the
202 * relevant data from the BamFile for the new ColorBlendAttrib.
203 */
204 void ColorBlendAttrib::
205 fillin(DatagramIterator &scan, BamReader *manager) {
206 RenderAttrib::fillin(scan, manager);
207
208 _mode = (Mode)scan.get_uint8();
209 _a = (Operand)scan.get_uint8();
210 _b = (Operand)scan.get_uint8();
211
212 if (manager->get_file_minor_ver() >= 42) {
213 _alpha_mode = (Mode)scan.get_uint8();
214 _alpha_a = (Operand)scan.get_uint8();
215 _alpha_b = (Operand)scan.get_uint8();
216 } else {
217 // Before bam 6.42, these were shifted by four.
218 if (_a >= O_incoming1_color) {
219 _a = (Operand)(_a + 4);
220 }
221 if (_b >= O_incoming1_color) {
222 _b = (Operand)(_b + 4);
223 }
224
225 // And there was only one set of blend constants for both RGB and alpha.
226 _alpha_mode = _mode;
227 _alpha_a = _a;
228 _alpha_b = _b;
229 }
230
231 _color.read_datagram(scan);
232
233 _involves_constant_color =
234 involves_constant_color(_a) || involves_constant_color(_alpha_a) ||
235 involves_constant_color(_b) || involves_constant_color(_alpha_b);
236 _involves_color_scale =
237 involves_color_scale(_a) || involves_color_scale(_alpha_a) ||
238 involves_color_scale(_b) || involves_color_scale(_alpha_b);
239 }
240
241 /**
242 *
243 */
244 ostream &
245 operator << (ostream &out, ColorBlendAttrib::Mode mode) {
246 switch (mode) {
247 case ColorBlendAttrib::M_none:
248 return out << "none";
249
250 case ColorBlendAttrib::M_add:
251 return out << "add";
252
253 case ColorBlendAttrib::M_subtract:
254 return out << "subtract";
255
256 case ColorBlendAttrib::M_inv_subtract:
257 return out << "inv_subtract";
258
259 case ColorBlendAttrib::M_min:
260 return out << "min";
261
262 case ColorBlendAttrib::M_max:
263 return out << "max";
264 }
265
266 return out << "**invalid ColorBlendAttrib::Mode(" << (int)mode << ")**";
267 }
268
269 /**
270 *
271 */
272 ostream &
273 operator << (ostream &out, ColorBlendAttrib::Operand operand) {
274 switch (operand) {
275 case ColorBlendAttrib::O_zero:
276 return out << "zero";
277
278 case ColorBlendAttrib::O_one:
279 return out << "one";
280
281 case ColorBlendAttrib::O_incoming_color:
282 return out << "incoming_color";
283
284 case ColorBlendAttrib::O_one_minus_incoming_color:
285 return out << "one_minus_incoming_color";
286
287 case ColorBlendAttrib::O_fbuffer_color:
288 return out << "fbuffer_color";
289
290 case ColorBlendAttrib::O_one_minus_fbuffer_color:
291 return out << "one_minus_fbuffer_color";
292
293 case ColorBlendAttrib::O_incoming_alpha:
294 return out << "incoming_alpha";
295
296 case ColorBlendAttrib::O_one_minus_incoming_alpha:
297 return out << "one_minus_incoming_alpha";
298
299 case ColorBlendAttrib::O_fbuffer_alpha:
300 return out << "fbuffer_alpha";
301
302 case ColorBlendAttrib::O_one_minus_fbuffer_alpha:
303 return out << "one_minus_fbuffer_alpha";
304
305 case ColorBlendAttrib::O_constant_color:
306 return out << "constant_color";
307
308 case ColorBlendAttrib::O_one_minus_constant_color:
309 return out << "one_minus_constant_color";
310
311 case ColorBlendAttrib::O_constant_alpha:
312 return out << "constant_alpha";
313
314 case ColorBlendAttrib::O_one_minus_constant_alpha:
315 return out << "one_minus_constant_alpha";
316
317 case ColorBlendAttrib::O_incoming_color_saturate:
318 return out << "incoming_color_saturate";
319
320 case ColorBlendAttrib::O_color_scale:
321 return out << "color_scale";
322
323 case ColorBlendAttrib::O_one_minus_color_scale:
324 return out << "one_minus_color_scale";
325
326 case ColorBlendAttrib::O_alpha_scale:
327 return out << "alpha_scale";
328
329 case ColorBlendAttrib::O_one_minus_alpha_scale:
330 return out << "one_minus_alpha_scale";
331
332 case ColorBlendAttrib::O_incoming1_color:
333 return out << "incoming1_color";
334
335 case ColorBlendAttrib::O_one_minus_incoming1_color:
336 return out << "one_minus_incoming1_color";
337
338 case ColorBlendAttrib::O_incoming1_alpha:
339 return out << "incoming1_alpha";
340
341 case ColorBlendAttrib::O_one_minus_incoming1_alpha:
342 return out << "one_minus_incoming1_alpha";
343 }
344
345 return out << "**invalid ColorBlendAttrib::Operand(" << (int)operand << ")**";
346 }
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
parse_params
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
BamReader::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
BamWriter::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being written.
Definition: bamWriter.I:59
ColorBlendAttrib
This specifies how colors are blended into the frame buffer, for special effects.
Definition: colorBlendAttrib.h:27
ColorBlendAttrib::get_operand_a
get_operand_a
Returns the RGB multiplier for the first component.
Definition: colorBlendAttrib.h:109
ColorBlendAttrib::register_with_read_factory
static void register_with_read_factory()
Tells the BamReader how to create objects of type ColorBlendAttrib.
Definition: colorBlendAttrib.cxx:158
ColorBlendAttrib::involves_color_scale
bool involves_color_scale() const
Returns true if the this attrib uses the color scale attrib, false otherwise.
Definition: colorBlendAttrib.I:127
ColorBlendAttrib::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: colorBlendAttrib.cxx:167
ColorBlendAttrib::get_color
get_color
Returns the constant color associated with the attrib.
Definition: colorBlendAttrib.h:116
ColorBlendAttrib::involves_constant_color
bool involves_constant_color() const
Returns true if the this attrib uses the constant color, false otherwise.
Definition: colorBlendAttrib.I:118
ColorBlendAttrib::get_operand_b
get_operand_b
Returns the RGB multiplier for the second component.
Definition: colorBlendAttrib.h:110
ColorBlendAttrib::get_mode
get_mode
Returns the blending mode for the RGB channels.
Definition: colorBlendAttrib.h:108
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
DatagramIterator::get_uint8
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
Definition: datagramIterator.I:72
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
Datagram::add_uint8
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:50
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
Factory::register_factory
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
RenderAttrib
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
RenderAttrib::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: renderAttrib.cxx:527
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
integer_hash::add_hash
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
CPT
CPT(RenderAttrib) ColorBlendAttrib
Constructs a new ColorBlendAttrib object that disables special-effect blending, allowing normal trans...
Definition: colorBlendAttrib.cxx:31
colorBlendAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcast.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
graphicsStateGuardianBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.