Panda3D
fog.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 fog.cxx
10  * @author drose
11  * @date 2002-03-14
12  */
13 
14 #include "pandabase.h"
15 
16 #include "fog.h"
17 
18 #include "mathNumbers.h"
19 #include "nodePath.h"
20 #include "transformState.h"
21 #include "bamReader.h"
22 #include "bamWriter.h"
23 #include "datagram.h"
24 #include "datagramIterator.h"
25 
26 #include <stddef.h>
27 
28 TypeHandle Fog::_type_handle;
29 
30 std::ostream &
31 operator << (std::ostream &out, Fog::Mode mode) {
32  switch (mode) {
33  case Fog::M_linear:
34  return out << "linear";
35 
36  case Fog::M_exponential:
37  return out << "exponential";
38 
39  case Fog::M_exponential_squared:
40  return out << "exponential-squared";
41  }
42 
43  return out << "**invalid**(" << (int)mode << ")";
44 }
45 
46 /**
47  *
48  */
49 Fog::
50 Fog(const std::string &name) :
51  PandaNode(name)
52 {
53  _mode = M_linear;
54  _color.set(1.0f, 1.0f, 1.0f, 1.0f);
55  _linear_onset_point.set(0.0f, 0.0f, 0.0f);
56  _linear_opaque_point.set(0.0f, 100.0f, 0.0f);
57  _exp_density = 0.5f;
58  _linear_fallback_cosa = -1.0f;
59  _linear_fallback_onset = 0.0f;
60  _linear_fallback_opaque = 0.0f;
61  _transformed_onset = 0.0f;
62  _transformed_opaque = 0.0f;
63 }
64 
65 /**
66  *
67  */
68 Fog::
69 Fog(const Fog &copy) :
70  PandaNode(copy)
71 {
72  _mode = copy._mode;
73  _color = copy._color;
74  _linear_onset_point = copy._linear_onset_point;
75  _linear_opaque_point = copy._linear_opaque_point;
76  _exp_density = copy._exp_density;
77  _linear_fallback_cosa = copy._linear_fallback_cosa;
78  _linear_fallback_onset = copy._linear_fallback_onset;
79  _linear_fallback_opaque = copy._linear_fallback_opaque;
80  _transformed_onset = copy._transformed_onset;
81  _transformed_opaque = copy._transformed_opaque;
82 }
83 
84 /**
85  *
86  */
87 Fog::
88 ~Fog() {
89 }
90 
91 /**
92  * Returns a newly-allocated Node that is a shallow copy of this one. It will
93  * be a different Node pointer, but its internal data may or may not be shared
94  * with that of the original Node.
95  */
97 make_copy() const {
98  return new Fog(*this);
99 }
100 
101 /**
102  * Transforms the contents of this node by the indicated matrix, if it means
103  * anything to do so. For most kinds of nodes, this does nothing.
104  */
105 void Fog::
106 xform(const LMatrix4 &mat) {
107  _linear_onset_point = _linear_onset_point * mat;
108  _linear_opaque_point = _linear_opaque_point * mat;
109 }
110 
111 /**
112  *
113  */
114 void Fog::
115 output(std::ostream &out) const {
116  out << "fog: " << _mode;
117  switch (_mode) {
118  case M_linear:
119  out << "(" << _linear_onset_point << ") -> ("
120  << _linear_opaque_point << ")";
121  break;
122 
123  case M_exponential:
124  case M_exponential_squared:
125  out << _exp_density;
126  break;
127  };
128 }
129 
130 /**
131  * This function is intended to be called by the cull traverser to compute the
132  * appropriate camera-relative onset and opaque distances, based on the fog
133  * node's position within the scene graph (if linear fog is in effect).
134  */
135 void Fog::
136 adjust_to_camera(const TransformState *camera_transform) {
137  LVector3 forward = LVector3::forward();
138 
139  if (get_num_parents() != 0) {
140  // Linear fog is relative to the fog's net transform in the scene graph.
141  NodePath this_np(this);
142 
143  CPT(TransformState) rel_transform =
144  camera_transform->invert_compose(this_np.get_net_transform());
145 
146  const LMatrix4 &mat = rel_transform->get_mat();
147 
148  // How far out of whack are we?
149  LVector3 fog_vector = (_linear_opaque_point - _linear_onset_point) * mat;
150  fog_vector.normalize();
151  PN_stdfloat cosa = fog_vector.dot(forward);
152  if (cabs(cosa) < _linear_fallback_cosa) {
153  // The fog vector is too far from the eye vector; use the fallback mode.
154  _transformed_onset = _linear_fallback_onset;
155  _transformed_opaque = _linear_fallback_opaque;
156 
157  } else {
158  _transformed_onset = forward.dot(_linear_onset_point * mat);
159  _transformed_opaque = forward.dot(_linear_opaque_point * mat);
160  }
161 
162  } else {
163  // Not a camera-relative fog.
164  _transformed_onset = forward.dot(_linear_onset_point);
165  _transformed_opaque = forward.dot(_linear_opaque_point);
166  }
167 }
168 
169 /**
170  * Retrieves the current onset and offset ranges.
171  */
172 void Fog::
173 get_linear_range(PN_stdfloat &onset, PN_stdfloat &opaque) {
174  onset = _transformed_onset;
175  opaque = _transformed_opaque;
176 }
177 
178 /**
179  * Tells the BamReader how to create objects of type Fog.
180  */
181 void Fog::
183  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
184 }
185 
186 /**
187  * Writes the contents of this object to the datagram for shipping out to a
188  * Bam file.
189  */
190 void Fog::
192  PandaNode::write_datagram(manager, dg);
193 
194  dg.add_int8(_mode);
195  _color.write_datagram(dg);
196  _linear_onset_point.write_datagram(dg);
197  _linear_opaque_point.write_datagram(dg);
198  dg.add_stdfloat(_exp_density);
199  dg.add_stdfloat(_linear_fallback_cosa);
200  dg.add_stdfloat(_linear_fallback_onset);
201  dg.add_stdfloat(_linear_fallback_opaque);
202 }
203 
204 /**
205  * This function is called by the BamReader's factory when a new object of
206  * type Fog is encountered in the Bam file. It should create the Fog and
207  * extract its information from the file.
208  */
209 TypedWritable *Fog::
210 make_from_bam(const FactoryParams &params) {
211  Fog *node = new Fog("");
212  DatagramIterator scan;
213  BamReader *manager;
214 
215  parse_params(params, scan, manager);
216  node->fillin(scan, manager);
217 
218  return node;
219 }
220 
221 /**
222  * This internal function is called by make_from_bam to read in all of the
223  * relevant data from the BamFile for the new Fog.
224  */
225 void Fog::
226 fillin(DatagramIterator &scan, BamReader *manager) {
227  PandaNode::fillin(scan, manager);
228 
229  _mode = (Mode)scan.get_int8();
230  _color.read_datagram(scan);
231  _linear_onset_point.read_datagram(scan);
232  _linear_opaque_point.read_datagram(scan);
233  _exp_density = scan.get_stdfloat();
234  _linear_fallback_cosa = scan.get_stdfloat();
235  _linear_fallback_onset = scan.get_stdfloat();
236  _linear_fallback_opaque = scan.get_stdfloat();
237 }
void get_linear_range(PN_stdfloat &onset, PN_stdfloat &opaque)
Retrieves the current onset and offset ranges.
Definition: fog.cxx:173
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
Indicates a coordinate-system transform on vertices.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
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: pandaNode.cxx:3589
static void register_with_read_factory()
Tells the BamReader how to create objects of type Fog.
Definition: fog.cxx:182
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
get_num_parents
Returns the number of parent nodes this node has.
Definition: pandaNode.h:118
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual PandaNode * make_copy() const
Returns a newly-allocated Node that is a shallow copy of this one.
Definition: fog.cxx:97
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void adjust_to_camera(const TransformState *camera_transform)
This function is intended to be called by the cull traverser to compute the appropriate camera-relati...
Definition: fog.cxx:136
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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: fog.cxx:191
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
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
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
Specifies how atmospheric fog effects are applied to geometry.
Definition: fog.h:41
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A class to retrieve the individual data elements previously stored in a Datagram.
int8_t get_int8()
Extracts a signed 8-bit integer.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual void xform(const LMatrix4 &mat)
Transforms the contents of this node by the indicated matrix, if it means anything to do so.
Definition: fog.cxx:106
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.