Panda3D
alphaTestAttrib.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 alphaTestAttrib.cxx
10  * @author drose
11  * @date 2002-03-04
12  */
13 
14 #include "alphaTestAttrib.h"
16 #include "dcast.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 #include "auxBitplaneAttrib.h"
22 
23 TypeHandle AlphaTestAttrib::_type_handle;
24 int AlphaTestAttrib::_attrib_slot;
25 
26 /**
27  * Constructs a new AlphaTestAttrib object.
28  */
29 CPT(RenderAttrib) AlphaTestAttrib::
30 make(PandaCompareFunc mode, PN_stdfloat reference_value) {
31  assert((reference_value >=0.0f) && (reference_value <=1.0f));
32  AlphaTestAttrib *attrib = new AlphaTestAttrib(mode,reference_value);
33  return return_new(attrib);
34 }
35 
36 /**
37  * Returns a RenderAttrib that corresponds to whatever the standard default
38  * properties for render attributes of this type ought to be.
39  */
40 CPT(RenderAttrib) AlphaTestAttrib::
41 make_default() {
42  return return_new(new AlphaTestAttrib);
43 }
44 
45 /**
46  *
47  */
48 void AlphaTestAttrib::
49 output(std::ostream &out) const {
50  out << get_type() << ":";
51  output_comparefunc(out,_mode);
52  out << "," << _reference_alpha;
53 }
54 
55 /**
56  * Intended to be overridden by derived AlphaTestAttrib types to return a
57  * unique number indicating whether this AlphaTestAttrib is equivalent to the
58  * other one.
59  *
60  * This should return 0 if the two AlphaTestAttrib objects are equivalent, a
61  * number less than zero if this one should be sorted before the other one,
62  * and a number greater than zero otherwise.
63  *
64  * This will only be called with two AlphaTestAttrib objects whose get_type()
65  * functions return the same.
66  */
67 int AlphaTestAttrib::
68 compare_to_impl(const RenderAttrib *other) const {
69  const AlphaTestAttrib *ta = (const AlphaTestAttrib *)other;
70 
71  int compare_result = ((int)_mode - (int)ta->_mode) ;
72  if (compare_result != 0) {
73  return compare_result;
74  }
75 
76  if (_reference_alpha != ta->_reference_alpha) {
77  return _reference_alpha < ta->_reference_alpha ? -1 : 1;
78  }
79 
80  return 0;
81 }
82 
83 /**
84  * Intended to be overridden by derived RenderAttrib types to return a unique
85  * hash for these particular properties. RenderAttribs that compare the same
86  * with compare_to_impl(), above, should return the same hash; RenderAttribs
87  * that compare differently should return a different hash.
88  */
89 size_t AlphaTestAttrib::
90 get_hash_impl() const {
91  size_t hash = 0;
92  hash = int_hash::add_hash(hash, (int)_mode);
93  hash = float_hash().add_hash(hash, _reference_alpha);
94  return hash;
95 }
96 
97 /**
98  * Tells the BamReader how to create objects of type AlphaTestAttrib.
99  */
102  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
103 }
104 
105 /**
106  * Writes the contents of this object to the datagram for shipping out to a
107  * Bam file.
108  */
110 write_datagram(BamWriter *manager, Datagram &dg) {
111  RenderAttrib::write_datagram(manager, dg);
112 
113  dg.add_int8(_mode);
114  dg.add_stdfloat(_reference_alpha);
115 }
116 
117 /**
118  * This function is called by the BamReader's factory when a new object of
119  * type AlphaTestAttrib is encountered in the Bam file. It should create the
120  * AlphaTestAttrib and extract its information from the file.
121  */
122 TypedWritable *AlphaTestAttrib::
123 make_from_bam(const FactoryParams &params) {
124  AlphaTestAttrib *attrib = new AlphaTestAttrib;
125  DatagramIterator scan;
126  BamReader *manager;
127 
128  parse_params(params, scan, manager);
129  attrib->fillin(scan, manager);
130 
131  return attrib;
132 }
133 
134 /**
135  * This internal function is called by make_from_bam to read in all of the
136  * relevant data from the BamFile for the new AlphaTestAttrib.
137  */
138 void AlphaTestAttrib::
139 fillin(DatagramIterator &scan, BamReader *manager) {
140  RenderAttrib::fillin(scan, manager);
141 
142  _mode = (PandaCompareFunc)scan.get_int8();
143  _reference_alpha = scan.get_stdfloat();
144 }
CPT
CPT(RenderAttrib) AlphaTestAttrib
Constructs a new AlphaTestAttrib object.
Definition: alphaTestAttrib.cxx:29
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
dcast.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
RenderAttrib::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: renderAttrib.cxx:527
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
RenderAttrib
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
floating_point_hash
This hash_compare class hashes a float or a double.
Definition: stl_compares.h:140
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
AlphaTestAttrib::register_with_read_factory
static void register_with_read_factory()
Tells the BamReader how to create objects of type AlphaTestAttrib.
Definition: alphaTestAttrib.cxx:101
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
DatagramIterator::get_stdfloat
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
Definition: datagramIterator.I:242
Datagram::add_stdfloat
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
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
DatagramIterator::get_int8
int8_t get_int8()
Extracts a signed 8-bit integer.
Definition: datagramIterator.I:56
auxBitplaneAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
AlphaTestAttrib
Enables or disables writing of pixel to framebuffer based on its alpha value relative to a reference ...
Definition: alphaTestAttrib.h:26
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Factory::register_factory
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
integer_hash::add_hash
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
floating_point_hash::add_hash
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
Definition: stl_compares.I:149
Datagram::add_int8
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
alphaTestAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
graphicsStateGuardianBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
AlphaTestAttrib::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: alphaTestAttrib.cxx:110
parse_params
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