Panda3D

geomLines.cxx

00001 // Filename: geomLines.cxx
00002 // Created by:  drose (22Mar05)
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 "geomLines.h"
00016 #include "pStatTimer.h"
00017 #include "bamReader.h"
00018 #include "bamWriter.h"
00019 #include "graphicsStateGuardianBase.h"
00020 #include "geomVertexReader.h"
00021 #include "geomVertexWriter.h"
00022 
00023 TypeHandle GeomLines::_type_handle;
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: GeomLines::Constructor
00027 //       Access: Published
00028 //  Description: 
00029 ////////////////////////////////////////////////////////////////////
00030 GeomLines::
00031 GeomLines(GeomLines::UsageHint usage_hint) :
00032   GeomPrimitive(usage_hint)
00033 {
00034 }
00035  
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: GeomLines::Copy Constructor
00038 //       Access: Published
00039 //  Description: 
00040 ////////////////////////////////////////////////////////////////////
00041 GeomLines::
00042 GeomLines(const GeomLines &copy) :
00043   GeomPrimitive(copy)
00044 {
00045 }
00046 
00047 ////////////////////////////////////////////////////////////////////
00048 //     Function: GeomLines::Destructor
00049 //       Access: Published, Virtual
00050 //  Description: 
00051 ////////////////////////////////////////////////////////////////////
00052 GeomLines::
00053 ~GeomLines() {
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: GeomLines::make_copy
00058 //       Access: Public, Virtual
00059 //  Description: 
00060 ////////////////////////////////////////////////////////////////////
00061 PT(GeomPrimitive) GeomLines::
00062 make_copy() const {
00063   return new GeomLines(*this);
00064 }
00065 
00066 ////////////////////////////////////////////////////////////////////
00067 //     Function: GeomLines::get_primitive_type
00068 //       Access: Public, Virtual
00069 //  Description: Returns the fundamental rendering type of this
00070 //               primitive: whether it is points, lines, or polygons.
00071 //
00072 //               This is used to set up the appropriate antialiasing
00073 //               settings when AntialiasAttrib::M_auto is in effect;
00074 //               it also implies the type of primitive that will be
00075 //               produced when decompose() is called.
00076 ////////////////////////////////////////////////////////////////////
00077 GeomPrimitive::PrimitiveType GeomLines::
00078 get_primitive_type() const {
00079   return PT_lines;
00080 }
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: GeomLines::get_num_vertices_per_primitive
00084 //       Access: Public, Virtual
00085 //  Description: If the primitive type is a simple type in which all
00086 //               primitives have the same number of vertices, like
00087 //               lines, returns the number of vertices per
00088 //               primitive.  If the primitive type is a more complex
00089 //               type in which different primitives might have
00090 //               different numbers of vertices, for instance a
00091 //               line strip, returns 0.
00092 ////////////////////////////////////////////////////////////////////
00093 int GeomLines::
00094 get_num_vertices_per_primitive() const {
00095   return 2;
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: GeomLines::get_min_num_vertices_per_primitive
00100 //       Access: Public, Virtual
00101 //  Description: Returns the minimum number of vertices that must be
00102 //               added before close_primitive() may legally be called.
00103 ////////////////////////////////////////////////////////////////////
00104 int GeomLines::
00105 get_min_num_vertices_per_primitive() const {
00106   return 2;
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: GeomLines::draw
00111 //       Access: Public, Virtual
00112 //  Description: Calls the appropriate method on the GSG to draw the
00113 //               primitive.
00114 ////////////////////////////////////////////////////////////////////
00115 bool GeomLines::
00116 draw(GraphicsStateGuardianBase *gsg, const GeomPrimitivePipelineReader *reader,
00117      bool force) const {
00118   return gsg->draw_lines(reader, force);
00119 }
00120 
00121 ////////////////////////////////////////////////////////////////////
00122 //     Function: GeomLines::rotate_impl
00123 //       Access: Protected, Virtual
00124 //  Description: The virtual implementation of do_rotate().
00125 ////////////////////////////////////////////////////////////////////
00126 CPT(GeomVertexArrayData) GeomLines::
00127 rotate_impl() const {
00128   // To rotate lines, we just move reverse the pairs of vertices.
00129   int num_vertices = get_num_vertices();
00130     
00131   PT(GeomVertexArrayData) new_vertices = make_index_data();
00132   new_vertices->set_num_rows(num_vertices);
00133 
00134   if (is_indexed()) {
00135     CPT(GeomVertexArrayData) vertices = get_vertices();
00136     GeomVertexReader from(vertices, 0);
00137     GeomVertexWriter to(new_vertices, 0);
00138 
00139     for (int begin = 0; begin < num_vertices; begin += 2) {
00140       from.set_row_unsafe(begin + 1);
00141       to.set_data1i(from.get_data1i());
00142       from.set_row_unsafe(begin);
00143       to.set_data1i(from.get_data1i());
00144     }
00145   
00146     nassertr(to.is_at_end(), NULL);
00147 
00148   } else {
00149     // Nonindexed case.
00150     int first_vertex = get_first_vertex();
00151     GeomVertexWriter to(new_vertices, 0);
00152 
00153     for (int begin = 0; begin < num_vertices; begin += 2) {
00154       to.set_data1i(begin + 1 + first_vertex);
00155       to.set_data1i(begin + first_vertex);
00156     }
00157   
00158     nassertr(to.is_at_end(), NULL);
00159   }
00160     
00161   return new_vertices;
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: GeomLines::register_with_read_factory
00166 //       Access: Public, Static
00167 //  Description: Tells the BamReader how to create objects of type
00168 //               Geom.
00169 ////////////////////////////////////////////////////////////////////
00170 void GeomLines::
00171 register_with_read_factory() {
00172   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00173 }
00174 
00175 ////////////////////////////////////////////////////////////////////
00176 //     Function: GeomLines::make_from_bam
00177 //       Access: Protected, Static
00178 //  Description: This function is called by the BamReader's factory
00179 //               when a new object of type Geom is encountered
00180 //               in the Bam file.  It should create the Geom
00181 //               and extract its information from the file.
00182 ////////////////////////////////////////////////////////////////////
00183 TypedWritable *GeomLines::
00184 make_from_bam(const FactoryParams &params) {
00185   GeomLines *object = new GeomLines(UH_unspecified);
00186   DatagramIterator scan;
00187   BamReader *manager;
00188 
00189   parse_params(params, scan, manager);
00190   object->fillin(scan, manager);
00191 
00192   return object;
00193 }
 All Classes Functions Variables Enumerations