Panda3D
 All Classes Functions Variables Enumerations
alphaTestAttrib.cxx
1 // Filename: alphaTestAttrib.cxx
2 // Created by: drose (04Mar02)
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 "alphaTestAttrib.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 AlphaTestAttrib::_type_handle;
24 int AlphaTestAttrib::_attrib_slot;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: AlphaTestAttrib::make
28 // Access: Published, Static
29 // Description: Constructs a new AlphaTestAttrib object.
30 ////////////////////////////////////////////////////////////////////
32 make(PandaCompareFunc mode, PN_stdfloat reference_value) {
33  assert((reference_value >=0.0f) && (reference_value <=1.0f));
34  AlphaTestAttrib *attrib = new AlphaTestAttrib(mode,reference_value);
35  return return_new(attrib);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: AlphaTestAttrib::make_default
40 // Access: Published, Static
41 // Description: Returns a RenderAttrib that corresponds to whatever
42 // the standard default properties for render attributes
43 // of this type ought to be.
44 ////////////////////////////////////////////////////////////////////
46 make_default() {
47  return return_new(new AlphaTestAttrib);
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: AlphaTestAttrib::output
52 // Access: Public, Virtual
53 // Description:
54 ////////////////////////////////////////////////////////////////////
55 void AlphaTestAttrib::
56 output(ostream &out) const {
57  out << get_type() << ":";
58  output_comparefunc(out,_mode);
59  out << "," << _reference_alpha;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: AlphaTestAttrib::compare_to_impl
64 // Access: Protected, Virtual
65 // Description: Intended to be overridden by derived AlphaTestAttrib
66 // types to return a unique number indicating whether
67 // this AlphaTestAttrib is equivalent to the other one.
68 //
69 // This should return 0 if the two AlphaTestAttrib objects
70 // are equivalent, a number less than zero if this one
71 // should be sorted before the other one, and a number
72 // greater than zero otherwise.
73 //
74 // This will only be called with two AlphaTestAttrib
75 // objects whose get_type() functions return the same.
76 ////////////////////////////////////////////////////////////////////
77 int AlphaTestAttrib::
78 compare_to_impl(const RenderAttrib *other) const {
79  const AlphaTestAttrib *ta;
80  DCAST_INTO_R(ta, other, 0);
81  int compare_result = ((int)_mode - (int)ta->_mode) ;
82  if (compare_result != 0) {
83  return compare_result;
84  }
85 
86  if (_reference_alpha != ta->_reference_alpha) {
87  return _reference_alpha < ta->_reference_alpha ? -1 : 1;
88  }
89 
90  return 0;
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function: AlphaTestAttrib::get_hash_impl
95 // Access: Protected, Virtual
96 // Description: Intended to be overridden by derived RenderAttrib
97 // types to return a unique hash for these particular
98 // properties. RenderAttribs that compare the same with
99 // compare_to_impl(), above, should return the same
100 // hash; RenderAttribs that compare differently should
101 // return a different hash.
102 ////////////////////////////////////////////////////////////////////
103 size_t AlphaTestAttrib::
104 get_hash_impl() const {
105  size_t hash = 0;
106  hash = int_hash::add_hash(hash, (int)_mode);
107  hash = float_hash().add_hash(hash, _reference_alpha);
108  return hash;
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: AlphaTestAttrib::get_auto_shader_attrib_impl
113 // Access: Protected, Virtual
114 // Description:
115 ////////////////////////////////////////////////////////////////////
117 get_auto_shader_attrib_impl(const RenderState *state) const {
118  return this;
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: AlphaTestAttrib::register_with_read_factory
123 // Access: Public, Static
124 // Description: Tells the BamReader how to create objects of type
125 // AlphaTestAttrib.
126 ////////////////////////////////////////////////////////////////////
127 void AlphaTestAttrib::
128 register_with_read_factory() {
129  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
130 }
131 
132 ////////////////////////////////////////////////////////////////////
133 // Function: AlphaTestAttrib::write_datagram
134 // Access: Public, Virtual
135 // Description: Writes the contents of this object to the datagram
136 // for shipping out to a Bam file.
137 ////////////////////////////////////////////////////////////////////
140  RenderAttrib::write_datagram(manager, dg);
141 
142  dg.add_int8(_mode);
143  dg.add_stdfloat(_reference_alpha);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: AlphaTestAttrib::make_from_bam
148 // Access: Protected, Static
149 // Description: This function is called by the BamReader's factory
150 // when a new object of type AlphaTestAttrib is encountered
151 // in the Bam file. It should create the AlphaTestAttrib
152 // and extract its information from the file.
153 ////////////////////////////////////////////////////////////////////
154 TypedWritable *AlphaTestAttrib::
155 make_from_bam(const FactoryParams &params) {
156  AlphaTestAttrib *attrib = new AlphaTestAttrib;
157  DatagramIterator scan;
158  BamReader *manager;
159 
160  parse_params(params, scan, manager);
161  attrib->fillin(scan, manager);
162 
163  return attrib;
164 }
165 
166 ////////////////////////////////////////////////////////////////////
167 // Function: AlphaTestAttrib::fillin
168 // Access: Protected
169 // Description: This internal function is called by make_from_bam to
170 // read in all of the relevant data from the BamFile for
171 // the new AlphaTestAttrib.
172 ////////////////////////////////////////////////////////////////////
173 void AlphaTestAttrib::
174 fillin(DatagramIterator &scan, BamReader *manager) {
175  RenderAttrib::fillin(scan, manager);
176 
177  _mode = (PandaCompareFunc)scan.get_int8();
178  _reference_alpha = scan.get_stdfloat();
179 }
PN_int8 get_int8()
Extracts a signed 8-bit integer.
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
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
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
Enables or disables writing of pixel to framebuffer based on its alpha value relative to a reference ...
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
Definition: stl_compares.I:180
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