Panda3D

geomTrifans.cxx

00001 // Filename: geomTrifans.cxx
00002 // Created by:  drose (08Mar05)
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 "geomTrifans.h"
00016 #include "geomTriangles.h"
00017 #include "geomVertexRewriter.h"
00018 #include "bamReader.h"
00019 #include "bamWriter.h"
00020 #include "graphicsStateGuardianBase.h"
00021 
00022 TypeHandle GeomTrifans::_type_handle;
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: GeomTrifans::Constructor
00026 //       Access: Published
00027 //  Description: 
00028 ////////////////////////////////////////////////////////////////////
00029 GeomTrifans::
00030 GeomTrifans(GeomTrifans::UsageHint usage_hint) :
00031   GeomPrimitive(usage_hint)
00032 {
00033 }
00034  
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: GeomTrifans::Copy Constructor
00037 //       Access: Published
00038 //  Description: 
00039 ////////////////////////////////////////////////////////////////////
00040 GeomTrifans::
00041 GeomTrifans(const GeomTrifans &copy) :
00042   GeomPrimitive(copy)
00043 {
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //     Function: GeomTrifans::Destructor
00048 //       Access: Published, Virtual
00049 //  Description: 
00050 ////////////////////////////////////////////////////////////////////
00051 GeomTrifans::
00052 ~GeomTrifans() {
00053 }
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 //     Function: GeomTrifans::make_copy
00057 //       Access: Public, Virtual
00058 //  Description: 
00059 ////////////////////////////////////////////////////////////////////
00060 PT(GeomPrimitive) GeomTrifans::
00061 make_copy() const {
00062   return new GeomTrifans(*this);
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: GeomTrifans::get_primitive_type
00067 //       Access: Public, Virtual
00068 //  Description: Returns the fundamental rendering type of this
00069 //               primitive: whether it is points, lines, or polygons.
00070 //
00071 //               This is used to set up the appropriate antialiasing
00072 //               settings when AntialiasAttrib::M_auto is in effect;
00073 //               it also implies the type of primitive that will be
00074 //               produced when decompose() is called.
00075 ////////////////////////////////////////////////////////////////////
00076 GeomPrimitive::PrimitiveType GeomTrifans::
00077 get_primitive_type() const {
00078   return PT_polygons;
00079 }
00080 
00081 ////////////////////////////////////////////////////////////////////
00082 //     Function: GeomTrifans::get_geom_rendering
00083 //       Access: Published, Virtual
00084 //  Description: Returns the set of GeomRendering bits that represent
00085 //               the rendering properties required to properly render
00086 //               this primitive.
00087 ////////////////////////////////////////////////////////////////////
00088 int GeomTrifans::
00089 get_geom_rendering() const {
00090   if (is_indexed()) {
00091     return GR_triangle_fan | GR_indexed_other;
00092   } else {
00093     return GR_triangle_fan;
00094   }
00095 }
00096 
00097 ////////////////////////////////////////////////////////////////////
00098 //     Function: GeomTrifans::draw
00099 //       Access: Public, Virtual
00100 //  Description: Calls the appropriate method on the GSG to draw the
00101 //               primitive.
00102 ////////////////////////////////////////////////////////////////////
00103 bool GeomTrifans::
00104 draw(GraphicsStateGuardianBase *gsg, const GeomPrimitivePipelineReader *reader,
00105      bool force) const {
00106   return gsg->draw_trifans(reader, force);
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: GeomTrifans::decompose_impl
00111 //       Access: Protected, Virtual
00112 //  Description: Decomposes a complex primitive type into a simpler
00113 //               primitive type, for instance triangle strips to
00114 //               triangles, and returns a pointer to the new primitive
00115 //               definition.  If the decomposition cannot be
00116 //               performed, this might return the original object.
00117 //
00118 //               This method is useful for application code that wants
00119 //               to iterate through the set of triangles on the
00120 //               primitive without having to write handlers for each
00121 //               possible kind of primitive type.
00122 ////////////////////////////////////////////////////////////////////
00123 CPT(GeomPrimitive) GeomTrifans::
00124 decompose_impl() const {
00125   PT(GeomTriangles) triangles = new GeomTriangles(get_usage_hint());
00126   triangles->set_shade_model(get_shade_model());
00127   CPTA_int ends = get_ends();
00128 
00129   int num_vertices = get_num_vertices();
00130 
00131   int vi = 0;
00132   int li = 0;
00133   while (li < (int)ends.size()) {
00134     int end = ends[li];
00135     nassertr(vi + 2 <= end, triangles.p());
00136     int v0 = get_vertex(vi);
00137     ++vi;
00138     int v1 = get_vertex(vi);
00139     ++vi;
00140     while (vi < end) {
00141       int v2 = get_vertex(vi);
00142       ++vi;
00143       triangles->add_vertex(v0);
00144       triangles->add_vertex(v1);
00145       triangles->add_vertex(v2);
00146       v1 = v2;
00147       triangles->close_primitive();
00148     }
00149     ++li;
00150   }
00151 
00152   nassertr(vi == num_vertices, NULL);
00153 
00154   return triangles.p();
00155 }
00156 
00157 ////////////////////////////////////////////////////////////////////
00158 //     Function: GeomTrifans::rotate_impl
00159 //       Access: Protected, Virtual
00160 //  Description: The virtual implementation of do_rotate().
00161 ////////////////////////////////////////////////////////////////////
00162 CPT(GeomVertexArrayData) GeomTrifans::
00163 rotate_impl() const {
00164   // Actually, we can't rotate fans without chaging the winding order.
00165   // It's an error to define a flat shade model for a GeomTrifan.
00166   nassertr(false, NULL);
00167   return NULL;
00168 }
00169 
00170 ////////////////////////////////////////////////////////////////////
00171 //     Function: GeomTrifans::register_with_read_factory
00172 //       Access: Public, Static
00173 //  Description: Tells the BamReader how to create objects of type
00174 //               Geom.
00175 ////////////////////////////////////////////////////////////////////
00176 void GeomTrifans::
00177 register_with_read_factory() {
00178   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00179 }
00180 
00181 ////////////////////////////////////////////////////////////////////
00182 //     Function: GeomTrifans::make_from_bam
00183 //       Access: Protected, Static
00184 //  Description: This function is called by the BamReader's factory
00185 //               when a new object of type Geom is encountered
00186 //               in the Bam file.  It should create the Geom
00187 //               and extract its information from the file.
00188 ////////////////////////////////////////////////////////////////////
00189 TypedWritable *GeomTrifans::
00190 make_from_bam(const FactoryParams &params) {
00191   GeomTrifans *object = new GeomTrifans(UH_unspecified);
00192   DatagramIterator scan;
00193   BamReader *manager;
00194 
00195   parse_params(params, scan, manager);
00196   object->fillin(scan, manager);
00197 
00198   return object;
00199 }
 All Classes Functions Variables Enumerations