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