Panda3D
depthOffsetAttrib.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 depthOffsetAttrib.cxx
10  * @author drose
11  * @date 2002-03-14
12  */
13 
14 #include "depthOffsetAttrib.h"
16 #include "dcast.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle DepthOffsetAttrib::_type_handle;
23 int DepthOffsetAttrib::_attrib_slot;
24 
25 /**
26  * Constructs a new DepthOffsetAttrib object that indicates the relative
27  * amount of bias to write to the depth buffer for subsequent geometry.
28  */
29 CPT(RenderAttrib) DepthOffsetAttrib::
30 make(int offset) {
31  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(offset, 0.0f, 1.0f);
32  return return_new(attrib);
33 }
34 
35 /**
36  * Constructs a new DepthOffsetAttrib object that indicates the bias, and also
37  * specifies a minimum and maximum (or, more precisely, nearest and farthest)
38  * values to write to the depth buffer, in the range 0 .. 1. This range is 0,
39  * 1 by default; setting it to some other range can be used to create
40  * additional depth buffer effects.
41  */
42 CPT(RenderAttrib) DepthOffsetAttrib::
43 make(int offset, PN_stdfloat min_value, PN_stdfloat max_value) {
44  nassertr(min_value >= 0.0f && min_value <= 1.0f, nullptr);
45  nassertr(max_value >= 0.0f && max_value <= 1.0f, nullptr);
46  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(offset, min_value, max_value);
47  return return_new(attrib);
48 }
49 
50 /**
51  * Returns a RenderAttrib that corresponds to whatever the standard default
52  * properties for render attributes of this type ought to be.
53  */
54 CPT(RenderAttrib) DepthOffsetAttrib::
55 make_default() {
56  return return_new(new DepthOffsetAttrib(0, 0.0f, 1.0f));
57 }
58 
59 /**
60  *
61  */
62 void DepthOffsetAttrib::
63 output(std::ostream &out) const {
64  out << get_type() << ":(" << get_offset() << ", " << get_min_value()
65  << ", " << get_max_value() << ")";
66 }
67 
68 /**
69  * Intended to be overridden by derived DepthOffsetAttrib types to return a
70  * unique number indicating whether this DepthOffsetAttrib is equivalent to
71  * the other one.
72  *
73  * This should return 0 if the two DepthOffsetAttrib objects are equivalent, a
74  * number less than zero if this one should be sorted before the other one,
75  * and a number greater than zero otherwise.
76  *
77  * This will only be called with two DepthOffsetAttrib objects whose
78  * get_type() functions return the same.
79  */
80 int DepthOffsetAttrib::
81 compare_to_impl(const RenderAttrib *other) const {
82  const DepthOffsetAttrib *ta = (const DepthOffsetAttrib *)other;
83 
84  if (_offset != ta->_offset) {
85  return _offset - ta->_offset;
86  }
87  if (_min_value != ta->_min_value) {
88  return _min_value < ta->_min_value ? -1 : 1;
89  }
90  if (_max_value != ta->_max_value) {
91  return _max_value < ta->_max_value ? -1 : 1;
92  }
93  return 0;
94 }
95 
96 /**
97  * Intended to be overridden by derived RenderAttrib types to return a unique
98  * hash for these particular properties. RenderAttribs that compare the same
99  * with compare_to_impl(), above, should return the same hash; RenderAttribs
100  * that compare differently should return a different hash.
101  */
102 size_t DepthOffsetAttrib::
103 get_hash_impl() const {
104  size_t hash = 0;
105  hash = int_hash::add_hash(hash, _offset);
106  hash = float_hash().add_hash(hash, _min_value);
107  hash = float_hash().add_hash(hash, _max_value);
108  return hash;
109 }
110 
111 /**
112  * Intended to be overridden by derived RenderAttrib types to specify how two
113  * consecutive RenderAttrib objects of the same type interact.
114  *
115  * This should return the result of applying the other RenderAttrib to a node
116  * in the scene graph below this RenderAttrib, which was already applied. In
117  * most cases, the result is the same as the other RenderAttrib (that is, a
118  * subsequent RenderAttrib completely replaces the preceding one). On the
119  * other hand, some kinds of RenderAttrib (for instance, ColorTransformAttrib)
120  * might combine in meaningful ways.
121  */
122 CPT(RenderAttrib) DepthOffsetAttrib::
123 compose_impl(const RenderAttrib *other) const {
124  const DepthOffsetAttrib *ta = (const DepthOffsetAttrib *)other;
125 
126  int new_offset = ta->_offset + _offset;
127 
128  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(new_offset, ta->_min_value, ta->_max_value);
129  return return_new(attrib);
130 }
131 
132 /**
133  * Intended to be overridden by derived RenderAttrib types to specify how two
134  * consecutive RenderAttrib objects of the same type interact.
135  *
136  * See invert_compose() and compose_impl().
137  */
138 CPT(RenderAttrib) DepthOffsetAttrib::
139 invert_compose_impl(const RenderAttrib *other) const {
140  const DepthOffsetAttrib *ta = (const DepthOffsetAttrib *)other;
141 
142  int new_offset = ta->_offset - _offset;
143 
144  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(new_offset, ta->_min_value, ta->_max_value);
145  return return_new(attrib);
146 }
147 
148 /**
149  * Tells the BamReader how to create objects of type DepthOffsetAttrib.
150  */
151 void DepthOffsetAttrib::
152 register_with_read_factory() {
153  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
154 }
155 
156 /**
157  * Writes the contents of this object to the datagram for shipping out to a
158  * Bam file.
159  */
162  RenderAttrib::write_datagram(manager, dg);
163 
164  dg.add_int32(_offset);
165  if (manager->get_file_minor_ver() >= 31) {
166  dg.add_stdfloat(_min_value);
167  dg.add_stdfloat(_max_value);
168  }
169 }
170 
171 /**
172  * This function is called by the BamReader's factory when a new object of
173  * type DepthOffsetAttrib is encountered in the Bam file. It should create
174  * the DepthOffsetAttrib and extract its information from the file.
175  */
176 TypedWritable *DepthOffsetAttrib::
177 make_from_bam(const FactoryParams &params) {
178  DepthOffsetAttrib *attrib = new DepthOffsetAttrib(0, 0.0f, 1.0f);
179  DatagramIterator scan;
180  BamReader *manager;
181 
182  parse_params(params, scan, manager);
183  attrib->fillin(scan, manager);
184 
185  return attrib;
186 }
187 
188 /**
189  * This internal function is called by make_from_bam to read in all of the
190  * relevant data from the BamFile for the new DepthOffsetAttrib.
191  */
192 void DepthOffsetAttrib::
193 fillin(DatagramIterator &scan, BamReader *manager) {
194  RenderAttrib::fillin(scan, manager);
195 
196  _offset = scan.get_int32();
197  _min_value = 0.0f;
198  _max_value = 1.0f;
199  if (manager->get_file_minor_ver() >= 31) {
200  _min_value = scan.get_stdfloat();
201  _max_value = scan.get_stdfloat();
202  }
203 }
DatagramIterator::get_int32
int32_t get_int32()
Extracts a signed 32-bit integer.
Definition: datagramIterator.I:107
DepthOffsetAttrib::get_min_value
get_min_value
Returns the value for the minimum (closest) depth value to be stored in the buffer,...
Definition: depthOffsetAttrib.h:65
DepthOffsetAttrib::get_max_value
get_max_value
Returns the value for the maximum (farthest) depth value to be stored in the buffer,...
Definition: depthOffsetAttrib.h:66
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:526
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
CPT
CPT(RenderAttrib) DepthOffsetAttrib
Constructs a new DepthOffsetAttrib object that indicates the relative amount of bias to write to the ...
Definition: depthOffsetAttrib.cxx:29
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
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
BamWriter::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being written.
Definition: bamWriter.I:59
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
DepthOffsetAttrib::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: depthOffsetAttrib.cxx:161
DepthOffsetAttrib::get_offset
get_offset
Returns the depth offset represented by this attrib.
Definition: depthOffsetAttrib.h:64
depthOffsetAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
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
Datagram::add_int32
void add_int32(int32_t value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:67
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
BamReader::get_file_minor_ver
int get_file_minor_ver() const
Returns the minor version number of the Bam file currently being read.
Definition: bamReader.I:83
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DepthOffsetAttrib
This is a special kind of attribute that instructs the graphics driver to apply an offset or bias to ...
Definition: depthOffsetAttrib.h:50
graphicsStateGuardianBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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