Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/pgraph/fog.cxx

Go to the documentation of this file.
00001 // Filename: fog.cxx
00002 // Created by:  drose (14Mar02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "pandabase.h"
00020 
00021 #include "fog.h"
00022 
00023 #include "mathNumbers.h"
00024 #include "nodePath.h"
00025 #include "transformState.h"
00026 #include "bamReader.h"
00027 #include "bamWriter.h"
00028 #include "datagram.h"
00029 #include "datagramIterator.h"
00030 
00031 #include <stddef.h>
00032 
00033 TypeHandle Fog::_type_handle;
00034 
00035 ostream &
00036 operator << (ostream &out, Fog::Mode mode) {
00037   switch (mode) {
00038   case Fog::M_linear:
00039     return out << "linear";
00040 
00041   case Fog::M_exponential:
00042     return out << "exponential";
00043 
00044   case Fog::M_exponential_squared:
00045     return out << "exponential-squared";
00046   }
00047 
00048   return out << "**invalid**(" << (int)mode << ")";
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: Fog::Constructor
00053 //       Access: Published
00054 //  Description:
00055 ////////////////////////////////////////////////////////////////////
00056 Fog::
00057 Fog(const string &name) : 
00058   PandaNode(name) 
00059 {
00060   _mode = M_linear;
00061   _color.set(1.0f, 1.0f, 1.0f, 1.0f);
00062   _linear_onset_point.set(0.0f, 0.0f, 0.0f);
00063   _linear_opaque_point.set(0.0f, 100.0f, 0.0f);
00064   _exp_density = 0.5f;
00065   _linear_fallback_cosa = -1.0f;
00066   _linear_fallback_onset = 0.0f;
00067   _linear_fallback_opaque = 0.0f;
00068   _transformed_onset = 0.0f;
00069   _transformed_opaque = 0.0f;
00070 }
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //     Function: Fog::Copy Constructor
00074 //       Access: Protected
00075 //  Description:
00076 ////////////////////////////////////////////////////////////////////
00077 Fog::
00078 Fog(const Fog &copy) :
00079   PandaNode(copy)
00080 {
00081   _mode = copy._mode;
00082   _color = copy._color;
00083   _linear_onset_point = copy._linear_onset_point;
00084   _linear_opaque_point = copy._linear_opaque_point;
00085   _exp_density = copy._exp_density;
00086   _linear_fallback_cosa = copy._linear_fallback_cosa;
00087   _linear_fallback_onset = copy._linear_fallback_onset;
00088   _linear_fallback_opaque = copy._linear_fallback_opaque;
00089   _transformed_onset = copy._transformed_onset;
00090   _transformed_opaque = copy._transformed_opaque;
00091 }
00092 
00093 ////////////////////////////////////////////////////////////////////
00094 //     Function: Fog::Destructor
00095 //       Access: Public, Virtual
00096 //  Description:
00097 ////////////////////////////////////////////////////////////////////
00098 Fog::
00099 ~Fog() {
00100 }
00101 
00102 ////////////////////////////////////////////////////////////////////
00103 //     Function: Fog::make_copy
00104 //       Access: Public, Virtual
00105 //  Description: Returns a newly-allocated Node that is a shallow copy
00106 //               of this one.  It will be a different Node pointer,
00107 //               but its internal data may or may not be shared with
00108 //               that of the original Node.
00109 ////////////////////////////////////////////////////////////////////
00110 PandaNode *Fog::
00111 make_copy() const {
00112   return new Fog(*this);
00113 }
00114 
00115 ////////////////////////////////////////////////////////////////////
00116 //     Function: Fog::xform
00117 //       Access: Public, Virtual
00118 //  Description: Transforms the contents of this node by the indicated
00119 //               matrix, if it means anything to do so.  For most
00120 //               kinds of nodes, this does nothing.
00121 ////////////////////////////////////////////////////////////////////
00122 void Fog::
00123 xform(const LMatrix4f &mat) {
00124   _linear_onset_point = _linear_onset_point * mat;
00125   _linear_opaque_point = _linear_opaque_point * mat;
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: Fog::output
00130 //       Access: Public
00131 //  Description:
00132 ////////////////////////////////////////////////////////////////////
00133 void Fog::
00134 output(ostream &out) const {
00135   out << "fog: " << _mode;
00136   switch (_mode) {
00137   case M_linear:
00138     out << "(" << _linear_onset_point << ") -> ("
00139         << _linear_opaque_point << ")";
00140     break;
00141 
00142   case M_exponential:
00143   case M_exponential_squared:
00144     out << _exp_density;
00145     break;
00146   };
00147 }
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //     Function: Fog::adjust_to_camera
00151 //       Access: Public
00152 //  Description: This function is intended to be called by the cull
00153 //               traverser to compute the appropriate camera-relative
00154 //               onset and opaque distances, based on the fog node's
00155 //               position within the scene graph (if linear fog is in
00156 //               effect).
00157 ////////////////////////////////////////////////////////////////////
00158 void Fog::
00159 adjust_to_camera(const TransformState *camera_transform) {
00160   LVector3f forward = LVector3f::forward();
00161 
00162   LPoint3f onset_point, opaque_point;
00163   if (get_num_parents() != 0) {
00164     // Linear fog is relative to the fog's net transform in the scene
00165     // graph.
00166     NodePath this_np(this);
00167 
00168     CPT(TransformState) rel_transform = 
00169       camera_transform->invert_compose(this_np.get_net_transform());
00170     
00171     const LMatrix4f &mat = rel_transform->get_mat();
00172 
00173     // How far out of whack are we?
00174     LVector3f fog_vector = (_linear_opaque_point - _linear_onset_point) * mat;
00175     fog_vector.normalize();
00176     float cosa = fog_vector.dot(forward);
00177     if (cabs(cosa) < _linear_fallback_cosa) {
00178       // The fog vector is too far from the eye vector; use the
00179       // fallback mode.
00180       _transformed_onset = _linear_fallback_onset;
00181       _transformed_opaque = _linear_fallback_opaque;
00182 
00183     } else {
00184       _transformed_onset = forward.dot(_linear_onset_point * mat);
00185       _transformed_opaque = forward.dot(_linear_opaque_point * mat);
00186     }
00187 
00188   } else {
00189     // Not a camera-relative fog.
00190     _transformed_onset = forward.dot(_linear_onset_point);
00191     _transformed_opaque = forward.dot(_linear_opaque_point);
00192   }
00193 }
00194 
00195 ////////////////////////////////////////////////////////////////////
00196 //     Function: Fog::get_linear_range
00197 //       Access: Public
00198 //  Description: Retrieves the current onset and offset ranges.
00199 ////////////////////////////////////////////////////////////////////
00200 void Fog::
00201 get_linear_range(float &onset, float &opaque) {
00202   onset = _transformed_onset;
00203   opaque = _transformed_opaque;
00204 }
00205 
00206 ////////////////////////////////////////////////////////////////////
00207 //     Function: Fog::register_with_read_factory
00208 //       Access: Public, Static
00209 //  Description: Tells the BamReader how to create objects of type
00210 //               Fog.
00211 ////////////////////////////////////////////////////////////////////
00212 void Fog::
00213 register_with_read_factory() {
00214   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00215 }
00216 
00217 ////////////////////////////////////////////////////////////////////
00218 //     Function: Fog::write_datagram
00219 //       Access: Public, Virtual
00220 //  Description: Writes the contents of this object to the datagram
00221 //               for shipping out to a Bam file.
00222 ////////////////////////////////////////////////////////////////////
00223 void Fog::
00224 write_datagram(BamWriter *manager, Datagram &dg) {
00225   PandaNode::write_datagram(manager, dg);
00226 
00227   dg.add_int8(_mode);
00228   _color.write_datagram(dg);
00229   _linear_onset_point.write_datagram(dg);
00230   _linear_opaque_point.write_datagram(dg);
00231   dg.add_float32(_exp_density);
00232   dg.add_float32(_linear_fallback_cosa);
00233   dg.add_float32(_linear_fallback_onset);
00234   dg.add_float32(_linear_fallback_opaque);
00235 }
00236 
00237 ////////////////////////////////////////////////////////////////////
00238 //     Function: Fog::make_from_bam
00239 //       Access: Protected, Static
00240 //  Description: This function is called by the BamReader's factory
00241 //               when a new object of type Fog is encountered
00242 //               in the Bam file.  It should create the Fog
00243 //               and extract its information from the file.
00244 ////////////////////////////////////////////////////////////////////
00245 TypedWritable *Fog::
00246 make_from_bam(const FactoryParams &params) {
00247   Fog *node = new Fog("");
00248   DatagramIterator scan;
00249   BamReader *manager;
00250 
00251   parse_params(params, scan, manager);
00252   node->fillin(scan, manager);
00253 
00254   return node;
00255 }
00256 
00257 ////////////////////////////////////////////////////////////////////
00258 //     Function: Fog::fillin
00259 //       Access: Protected
00260 //  Description: This internal function is called by make_from_bam to
00261 //               read in all of the relevant data from the BamFile for
00262 //               the new Fog.
00263 ////////////////////////////////////////////////////////////////////
00264 void Fog::
00265 fillin(DatagramIterator &scan, BamReader *manager) {
00266   PandaNode::fillin(scan, manager);
00267 
00268   _mode = (Mode)scan.get_int8();
00269   _color.read_datagram(scan);
00270   _linear_onset_point.read_datagram(scan);
00271   _linear_opaque_point.read_datagram(scan);
00272   _exp_density = scan.get_float32();
00273   _linear_fallback_cosa = scan.get_float32();
00274   _linear_fallback_onset = scan.get_float32();
00275   _linear_fallback_opaque = scan.get_float32();
00276 }

Generated on Fri May 2 00:41:37 2003 for Panda by doxygen1.3