Panda3D
fltLocalVertexPool.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 fltLocalVertexPool.cxx
10  * @author drose
11  * @date 2001-02-28
12  */
13 
14 #include "fltLocalVertexPool.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 #include "fltHeader.h"
18 #include "fltMaterial.h"
19 
20 TypeHandle FltLocalVertexPool::_type_handle;
21 
22 /**
23  *
24  */
25 FltLocalVertexPool::
26 FltLocalVertexPool(FltHeader *header) : FltRecord(header) {
27 }
28 
29 /**
30  * Fills in the information in this bead based on the information given in the
31  * indicated datagram, whose opcode has already been read. Returns true on
32  * success, false if the datagram is invalid.
33  */
36  if (!FltRecord::extract_record(reader)) {
37  return false;
38  }
39 
40  nassertr(reader.get_opcode() == FO_local_vertex_pool, false);
41  DatagramIterator &iterator = reader.get_iterator();
42 
43  int num_vertices = iterator.get_be_int32();
44  int attributes = iterator.get_be_int32();
45 
46  for (int i = 0; i < num_vertices; i++) {
47  FltVertex *vertex = new FltVertex(_header);
48  _vertices.push_back(vertex);
49 
50  if ((attributes & AM_has_position) != 0) {
51  vertex->_pos[0] = iterator.get_be_float64();
52  vertex->_pos[1] = iterator.get_be_float64();
53  vertex->_pos[2] = iterator.get_be_float64();
54  }
55 
56  if ((attributes & AM_has_color_index) != 0) {
57  vertex->_color_index = iterator.get_be_int32();
58 
59  } else if ((attributes & AM_has_packed_color) != 0) {
60  if (!vertex->_packed_color.extract_record(reader)) {
61  return false;
62  }
63  vertex->_flags |= FltVertex::F_packed_color;
64 
65  } else {
66  vertex->_flags |= FltVertex::F_no_color;
67  }
68 
69  if ((attributes & AM_has_normal) != 0) {
70  vertex->_normal[0] = iterator.get_be_float32();
71  vertex->_normal[1] = iterator.get_be_float32();
72  vertex->_normal[2] = iterator.get_be_float32();
73  vertex->_has_normal = true;
74  }
75 
76  if ((attributes & AM_has_base_uv) != 0) {
77  vertex->_uv[0] = iterator.get_be_float32();
78  vertex->_uv[1] = iterator.get_be_float32();
79  vertex->_has_uv = true;
80  }
81 
82  if ((attributes & AM_has_uv_1) != 0) {
83  iterator.get_be_float32();
84  iterator.get_be_float32();
85  }
86 
87  if ((attributes & AM_has_uv_2) != 0) {
88  iterator.get_be_float32();
89  iterator.get_be_float32();
90  }
91 
92  if ((attributes & AM_has_uv_3) != 0) {
93  iterator.get_be_float32();
94  iterator.get_be_float32();
95  }
96 
97  if ((attributes & AM_has_uv_4) != 0) {
98  iterator.get_be_float32();
99  iterator.get_be_float32();
100  }
101 
102  if ((attributes & AM_has_uv_5) != 0) {
103  iterator.get_be_float32();
104  iterator.get_be_float32();
105  }
106 
107  if ((attributes & AM_has_uv_6) != 0) {
108  iterator.get_be_float32();
109  iterator.get_be_float32();
110  }
111 
112  if ((attributes & AM_has_uv_7) != 0) {
113  iterator.get_be_float32();
114  iterator.get_be_float32();
115  }
116  }
117 
118  check_remaining_size(iterator);
119  return true;
120 }
121 
122 /**
123  * Fills up the current record on the FltRecordWriter with data for this
124  * record, but does not advance the writer. Returns true on success, false if
125  * there is some error.
126  */
129  if (!FltRecord::build_record(writer)) {
130  return false;
131  }
132 
133  writer.set_opcode(FO_local_vertex_pool);
134  Datagram &datagram = writer.update_datagram();
135 
136  // Determine what kind of vertices we have.
137  int attributes = AM_has_position;
138 
139  Vertices::const_iterator vi;
140  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
141  FltVertex *vertex = (*vi);
142  if ((vertex->_flags & FltVertex::F_no_color) != 0) {
143  // No color.
144 
145  } else if ((vertex->_flags & FltVertex::F_packed_color) != 0) {
146  // Packed color.
147  attributes |= AM_has_packed_color;
148 
149  } else {
150  // Indexed color.
151  attributes |= AM_has_color_index;
152  }
153 
154  if (vertex->_has_normal) {
155  attributes |= AM_has_normal;
156  }
157 
158  if (vertex->_has_uv) {
159  attributes |= AM_has_base_uv;
160  }
161  }
162 
163  if ((attributes & AM_has_packed_color) != 0 &&
164  (attributes & AM_has_color_index) != 0) {
165  // We cannot have both a packed color and a color index. If we want both,
166  // used packed color.
167  attributes &= ~AM_has_color_index;
168  }
169 
170  datagram.add_be_int32(_vertices.size());
171  datagram.add_be_int32(attributes);
172 
173  // Now write out each vertex.
174  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
175  FltVertex *vertex = (*vi);
176 
177  if ((attributes & AM_has_position) != 0) {
178  datagram.add_be_float64(vertex->_pos[0]);
179  datagram.add_be_float64(vertex->_pos[1]);
180  datagram.add_be_float64(vertex->_pos[2]);
181  }
182 
183  if ((attributes & AM_has_color_index) != 0) {
184  if ((vertex->_flags & (FltVertex::F_no_color | FltVertex::F_packed_color)) != 0) {
185  // This particular vertex does not have a color index. Make it white.
186  datagram.add_be_int32(_header->get_closest_rgb(LRGBColor(1.0, 1.0, 1.0)));
187  } else {
188  datagram.add_be_int32(vertex->_color_index);
189  }
190 
191  } else if ((attributes & AM_has_packed_color) != 0) {
192  // We extract our own FltPackedColor instead of writing out the vertex's
193  // _packed_color directly, just in case the vertex is actually index
194  // colored. This bit of code will work regardless of the kind of color
195  // the vertex has.
196 
197  FltPackedColor color;
198  if (vertex->has_color()) {
199  color.set_color(vertex->get_color());
200  } else {
201  // An uncolored vertex. Make it white.
202  color.set_color(LColor(1.0, 1.0, 1.0, 1.0));
203  }
204 
205  if (!color.build_record(writer)) {
206  return false;
207  }
208  }
209 
210  if ((attributes & AM_has_normal) != 0) {
211  if (!vertex->_has_normal) {
212  datagram.add_be_float32(0.0);
213  datagram.add_be_float32(0.0);
214  datagram.add_be_float32(0.0);
215  } else {
216  datagram.add_be_float32(vertex->_normal[0]);
217  datagram.add_be_float32(vertex->_normal[1]);
218  datagram.add_be_float32(vertex->_normal[2]);
219  }
220  }
221 
222  if ((attributes & AM_has_base_uv) != 0) {
223  if (!vertex->_has_uv) {
224  datagram.add_be_float32(0.0);
225  datagram.add_be_float32(0.0);
226  } else {
227  datagram.add_be_float32(vertex->_uv[0]);
228  datagram.add_be_float32(vertex->_uv[1]);
229  }
230  }
231  }
232 
233  return true;
234 }
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...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void check_remaining_size(const DatagramIterator &di, const std::string &name=std::string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record.
Definition: fltRecord.cxx:254
int32_t get_be_int32()
Extracts a signed 32-bit big-endian integer.
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
bool has_color() const
Returns true if the vertex has a primary color indicated, false otherwise.
Definition: fltVertex.I:18
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the datagram.
Definition: datagram.I:209
void set_color(const LColor &color)
Sets the color according to the indicated four-component LColor value (including alpha).
void add_be_float32(PN_float32 value)
Adds a 32-bit single-precision big-endian floating-point number to the datagram.
Definition: datagram.I:200
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool extract_record(FltRecordReader &reader)
Fills in the information in this bead based on the information given in the indicated datagram,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The base class for all kinds of records in a MultiGen OpenFlight file.
Definition: fltRecord.h:36
A packed color record, A, B, G, R.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Represents a single vertex in the vertex palette.
Definition: fltVertex.h:32
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
A class to retrieve the individual data elements previously stored in a Datagram.
LColor get_color() const
If has_color() indicates true, returns the color of the vertex, as a four- component value.
Definition: fltVertex.cxx:111
void add_be_int32(int32_t value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition: datagram.I:154
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
virtual bool build_record(FltRecordWriter &writer) const
Fills up the current record on the FltRecordWriter with data for this record, but does not advance th...
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:38