Panda3D
colorAttrib.cxx
1 // Filename: colorAttrib.cxx
2 // Created by: drose (22Feb02)
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 "colorAttrib.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 ColorAttrib::_type_handle;
24 int ColorAttrib::_attrib_slot;
25 CPT(RenderAttrib) ColorAttrib::_off;
26 CPT(RenderAttrib) ColorAttrib::_vertex;
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: ColorAttrib::make_vertex
30 // Access: Published, Static
31 // Description: Constructs a new ColorAttrib object that indicates
32 // geometry should be rendered according to its own
33 // vertex color.
34 ////////////////////////////////////////////////////////////////////
35 CPT(RenderAttrib) ColorAttrib::
36 make_vertex() {
37  if (_vertex != 0) {
38  return _vertex;
39  }
40  ColorAttrib *attrib = new ColorAttrib(T_vertex, LColor::zero());
41  _vertex = return_new(attrib);
42  return _vertex;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: ColorAttrib::make_flat
47 // Access: Published, Static
48 // Description: Constructs a new ColorAttrib object that indicates
49 // geometry should be rendered in the indicated color.
50 ////////////////////////////////////////////////////////////////////
51 CPT(RenderAttrib) ColorAttrib::
52 make_flat(const LColor &color) {
53  ColorAttrib *attrib = new ColorAttrib(T_flat, color);
54  return return_new(attrib);
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: ColorAttrib::make_off
59 // Access: Published, Static
60 // Description: Constructs a new ColorAttrib object that indicates
61 // geometry should be rendered in white.
62 ////////////////////////////////////////////////////////////////////
63 CPT(RenderAttrib) ColorAttrib::
64 make_off() {
65  if (_off != 0) {
66  return _off;
67  }
68  ColorAttrib *attrib = new ColorAttrib(T_off, LColor(1.0f, 1.0f, 1.0f, 1.0f));
69  _off = return_new(attrib);
70  return _off;
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: ColorAttrib::make_default
75 // Access: Published, Static
76 // Description: Returns a RenderAttrib that corresponds to whatever
77 // the standard default properties for render attributes
78 // of this type ought to be.
79 ////////////////////////////////////////////////////////////////////
80 CPT(RenderAttrib) ColorAttrib::
81 make_default() {
82  return make_off();
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: ColorAttrib::output
87 // Access: Public, Virtual
88 // Description:
89 ////////////////////////////////////////////////////////////////////
90 void ColorAttrib::
91 output(ostream &out) const {
92  out << get_type() << ":";
93  switch (get_color_type()) {
94  case T_vertex:
95  out << "vertex";
96  break;
97 
98  case T_flat:
99  out << "(" << get_color() << ")";
100  break;
101 
102  case T_off:
103  out << "off";
104  break;
105  }
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: ColorAttrib::compare_to_impl
110 // Access: Protected, Virtual
111 // Description: Intended to be overridden by derived ColorAttrib
112 // types to return a unique number indicating whether
113 // this ColorAttrib is equivalent to the other one.
114 //
115 // This should return 0 if the two ColorAttrib objects
116 // are equivalent, a number less than zero if this one
117 // should be sorted before the other one, and a number
118 // greater than zero otherwise.
119 //
120 // This will only be called with two ColorAttrib
121 // objects whose get_type() functions return the same.
122 ////////////////////////////////////////////////////////////////////
123 int ColorAttrib::
124 compare_to_impl(const RenderAttrib *other) const {
125  const ColorAttrib *ta;
126  DCAST_INTO_R(ta, other, 0);
127  if (_type != ta->_type) {
128  return (int)_type - (int)ta->_type;
129  }
130  if (_type == T_flat) {
131  return _color.compare_to(ta->_color);
132  }
133  return 0;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: ColorAttrib::get_hash_impl
138 // Access: Protected, Virtual
139 // Description: Intended to be overridden by derived RenderAttrib
140 // types to return a unique hash for these particular
141 // properties. RenderAttribs that compare the same with
142 // compare_to_impl(), above, should return the same
143 // hash; RenderAttribs that compare differently should
144 // return a different hash.
145 ////////////////////////////////////////////////////////////////////
146 size_t ColorAttrib::
147 get_hash_impl() const {
148  size_t hash = 0;
149  hash = int_hash::add_hash(hash, (int)_type);
150  if (_type == T_flat) {
151  hash = _color.add_hash(hash);
152  }
153  return hash;
154 }
155 
156 ////////////////////////////////////////////////////////////////////
157 // Function: ColorAttrib::get_auto_shader_attrib_impl
158 // Access: Protected, Virtual
159 // Description:
160 ////////////////////////////////////////////////////////////////////
161 CPT(RenderAttrib) ColorAttrib::
162 get_auto_shader_attrib_impl(const RenderState *state) const {
163  // For a ColorAttrib, the only relevant information is the type: is
164  // it flat-shaded or vertex-shaded? The actual color value is read
165  // by the shader from the graphics state.
166 
167  ColorAttrib *attrib = new ColorAttrib(_type, LColor(1.0f, 1.0f, 1.0f, 1.0f));
168  return return_new(attrib);
169 }
170 
171 ////////////////////////////////////////////////////////////////////
172 // Function: ColorAttrib::quantize_color
173 // Access: Private
174 // Description: Quantizes the color color to the nearest multiple of
175 // 1000, just to prevent runaway accumulation of
176 // only slightly-different ColorAttribs.
177 ////////////////////////////////////////////////////////////////////
178 void ColorAttrib::
179 quantize_color() {
180  switch (_type) {
181  case T_flat:
182  _color[0] = cfloor(_color[0] * 1000.0f + 0.5f) * 0.001f;
183  _color[1] = cfloor(_color[1] * 1000.0f + 0.5f) * 0.001f;
184  _color[2] = cfloor(_color[2] * 1000.0f + 0.5f) * 0.001f;
185  _color[3] = cfloor(_color[3] * 1000.0f + 0.5f) * 0.001f;
186  break;
187 
188  case T_off:
189  _color.set(1.0f, 1.0f, 1.0f, 1.0f);
190  break;
191 
192  case T_vertex:
193  _color.set(0.0f, 0.0f, 0.0f, 0.0f);
194  break;
195  }
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: ColorAttrib::register_with_read_factory
200 // Access: Public, Static
201 // Description: Tells the BamReader how to create objects of type
202 // ColorAttrib.
203 ////////////////////////////////////////////////////////////////////
204 void ColorAttrib::
206  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: ColorAttrib::write_datagram
211 // Access: Public, Virtual
212 // Description: Writes the contents of this object to the datagram
213 // for shipping out to a Bam file.
214 ////////////////////////////////////////////////////////////////////
215 void ColorAttrib::
217  RenderAttrib::write_datagram(manager, dg);
218 
219  dg.add_int8(_type);
220  _color.write_datagram(dg);
221 }
222 
223 ////////////////////////////////////////////////////////////////////
224 // Function: ColorAttrib::make_from_bam
225 // Access: Protected, Static
226 // Description: This function is called by the BamReader's factory
227 // when a new object of type ColorAttrib is encountered
228 // in the Bam file. It should create the ColorAttrib
229 // and extract its information from the file.
230 ////////////////////////////////////////////////////////////////////
231 TypedWritable *ColorAttrib::
232 make_from_bam(const FactoryParams &params) {
233  ColorAttrib *attrib = new ColorAttrib(T_off, LColor::zero());
234  DatagramIterator scan;
235  BamReader *manager;
236 
237  parse_params(params, scan, manager);
238  attrib->fillin(scan, manager);
239 
240  return attrib;
241 }
242 
243 ////////////////////////////////////////////////////////////////////
244 // Function: ColorAttrib::fillin
245 // Access: Protected
246 // Description: This internal function is called by make_from_bam to
247 // read in all of the relevant data from the BamFile for
248 // the new ColorAttrib.
249 ////////////////////////////////////////////////////////////////////
250 void ColorAttrib::
251 fillin(DatagramIterator &scan, BamReader *manager) {
252  RenderAttrib::fillin(scan, manager);
253 
254  _type = (Type)scan.get_int8();
255  _color.read_datagram(scan);
256  quantize_color();
257 }
PN_int8 get_int8()
Extracts a signed 8-bit integer.
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:60
const LColor & get_color() const
If the type is T_flat or T_off, this returns the color that will be applied to geometry.
Definition: colorAttrib.I:58
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
void add_int8(PN_int8 value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:128
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
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
Type get_color_type() const
Returns the type of color specified by this ColorAttrib.
Definition: colorAttrib.I:46
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.
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
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
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
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
static void register_with_read_factory()
Tells the BamReader how to create objects of type ColorAttrib.
Indicates what color should be applied to renderable geometry.
Definition: colorAttrib.h:30
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