Panda3D
 All Classes Functions Variables Enumerations
eggTriangleStrip.cxx
00001 // Filename: eggTriangleStrip.cxx
00002 // Created by:  drose (13Mar05)
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 "eggTriangleStrip.h"
00016 #include "eggGroupNode.h"
00017 #include "eggPolygon.h"
00018 #include "indent.h"
00019 
00020 TypeHandle EggTriangleStrip::_type_handle;
00021 
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: EggTriangleStrip::Destructor
00025 //       Access: Published, Virtual
00026 //  Description: 
00027 ////////////////////////////////////////////////////////////////////
00028 EggTriangleStrip::
00029 ~EggTriangleStrip() {
00030   clear();
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: EggTriangleStrip::write
00035 //       Access: Published, Virtual
00036 //  Description: Writes the triangle strip to the indicated output
00037 //               stream in Egg format.
00038 ////////////////////////////////////////////////////////////////////
00039 void EggTriangleStrip::
00040 write(ostream &out, int indent_level) const {
00041   write_header(out, indent_level, "<TriangleStrip>");
00042   write_body(out, indent_level+2);
00043   indent(out, indent_level) << "}\n";
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //     Function: EggTriangleStrip::get_num_lead_vertices
00048 //       Access: Protected, Virtual
00049 //  Description: Returns the number of initial vertices that are not
00050 //               used in defining any component; the first component
00051 //               is defined by the (n + 1)th vertex, and then a new
00052 //               component at each vertex thereafter.
00053 ////////////////////////////////////////////////////////////////////
00054 int EggTriangleStrip::
00055 get_num_lead_vertices() const {
00056   return 2;
00057 }
00058 
00059 ////////////////////////////////////////////////////////////////////
00060 //     Function: EggTriangleStrip::triangulate_poly
00061 //       Access: Protected, Virtual
00062 //  Description: Fills the container up with EggPolygons that
00063 //               represent the component triangles of this triangle
00064 //               strip.
00065 //
00066 //               It is assumed that the EggTriangleStrip is not
00067 //               already a child of any other group when this function
00068 //               is called.
00069 //
00070 //               Returns true if the triangulation is successful, or
00071 //               false if there was some error (in which case the
00072 //               container may contain some partial triangulation).
00073 ////////////////////////////////////////////////////////////////////
00074 bool EggTriangleStrip::
00075 do_triangulate(EggGroupNode *container) const {
00076   if (size() < 3) {
00077     return false;
00078   }
00079   const_iterator vi = begin();
00080   EggVertex *v0 = (*vi);
00081   ++vi;
00082   EggVertex *v1 = (*vi);
00083   ++vi;
00084   bool reversed = false;
00085 
00086   for (int i = 0; i < (int)size() - 2; i++) {
00087     PT(EggPolygon) poly = new EggPolygon;
00088     poly->copy_attributes(*this);
00089     const EggAttributes *attrib = get_component(i);
00090     if (attrib->has_color()) {
00091       poly->set_color(attrib->get_color());
00092     }
00093     if (attrib->has_normal()) {
00094       poly->set_normal(attrib->get_normal());
00095     }
00096 
00097     if (reversed) {
00098       poly->add_vertex(v1);
00099       poly->add_vertex(v0);
00100       reversed = false;
00101     } else {
00102       poly->add_vertex(v0);
00103       poly->add_vertex(v1);
00104       reversed = true;
00105     }
00106     poly->add_vertex(*vi);
00107     v0 = v1;
00108     v1 = *vi;
00109     container->add_child(poly);
00110     ++vi;
00111   }
00112 
00113   return true;
00114 }
 All Classes Functions Variables Enumerations