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