Panda3D

lwoPolygons.cxx

00001 // Filename: lwoPolygons.cxx
00002 // Created by:  drose (24Apr01)
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 "lwoPolygons.h"
00016 #include "lwoInputFile.h"
00017 
00018 #include "dcast.h"
00019 #include "indent.h"
00020 
00021 TypeHandle LwoPolygons::_type_handle;
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: LwoPolygons::get_num_polygons
00025 //       Access: Public
00026 //  Description: Returns the number of polygons of this group.
00027 ////////////////////////////////////////////////////////////////////
00028 int LwoPolygons::
00029 get_num_polygons() const {
00030   return _polygons.size();
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: LwoPolygons::get_polygon
00035 //       Access: Public
00036 //  Description: Returns the nth polygon of this group.
00037 ////////////////////////////////////////////////////////////////////
00038 LwoPolygons::Polygon *LwoPolygons::
00039 get_polygon(int n) const {
00040   nassertr(n >= 0 && n < (int)_polygons.size(), (Polygon *)NULL);
00041   return _polygons[n];
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: LwoPolygons::read_iff
00046 //       Access: Public, Virtual
00047 //  Description: Reads the data of the chunk in from the given input
00048 //               file, if possible.  The ID and length of the chunk
00049 //               have already been read.  stop_at is the byte position
00050 //               of the file to stop at (based on the current position
00051 //               at in->get_bytes_read()).  Returns true on success,
00052 //               false otherwise.
00053 ////////////////////////////////////////////////////////////////////
00054 bool LwoPolygons::
00055 read_iff(IffInputFile *in, size_t stop_at) {
00056   LwoInputFile *lin = DCAST(LwoInputFile, in);
00057 
00058   if (lin->get_lwo_version() >= 6.0) {
00059     // 6.x style syntax:
00060     // POLS { type[ID4], ( numvert+flags[U2], vert[VX] # numvert )* }
00061 
00062     _polygon_type = lin->get_id();
00063 
00064     while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
00065       int nf = lin->get_be_uint16();
00066       int num_vertices = nf & PF_numverts_mask;
00067 
00068       PT(Polygon) poly = new Polygon;
00069       poly->_flags = nf & ~PF_numverts_mask;
00070       poly->_surface_index = -1;
00071 
00072       for (int i = 0; i < num_vertices; i++) {
00073         int vindex = lin->get_vx();
00074         poly->_vertices.push_back(vindex);
00075       }
00076 
00077       _polygons.push_back(poly);
00078     }
00079 
00080   } else {
00081     // 5.x style syntax:
00082     // POLS { ( numvert[U2], vert[VX] # numvert, +/-(surf+1)[I2], numdetail[U2]? )* }
00083     _polygon_type = IffId("FACE");
00084 
00085     int num_decals = 0;
00086     while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
00087       int num_vertices = lin->get_be_uint16();
00088 
00089       PT(Polygon) poly = new Polygon;
00090       poly->_flags = 0;
00091 
00092       for (int i = 0; i < num_vertices; i++) {
00093         int vindex = lin->get_vx();
00094         poly->_vertices.push_back(vindex);
00095       }
00096 
00097       int surface = lin->get_be_int16();
00098 
00099       if (num_decals > 0) {
00100         // This is a decal polygon of a previous polygon.
00101         num_decals--;
00102         poly->_flags |= PF_decal;
00103 
00104       } else {
00105         if (surface < 0) {
00106           num_decals = lin->get_be_int16();
00107           surface = -surface;
00108         }
00109       }
00110 
00111       // The surface index is stored +1 to allow signedness to be
00112       // examined.
00113       poly->_surface_index = surface - 1;
00114 
00115       _polygons.push_back(poly);
00116     }
00117   }
00118 
00119   return true;
00120 }
00121 
00122 ////////////////////////////////////////////////////////////////////
00123 //     Function: LwoPolygons::write
00124 //       Access: Public, Virtual
00125 //  Description:
00126 ////////////////////////////////////////////////////////////////////
00127 void LwoPolygons::
00128 write(ostream &out, int indent_level) const {
00129   indent(out, indent_level)
00130     << get_id() << " { polygon_type = " << _polygon_type
00131     << ", " << _polygons.size() << " polygons }\n";
00132 }
 All Classes Functions Variables Enumerations