Panda3D
Loading...
Searching...
No Matches
fltVertex.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 fltVertex.cxx
10 * @author drose
11 * @date 2000-08-25
12 */
13
14#include "fltVertex.h"
15#include "fltRecordReader.h"
16#include "fltRecordWriter.h"
17#include "fltHeader.h"
18
19TypeHandle FltVertex::_type_handle;
20
21/**
22 *
23 */
24FltVertex::
25FltVertex(FltHeader *header) : FltRecord(header) {
26 _color_name_index = 0;
27 _flags = F_no_color;
28 _pos.set(0.0, 0.0, 0.0);
29 _normal.set(0.0, 0.0, 0.0);
30 _uv.set(0.0, 0.0);
31 _color_index = 0;
32
33 _has_normal = false;
34 _has_uv = false;
35}
36
37/**
38 * Returns the opcode that this record will be written as.
39 */
40FltOpcode FltVertex::
41get_opcode() const {
42 if (_has_normal) {
43 if (_has_uv) {
44 return FO_vertex_cnu;
45 } else {
46 return FO_vertex_cn;
47 }
48 } else {
49 if (_has_uv) {
50 return FO_vertex_cu;
51 } else {
52 return FO_vertex_c;
53 }
54 }
55}
56
57/**
58 * Returns the length of this record in bytes as it will be written to the flt
59 * file.
60 */
62get_record_length() const {
63 if (_header->get_flt_version() < 1520) {
64 // Version 14.2
65 switch (get_opcode()) {
66 case FO_vertex_c:
67 return 36;
68
69 case FO_vertex_cn:
70 return 48;
71
72 case FO_vertex_cnu:
73 return 56;
74
75 case FO_vertex_cu:
76 return 44;
77
78 default:
79 nassertr(false, 0);
80 }
81
82 } else {
83 // Version 15.2 and higher
84 switch (get_opcode()) {
85 case FO_vertex_c:
86 return 40;
87
88 case FO_vertex_cn:
89 return 56;
90
91 case FO_vertex_cnu:
92 return 64;
93
94 case FO_vertex_cu:
95 return 48;
96
97 default:
98 nassertr(false, 0);
99 }
100 }
101
102 return 0;
103}
104
105/**
106 * If has_color() indicates true, returns the color of the vertex, as a four-
107 * component value. In the case of a vertex, the alpha channel will always be
108 * 1.0, as MultiGen does not store transparency per-vertex.
109 */
111get_color() const {
112 nassertr(has_color(), LColor(0.0, 0.0, 0.0, 0.0));
113
114 return _header->get_color(_color_index, (_flags & F_packed_color) != 0,
115 _packed_color, 0);
116}
117
118/**
119 * If has_color() indicates true, returns the color of the vertex, as a three-
120 * component value.
121 */
122LRGBColor FltVertex::
123get_rgb() const {
124 nassertr(has_color(), LRGBColor(0.0, 0.0, 0.0));
125
126 return _header->get_rgb(_color_index, (_flags & F_packed_color) != 0,
127 _packed_color);
128}
129
130/**
131 * Sets the color of the vertex, using the packed color convention.
132 */
134set_rgb(const LRGBColor &rgb) {
135 _packed_color.set_rgb(rgb);
136 _flags = ((_flags & ~F_no_color) | F_packed_color);
137}
138
139/**
140 * Fills in the information in this record based on the information given in
141 * the indicated datagram, whose opcode has already been read. Returns true
142 * on success, false if the datagram is invalid.
143 */
144bool FltVertex::
145extract_record(FltRecordReader &reader) {
146 if (!FltRecord::extract_record(reader)) {
147 return false;
148 }
149
150 switch (reader.get_opcode()) {
151 case FO_vertex_c:
152 _has_normal = false;
153 _has_uv = false;
154 break;
155
156 case FO_vertex_cn:
157 _has_normal = true;
158 _has_uv = false;
159 break;
160
161 case FO_vertex_cnu:
162 _has_normal = true;
163 _has_uv = true;
164 break;
165
166 case FO_vertex_cu:
167 _has_normal = false;
168 _has_uv = true;
169 break;
170
171 default:
172 nassertr(false, false);
173 }
174
175 DatagramIterator &iterator = reader.get_iterator();
176
177 _color_name_index = iterator.get_be_int16();
178 _flags = iterator.get_be_uint16();
179 _pos[0] = iterator.get_be_float64();
180 _pos[1] = iterator.get_be_float64();
181 _pos[2] = iterator.get_be_float64();
182
183 if (_has_normal) {
184 _normal[0] = iterator.get_be_float32();
185 _normal[1] = iterator.get_be_float32();
186 _normal[2] = iterator.get_be_float32();
187 }
188 if (_has_uv) {
189 _uv[0] = iterator.get_be_float32();
190 _uv[1] = iterator.get_be_float32();
191 }
192
193 if (iterator.get_remaining_size() > 0) {
194 if (!_packed_color.extract_record(reader)) {
195 return false;
196 }
197 if (_header->get_flt_version() >= 1520) {
198 _color_index = iterator.get_be_int32();
199
200 if (_has_normal && iterator.get_remaining_size() > 0) {
201 // If we extracted a normal, our double-word alignment is off; now we
202 // have a few extra bytes to ignore.
203 iterator.skip_bytes(4);
204 }
205 }
206 }
207
208 check_remaining_size(iterator);
209 return true;
210}
211
212/**
213 * Fills up the current record on the FltRecordWriter with data for this
214 * record, but does not advance the writer. Returns true on success, false if
215 * there is some error.
216 */
217bool FltVertex::
218build_record(FltRecordWriter &writer) const {
219 if (!FltRecord::build_record(writer)) {
220 return false;
221 }
222
223 writer.set_opcode(get_opcode());
224 Datagram &datagram = writer.update_datagram();
225
226 datagram.add_be_int16(_color_name_index);
227 datagram.add_be_uint16(_flags);
228 datagram.add_be_float64(_pos[0]);
229 datagram.add_be_float64(_pos[1]);
230 datagram.add_be_float64(_pos[2]);
231
232 if (_has_normal) {
233 datagram.add_be_float32(_normal[0]);
234 datagram.add_be_float32(_normal[1]);
235 datagram.add_be_float32(_normal[2]);
236 }
237 if (_has_uv) {
238 datagram.add_be_float32(_uv[0]);
239 datagram.add_be_float32(_uv[1]);
240 }
241
242 if (!_packed_color.build_record(writer)) {
243 return false;
244 }
245
246 if (_header->get_flt_version() >= 1520) {
247 // New with 15.2
248 datagram.add_be_uint32(_color_index);
249
250 if (_has_normal) {
251 // If we added a normal, our double-word alignment is off; now we have a
252 // few extra bytes to add.
253 datagram.pad_bytes(4);
254 }
255 }
256
257 nassertr((int)datagram.get_length() == get_record_length() - 4, true);
258 return true;
259}
A class to retrieve the individual data elements previously stored in a Datagram.
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
uint16_t get_be_uint16()
Extracts an unsigned 16-bit big-endian integer.
int32_t get_be_int32()
Extracts a signed 32-bit big-endian integer.
PN_float32 get_be_float32()
Extracts a 32-bit big-endian single-precision floating-point number.
PN_float64 get_be_float64()
Extracts a 64-bit big-endian floating-point number.
int16_t get_be_int16()
Extracts a signed 16-bit big-endian integer.
size_t get_remaining_size() const
Return the bytes left in the datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition datagram.I:181
void add_be_uint16(uint16_t value)
Adds an unsigned 16-bit big-endian integer to the datagram.
Definition datagram.I:172
size_t get_length() const
Returns the number of bytes in the datagram.
Definition datagram.I:335
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
void add_be_int16(int16_t value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition datagram.I:145
void add_be_float64(PN_float64 value)
Adds a 64-bit big-endian floating-point number to the datagram.
Definition datagram.I:209
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition datagram.cxx:99
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 set_rgb(const LRGBColor &rgb)
Sets the color according to the indicated three-component LRGBColor value, and set the alpha to 1....
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
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.
The base class for all kinds of records in a MultiGen OpenFlight file.
Definition fltRecord.h:36
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.
int get_record_length() const
Returns the length of this record in bytes as it will be written to the flt file.
Definition fltVertex.cxx:62
LColor get_color() const
If has_color() indicates true, returns the color of the vertex, as a four- component value.
LRGBColor get_rgb() const
If has_color() indicates true, returns the color of the vertex, as a three- component value.
FltOpcode get_opcode() const
Returns the opcode that this record will be written as.
Definition fltVertex.cxx:41
void set_rgb(const LRGBColor &rgb)
Sets the color of the vertex, using the packed color convention.
bool has_color() const
Returns true if the vertex has a primary color indicated, false otherwise.
Definition fltVertex.I:18
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.