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