Panda3D

eggAttributes.cxx

00001 // Filename: eggAttributes.cxx
00002 // Created by:  drose (16Jan99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "eggAttributes.h"
00016 #include "eggParameters.h"
00017 #include "eggMorph.h"
00018 #include "eggMorphList.h"
00019 
00020 #include "indent.h"
00021 
00022 TypeHandle EggAttributes::_type_handle;
00023 
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: EggAttributes::Constructor
00027 //       Access: Published
00028 //  Description:
00029 ////////////////////////////////////////////////////////////////////
00030 EggAttributes::
00031 EggAttributes() {
00032   _flags = 0;
00033 }
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: EggAttributes::Copy constructor
00037 //       Access: Published
00038 //  Description:
00039 ////////////////////////////////////////////////////////////////////
00040 EggAttributes::
00041 EggAttributes(const EggAttributes &copy) {
00042   (*this) = copy;
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: EggAttributes::Copy assignment operator
00047 //       Access: Published
00048 //  Description:
00049 ////////////////////////////////////////////////////////////////////
00050 EggAttributes &EggAttributes::
00051 operator = (const EggAttributes &copy) {
00052   _flags = copy._flags;
00053   _normal = copy._normal;
00054   _color = copy._color;
00055   _dnormals = copy._dnormals;
00056   _drgbas = copy._drgbas;
00057   return *this;
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: EggAttributes::Destructor
00062 //       Access: Published, Virtual
00063 //  Description:
00064 ////////////////////////////////////////////////////////////////////
00065 EggAttributes::
00066 ~EggAttributes() {
00067 }
00068 
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //     Function: EggAttributes::write
00072 //       Access: Published
00073 //  Description: Writes the attributes to the indicated output stream in
00074 //               Egg format.
00075 ////////////////////////////////////////////////////////////////////
00076 void EggAttributes::
00077 write(ostream &out, int indent_level) const {
00078   if (has_normal()) {
00079     if (_dnormals.empty()) {
00080       indent(out, indent_level)
00081         << "<Normal> { " << get_normal() << " }\n";
00082     } else {
00083       indent(out, indent_level) << "<Normal> {\n";
00084       indent(out, indent_level + 2) << get_normal() << "\n";
00085       _dnormals.write(out, indent_level + 2, "<DNormal>", 3);
00086       indent(out, indent_level) << "}\n";
00087     }
00088   }
00089   if (has_color()) {
00090     if (_drgbas.empty()) {
00091       indent(out, indent_level)
00092         << "<RGBA> { " << get_color() << " }\n";
00093     } else {
00094       indent(out, indent_level) << "<RGBA> {\n";
00095       indent(out, indent_level + 2) << get_color() << "\n";
00096       _drgbas.write(out, indent_level + 2, "<DRBGA>", 4);
00097       indent(out, indent_level) << "}\n";
00098     }
00099   }
00100 }
00101 
00102 
00103 ////////////////////////////////////////////////////////////////////
00104 //     Function: EggAttributes::compare_to
00105 //       Access: Published
00106 //  Description: An ordering operator to compare two vertices for
00107 //               sorting order.  This imposes an arbitrary ordering
00108 //               useful to identify unique vertices.
00109 ////////////////////////////////////////////////////////////////////
00110 int EggAttributes::
00111 compare_to(const EggAttributes &other) const {
00112   if (_flags != other._flags) {
00113     return (int)_flags - (int)other._flags;
00114   }
00115 
00116   if (has_normal()) {
00117     int compare =
00118       _normal.compare_to(other._normal, egg_parameters->_normal_threshold);
00119     if (compare != 0) {
00120       return compare;
00121     }
00122     compare = _dnormals.compare_to(other._dnormals, egg_parameters->_normal_threshold);
00123     if (compare != 0) {
00124       return compare;
00125     }
00126   }
00127 
00128   if (has_color()) {
00129     int compare =
00130       _color.compare_to(other._color, egg_parameters->_color_threshold);
00131     if (compare != 0) {
00132       return compare;
00133     }
00134     compare = _drgbas.compare_to(other._drgbas, egg_parameters->_color_threshold);
00135     if (compare != 0) {
00136       return compare;
00137     }
00138   }
00139 
00140   return 0;
00141 }
00142 
00143 ////////////////////////////////////////////////////////////////////
00144 //     Function: EggAttributes::transform
00145 //       Access: Published, Virtual
00146 //  Description: Applies the indicated transformation matrix to the
00147 //               attributes.
00148 ////////////////////////////////////////////////////////////////////
00149 void EggAttributes::
00150 transform(const LMatrix4d &mat) {
00151   if (has_normal()) {
00152     _normal = _normal * mat;
00153     LVector3d old_normal = _normal;
00154     _normal.normalize();
00155 
00156     EggMorphNormalList::iterator mi;
00157     for (mi = _dnormals.begin(); mi != _dnormals.end(); ++mi) {
00158       // We can safely cast the morph object to a non-const, because
00159       // we're not changing its name, which is the only thing the set
00160       // cares about preserving.
00161       EggMorphNormal &morph = (EggMorphNormal &)(*mi);
00162 
00163       // A bit of funny business to ensure the offset normal is
00164       // normalized after the transform.  This will break strange
00165       // normal morphs that want to change the length of the normal,
00166       // but what else can we do?
00167       LVector3d offset = (*mi).get_offset() * mat;
00168       LVector3d n = old_normal + offset;
00169       n.normalize();
00170       morph.set_offset(n - _normal);
00171     }
00172   }
00173 }
 All Classes Functions Variables Enumerations