Panda3D
antialiasAttrib.cxx
1 // Filename: antialiasAttrib.cxx
2 // Created by: drose (26Jan05)
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 "antialiasAttrib.h"
16 #include "config_pgraph.h"
17 #include "graphicsStateGuardianBase.h"
18 #include "dcast.h"
19 #include "bamReader.h"
20 #include "bamWriter.h"
21 #include "datagram.h"
22 #include "datagramIterator.h"
23 
24 TypeHandle AntialiasAttrib::_type_handle;
25 int AntialiasAttrib::_attrib_slot;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: AntialiasAttrib::make
29 // Access: Published, Static
30 // Description: Constructs a new AntialiasAttrib object.
31 //
32 // The mode should be either M_none, M_auto, or a union
33 // of any or all of M_point, M_line, M_polygon, and
34 // M_multisample. Also, in addition to the above
35 // choices, it may include either of M_better of
36 // M_faster to specify a performance/quality tradeoff
37 // hint.
38 //
39 // If M_none is specified, no antialiasing is performed.
40 //
41 // If M_multisample is specified, it means to use the
42 // special framebuffer multisample bits for
43 // antialiasing, if it is available. If so, the
44 // M_point, M_line, and M_polygon modes are ignored.
45 // This advanced antialiasing mode is only available on
46 // certain graphics hardware. If it is not available,
47 // the M_multisample bit is ignored (and the other modes
48 // may be used instead, if specified).
49 //
50 // M_point, M_line, and/or M_polygon specify
51 // per-primitive smoothing. When enabled, M_point and
52 // M_line may force transparency on. M_polygon requires
53 // a frame buffer that includes an alpha channel, and it
54 // works best if the primitives are sorted
55 // front-to-back.
56 //
57 // If M_auto is specified, M_multisample is selected if
58 // it is available, otherwise M_polygon is selected,
59 // unless drawing lines or points, in which case M_line
60 // or M_point is selected (these two generally produce
61 // better results than M_multisample)
62 ////////////////////////////////////////////////////////////////////
63 CPT(RenderAttrib) AntialiasAttrib::
64 make(unsigned short mode) {
65  AntialiasAttrib *attrib = new AntialiasAttrib(mode);
66  return return_new(attrib);
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: AntialiasAttrib::make_default
71 // Access: Published, Static
72 // Description: Returns a RenderAttrib that corresponds to whatever
73 // the standard default properties for render attributes
74 // of this type ought to be.
75 ////////////////////////////////////////////////////////////////////
76 CPT(RenderAttrib) AntialiasAttrib::
77 make_default() {
78  if (default_antialias_enable) {
79  return return_new(new AntialiasAttrib(M_auto));
80  } else {
81  return return_new(new AntialiasAttrib(M_none));
82  }
83 }
84 
85 ////////////////////////////////////////////////////////////////////
86 // Function: AntialiasAttrib::output
87 // Access: Public, Virtual
88 // Description:
89 ////////////////////////////////////////////////////////////////////
90 void AntialiasAttrib::
91 output(ostream &out) const {
92  out << get_type() << ":";
93 
94  int type = get_mode_type();
95  char sep = ' ';
96 
97  if (type == M_none) {
98  out << " none";
99 
100  } else if (type == M_auto) {
101  out << " auto";
102 
103  } else {
104  if ((_mode & M_point) != 0) {
105  out << sep << "point";
106  sep = '|';
107  }
108  if ((_mode & M_line) != 0) {
109  out << sep << "line";
110  sep = '|';
111  }
112  if ((_mode & M_polygon) != 0) {
113  out << sep << "polygon";
114  sep = '|';
115  }
116  if ((_mode & M_auto) != 0) {
117  out << sep << "best";
118  sep = '|';
119  }
120  }
121 
122  if ((_mode & M_faster) != 0) {
123  out << sep << "faster";
124  sep = '|';
125  }
126  if ((_mode & M_better) != 0) {
127  out << sep << "better";
128  sep = '|';
129  }
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: AntialiasAttrib::compare_to_impl
134 // Access: Protected, Virtual
135 // Description: Intended to be overridden by derived AntialiasAttrib
136 // types to return a unique number indicating whether
137 // this AntialiasAttrib is equivalent to the other one.
138 //
139 // This should return 0 if the two AntialiasAttrib objects
140 // are equivalent, a number less than zero if this one
141 // should be sorted before the other one, and a number
142 // greater than zero otherwise.
143 //
144 // This will only be called with two AntialiasAttrib
145 // objects whose get_type() functions return the same.
146 ////////////////////////////////////////////////////////////////////
147 int AntialiasAttrib::
148 compare_to_impl(const RenderAttrib *other) const {
149  const AntialiasAttrib *ta;
150  DCAST_INTO_R(ta, other, 0);
151  if (_mode != ta->_mode) {
152  return (int)_mode - (int)ta->_mode;
153  }
154  return 0;
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: AntialiasAttrib::get_hash_impl
159 // Access: Protected, Virtual
160 // Description: Intended to be overridden by derived RenderAttrib
161 // types to return a unique hash for these particular
162 // properties. RenderAttribs that compare the same with
163 // compare_to_impl(), above, should return the same
164 // hash; RenderAttribs that compare differently should
165 // return a different hash.
166 ////////////////////////////////////////////////////////////////////
167 size_t AntialiasAttrib::
168 get_hash_impl() const {
169  size_t hash = 0;
170  hash = int_hash::add_hash(hash, (int)_mode);
171  return hash;
172 }
173 
174 ////////////////////////////////////////////////////////////////////
175 // Function: AntialiasAttrib::compose_impl
176 // Access: Protected, Virtual
177 // Description: Intended to be overridden by derived RenderAttrib
178 // types to specify how two consecutive RenderAttrib
179 // objects of the same type interact.
180 //
181 // This should return the result of applying the other
182 // RenderAttrib to a node in the scene graph below this
183 // RenderAttrib, which was already applied. In most
184 // cases, the result is the same as the other
185 // RenderAttrib (that is, a subsequent RenderAttrib
186 // completely replaces the preceding one). On the other
187 // hand, some kinds of RenderAttrib (for instance,
188 // ColorTransformAttrib) might combine in meaningful
189 // ways.
190 ////////////////////////////////////////////////////////////////////
191 CPT(RenderAttrib) AntialiasAttrib::
192 compose_impl(const RenderAttrib *other) const {
193  const AntialiasAttrib *ta;
194  DCAST_INTO_R(ta, other, 0);
195 
196  unsigned short mode_type;
197  unsigned short mode_quality;
198 
199  if (ta->get_mode_type() == M_none || ta->get_mode_type() == M_auto ||
200  get_mode_type() == M_auto) {
201  // These two special types don't combine: if one of these modes is
202  // involved, the lower attrib wins.
203  mode_type = ta->get_mode_type();
204 
205  } else {
206  // Otherwise, the both modes reflect an explicit setting. In that
207  // case, these modes combine in the sensible way, as a union of
208  // bits.
209  mode_type = get_mode_type() | ta->get_mode_type();
210  }
211 
212  if (ta->get_mode_quality() != 0) {
213  // If any quality is specified on the lower attrib, it wins.
214  mode_quality = ta->get_mode_quality();
215  } else {
216  // Otherwise, the upper quality wins.
217  mode_quality = get_mode_quality();
218  }
219 
220  return make(mode_type | mode_quality);
221 }
222 
223 ////////////////////////////////////////////////////////////////////
224 // Function: AntialiasAttrib::register_with_read_factory
225 // Access: Public, Static
226 // Description: Tells the BamReader how to create objects of type
227 // AntialiasAttrib.
228 ////////////////////////////////////////////////////////////////////
229 void AntialiasAttrib::
230 register_with_read_factory() {
231  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
232 }
233 
234 ////////////////////////////////////////////////////////////////////
235 // Function: AntialiasAttrib::write_datagram
236 // Access: Public, Virtual
237 // Description: Writes the contents of this object to the datagram
238 // for shipping out to a Bam file.
239 ////////////////////////////////////////////////////////////////////
242  RenderAttrib::write_datagram(manager, dg);
243 
244  dg.add_uint16(_mode);
245 }
246 
247 ////////////////////////////////////////////////////////////////////
248 // Function: AntialiasAttrib::make_from_bam
249 // Access: Protected, Static
250 // Description: This function is called by the BamReader's factory
251 // when a new object of type AntialiasAttrib is encountered
252 // in the Bam file. It should create the AntialiasAttrib
253 // and extract its information from the file.
254 ////////////////////////////////////////////////////////////////////
255 TypedWritable *AntialiasAttrib::
256 make_from_bam(const FactoryParams &params) {
257  AntialiasAttrib *attrib = new AntialiasAttrib(M_none);
258  DatagramIterator scan;
259  BamReader *manager;
260 
261  parse_params(params, scan, manager);
262  attrib->fillin(scan, manager);
263 
264  return attrib;
265 }
266 
267 ////////////////////////////////////////////////////////////////////
268 // Function: AntialiasAttrib::fillin
269 // Access: Protected
270 // Description: This internal function is called by make_from_bam to
271 // read in all of the relevant data from the BamFile for
272 // the new AntialiasAttrib.
273 ////////////////////////////////////////////////////////////////////
274 void AntialiasAttrib::
275 fillin(DatagramIterator &scan, BamReader *manager) {
276  RenderAttrib::fillin(scan, manager);
277 
278  _mode = scan.get_uint16();
279 }
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
Specifies whether or how to enable antialiasing, if supported by the backend renderer.
unsigned short get_mode_quality() const
Returns the specified antialias mode, with the type bits masked out.
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
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
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: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.
unsigned short get_mode_type() const
Returns the specified antialias mode, with the quality 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:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
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