Panda3D
antialiasAttrib.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 antialiasAttrib.cxx
10  * @author drose
11  * @date 2005-01-26
12  */
13 
14 #include "antialiasAttrib.h"
15 #include "config_pgraph.h"
17 #include "dcast.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 
23 TypeHandle AntialiasAttrib::_type_handle;
24 int AntialiasAttrib::_attrib_slot;
25 
26 /**
27  * Constructs a new AntialiasAttrib object.
28  *
29  * The mode should be either M_none, M_auto, or a union of any or all of
30  * M_point, M_line, M_polygon, and M_multisample. Also, in addition to the
31  * above choices, it may include either of M_better of M_faster to specify a
32  * performance/quality tradeoff hint.
33  *
34  * If M_none is specified, no antialiasing is performed.
35  *
36  * If M_multisample is specified, it means to use the special framebuffer
37  * multisample bits for antialiasing, if it is available. If so, the M_point,
38  * M_line, and M_polygon modes are ignored. This advanced antialiasing mode
39  * is only available on certain graphics hardware. If it is not available,
40  * the M_multisample bit is ignored (and the other modes may be used instead,
41  * if specified).
42  *
43  * M_point, M_line, and/or M_polygon specify per-primitive smoothing. When
44  * enabled, M_point and M_line may force transparency on. M_polygon requires
45  * a frame buffer that includes an alpha channel, and it works best if the
46  * primitives are sorted front-to-back.
47  *
48  * If M_auto is specified, M_multisample is selected if it is available,
49  * otherwise M_polygon is selected, unless drawing lines or points, in which
50  * case M_line or M_point is selected (these two generally produce better
51  * results than M_multisample)
52  */
53 CPT(RenderAttrib) AntialiasAttrib::
54 make(unsigned short mode) {
55  AntialiasAttrib *attrib = new AntialiasAttrib(mode);
56  return return_new(attrib);
57 }
58 
59 /**
60  * Returns a RenderAttrib that corresponds to whatever the standard default
61  * properties for render attributes of this type ought to be.
62  */
63 CPT(RenderAttrib) AntialiasAttrib::
64 make_default() {
66 }
67 
68 /**
69  *
70  */
71 void AntialiasAttrib::
72 output(std::ostream &out) const {
73  out << get_type() << ":";
74 
75  int type = get_mode_type();
76  char sep = ' ';
77 
78  if (type == M_none) {
79  out << " none";
80 
81  } else if (type == M_auto) {
82  out << " auto";
83 
84  } else {
85  if ((_mode & M_point) != 0) {
86  out << sep << "point";
87  sep = '|';
88  }
89  if ((_mode & M_line) != 0) {
90  out << sep << "line";
91  sep = '|';
92  }
93  if ((_mode & M_polygon) != 0) {
94  out << sep << "polygon";
95  sep = '|';
96  }
97  if ((_mode & M_auto) != 0) {
98  out << sep << "best";
99  sep = '|';
100  }
101  }
102 
103  if ((_mode & M_faster) != 0) {
104  out << sep << "faster";
105  sep = '|';
106  }
107  if ((_mode & M_better) != 0) {
108  out << sep << "better";
109  sep = '|';
110  }
111 }
112 
113 /**
114  * Intended to be overridden by derived AntialiasAttrib types to return a
115  * unique number indicating whether this AntialiasAttrib is equivalent to the
116  * other one.
117  *
118  * This should return 0 if the two AntialiasAttrib objects are equivalent, a
119  * number less than zero if this one should be sorted before the other one,
120  * and a number greater than zero otherwise.
121  *
122  * This will only be called with two AntialiasAttrib objects whose get_type()
123  * functions return the same.
124  */
125 int AntialiasAttrib::
126 compare_to_impl(const RenderAttrib *other) const {
127  const AntialiasAttrib *ta = (const AntialiasAttrib *)other;
128 
129  if (_mode != ta->_mode) {
130  return (int)_mode - (int)ta->_mode;
131  }
132  return 0;
133 }
134 
135 /**
136  * Intended to be overridden by derived RenderAttrib types to return a unique
137  * hash for these particular properties. RenderAttribs that compare the same
138  * with compare_to_impl(), above, should return the same hash; RenderAttribs
139  * that compare differently should return a different hash.
140  */
141 size_t AntialiasAttrib::
142 get_hash_impl() const {
143  size_t hash = 0;
144  hash = int_hash::add_hash(hash, (int)_mode);
145  return hash;
146 }
147 
148 /**
149  * Intended to be overridden by derived RenderAttrib types to specify how two
150  * consecutive RenderAttrib objects of the same type interact.
151  *
152  * This should return the result of applying the other RenderAttrib to a node
153  * in the scene graph below this RenderAttrib, which was already applied. In
154  * most cases, the result is the same as the other RenderAttrib (that is, a
155  * subsequent RenderAttrib completely replaces the preceding one). On the
156  * other hand, some kinds of RenderAttrib (for instance, ColorTransformAttrib)
157  * might combine in meaningful ways.
158  */
159 CPT(RenderAttrib) AntialiasAttrib::
160 compose_impl(const RenderAttrib *other) const {
161  const AntialiasAttrib *ta = (const AntialiasAttrib *)other;
162 
163  unsigned short mode_type;
164  unsigned short mode_quality;
165 
166  if (ta->get_mode_type() == M_none || ta->get_mode_type() == M_auto ||
167  get_mode_type() == M_auto) {
168  // These two special types don't combine: if one of these modes is
169  // involved, the lower attrib wins.
170  mode_type = ta->get_mode_type();
171 
172  } else {
173  // Otherwise, the both modes reflect an explicit setting. In that case,
174  // these modes combine in the sensible way, as a union of bits.
175  mode_type = get_mode_type() | ta->get_mode_type();
176  }
177 
178  if (ta->get_mode_quality() != 0) {
179  // If any quality is specified on the lower attrib, it wins.
180  mode_quality = ta->get_mode_quality();
181  } else {
182  // Otherwise, the upper quality wins.
183  mode_quality = get_mode_quality();
184  }
185 
186  return make(mode_type | mode_quality);
187 }
188 
189 /**
190  * Tells the BamReader how to create objects of type AntialiasAttrib.
191  */
192 void AntialiasAttrib::
193 register_with_read_factory() {
194  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
195 }
196 
197 /**
198  * Writes the contents of this object to the datagram for shipping out to a
199  * Bam file.
200  */
203  RenderAttrib::write_datagram(manager, dg);
204 
205  dg.add_uint16(_mode);
206 }
207 
208 /**
209  * This function is called by the BamReader's factory when a new object of
210  * type AntialiasAttrib is encountered in the Bam file. It should create the
211  * AntialiasAttrib and extract its information from the file.
212  */
213 TypedWritable *AntialiasAttrib::
214 make_from_bam(const FactoryParams &params) {
215  AntialiasAttrib *attrib = new AntialiasAttrib(M_none);
216  DatagramIterator scan;
217  BamReader *manager;
218 
219  parse_params(params, scan, manager);
220  attrib->fillin(scan, manager);
221 
222  return attrib;
223 }
224 
225 /**
226  * This internal function is called by make_from_bam to read in all of the
227  * relevant data from the BamFile for the new AntialiasAttrib.
228  */
229 void AntialiasAttrib::
230 fillin(DatagramIterator &scan, BamReader *manager) {
231  RenderAttrib::fillin(scan, manager);
232 
233  _mode = scan.get_uint16();
234 }
235 
236 /**
237  *
238  */
239 void AntialiasAttrib::
240 init_type() {
241  RenderAttrib::init_type();
242  register_type(_type_handle, "AntialiasAttrib",
243  RenderAttrib::get_class_type());
244 
245  // This is defined here, since we have otherwise no guarantee that the
246  // config var has already been constructed by the time we call init_type()
247  // at static init time.
248  static ConfigVariableBool default_antialias_enable
249  ("default-antialias-enable", false,
250  PRC_DESC("Set this true to enable the M_auto antialiasing mode for all "
251  "nodes by default."));
252 
253  _attrib_slot = register_slot(_type_handle, 100,
254  new AntialiasAttrib(default_antialias_enable ? M_auto : M_none));
255 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
Specifies whether or how to enable antialiasing, if supported by the backend renderer.
This is a convenience class to specialize ConfigVariable as a boolean type.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
get_mode_type
Returns the specified antialias mode, with the quality bits masked out.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
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
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
static int register_slot(TypeHandle type_handle, int sort, RenderAttrib *default_attrib)
Adds the indicated TypeHandle to the registry, if it is not there already, and returns a unique slot ...
Definition: renderAttrib.I:101
get_mode_quality
Returns the specified antialias mode, with the type bits masked out.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
const RenderAttrib * get_slot_default(int slot) const
Returns the default RenderAttrib object associated with slot n.
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:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
CPT(RenderAttrib) AntialiasAttrib
Constructs a new AntialiasAttrib object.
static RenderAttribRegistry * quick_get_global_ptr()
Returns the global_ptr without first ensuring it has been initialized.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.