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