Panda3D
 All Classes Functions Variables Enumerations
renderModeAttrib.cxx
1 // Filename: renderModeAttrib.cxx
2 // Created by: drose (14Mar02)
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 "renderModeAttrib.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 RenderModeAttrib::_type_handle;
24 int RenderModeAttrib::_attrib_slot;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: RenderModeAttrib::make
28 // Access: Published, Static
29 // Description: Constructs a new RenderModeAttrib object that specifies
30 // whether to draw polygons in the normal, filled mode,
31 // or wireframe mode, or in some other yet-to-be-defined
32 // mode.
33 //
34 // The thickness parameter specifies the thickness to be
35 // used for wireframe lines, as well as for ordinary
36 // linestrip lines; it also specifies the diameter of
37 // points. (Thick lines are presently only supported in
38 // OpenGL; but thick points are supported on either
39 // platform.)
40 //
41 // If perspective is true, the point thickness
42 // represented is actually a width in 3-d units, and the
43 // points should scale according to perspective. When
44 // it is false, the point thickness is actually a width
45 // in pixels, and points are a uniform screen size
46 // regardless of distance from the camera.
47 //
48 // In M_filled_wireframe mode, you should also specify
49 // the wireframe_color, indicating the flat color to
50 // assign to the overlayed wireframe.
51 ////////////////////////////////////////////////////////////////////
53 make(RenderModeAttrib::Mode mode, PN_stdfloat thickness,
54  bool perspective, const LColor &wireframe_color) {
55  RenderModeAttrib *attrib = new RenderModeAttrib(mode, thickness, perspective, wireframe_color);
56  return return_new(attrib);
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: RenderModeAttrib::make_default
61 // Access: Published, Static
62 // Description: Returns a RenderAttrib that corresponds to whatever
63 // the standard default properties for render attributes
64 // of this type ought to be.
65 ////////////////////////////////////////////////////////////////////
67 make_default() {
68  return return_new(new RenderModeAttrib(M_filled, 1.0f, false));
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: RenderModeAttrib::output
73 // Access: Public, Virtual
74 // Description:
75 ////////////////////////////////////////////////////////////////////
76 void RenderModeAttrib::
77 output(ostream &out) const {
78  out << get_type() << ":";
79  switch (get_mode()) {
80  case M_unchanged:
81  out << "unchanged";
82  break;
83 
84  case M_filled:
85  out << "filled";
86  break;
87 
88  case M_wireframe:
89  out << "wireframe(" << get_thickness() << ")";
90  break;
91 
92  case M_point:
93  out << "point(" << get_thickness() << ")";
94  break;
95 
96  case M_filled_flat:
97  out << "filled_flat";
98  break;
99 
100  case M_filled_wireframe:
101  out << "filled_wireframe(" << get_wireframe_color() << ")";
102  break;
103  }
104 
105  if (get_thickness() != 1.0f) {
106  out << ", thick " << get_thickness();
107  }
108 
109  if (get_perspective()) {
110  out << ", perspective";
111  }
112 }
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: RenderModeAttrib::compare_to_impl
116 // Access: Protected, Virtual
117 // Description: Intended to be overridden by derived RenderModeAttrib
118 // types to return a unique number indicating whether
119 // this RenderModeAttrib is equivalent to the other one.
120 //
121 // This should return 0 if the two RenderModeAttrib objects
122 // are equivalent, a number less than zero if this one
123 // should be sorted before the other one, and a number
124 // greater than zero otherwise.
125 //
126 // This will only be called with two RenderModeAttrib
127 // objects whose get_type() functions return the same.
128 ////////////////////////////////////////////////////////////////////
129 int RenderModeAttrib::
130 compare_to_impl(const RenderAttrib *other) const {
131  const RenderModeAttrib *ta;
132  DCAST_INTO_R(ta, other, 0);
133  if (_mode != ta->_mode) {
134  return (int)_mode - (int)ta->_mode;
135  }
136  if (_thickness != ta->_thickness) {
137  return _thickness < ta->_thickness ? -1 : 1;
138  }
139  if (_perspective != ta->_perspective) {
140  return (int)_perspective - (int)ta->_perspective;
141  }
142  if (_mode == M_filled_wireframe && _wireframe_color != ta->_wireframe_color) {
143  return _wireframe_color.compare_to(ta->_wireframe_color);
144  }
145  return 0;
146 }
147 
148 ////////////////////////////////////////////////////////////////////
149 // Function: RenderModeAttrib::get_hash_impl
150 // Access: Protected, Virtual
151 // Description: Intended to be overridden by derived RenderAttrib
152 // types to return a unique hash for these particular
153 // properties. RenderAttribs that compare the same with
154 // compare_to_impl(), above, should return the same
155 // hash; RenderAttribs that compare differently should
156 // return a different hash.
157 ////////////////////////////////////////////////////////////////////
158 size_t RenderModeAttrib::
159 get_hash_impl() const {
160  size_t hash = 0;
161  hash = int_hash::add_hash(hash, (int)_mode);
162  hash = float_hash().add_hash(hash, _thickness);
163  hash = int_hash::add_hash(hash, (int)_perspective);
164  if (_mode == M_filled_wireframe) {
165  hash = _wireframe_color.add_hash(hash);
166  }
167  return hash;
168 }
169 
170 ////////////////////////////////////////////////////////////////////
171 // Function: RenderModeAttrib::compose_impl
172 // Access: Protected, Virtual
173 // Description: Intended to be overridden by derived RenderAttrib
174 // types to specify how two consecutive RenderAttrib
175 // objects of the same type interact.
176 //
177 // This should return the result of applying the other
178 // RenderAttrib to a node in the scene graph below this
179 // RenderAttrib, which was already applied. In most
180 // cases, the result is the same as the other
181 // RenderAttrib (that is, a subsequent RenderAttrib
182 // completely replaces the preceding one). On the other
183 // hand, some kinds of RenderAttrib (for instance,
184 // ColorTransformAttrib) might combine in meaningful
185 // ways.
186 ////////////////////////////////////////////////////////////////////
188 compose_impl(const RenderAttrib *other) const {
189  const RenderModeAttrib *ta;
190  DCAST_INTO_R(ta, other, 0);
191 
192  // The special mode M_unchanged means to keep the current mode.
193  Mode mode = ta->get_mode();
194  if (mode == M_unchanged) {
195  mode = get_mode();
196  }
197 
198  return make(mode, ta->get_thickness(), ta->get_perspective(),
199  ta->get_wireframe_color());
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function: RenderModeAttrib::register_with_read_factory
204 // Access: Public, Static
205 // Description: Tells the BamReader how to create objects of type
206 // RenderModeAttrib.
207 ////////////////////////////////////////////////////////////////////
210  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
211 }
212 
213 ////////////////////////////////////////////////////////////////////
214 // Function: RenderModeAttrib::write_datagram
215 // Access: Public, Virtual
216 // Description: Writes the contents of this object to the datagram
217 // for shipping out to a Bam file.
218 ////////////////////////////////////////////////////////////////////
221  RenderAttrib::write_datagram(manager, dg);
222 
223  dg.add_int8(_mode);
224  dg.add_stdfloat(_thickness);
225  dg.add_bool(_perspective);
226 
227  if (_mode == M_filled_wireframe) {
228  _wireframe_color.write_datagram(dg);
229  }
230 }
231 
232 ////////////////////////////////////////////////////////////////////
233 // Function: RenderModeAttrib::make_from_bam
234 // Access: Protected, Static
235 // Description: This function is called by the BamReader's factory
236 // when a new object of type RenderModeAttrib is encountered
237 // in the Bam file. It should create the RenderModeAttrib
238 // and extract its information from the file.
239 ////////////////////////////////////////////////////////////////////
240 TypedWritable *RenderModeAttrib::
241 make_from_bam(const FactoryParams &params) {
242  RenderModeAttrib *attrib = new RenderModeAttrib(M_filled, 1.0f, false);
243  DatagramIterator scan;
244  BamReader *manager;
245 
246  parse_params(params, scan, manager);
247  attrib->fillin(scan, manager);
248 
249  return attrib;
250 }
251 
252 ////////////////////////////////////////////////////////////////////
253 // Function: RenderModeAttrib::fillin
254 // Access: Protected
255 // Description: This internal function is called by make_from_bam to
256 // read in all of the relevant data from the BamFile for
257 // the new RenderModeAttrib.
258 ////////////////////////////////////////////////////////////////////
259 void RenderModeAttrib::
260 fillin(DatagramIterator &scan, BamReader *manager) {
261  RenderAttrib::fillin(scan, manager);
262 
263  _mode = (Mode)scan.get_int8();
264  _thickness = scan.get_stdfloat();
265  _perspective = scan.get_bool();
266 
267  if (_mode == M_filled_wireframe) {
268  _wireframe_color.read_datagram(scan);
269  }
270 }
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:971
PN_int8 get_int8()
Extracts a signed 8-bit integer.
bool get_bool()
Extracts a boolean value.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
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
void add_int8(PN_int8 value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:128
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
bool get_perspective() const
Returns the perspective flag.
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 fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition: datagram.I:240
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:122
This hash_compare class hashes a float or a double.
Definition: stl_compares.h:158
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition: datagram.I:118
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:40
const LColor & get_wireframe_color() const
Returns the color that is used in M_filled_wireframe mode to distinguish the wireframe from the rest ...
void read_datagram(DatagramIterator &source)
Reads the vector from the Datagram using get_stdfloat().
Definition: lvecBase4.h:1442
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
Mode get_mode() const
Returns the render mode.
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
Definition: stl_compares.I:180
void write_datagram(Datagram &destination) const
Writes the vector to the Datagram using add_stdfloat().
Definition: lvecBase4.h:1422
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 RenderModeAttrib.
Specifies how polygons are to be drawn.
PN_stdfloat get_thickness() const
Returns the line width or point thickness.
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
size_t add_hash(size_t hash) const
Adds the vector into the running hash.
Definition: lvecBase4.h:1009