00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "fltLocalVertexPool.h"
00016 #include "fltRecordReader.h"
00017 #include "fltRecordWriter.h"
00018 #include "fltHeader.h"
00019 #include "fltMaterial.h"
00020
00021 TypeHandle FltLocalVertexPool::_type_handle;
00022
00023
00024
00025
00026
00027
00028 FltLocalVertexPool::
00029 FltLocalVertexPool(FltHeader *header) : FltRecord(header) {
00030 }
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 bool FltLocalVertexPool::
00041 extract_record(FltRecordReader &reader) {
00042 if (!FltRecord::extract_record(reader)) {
00043 return false;
00044 }
00045
00046 nassertr(reader.get_opcode() == FO_local_vertex_pool, false);
00047 DatagramIterator &iterator = reader.get_iterator();
00048
00049 int num_vertices = iterator.get_be_int32();
00050 int attributes = iterator.get_be_int32();
00051
00052 for (int i = 0; i < num_vertices; i++) {
00053 FltVertex *vertex = new FltVertex(_header);
00054 _vertices.push_back(vertex);
00055
00056 if ((attributes & AM_has_position) != 0) {
00057 vertex->_pos[0] = iterator.get_be_float64();
00058 vertex->_pos[1] = iterator.get_be_float64();
00059 vertex->_pos[2] = iterator.get_be_float64();
00060 }
00061
00062 if ((attributes & AM_has_color_index) != 0) {
00063 vertex->_color_index = iterator.get_be_int32();
00064
00065 } else if ((attributes & AM_has_packed_color) != 0) {
00066 if (!vertex->_packed_color.extract_record(reader)) {
00067 return false;
00068 }
00069 vertex->_flags |= FltVertex::F_packed_color;
00070
00071 } else {
00072 vertex->_flags |= FltVertex::F_no_color;
00073 }
00074
00075 if ((attributes & AM_has_normal) != 0) {
00076 vertex->_normal[0] = iterator.get_be_float32();
00077 vertex->_normal[1] = iterator.get_be_float32();
00078 vertex->_normal[2] = iterator.get_be_float32();
00079 vertex->_has_normal = true;
00080 }
00081
00082 if ((attributes & AM_has_base_uv) != 0) {
00083 vertex->_uv[0] = iterator.get_be_float32();
00084 vertex->_uv[1] = iterator.get_be_float32();
00085 vertex->_has_uv = true;
00086 }
00087
00088 if ((attributes & AM_has_uv_1) != 0) {
00089 iterator.get_be_float32();
00090 iterator.get_be_float32();
00091 }
00092
00093 if ((attributes & AM_has_uv_2) != 0) {
00094 iterator.get_be_float32();
00095 iterator.get_be_float32();
00096 }
00097
00098 if ((attributes & AM_has_uv_3) != 0) {
00099 iterator.get_be_float32();
00100 iterator.get_be_float32();
00101 }
00102
00103 if ((attributes & AM_has_uv_4) != 0) {
00104 iterator.get_be_float32();
00105 iterator.get_be_float32();
00106 }
00107
00108 if ((attributes & AM_has_uv_5) != 0) {
00109 iterator.get_be_float32();
00110 iterator.get_be_float32();
00111 }
00112
00113 if ((attributes & AM_has_uv_6) != 0) {
00114 iterator.get_be_float32();
00115 iterator.get_be_float32();
00116 }
00117
00118 if ((attributes & AM_has_uv_7) != 0) {
00119 iterator.get_be_float32();
00120 iterator.get_be_float32();
00121 }
00122 }
00123
00124 check_remaining_size(iterator);
00125 return true;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 bool FltLocalVertexPool::
00137 build_record(FltRecordWriter &writer) const {
00138 if (!FltRecord::build_record(writer)) {
00139 return false;
00140 }
00141
00142 writer.set_opcode(FO_local_vertex_pool);
00143 Datagram &datagram = writer.update_datagram();
00144
00145
00146 int attributes = AM_has_position;
00147
00148 Vertices::const_iterator vi;
00149 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
00150 FltVertex *vertex = (*vi);
00151 if ((vertex->_flags & FltVertex::F_no_color) != 0) {
00152
00153
00154 } else if ((vertex->_flags & FltVertex::F_packed_color) != 0) {
00155
00156 attributes |= AM_has_packed_color;
00157
00158 } else {
00159
00160 attributes |= AM_has_color_index;
00161 }
00162
00163 if (vertex->_has_normal) {
00164 attributes |= AM_has_normal;
00165 }
00166
00167 if (vertex->_has_uv) {
00168 attributes |= AM_has_base_uv;
00169 }
00170 }
00171
00172 if ((attributes & AM_has_packed_color) != 0 &&
00173 (attributes & AM_has_color_index) != 0) {
00174
00175
00176 attributes &= ~AM_has_color_index;
00177 }
00178
00179 datagram.add_be_int32(_vertices.size());
00180 datagram.add_be_int32(attributes);
00181
00182
00183 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
00184 FltVertex *vertex = (*vi);
00185
00186 if ((attributes & AM_has_position) != 0) {
00187 datagram.add_be_float64(vertex->_pos[0]);
00188 datagram.add_be_float64(vertex->_pos[1]);
00189 datagram.add_be_float64(vertex->_pos[2]);
00190 }
00191
00192 if ((attributes & AM_has_color_index) != 0) {
00193 if ((vertex->_flags & (FltVertex::F_no_color | FltVertex::F_packed_color)) != 0) {
00194
00195
00196 datagram.add_be_int32(_header->get_closest_rgb(LRGBColor(1.0, 1.0, 1.0)));
00197 } else {
00198 datagram.add_be_int32(vertex->_color_index);
00199 }
00200
00201 } else if ((attributes & AM_has_packed_color) != 0) {
00202
00203
00204
00205
00206
00207 FltPackedColor color;
00208 if (vertex->has_color()) {
00209 color.set_color(vertex->get_color());
00210 } else {
00211
00212 color.set_color(LColor(1.0, 1.0, 1.0, 1.0));
00213 }
00214
00215 if (!color.build_record(writer)) {
00216 return false;
00217 }
00218 }
00219
00220 if ((attributes & AM_has_normal) != 0) {
00221 if (!vertex->_has_normal) {
00222 datagram.add_be_float32(0.0);
00223 datagram.add_be_float32(0.0);
00224 datagram.add_be_float32(0.0);
00225 } else {
00226 datagram.add_be_float32(vertex->_normal[0]);
00227 datagram.add_be_float32(vertex->_normal[1]);
00228 datagram.add_be_float32(vertex->_normal[2]);
00229 }
00230 }
00231
00232 if ((attributes & AM_has_base_uv) != 0) {
00233 if (!vertex->_has_uv) {
00234 datagram.add_be_float32(0.0);
00235 datagram.add_be_float32(0.0);
00236 } else {
00237 datagram.add_be_float32(vertex->_uv[0]);
00238 datagram.add_be_float32(vertex->_uv[1]);
00239 }
00240 }
00241 }
00242
00243 return true;
00244 }