Panda3D
colorAttrib.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 colorAttrib.cxx
10  * @author drose
11  * @date 2002-02-22
12  */
13 
14 #include "colorAttrib.h"
16 #include "dcast.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle ColorAttrib::_type_handle;
23 int ColorAttrib::_attrib_slot;
24 CPT(RenderAttrib) ColorAttrib::_off;
25 CPT(RenderAttrib) ColorAttrib::_vertex;
26 
27 /**
28  * Constructs a new ColorAttrib object that indicates geometry should be
29  * rendered according to its own vertex color.
30  */
31 CPT(RenderAttrib) ColorAttrib::
32 make_vertex() {
33  if (_vertex != nullptr) {
34  return _vertex;
35  }
36  ColorAttrib *attrib = new ColorAttrib(T_vertex, LColor::zero());
37  _vertex = return_new(attrib);
38  return _vertex;
39 }
40 
41 /**
42  * Constructs a new ColorAttrib object that indicates geometry should be
43  * rendered in the indicated color.
44  */
45 CPT(RenderAttrib) ColorAttrib::
46 make_flat(const LColor &color) {
47  ColorAttrib *attrib = new ColorAttrib(T_flat, color);
48  return return_new(attrib);
49 }
50 
51 /**
52  * Constructs a new ColorAttrib object that indicates geometry should be
53  * rendered in white.
54  */
55 CPT(RenderAttrib) ColorAttrib::
56 make_off() {
57  if (_off != nullptr) {
58  return _off;
59  }
60  ColorAttrib *attrib = new ColorAttrib(T_off, LColor(1.0f, 1.0f, 1.0f, 1.0f));
61  _off = return_new(attrib);
62  return _off;
63 }
64 
65 /**
66  * Returns a RenderAttrib that corresponds to whatever the standard default
67  * properties for render attributes of this type ought to be.
68  */
69 CPT(RenderAttrib) ColorAttrib::
70 make_default() {
71  return make_vertex();
72 }
73 
74 /**
75  *
76  */
77 void ColorAttrib::
78 output(std::ostream &out) const {
79  out << get_type() << ":";
80  switch (get_color_type()) {
81  case T_vertex:
82  out << "vertex";
83  break;
84 
85  case T_flat:
86  out << "(" << get_color() << ")";
87  break;
88 
89  case T_off:
90  out << "off";
91  break;
92  }
93 }
94 
95 /**
96  * Intended to be overridden by derived ColorAttrib types to return a unique
97  * number indicating whether this ColorAttrib is equivalent to the other one.
98  *
99  * This should return 0 if the two ColorAttrib objects are equivalent, a
100  * number less than zero if this one should be sorted before the other one,
101  * and a number greater than zero otherwise.
102  *
103  * This will only be called with two ColorAttrib objects whose get_type()
104  * functions return the same.
105  */
106 int ColorAttrib::
107 compare_to_impl(const RenderAttrib *other) const {
108  const ColorAttrib *ta = (const ColorAttrib *)other;
109 
110  if (_type != ta->_type) {
111  return (int)_type - (int)ta->_type;
112  }
113  if (_type == T_flat) {
114  return _color.compare_to(ta->_color);
115  }
116  return 0;
117 }
118 
119 /**
120  * Intended to be overridden by derived RenderAttrib types to return a unique
121  * hash for these particular properties. RenderAttribs that compare the same
122  * with compare_to_impl(), above, should return the same hash; RenderAttribs
123  * that compare differently should return a different hash.
124  */
125 size_t ColorAttrib::
126 get_hash_impl() const {
127  size_t hash = 0;
128  hash = int_hash::add_hash(hash, (int)_type);
129  if (_type == T_flat) {
130  hash = _color.add_hash(hash);
131  }
132  return hash;
133 }
134 
135 /**
136  * Quantizes the flat color to the nearest multiple of 1024, just to prevent
137  * runaway accumulation of only slightly-different ColorAttribs.
138  */
139 void ColorAttrib::
140 quantize_color() {
141  switch (_type) {
142  case T_flat:
143  _color[0] = cfloor(_color[0] * 1024.0f + 0.5f) / 1024.0f;
144  _color[1] = cfloor(_color[1] * 1024.0f + 0.5f) / 1024.0f;
145  _color[2] = cfloor(_color[2] * 1024.0f + 0.5f) / 1024.0f;
146  _color[3] = cfloor(_color[3] * 1024.0f + 0.5f) / 1024.0f;
147  break;
148 
149  case T_off:
150  _color.set(1.0f, 1.0f, 1.0f, 1.0f);
151  break;
152 
153  case T_vertex:
154  _color.set(0.0f, 0.0f, 0.0f, 0.0f);
155  break;
156  }
157 }
158 
159 /**
160  * Tells the BamReader how to create objects of type ColorAttrib.
161  */
164  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
165 }
166 
167 /**
168  * Writes the contents of this object to the datagram for shipping out to a
169  * Bam file.
170  */
172 write_datagram(BamWriter *manager, Datagram &dg) {
173  RenderAttrib::write_datagram(manager, dg);
174 
175  dg.add_int8(_type);
176  _color.write_datagram(dg);
177 }
178 
179 /**
180  * This function is called by the BamReader's factory when a new object of
181  * type ColorAttrib is encountered in the Bam file. It should create the
182  * ColorAttrib and extract its information from the file.
183  */
184 TypedWritable *ColorAttrib::
185 make_from_bam(const FactoryParams &params) {
186  ColorAttrib *attrib = new ColorAttrib(T_off, LColor::zero());
187  DatagramIterator scan;
188  BamReader *manager;
189 
190  parse_params(params, scan, manager);
191  attrib->fillin(scan, manager);
192 
193  return attrib;
194 }
195 
196 /**
197  * This internal function is called by make_from_bam to read in all of the
198  * relevant data from the BamFile for the new ColorAttrib.
199  */
200 void ColorAttrib::
201 fillin(DatagramIterator &scan, BamReader *manager) {
202  RenderAttrib::fillin(scan, manager);
203 
204  _type = (Type)scan.get_int8();
205  _color.read_datagram(scan);
206  quantize_color();
207 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
Indicates what color should be applied to renderable geometry.
Definition: colorAttrib.h:27
get_color
If the type is T_flat or T_off, this returns the color that will be applied to geometry.
Definition: colorAttrib.h:47
static void register_with_read_factory()
Tells the BamReader how to create objects of type ColorAttrib.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
get_color_type
Returns the type of color specified by this ColorAttrib.
Definition: colorAttrib.h:46
A class to retrieve the individual data elements previously stored in a Datagram.
int8_t get_int8()
Extracts a signed 8-bit integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
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
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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(RenderAttrib) ColorAttrib CPT(RenderAttrib) ColorAttrib CPT(RenderAttrib) ColorAttrib
Constructs a new ColorAttrib object that indicates geometry should be rendered according to its own v...
Definition: colorAttrib.cxx:31
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.