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

panda/src/gobj/LOD.cxx

Go to the documentation of this file.
00001 // Filename: LOD.cxx
00002 // Created by:  mike (09Jan97)
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 #include "LOD.h"
00019 
00020 #include "datagram.h"
00021 #include "datagramIterator.h"
00022 #include "indent.h"
00023 #include "config_gobj.h"
00024 
00025 #define EXPCL EXPCL_PANDA
00026 #define EXPTP EXPTP_PANDA
00027 #define TYPE LODSwitch
00028 #define NAME LODSwitchVector
00029 
00030 #include "vector_src.cxx"
00031 
00032 float LOD::_stress_factor = lod_stress_factor;
00033 
00034 ////////////////////////////////////////////////////////////////////
00035 //     Function: LOD::constructor
00036 //       Access: Public
00037 //  Description:
00038 ////////////////////////////////////////////////////////////////////
00039 LOD::
00040 LOD(void) {
00041   _center.set(0.0f, 0.0f, 0.0f);
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: LOD::Copy Constructor
00046 //       Access: Public
00047 //  Description:
00048 ////////////////////////////////////////////////////////////////////
00049 LOD::
00050 LOD(const LOD &copy) :
00051   _center(copy._center),
00052   _switch_vector(copy._switch_vector)
00053 {
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: LOD::destructor
00058 //       Access: Public
00059 //  Description:
00060 ////////////////////////////////////////////////////////////////////
00061 LOD::
00062 ~LOD(void) {
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: LOD::xform
00067 //       Access: Public
00068 //  Description: Transforms the LOD specification by the indicated
00069 //               matrix.
00070 ////////////////////////////////////////////////////////////////////
00071 void LOD::
00072 xform(const LMatrix4f &mat) {
00073   _center = _center * mat;
00074 
00075   // We'll take just the length of the y axis as the matrix's scale.
00076   LVector3f y;
00077   mat.get_row3(y,1);
00078   float factor_squared = y.length_squared();
00079 
00080   LODSwitchVector::iterator si;
00081   for (si = _switch_vector.begin(); si != _switch_vector.end(); ++si) {
00082     (*si).rescale(factor_squared);
00083   }
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: LOD::compute_child
00088 //       Access: Public
00089 //  Description: Computes the distance between two points and returns
00090 //               the index for the child of the LOD by testing against
00091 //               the corresponding list of switching distances.
00092 ////////////////////////////////////////////////////////////////////
00093 int LOD::
00094 compute_child(const LPoint3f &cam_pos, const LPoint3f &center) const {
00095 
00096   LVector3f v = cam_pos - center;
00097   float dist = dot(v, v) * _stress_factor;
00098   LODSwitchVector::const_iterator i;
00099   int child = 0;
00100   for (i = _switch_vector.begin(), child = 0;
00101        i != _switch_vector.end(); ++i, ++child) {
00102     if ((*i).in_range(dist))
00103       break;
00104   }
00105 
00106   if(debug_LOD_mode) {
00107       //not ifndef NDEBUG'ing this out since need it at Opt4 for perf measurements
00108       if(select_LOD_number>=0) {
00109           return select_LOD_number;
00110       }
00111 
00112       // since lowest level LOD is lev 0, must invert the meaning of
00113       // so minimum_LOD_number 0 will screen out no LODs, and increasing it
00114       // will screen out successively higher levels
00115       int max_allowed_LOD_number = _switch_vector.size() - minimum_LOD_number;
00116       if(child > max_allowed_LOD_number) {
00117           child = max_allowed_LOD_number;
00118       }
00119   }
00120 
00121   return child;
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: LOD::write_datagram
00126 //       Access: Public
00127 //  Description: Writes the contents of the LOD out to the datagram,
00128 //               presumably in preparation to writing to a Bam file.
00129 ////////////////////////////////////////////////////////////////////
00130 void LOD::
00131 write_datagram(Datagram &destination) const {
00132   _center.write_datagram(destination);
00133 
00134   destination.add_uint16(_switch_vector.size());
00135 
00136   LODSwitchVector::const_iterator si;
00137   for (si = _switch_vector.begin();
00138        si != _switch_vector.end();
00139        ++si) {
00140     (*si).write_datagram(destination);
00141   }
00142 }
00143 
00144 ////////////////////////////////////////////////////////////////////
00145 //     Function: LOD::read_datagram
00146 //       Access: Public
00147 //  Description: Reads the contents of the LOD from the datagram,
00148 //               presumably in response to reading a Bam file.
00149 ////////////////////////////////////////////////////////////////////
00150 void LOD::
00151 read_datagram(DatagramIterator &source) {
00152   _center.read_datagram(source);
00153 
00154   _switch_vector.clear();
00155 
00156   int num_switches = source.get_uint16();
00157   _switch_vector.reserve(num_switches);
00158   for (int i = 0; i < num_switches; i++) {
00159     _switch_vector.push_back(LODSwitch(0, 0));
00160     _switch_vector.back().read_datagram(source);
00161   }
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: LOD::output
00166 //       Access: Public
00167 //  Description:
00168 ////////////////////////////////////////////////////////////////////
00169 void LOD::
00170 output(ostream &out) const {
00171   if (_switch_vector.empty()) {
00172     out << "no switches.";
00173   } else {
00174     LODSwitchVector::const_iterator si;
00175     si = _switch_vector.begin();
00176     out << "(" << (*si).get_in() << "/" << (*si).get_out() << ")";
00177     ++si;
00178     while (si != _switch_vector.end()) {
00179       out << " (" << (*si).get_in() << "/" << (*si).get_out() << ")";
00180       ++si;
00181     }
00182   }
00183 }
00184 
00185 ////////////////////////////////////////////////////////////////////
00186 //     Function: LOD::write
00187 //       Access: Public
00188 //  Description:
00189 ////////////////////////////////////////////////////////////////////
00190 void LOD::
00191 write(ostream &out, int indent_level) const {
00192   indent(out, indent_level)
00193     << "LOD, " << _switch_vector.size() << " switches:\n";
00194   LODSwitchVector::const_iterator si;
00195   int i = 0;
00196   for (si = _switch_vector.begin();
00197        si != _switch_vector.end();
00198        ++si) {
00199     indent(out, indent_level + 2)
00200       << i << ". in at " << (*si).get_in()
00201       << ", out at " << (*si).get_out() << "\n";
00202     i++;
00203   }
00204 }
00205 
00206 ////////////////////////////////////////////////////////////////////
00207 //     Function: LOD::set_stress_factor
00208 //       Access: Published, Static
00209 //  Description: Sets the factor that globally scales all LOD's.  This
00210 //               factor is applied to the square of the LOD distance,
00211 //               so the larger the number, the lower the detail that
00212 //               is presented.  The normal value is 1.0.
00213 ////////////////////////////////////////////////////////////////////
00214 void LOD::
00215 set_stress_factor(float stress_factor) {
00216   _stress_factor = stress_factor;
00217 }
00218 
00219 ////////////////////////////////////////////////////////////////////
00220 //     Function: LOD::get_stress_factor
00221 //       Access: Published, Static
00222 //  Description: Returns the factor that globally scales all LOD's.
00223 //               See get_stress_factor().
00224 ////////////////////////////////////////////////////////////////////
00225 float LOD::
00226 get_stress_factor() {
00227   return _stress_factor;
00228 }

Generated on Fri May 2 00:39:36 2003 for Panda by doxygen1.3