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