Panda3D
 All Classes Functions Variables Enumerations
fltMeshPrimitive.cxx
1 // Filename: fltMeshPrimitive.cxx
2 // Created by: drose (28Feb01)
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 "fltMeshPrimitive.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 #include "fltHeader.h"
19 #include "fltMaterial.h"
20 
21 TypeHandle FltMeshPrimitive::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: FltMeshPrimitive::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 FltMeshPrimitive::
29 FltMeshPrimitive(FltHeader *header) : FltBead(header) {
30  _primitive_type = PT_tristrip;
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: FltMeshPrimitive::extract_record
35 // Access: Protected, Virtual
36 // Description: Fills in the information in this bead based on the
37 // information given in the indicated datagram, whose
38 // opcode has already been read. Returns true on
39 // success, false if the datagram is invalid.
40 ////////////////////////////////////////////////////////////////////
41 bool FltMeshPrimitive::
42 extract_record(FltRecordReader &reader) {
43  if (!FltBead::extract_record(reader)) {
44  return false;
45  }
46 
47  nassertr(reader.get_opcode() == FO_mesh_primitive, false);
48  DatagramIterator &iterator = reader.get_iterator();
49 
50  _primitive_type = (PrimitiveType)iterator.get_be_int16();
51 
52  int vertex_width = iterator.get_be_int16();
53  int num_vertices = iterator.get_be_int32();
54 
55  if (vertex_width == 1) {
56  for (int i = 0; i < num_vertices; i++) {
57  _vertices.push_back(iterator.get_uint8());
58  }
59 
60  } else if (vertex_width == 2) {
61  for (int i = 0; i < num_vertices; i++) {
62  _vertices.push_back(iterator.get_be_uint16());
63  }
64 
65  } else if (vertex_width == 4) {
66  for (int i = 0; i < num_vertices; i++) {
67  _vertices.push_back(iterator.get_be_int32());
68  }
69 
70  } else {
71  nout << "Invalid vertex width in mesh primitive: " << vertex_width
72  << "\n";
73  return false;
74  }
75 
76  check_remaining_size(iterator);
77  return true;
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: FltMeshPrimitive::build_record
82 // Access: Protected, Virtual
83 // Description: Fills up the current record on the FltRecordWriter with
84 // data for this record, but does not advance the
85 // writer. Returns true on success, false if there is
86 // some error.
87 ////////////////////////////////////////////////////////////////////
88 bool FltMeshPrimitive::
89 build_record(FltRecordWriter &writer) const {
90  if (!FltBead::build_record(writer)) {
91  return false;
92  }
93 
94  writer.set_opcode(FO_mesh_primitive);
95  Datagram &datagram = writer.update_datagram();
96 
97  datagram.add_be_int16(_primitive_type);
98 
99  // Determine the optimum index width, based on the largest vertex
100  // index.
101  int max_index = 0;
102  Vertices::const_iterator vi;
103  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
104  max_index = max(max_index, (*vi));
105  }
106 
107  int vertex_width;
108  if (max_index < 0x100) {
109  vertex_width = 1;
110  } else if (max_index < 0x10000) {
111  vertex_width = 2;
112  } else {
113  vertex_width = 4;
114  }
115 
116  datagram.add_be_int16(vertex_width);
117  datagram.add_be_int32(_vertices.size());
118 
119  if (vertex_width == 1) {
120  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
121  datagram.add_uint8(*vi);
122  }
123 
124  } else if (vertex_width == 2) {
125  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
126  datagram.add_be_uint16(*vi);
127  }
128 
129  } else {
130  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
131  datagram.add_be_int32(*vi);
132  }
133  }
134 
135  return true;
136 }
void add_uint8(PN_uint8 value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:138
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly...
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition: fltBead.h:33
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
void add_be_int32(PN_int32 value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition: datagram.I:267
void check_remaining_size(const DatagramIterator &di, const string &name=string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record...
Definition: fltRecord.cxx:313
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
void add_be_uint16(PN_uint16 value)
Adds an unsigned 16-bit big-endian integer to the datagram.
Definition: datagram.I:291
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
void add_be_int16(PN_int16 value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition: datagram.I:255