Panda3D
Loading...
Searching...
No Matches
renderModeAttrib.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 renderModeAttrib.cxx
10 * @author drose
11 * @date 2002-03-14
12 */
13
14#include "renderModeAttrib.h"
16#include "dcast.h"
17#include "bamReader.h"
18#include "bamWriter.h"
19#include "datagram.h"
20#include "datagramIterator.h"
21
22TypeHandle RenderModeAttrib::_type_handle;
23int RenderModeAttrib::_attrib_slot;
24
25/**
26 * Constructs a new RenderModeAttrib object that specifies whether to draw
27 * polygons in the normal, filled mode, or wireframe mode, or in some other
28 * yet-to-be-defined mode.
29 *
30 * The thickness parameter specifies the thickness to be used for wireframe
31 * lines, as well as for ordinary linestrip lines; it also specifies the
32 * diameter of points. (Thick lines are presently only supported in OpenGL;
33 * but thick points are supported on either platform.)
34 *
35 * If perspective is true, the point thickness represented is actually a width
36 * in 3-d units, and the points should scale according to perspective. When
37 * it is false, the point thickness is actually a width in pixels, and points
38 * are a uniform screen size regardless of distance from the camera.
39 *
40 * In M_filled_wireframe mode, you should also specify the wireframe_color,
41 * indicating the flat color to assign to the overlayed wireframe.
42 */
43CPT(RenderAttrib) RenderModeAttrib::
44make(RenderModeAttrib::Mode mode, PN_stdfloat thickness,
45 bool perspective, const LColor &wireframe_color) {
46 RenderModeAttrib *attrib = new RenderModeAttrib(mode, thickness, perspective, wireframe_color);
47 return return_new(attrib);
48}
49
50/**
51 * Returns a RenderAttrib that corresponds to whatever the standard default
52 * properties for render attributes of this type ought to be.
53 */
54CPT(RenderAttrib) RenderModeAttrib::
55make_default() {
56 return return_new(new RenderModeAttrib(M_filled, 1.0f, false));
57}
58
59/**
60 *
61 */
62void RenderModeAttrib::
63output(std::ostream &out) const {
64 out << get_type() << ":";
65 switch (get_mode()) {
66 case M_unchanged:
67 out << "unchanged";
68 break;
69
70 case M_filled:
71 out << "filled";
72 break;
73
74 case M_wireframe:
75 out << "wireframe(" << get_thickness() << ")";
76 break;
77
78 case M_point:
79 out << "point(" << get_thickness() << ")";
80 break;
81
82 case M_filled_flat:
83 out << "filled_flat";
84 break;
85
86 case M_filled_wireframe:
87 out << "filled_wireframe(" << get_wireframe_color() << ")";
88 break;
89 }
90
91 if (get_thickness() != 1.0f) {
92 out << ", thick " << get_thickness();
93 }
94
95 if (get_perspective()) {
96 out << ", perspective";
97 }
98}
99
100/**
101 * Intended to be overridden by derived RenderModeAttrib types to return a
102 * unique number indicating whether this RenderModeAttrib is equivalent to the
103 * other one.
104 *
105 * This should return 0 if the two RenderModeAttrib objects are equivalent, a
106 * number less than zero if this one should be sorted before the other one,
107 * and a number greater than zero otherwise.
108 *
109 * This will only be called with two RenderModeAttrib objects whose get_type()
110 * functions return the same.
111 */
112int RenderModeAttrib::
113compare_to_impl(const RenderAttrib *other) const {
114 const RenderModeAttrib *ta = (const RenderModeAttrib *)other;
115
116 if (_mode != ta->_mode) {
117 return (int)_mode - (int)ta->_mode;
118 }
119 if (_thickness != ta->_thickness) {
120 return _thickness < ta->_thickness ? -1 : 1;
121 }
122 if (_perspective != ta->_perspective) {
123 return (int)_perspective - (int)ta->_perspective;
124 }
125 if (_mode == M_filled_wireframe && _wireframe_color != ta->_wireframe_color) {
126 return _wireframe_color.compare_to(ta->_wireframe_color);
127 }
128 return 0;
129}
130
131/**
132 * Intended to be overridden by derived RenderAttrib types to return a unique
133 * hash for these particular properties. RenderAttribs that compare the same
134 * with compare_to_impl(), above, should return the same hash; RenderAttribs
135 * that compare differently should return a different hash.
136 */
137size_t RenderModeAttrib::
138get_hash_impl() const {
139 size_t hash = 0;
140 hash = int_hash::add_hash(hash, (int)_mode);
141 hash = float_hash().add_hash(hash, _thickness);
142 hash = int_hash::add_hash(hash, (int)_perspective);
143 if (_mode == M_filled_wireframe) {
144 hash = _wireframe_color.add_hash(hash);
145 }
146 return hash;
147}
148
149/**
150 * Intended to be overridden by derived RenderAttrib types to specify how two
151 * consecutive RenderAttrib objects of the same type interact.
152 *
153 * This should return the result of applying the other RenderAttrib to a node
154 * in the scene graph below this RenderAttrib, which was already applied. In
155 * most cases, the result is the same as the other RenderAttrib (that is, a
156 * subsequent RenderAttrib completely replaces the preceding one). On the
157 * other hand, some kinds of RenderAttrib (for instance, ColorTransformAttrib)
158 * might combine in meaningful ways.
159 */
160CPT(RenderAttrib) RenderModeAttrib::
161compose_impl(const RenderAttrib *other) const {
162 const RenderModeAttrib *ta = (const RenderModeAttrib *)other;
163
164 // The special mode M_unchanged means to keep the current mode.
165 Mode mode = ta->get_mode();
166 if (mode == M_unchanged) {
167 mode = get_mode();
168 }
169
170 return make(mode, ta->get_thickness(), ta->get_perspective(),
171 ta->get_wireframe_color());
172}
173
174/**
175 * Tells the BamReader how to create objects of type RenderModeAttrib.
176 */
177void RenderModeAttrib::
178register_with_read_factory() {
179 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
180}
181
182/**
183 * Writes the contents of this object to the datagram for shipping out to a
184 * Bam file.
185 */
187write_datagram(BamWriter *manager, Datagram &dg) {
188 RenderAttrib::write_datagram(manager, dg);
189
190 dg.add_int8(_mode);
191 dg.add_stdfloat(_thickness);
192 dg.add_bool(_perspective);
193
194 if (_mode == M_filled_wireframe) {
195 _wireframe_color.write_datagram(dg);
196 }
197}
198
199/**
200 * This function is called by the BamReader's factory when a new object of
201 * type RenderModeAttrib is encountered in the Bam file. It should create the
202 * RenderModeAttrib and extract its information from the file.
203 */
204TypedWritable *RenderModeAttrib::
205make_from_bam(const FactoryParams &params) {
206 RenderModeAttrib *attrib = new RenderModeAttrib(M_filled, 1.0f, false);
207 DatagramIterator scan;
208 BamReader *manager;
209
210 parse_params(params, scan, manager);
211 attrib->fillin(scan, manager);
212
213 return attrib;
214}
215
216/**
217 * This internal function is called by make_from_bam to read in all of the
218 * relevant data from the BamFile for the new RenderModeAttrib.
219 */
220void RenderModeAttrib::
221fillin(DatagramIterator &scan, BamReader *manager) {
222 RenderAttrib::fillin(scan, manager);
223
224 _mode = (Mode)scan.get_int8();
225 _thickness = scan.get_stdfloat();
226 _perspective = scan.get_bool();
227
228 if (_mode == M_filled_wireframe) {
229 _wireframe_color.read_datagram(scan);
230 }
231}
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
A class to retrieve the individual data elements previously stored in a Datagram.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
int8_t get_int8()
Extracts a signed 8-bit integer.
bool get_bool()
Extracts a boolean value.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
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:133
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition datagram.I:34
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...
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...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Specifies how polygons are to be drawn.
get_perspective
Returns the perspective flag.
get_thickness
Returns the line width or point thickness.
get_wireframe_color
Returns the color that is used in M_filled_wireframe mode to distinguish the wireframe from the rest ...
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_mode
Returns the render mode.
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.
This hash_compare class hashes a float or a double.
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
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.