Panda3D
fltGeometry.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 fltGeometry.cxx
10  * @author drose
11  * @date 2001-02-28
12  */
13 
14 #include "fltGeometry.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 #include "fltHeader.h"
18 #include "fltMaterial.h"
19 
20 TypeHandle FltGeometry::_type_handle;
21 
22 /**
23  *
24  */
25 FltGeometry::
26 FltGeometry(FltHeader *header) : FltBeadID(header) {
27  _ir_color = 0;
28  _relative_priority = 0;
29  _draw_type = DT_solid_cull_backface;
30  _texwhite = false;
31  _color_name_index = 0;
32  _alt_color_name_index = 0;
33  _billboard_type = BT_none;
34  _detail_texture_index = -1;
35  _texture_index = -1;
36  _material_index = -1;
37  _dfad_material_code = 0;
38  _dfad_feature_id = 0;
39  _ir_material_code = 0;
40  _transparency = 0;
41  _lod_generation_control = 0;
42  _line_style_index = 0;
43  _flags = F_no_color;
44  _light_mode = LM_face_no_normal;
45  _texture_mapping_index = 0;
46  _color_index = 0;
47  _alt_color_index = 0;
48 }
49 
50 
51 /**
52  * Returns the primary color of the face, as a four-component value (including
53  * alpha as the transparency channel).
54  *
55  * If has_color() is false, the result is white, but still reflects the
56  * transparency correctly.
57  */
58 LColor FltGeometry::
59 get_color() const {
60  LColor color;
61 
62  if (!has_color() || (_texwhite && has_texture())) {
63  // Force this one white.
64  color.set(1.0, 1.0, 1.0, 1.0);
65 
66  } else if (has_material()) {
67  // If we have a material, that replaces the color.
68  FltMaterial *material = get_material();
69  color.set(material->_diffuse[0],
70  material->_diffuse[1],
71  material->_diffuse[2],
72  material->_alpha);
73  } else {
74  LRGBColor rgb =
75  _header->get_rgb(_color_index, (_flags & F_packed_color) != 0,
76  _packed_color);
77  color.set(rgb[0], rgb[1], rgb[2], 1.0);
78  }
79 
80  // Modify the whole thing by our transparency.
81  PN_stdfloat alpha = 1.0 - (_transparency / 65535.0);
82  color[3] *= alpha;
83 
84  return color;
85 }
86 
87 /**
88  * Sets the primary color of the face, using the packed color convention.
89  */
90 void FltGeometry::
91 set_color(const LColor &color) {
92  set_rgb(LRGBColor(color[0], color[1], color[2]));
93  _transparency = (int)floor((1.0 - color[3]) * 65535.0);
94 }
95 
96 /**
97  * Returns the primary color of the face, as a three-component value ignoring
98  * transparency.
99  */
100 LRGBColor FltGeometry::
101 get_rgb() const {
102  if (!has_color() || (_texwhite && has_texture())) {
103  // Force this one white.
104  return LRGBColor(1.0, 1.0, 1.0);
105  }
106 
107  if (has_material()) {
108  // If we have a material, that replaces the color.
109  FltMaterial *material = get_material();
110  return material->_diffuse;
111  }
112 
113  return _header->get_rgb(_color_index, (_flags & F_packed_color) != 0,
114  _packed_color);
115 }
116 
117 /**
118  * Sets the primary color of the face, using the packed color convention; does
119  * not affect transparency.
120  */
121 void FltGeometry::
122 set_rgb(const LRGBColor &rgb) {
123  _packed_color.set_rgb(rgb);
124  _flags = ((_flags & ~F_no_color) | F_packed_color);
125 
126  // If we have a color, we can't have a material.
127  _material_index = -1;
128  _texwhite = false;
129 }
130 
131 /**
132  * Returns true if the face has an alternate color indicated, false otherwise.
133  */
134 bool FltGeometry::
135 has_alt_color() const {
136  return (_flags & F_no_alt_color) == 0;
137 }
138 
139 /**
140  * If has_alt_color() indicates true, returns the alternate color of the face,
141  * as a four-component value (including alpha as the transparency channel).
142  */
143 LColor FltGeometry::
144 get_alt_color() const {
145  nassertr(has_alt_color(), LColor(0.0, 0.0, 0.0, 0.0));
146 
147  return _header->get_color(_alt_color_index, (_flags & F_packed_color) != 0,
148  _alt_packed_color, _transparency);
149 }
150 
151 /**
152  * If has_alt_color() indicates true, returns the alternate color of the face,
153  * as a three-component value ignoring transparency.
154  */
155 LRGBColor FltGeometry::
156 get_alt_rgb() const {
157  nassertr(has_alt_color(), LRGBColor(0.0, 0.0, 0.0));
158 
159  return _header->get_rgb(_alt_color_index, (_flags & F_packed_color) != 0,
160  _alt_packed_color);
161 }
162 
163 /**
164  * Fills in the information in this bead based on the information given in the
165  * indicated datagram, whose opcode has already been read. Returns true on
166  * success, false if the datagram is invalid.
167  */
168 bool FltGeometry::
169 extract_record(FltRecordReader &reader) {
170  DatagramIterator &iterator = reader.get_iterator();
171 
172  _ir_color = iterator.get_be_int32();
173  _relative_priority = iterator.get_be_int16();
174  _draw_type = (DrawType)iterator.get_int8();
175  _texwhite = (iterator.get_int8() != 0);
176  _color_name_index = iterator.get_be_int16();
177  _alt_color_name_index = iterator.get_be_int16();
178  iterator.skip_bytes(1);
179  _billboard_type = (BillboardType)iterator.get_int8();
180  _detail_texture_index = iterator.get_be_int16();
181  _texture_index = iterator.get_be_int16();
182  _material_index = iterator.get_be_int16();
183  _dfad_material_code = iterator.get_be_int16();
184  _dfad_feature_id = iterator.get_be_int16();
185  _ir_material_code = iterator.get_be_int32();
186  _transparency = iterator.get_be_uint16();
187  _lod_generation_control = iterator.get_uint8();
188  _line_style_index = iterator.get_uint8();
189  if (_header->get_flt_version() >= 1420) {
190  _flags = iterator.get_be_uint32();
191  _light_mode = (LightMode)iterator.get_uint8();
192  iterator.skip_bytes(1 + 4);
193  iterator.skip_bytes(2); // Undocumented padding.
194 
195  if (!_packed_color.extract_record(reader)) {
196  return false;
197  }
198  if (!_alt_packed_color.extract_record(reader)) {
199  return false;
200  }
201 
202  if (_header->get_flt_version() >= 1520) {
203  _texture_mapping_index = iterator.get_be_int16();
204  iterator.skip_bytes(2);
205  _color_index = iterator.get_be_int32();
206  _alt_color_index = iterator.get_be_int32();
207  iterator.skip_bytes(2 + 2);
208  }
209  }
210 
211  return true;
212 }
213 
214 /**
215  * Fills up the current record on the FltRecordWriter with data for this
216  * record, but does not advance the writer. Returns true on success, false if
217  * there is some error.
218  */
219 bool FltGeometry::
220 build_record(FltRecordWriter &writer) const {
221  Datagram &datagram = writer.update_datagram();
222 
223  datagram.add_be_int32(_ir_color);
224  datagram.add_be_int16(_relative_priority);
225  datagram.add_int8(_draw_type);
226  datagram.add_int8(_texwhite);
227  datagram.add_be_uint16(_color_name_index);
228  datagram.add_be_uint16(_alt_color_name_index);
229  datagram.pad_bytes(1);
230  datagram.add_int8(_billboard_type);
231  datagram.add_be_int16(_detail_texture_index);
232  datagram.add_be_int16(_texture_index);
233  datagram.add_be_int16(_material_index);
234  datagram.add_be_int16(_dfad_material_code);
235  datagram.add_be_int16(_dfad_feature_id);
236  datagram.add_be_int32(_ir_material_code);
237  datagram.add_be_uint16(_transparency);
238  datagram.add_uint8(_lod_generation_control);
239  datagram.add_uint8(_line_style_index);
240  datagram.add_be_uint32(_flags);
241  datagram.add_uint8(_light_mode);
242  datagram.pad_bytes(1 + 4);
243  datagram.pad_bytes(2); // Undocumented padding.
244 
245  if (!_packed_color.build_record(writer)) {
246  return false;
247  }
248  if (!_alt_packed_color.build_record(writer)) {
249  return false;
250  }
251 
252  if (_header->get_flt_version() >= 1520) {
253  // New with 15.2
254  datagram.add_be_int16(_texture_mapping_index);
255  datagram.pad_bytes(2);
256  datagram.add_be_int32(_color_index);
257  datagram.add_be_int32(_alt_color_index);
258  datagram.pad_bytes(2 + 2);
259  }
260 
261  return true;
262 }
LRGBColor get_rgb() const
Returns the primary color of the face, as a three-component value ignoring transparency.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
A base class for any of a broad family of flt beads that include an ID.
Definition: fltBeadID.h:24
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_color(const LColor &color)
Sets the primary color of the face, using the packed color convention.
Definition: fltGeometry.cxx:91
int32_t get_be_int32()
Extracts a signed 32-bit big-endian integer.
Represents a single material in the material palette.
Definition: fltMaterial.h:28
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
void set_rgb(const LRGBColor &rgb)
Sets the color according to the indicated three-component LRGBColor value, and set the alpha to 1....
FltMaterial * get_material() const
Returns the material applied to this face, or NULL if no material was applied.
Definition: fltGeometry.I:58
bool has_material() const
Returns true if the face has a material applied, false otherwise.
Definition: fltGeometry.I:49
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:99
bool has_color() const
Returns true if the face has a primary color indicated, false otherwise.
Definition: fltGeometry.I:80
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
uint16_t get_be_uint16()
Extracts an unsigned 16-bit big-endian integer.
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_be_uint16(uint16_t value)
Adds an unsigned 16-bit big-endian integer to the datagram.
Definition: datagram.I:172
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
void add_be_int16(int16_t value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition: datagram.I:145
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition: datagram.I:181
LColor get_alt_color() const
If has_alt_color() indicates true, returns the alternate color of the face, as a four-component value...
LColor get_color() const
Returns the primary color of the face, as a four-component value (including alpha as the transparency...
Definition: fltGeometry.cxx:59
LRGBColor get_alt_rgb() const
If has_alt_color() indicates true, returns the alternate color of the face, as a three-component valu...
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:50
A class to retrieve the individual data elements previously stored in a Datagram.
int16_t get_be_int16()
Extracts a signed 16-bit big-endian integer.
void add_be_int32(int32_t value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition: datagram.I:154
int8_t get_int8()
Extracts a signed 8-bit integer.
bool has_texture() const
Returns true if the face has a texture applied, false otherwise.
Definition: fltGeometry.I:18
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
uint32_t get_be_uint32()
Extracts an unsigned 32-bit big-endian integer.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
void set_rgb(const LRGBColor &rgb)
Sets the primary color of the face, using the packed color convention; does not affect transparency.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
bool has_alt_color() const
Returns true if the face has an alternate color indicated, false otherwise.