00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "fltGeometry.h"
00016 #include "fltRecordReader.h"
00017 #include "fltRecordWriter.h"
00018 #include "fltHeader.h"
00019 #include "fltMaterial.h"
00020
00021 TypeHandle FltGeometry::_type_handle;
00022
00023
00024
00025
00026
00027
00028 FltGeometry::
00029 FltGeometry(FltHeader *header) : FltBeadID(header) {
00030 _ir_color = 0;
00031 _relative_priority = 0;
00032 _draw_type = DT_solid_cull_backface;
00033 _texwhite = false;
00034 _color_name_index = 0;
00035 _alt_color_name_index = 0;
00036 _billboard_type = BT_none;
00037 _detail_texture_index = -1;
00038 _texture_index = -1;
00039 _material_index = -1;
00040 _dfad_material_code = 0;
00041 _dfad_feature_id = 0;
00042 _ir_material_code = 0;
00043 _transparency = 0;
00044 _lod_generation_control = 0;
00045 _line_style_index = 0;
00046 _flags = F_no_color;
00047 _light_mode = LM_face_no_normal;
00048 _texture_mapping_index = 0;
00049 _color_index = 0;
00050 _alt_color_index = 0;
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 LColor FltGeometry::
00065 get_color() const {
00066 LColor color;
00067
00068 if (!has_color() || (_texwhite && has_texture())) {
00069
00070 color.set(1.0, 1.0, 1.0, 1.0);
00071
00072 } else if (has_material()) {
00073
00074 FltMaterial *material = get_material();
00075 color.set(material->_diffuse[0],
00076 material->_diffuse[1],
00077 material->_diffuse[2],
00078 material->_alpha);
00079 } else {
00080 LRGBColor rgb =
00081 _header->get_rgb(_color_index, (_flags & F_packed_color) != 0,
00082 _packed_color);
00083 color.set(rgb[0], rgb[1], rgb[2], 1.0);
00084 }
00085
00086
00087 PN_stdfloat alpha = 1.0 - (_transparency / 65535.0);
00088 color[3] *= alpha;
00089
00090 return color;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 void FltGeometry::
00100 set_color(const LColor &color) {
00101 set_rgb(LRGBColor(color[0], color[1], color[2]));
00102 _transparency = (int)floor((1.0 - color[3]) * 65535.0);
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 LRGBColor FltGeometry::
00112 get_rgb() const {
00113 if (!has_color() || (_texwhite && has_texture())) {
00114
00115 return LRGBColor(1.0, 1.0, 1.0);
00116 }
00117
00118 if (has_material()) {
00119
00120 FltMaterial *material = get_material();
00121 return material->_diffuse;
00122 }
00123
00124 return _header->get_rgb(_color_index, (_flags & F_packed_color) != 0,
00125 _packed_color);
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 void FltGeometry::
00135 set_rgb(const LRGBColor &rgb) {
00136 _packed_color.set_rgb(rgb);
00137 _flags = ((_flags & ~F_no_color) | F_packed_color);
00138
00139
00140 _material_index = -1;
00141 _texwhite = false;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 bool FltGeometry::
00151 has_alt_color() const {
00152 return (_flags & F_no_alt_color) == 0;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162 LColor FltGeometry::
00163 get_alt_color() const {
00164 nassertr(has_alt_color(), LColor(0.0, 0.0, 0.0, 0.0));
00165
00166 return _header->get_color(_alt_color_index, (_flags & F_packed_color) != 0,
00167 _alt_packed_color, _transparency);
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 LRGBColor FltGeometry::
00178 get_alt_rgb() const {
00179 nassertr(has_alt_color(), LRGBColor(0.0, 0.0, 0.0));
00180
00181 return _header->get_rgb(_alt_color_index, (_flags & F_packed_color) != 0,
00182 _alt_packed_color);
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 bool FltGeometry::
00194 extract_record(FltRecordReader &reader) {
00195 DatagramIterator &iterator = reader.get_iterator();
00196
00197 _ir_color = iterator.get_be_int32();
00198 _relative_priority = iterator.get_be_int16();
00199 _draw_type = (DrawType)iterator.get_int8();
00200 _texwhite = (iterator.get_int8() != 0);
00201 _color_name_index = iterator.get_be_int16();
00202 _alt_color_name_index = iterator.get_be_int16();
00203 iterator.skip_bytes(1);
00204 _billboard_type = (BillboardType)iterator.get_int8();
00205 _detail_texture_index = iterator.get_be_int16();
00206 _texture_index = iterator.get_be_int16();
00207 _material_index = iterator.get_be_int16();
00208 _dfad_material_code = iterator.get_be_int16();
00209 _dfad_feature_id = iterator.get_be_int16();
00210 _ir_material_code = iterator.get_be_int32();
00211 _transparency = iterator.get_be_uint16();
00212 _lod_generation_control = iterator.get_uint8();
00213 _line_style_index = iterator.get_uint8();
00214 if (_header->get_flt_version() >= 1420) {
00215 _flags = iterator.get_be_uint32();
00216 _light_mode = (LightMode)iterator.get_uint8();
00217 iterator.skip_bytes(1 + 4);
00218 iterator.skip_bytes(2);
00219
00220 if (!_packed_color.extract_record(reader)) {
00221 return false;
00222 }
00223 if (!_alt_packed_color.extract_record(reader)) {
00224 return false;
00225 }
00226
00227 if (_header->get_flt_version() >= 1520) {
00228 _texture_mapping_index = iterator.get_be_int16();
00229 iterator.skip_bytes(2);
00230 _color_index = iterator.get_be_int32();
00231 _alt_color_index = iterator.get_be_int32();
00232 iterator.skip_bytes(2 + 2);
00233 }
00234 }
00235
00236 return true;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 bool FltGeometry::
00248 build_record(FltRecordWriter &writer) const {
00249 Datagram &datagram = writer.update_datagram();
00250
00251 datagram.add_be_int32(_ir_color);
00252 datagram.add_be_int16(_relative_priority);
00253 datagram.add_int8(_draw_type);
00254 datagram.add_int8(_texwhite);
00255 datagram.add_be_uint16(_color_name_index);
00256 datagram.add_be_uint16(_alt_color_name_index);
00257 datagram.pad_bytes(1);
00258 datagram.add_int8(_billboard_type);
00259 datagram.add_be_int16(_detail_texture_index);
00260 datagram.add_be_int16(_texture_index);
00261 datagram.add_be_int16(_material_index);
00262 datagram.add_be_int16(_dfad_material_code);
00263 datagram.add_be_int16(_dfad_feature_id);
00264 datagram.add_be_int32(_ir_material_code);
00265 datagram.add_be_uint16(_transparency);
00266 datagram.add_uint8(_lod_generation_control);
00267 datagram.add_uint8(_line_style_index);
00268 datagram.add_be_uint32(_flags);
00269 datagram.add_uint8(_light_mode);
00270 datagram.pad_bytes(1 + 4);
00271 datagram.pad_bytes(2);
00272
00273 if (!_packed_color.build_record(writer)) {
00274 return false;
00275 }
00276 if (!_alt_packed_color.build_record(writer)) {
00277 return false;
00278 }
00279
00280 if (_header->get_flt_version() >= 1520) {
00281
00282 datagram.add_be_int16(_texture_mapping_index);
00283 datagram.pad_bytes(2);
00284 datagram.add_be_int32(_color_index);
00285 datagram.add_be_int32(_alt_color_index);
00286 datagram.pad_bytes(2 + 2);
00287 }
00288
00289 return true;
00290 }