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

panda/src/egg/eggAttributes.cxx

Go to the documentation of this file.
00001 // Filename: eggAttributes.cxx
00002 // Created by:  drose (16Jan99)
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 "eggAttributes.h"
00020 #include "eggParameters.h"
00021 #include "eggMorph.h"
00022 #include "eggMorphList.h"
00023 
00024 #include <indent.h>
00025 #include <math.h>
00026 
00027 TypeHandle EggAttributes::_type_handle;
00028 
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //     Function: EggAttributes::Constructor
00032 //       Access: Public
00033 //  Description:
00034 ////////////////////////////////////////////////////////////////////
00035 EggAttributes::
00036 EggAttributes() {
00037   _flags = 0;
00038 }
00039 
00040 ////////////////////////////////////////////////////////////////////
00041 //     Function: EggAttributes::Copy constructor
00042 //       Access: Public
00043 //  Description:
00044 ////////////////////////////////////////////////////////////////////
00045 EggAttributes::
00046 EggAttributes(const EggAttributes &copy) {
00047   (*this) = copy;
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: EggAttributes::Copy assignment operator
00052 //       Access: Public
00053 //  Description:
00054 ////////////////////////////////////////////////////////////////////
00055 EggAttributes &EggAttributes::
00056 operator = (const EggAttributes &copy) {
00057   _flags = copy._flags;
00058   _normal = copy._normal;
00059   _uv = copy._uv;
00060   _color = copy._color;
00061   _dnormals = copy._dnormals;
00062   _duvs = copy._duvs;
00063   _drgbas = copy._drgbas;
00064   return *this;
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: EggAttributes::Destructor
00069 //       Access: Public, Virtual
00070 //  Description:
00071 ////////////////////////////////////////////////////////////////////
00072 EggAttributes::
00073 ~EggAttributes() {
00074 }
00075 
00076 
00077 ////////////////////////////////////////////////////////////////////
00078 //     Function: EggAttributes::write
00079 //       Access: Public
00080 //  Description: Writes the attributes to the indicated output stream in
00081 //               Egg format.
00082 ////////////////////////////////////////////////////////////////////
00083 void EggAttributes::
00084 write(ostream &out, int indent_level) const {
00085   if (has_normal()) {
00086     if (_dnormals.empty()) {
00087       indent(out, indent_level)
00088         << "<Normal> { " << get_normal() << " }\n";
00089     } else {
00090       indent(out, indent_level) << "<Normal> {\n";
00091       indent(out, indent_level+2) << get_normal() << "\n";
00092       _dnormals.write(out, indent_level+2);
00093       indent(out, indent_level) << "}\n";
00094     }
00095   }
00096   if (has_uv()) {
00097     if (_duvs.empty()) {
00098       indent(out, indent_level)
00099         << "<UV> { " << get_uv() << " }\n";
00100     } else {
00101       indent(out, indent_level) << "<UV> {\n";
00102       indent(out, indent_level+2) << get_uv() << "\n";
00103       _duvs.write(out, indent_level+2);
00104       indent(out, indent_level) << "}\n";
00105     }
00106   }
00107   if (has_color()) {
00108     if (_drgbas.empty()) {
00109       indent(out, indent_level)
00110         << "<RGBA> { " << get_color() << " }\n";
00111     } else {
00112       indent(out, indent_level) << "<RGBA> {\n";
00113       indent(out, indent_level+2) << get_color() << "\n";
00114       _drgbas.write(out, indent_level+2);
00115       indent(out, indent_level) << "}\n";
00116     }
00117   }
00118 }
00119 
00120 
00121 ////////////////////////////////////////////////////////////////////
00122 //     Function: EggAttributes::sorts_less_than
00123 //       Access: Public
00124 //  Description: An ordering operator to compare two vertices for
00125 //               sorting order.  This imposes an arbitrary ordering
00126 //               useful to identify unique vertices.
00127 ////////////////////////////////////////////////////////////////////
00128 bool EggAttributes::
00129 sorts_less_than(const EggAttributes &other) const {
00130   if (_flags != other._flags) {
00131     return _flags < other._flags;
00132   }
00133 
00134   if (has_normal()) {
00135     int compare =
00136       _normal.compare_to(other._normal, egg_parameters->_normal_threshold);
00137     if (compare != 0) {
00138       return compare < 0;
00139     }
00140     if (_dnormals != other._dnormals) {
00141       return _dnormals < other._dnormals;
00142     }
00143   }
00144 
00145   if (has_uv()) {
00146     int compare =
00147       _uv.compare_to(other._uv, egg_parameters->_uv_threshold);
00148     if (compare != 0) {
00149       return compare < 0;
00150     }
00151     if (_duvs != other._duvs) {
00152       return _duvs < other._duvs;
00153     }
00154   }
00155 
00156   if (has_color()) {
00157     int compare =
00158       _color.compare_to(other._color, egg_parameters->_color_threshold);
00159     if (compare != 0) {
00160       return compare < 0;
00161     }
00162     if (_drgbas != other._drgbas) {
00163       return _drgbas < other._drgbas;
00164     }
00165   }
00166 
00167   return false;
00168 }
00169 
00170 ////////////////////////////////////////////////////////////////////
00171 //     Function: EggAttributes::transform
00172 //       Access: Public, Virtual
00173 //  Description: Applies the indicated transformation matrix to the
00174 //               attributes.
00175 ////////////////////////////////////////////////////////////////////
00176 void EggAttributes::
00177 transform(const LMatrix4d &mat) {
00178   if (has_normal()) {
00179     _normal = _normal * mat;
00180     LVector3d old_normal = _normal;
00181     _normal.normalize();
00182 
00183     EggMorphNormalList::iterator mi;
00184     for (mi = _dnormals.begin(); mi != _dnormals.end(); ++mi) {
00185       // We can safely cast the morph object to a non-const, because
00186       // we're not changing its name, which is the only thing the set
00187       // cares about preserving.
00188       EggMorphNormal &morph = (EggMorphNormal &)(*mi);
00189 
00190       // A bit of funny business to ensure the offset normal is
00191       // normalized after the transform.  This will break strange
00192       // normal morphs that want to change the length of the normal,
00193       // but what else can we do?
00194       LVector3d offset = (*mi).get_offset() * mat;
00195       LVector3d n = old_normal + offset;
00196       n.normalize();
00197       morph.set_offset(n - _normal);
00198     }
00199   }
00200 }

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