Panda3D
transparencyAttrib.cxx
1 // Filename: transparencyAttrib.cxx
2 // Created by: drose (28Feb02)
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 "transparencyAttrib.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 TransparencyAttrib::_type_handle;
24 int TransparencyAttrib::_attrib_slot;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: TransparencyAttrib::make
28 // Access: Published, Static
29 // Description: Constructs a new TransparencyAttrib object.
30 ////////////////////////////////////////////////////////////////////
31 CPT(RenderAttrib) TransparencyAttrib::
32 make(TransparencyAttrib::Mode mode) {
33  TransparencyAttrib *attrib = new TransparencyAttrib(mode);
34  return return_new(attrib);
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: TransparencyAttrib::make_default
39 // Access: Published, Static
40 // Description: Returns a RenderAttrib that corresponds to whatever
41 // the standard default properties for render attributes
42 // of this type ought to be.
43 ////////////////////////////////////////////////////////////////////
44 CPT(RenderAttrib) TransparencyAttrib::
45 make_default() {
46  return return_new(new TransparencyAttrib);
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: TransparencyAttrib::output
51 // Access: Public, Virtual
52 // Description:
53 ////////////////////////////////////////////////////////////////////
54 void TransparencyAttrib::
55 output(ostream &out) const {
56  out << get_type() << ":";
57  switch (get_mode()) {
58  case M_none:
59  out << "none";
60  break;
61 
62  case M_alpha:
63  out << "alpha";
64  break;
65 
66  case M_multisample:
67  out << "multisample";
68  break;
69 
70  case M_multisample_mask:
71  out << "multisample mask";
72  break;
73 
74  case M_binary:
75  out << "binary";
76  break;
77 
78  case M_dual:
79  out << "dual";
80  break;
81 
82  case M_notused:
83  break;
84  }
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: TransparencyAttrib::compare_to_impl
89 // Access: Protected, Virtual
90 // Description: Intended to be overridden by derived TransparencyAttrib
91 // types to return a unique number indicating whether
92 // this TransparencyAttrib is equivalent to the other one.
93 //
94 // This should return 0 if the two TransparencyAttrib objects
95 // are equivalent, a number less than zero if this one
96 // should be sorted before the other one, and a number
97 // greater than zero otherwise.
98 //
99 // This will only be called with two TransparencyAttrib
100 // objects whose get_type() functions return the same.
101 ////////////////////////////////////////////////////////////////////
102 int TransparencyAttrib::
103 compare_to_impl(const RenderAttrib *other) const {
104  const TransparencyAttrib *ta;
105  DCAST_INTO_R(ta, other, 0);
106  return (int)_mode - (int)ta->_mode;
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: TransparencyAttrib::get_hash_impl
111 // Access: Protected, Virtual
112 // Description: Intended to be overridden by derived RenderAttrib
113 // types to return a unique hash for these particular
114 // properties. RenderAttribs that compare the same with
115 // compare_to_impl(), above, should return the same
116 // hash; RenderAttribs that compare differently should
117 // return a different hash.
118 ////////////////////////////////////////////////////////////////////
119 size_t TransparencyAttrib::
120 get_hash_impl() const {
121  size_t hash = 0;
122  hash = int_hash::add_hash(hash, (int)_mode);
123  return hash;
124 }
125 
126 ////////////////////////////////////////////////////////////////////
127 // Function: TransparencyAttrib::get_auto_shader_attrib_impl
128 // Access: Protected, Virtual
129 // Description:
130 ////////////////////////////////////////////////////////////////////
131 CPT(RenderAttrib) TransparencyAttrib::
132 get_auto_shader_attrib_impl(const RenderState *state) const {
133  return this;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: TransparencyAttrib::register_with_read_factory
138 // Access: Public, Static
139 // Description: Tells the BamReader how to create objects of type
140 // TransparencyAttrib.
141 ////////////////////////////////////////////////////////////////////
142 void TransparencyAttrib::
143 register_with_read_factory() {
144  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
145 }
146 
147 ////////////////////////////////////////////////////////////////////
148 // Function: TransparencyAttrib::write_datagram
149 // Access: Public, Virtual
150 // Description: Writes the contents of this object to the datagram
151 // for shipping out to a Bam file.
152 ////////////////////////////////////////////////////////////////////
155  RenderAttrib::write_datagram(manager, dg);
156 
157  dg.add_int8(_mode);
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: TransparencyAttrib::make_from_bam
162 // Access: Protected, Static
163 // Description: This function is called by the BamReader's factory
164 // when a new object of type TransparencyAttrib is encountered
165 // in the Bam file. It should create the TransparencyAttrib
166 // and extract its information from the file.
167 ////////////////////////////////////////////////////////////////////
168 TypedWritable *TransparencyAttrib::
169 make_from_bam(const FactoryParams &params) {
171  DatagramIterator scan;
172  BamReader *manager;
173 
174  parse_params(params, scan, manager);
175  attrib->fillin(scan, manager);
176 
177  return attrib;
178 }
179 
180 ////////////////////////////////////////////////////////////////////
181 // Function: TransparencyAttrib::fillin
182 // Access: Protected
183 // Description: This internal function is called by make_from_bam to
184 // read in all of the relevant data from the BamFile for
185 // the new TransparencyAttrib.
186 ////////////////////////////////////////////////////////////////////
187 void TransparencyAttrib::
188 fillin(DatagramIterator &scan, BamReader *manager) {
189  RenderAttrib::fillin(scan, manager);
190 
191  _mode = (Mode)scan.get_int8();
192 }
PN_int8 get_int8()
Extracts a signed 8-bit integer.
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
This controls the enabling of transparency.
Mode get_mode() const
Returns the transparency mode.
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
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.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
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
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