Panda3D
 All Classes Functions Variables Enumerations
depthOffsetAttrib.cxx
1 // Filename: depthOffsetAttrib.cxx
2 // Created by: drose (14Mar02)
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 "depthOffsetAttrib.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 DepthOffsetAttrib::_type_handle;
24 int DepthOffsetAttrib::_attrib_slot;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: DepthOffsetAttrib::make
28 // Access: Published, Static
29 // Description: Constructs a new DepthOffsetAttrib object that
30 // indicates the relative amount of bias to write to the
31 // depth buffer for subsequent geometry.
32 ////////////////////////////////////////////////////////////////////
34 make(int offset) {
35  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(offset, 0.0f, 1.0f);
36  return return_new(attrib);
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: DepthOffsetAttrib::make
41 // Access: Published, Static
42 // Description: Constructs a new DepthOffsetAttrib object that
43 // indicates the bias, and also specifies a minimum and
44 // maximum (or, more precisely, nearest and farthest)
45 // values to write to the depth buffer, in the range 0
46 // .. 1. This range is 0, 1 by default; setting it to
47 // some other range can be used to create additional
48 // depth buffer effects.
49 ////////////////////////////////////////////////////////////////////
51 make(int offset, PN_stdfloat min_value, PN_stdfloat max_value) {
52  nassertr(min_value >= 0.0f && min_value <= 1.0f, NULL);
53  nassertr(max_value >= 0.0f && max_value <= 1.0f, NULL);
54  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(offset, min_value, max_value);
55  return return_new(attrib);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: DepthOffsetAttrib::make_default
60 // Access: Published, Static
61 // Description: Returns a RenderAttrib that corresponds to whatever
62 // the standard default properties for render attributes
63 // of this type ought to be.
64 ////////////////////////////////////////////////////////////////////
66 make_default() {
67  return return_new(new DepthOffsetAttrib(0, 0.0f, 1.0f));
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: DepthOffsetAttrib::output
72 // Access: Public, Virtual
73 // Description:
74 ////////////////////////////////////////////////////////////////////
75 void DepthOffsetAttrib::
76 output(ostream &out) const {
77  out << get_type() << ":(" << get_offset() << ", " << get_min_value()
78  << ", " << get_max_value() << ")";
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: DepthOffsetAttrib::compare_to_impl
83 // Access: Protected, Virtual
84 // Description: Intended to be overridden by derived DepthOffsetAttrib
85 // types to return a unique number indicating whether
86 // this DepthOffsetAttrib is equivalent to the other one.
87 //
88 // This should return 0 if the two DepthOffsetAttrib objects
89 // are equivalent, a number less than zero if this one
90 // should be sorted before the other one, and a number
91 // greater than zero otherwise.
92 //
93 // This will only be called with two DepthOffsetAttrib
94 // objects whose get_type() functions return the same.
95 ////////////////////////////////////////////////////////////////////
96 int DepthOffsetAttrib::
97 compare_to_impl(const RenderAttrib *other) const {
98  const DepthOffsetAttrib *ta;
99  DCAST_INTO_R(ta, other, 0);
100  if (_offset != ta->_offset) {
101  return _offset - ta->_offset;
102  }
103  if (_min_value != ta->_min_value) {
104  return _min_value < ta->_min_value ? -1 : 1;
105  }
106  if (_max_value != ta->_max_value) {
107  return _max_value < ta->_max_value ? -1 : 1;
108  }
109  return 0;
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: DepthOffsetAttrib::get_hash_impl
114 // Access: Protected, Virtual
115 // Description: Intended to be overridden by derived RenderAttrib
116 // types to return a unique hash for these particular
117 // properties. RenderAttribs that compare the same with
118 // compare_to_impl(), above, should return the same
119 // hash; RenderAttribs that compare differently should
120 // return a different hash.
121 ////////////////////////////////////////////////////////////////////
122 size_t DepthOffsetAttrib::
123 get_hash_impl() const {
124  size_t hash = 0;
125  hash = int_hash::add_hash(hash, _offset);
126  hash = float_hash().add_hash(hash, _min_value);
127  hash = float_hash().add_hash(hash, _max_value);
128  return hash;
129 }
130 
131 ////////////////////////////////////////////////////////////////////
132 // Function: DepthOffsetAttrib::compose_impl
133 // Access: Protected, Virtual
134 // Description: Intended to be overridden by derived RenderAttrib
135 // types to specify how two consecutive RenderAttrib
136 // objects of the same type interact.
137 //
138 // This should return the result of applying the other
139 // RenderAttrib to a node in the scene graph below this
140 // RenderAttrib, which was already applied. In most
141 // cases, the result is the same as the other
142 // RenderAttrib (that is, a subsequent RenderAttrib
143 // completely replaces the preceding one). On the other
144 // hand, some kinds of RenderAttrib (for instance,
145 // ColorTransformAttrib) might combine in meaningful
146 // ways.
147 ////////////////////////////////////////////////////////////////////
149 compose_impl(const RenderAttrib *other) const {
150  const DepthOffsetAttrib *ta;
151  DCAST_INTO_R(ta, other, 0);
152  int new_offset = ta->_offset + _offset;
153 
154  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(new_offset, ta->_min_value, ta->_max_value);
155  return return_new(attrib);
156 }
157 
158 ////////////////////////////////////////////////////////////////////
159 // Function: DepthOffsetAttrib::invert_compose_impl
160 // Access: Protected, Virtual
161 // Description: Intended to be overridden by derived RenderAttrib
162 // types to specify how two consecutive RenderAttrib
163 // objects of the same type interact.
164 //
165 // See invert_compose() and compose_impl().
166 ////////////////////////////////////////////////////////////////////
168 invert_compose_impl(const RenderAttrib *other) const {
169  const DepthOffsetAttrib *ta;
170  DCAST_INTO_R(ta, other, 0);
171  int new_offset = ta->_offset - _offset;
172 
173  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(new_offset, ta->_min_value, ta->_max_value);
174  return return_new(attrib);
175 }
176 
177 ////////////////////////////////////////////////////////////////////
178 // Function: DepthOffsetAttrib::register_with_read_factory
179 // Access: Public, Static
180 // Description: Tells the BamReader how to create objects of type
181 // DepthOffsetAttrib.
182 ////////////////////////////////////////////////////////////////////
185  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: DepthOffsetAttrib::write_datagram
190 // Access: Public, Virtual
191 // Description: Writes the contents of this object to the datagram
192 // for shipping out to a Bam file.
193 ////////////////////////////////////////////////////////////////////
196  RenderAttrib::write_datagram(manager, dg);
197 
198  dg.add_int32(_offset);
199  dg.add_stdfloat(_min_value);
200  dg.add_stdfloat(_max_value);
201 }
202 
203 ////////////////////////////////////////////////////////////////////
204 // Function: DepthOffsetAttrib::make_from_bam
205 // Access: Protected, Static
206 // Description: This function is called by the BamReader's factory
207 // when a new object of type DepthOffsetAttrib is encountered
208 // in the Bam file. It should create the DepthOffsetAttrib
209 // and extract its information from the file.
210 ////////////////////////////////////////////////////////////////////
211 TypedWritable *DepthOffsetAttrib::
212 make_from_bam(const FactoryParams &params) {
213  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(0, 0.0f, 1.0f);
214  DatagramIterator scan;
215  BamReader *manager;
216 
217  parse_params(params, scan, manager);
218  attrib->fillin(scan, manager);
219 
220  return attrib;
221 }
222 
223 ////////////////////////////////////////////////////////////////////
224 // Function: DepthOffsetAttrib::fillin
225 // Access: Protected
226 // Description: This internal function is called by make_from_bam to
227 // read in all of the relevant data from the BamFile for
228 // the new DepthOffsetAttrib.
229 ////////////////////////////////////////////////////////////////////
230 void DepthOffsetAttrib::
231 fillin(DatagramIterator &scan, BamReader *manager) {
232  RenderAttrib::fillin(scan, manager);
233 
234  _offset = scan.get_int32();
235  _min_value = 0.0f;
236  _max_value = 1.0f;
237  if (manager->get_file_minor_ver() >= 31) {
238  _min_value = scan.get_stdfloat();
239  _max_value = scan.get_stdfloat();
240  }
241 }
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
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_int32 get_int32()
Extracts a signed 32-bit integer.
static void register_with_read_factory()
Tells the BamReader how to create objects of type DepthOffsetAttrib.
PN_stdfloat get_min_value() const
Returns the value for the minimum (closest) depth value to be stored in the buffer, in the range 0
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.
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
int get_offset() const
Returns the depth offset represented by this attrib.
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
PN_stdfloat get_max_value() const
Returns the value for the maximum (farthest) depth value to be stored in the buffer, in the range 0
This is a special kind of attribute that instructs the graphics driver to apply an offset or bias to ...
void add_int32(PN_int32 value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:159
A class to retrieve the individual data elements previously stored in a Datagram. ...
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:105
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