Panda3D
 All Classes Functions Variables Enumerations
lwoPolygons.cxx
1 // Filename: lwoPolygons.cxx
2 // Created by: drose (24Apr01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "lwoPolygons.h"
16 #include "lwoInputFile.h"
17 
18 #include "dcast.h"
19 #include "indent.h"
20 
21 TypeHandle LwoPolygons::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: LwoPolygons::get_num_polygons
25 // Access: Public
26 // Description: Returns the number of polygons of this group.
27 ////////////////////////////////////////////////////////////////////
28 int LwoPolygons::
30  return _polygons.size();
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: LwoPolygons::get_polygon
35 // Access: Public
36 // Description: Returns the nth polygon of this group.
37 ////////////////////////////////////////////////////////////////////
39 get_polygon(int n) const {
40  nassertr(n >= 0 && n < (int)_polygons.size(), (Polygon *)NULL);
41  return _polygons[n];
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: LwoPolygons::read_iff
46 // Access: Public, Virtual
47 // Description: Reads the data of the chunk in from the given input
48 // file, if possible. The ID and length of the chunk
49 // have already been read. stop_at is the byte position
50 // of the file to stop at (based on the current position
51 // at in->get_bytes_read()). Returns true on success,
52 // false otherwise.
53 ////////////////////////////////////////////////////////////////////
54 bool LwoPolygons::
55 read_iff(IffInputFile *in, size_t stop_at) {
56  LwoInputFile *lin = DCAST(LwoInputFile, in);
57 
58  if (lin->get_lwo_version() >= 6.0) {
59  // 6.x style syntax:
60  // POLS { type[ID4], ( numvert+flags[U2], vert[VX] # numvert )* }
61 
62  _polygon_type = lin->get_id();
63 
64  while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
65  int nf = lin->get_be_uint16();
66  int num_vertices = nf & PF_numverts_mask;
67 
68  PT(Polygon) poly = new Polygon;
69  poly->_flags = nf & ~PF_numverts_mask;
70  poly->_surface_index = -1;
71 
72  for (int i = 0; i < num_vertices; i++) {
73  int vindex = lin->get_vx();
74  poly->_vertices.push_back(vindex);
75  }
76 
77  _polygons.push_back(poly);
78  }
79 
80  } else {
81  // 5.x style syntax:
82  // POLS { ( numvert[U2], vert[VX] # numvert, +/-(surf+1)[I2], numdetail[U2]? )* }
83  _polygon_type = IffId("FACE");
84 
85  int num_decals = 0;
86  while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
87  int num_vertices = lin->get_be_uint16();
88 
89  PT(Polygon) poly = new Polygon;
90  poly->_flags = 0;
91 
92  for (int i = 0; i < num_vertices; i++) {
93  int vindex = lin->get_vx();
94  poly->_vertices.push_back(vindex);
95  }
96 
97  int surface = lin->get_be_int16();
98 
99  if (num_decals > 0) {
100  // This is a decal polygon of a previous polygon.
101  num_decals--;
102  poly->_flags |= PF_decal;
103 
104  } else {
105  if (surface < 0) {
106  num_decals = lin->get_be_int16();
107  surface = -surface;
108  }
109  }
110 
111  // The surface index is stored +1 to allow signedness to be
112  // examined.
113  poly->_surface_index = surface - 1;
114 
115  _polygons.push_back(poly);
116  }
117  }
118 
119  return true;
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: LwoPolygons::write
124 // Access: Public, Virtual
125 // Description:
126 ////////////////////////////////////////////////////////////////////
127 void LwoPolygons::
128 write(ostream &out, int indent_level) const {
129  indent(out, indent_level)
130  << get_id() << " { polygon_type = " << _polygon_type
131  << ", " << _polygons.size() << " polygons }\n";
132 }
int get_vx()
Reads a Lightwave variable-length index.
PN_uint16 get_be_uint16()
Extracts an unsigned 16-bit big-endian integer.
PN_int16 get_be_int16()
Extracts a signed 16-bit big-endian integer.
int get_num_polygons() const
Returns the number of polygons of this group.
Definition: lwoPolygons.cxx:29
A wrapper around an istream used for reading an IFF file.
Definition: iffInputFile.h:33
IffId get_id() const
Returns the ID associated with this chunk.
Definition: iffChunk.I:31
bool is_eof() const
Returns true if the last read operation failed because of reaching EOF, false otherwise.
Definition: iffInputFile.I:45
Polygon * get_polygon(int n) const
Returns the nth polygon of this group.
Definition: lwoPolygons.cxx:39
double get_lwo_version() const
Returns the version of the Lightwave file being read.
Definition: lwoInputFile.I:24
A specialization of IffInputFile to handle reading a Lightwave Object file.
Definition: lwoInputFile.h:29
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A four-byte chunk ID appearing in an &quot;IFF&quot; file.
Definition: iffId.h:29
size_t get_bytes_read() const
Returns the number of bytes read so far from the input file.
Definition: iffInputFile.I:56
IffId get_id()
Extracts a 4-character IFF ID.
virtual bool read_iff(IffInputFile *in, size_t stop_at)
Reads the data of the chunk in from the given input file, if possible.
Definition: lwoPolygons.cxx:55