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