00001 // Filename: fltLocalVertexPool.cxx 00002 // Created by: drose (28Feb01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "fltLocalVertexPool.h" 00020 #include "fltRecordReader.h" 00021 #include "fltRecordWriter.h" 00022 #include "fltHeader.h" 00023 #include "fltMaterial.h" 00024 00025 TypeHandle FltLocalVertexPool::_type_handle; 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: FltLocalVertexPool::Constructor 00029 // Access: Public 00030 // Description: 00031 //////////////////////////////////////////////////////////////////// 00032 FltLocalVertexPool:: 00033 FltLocalVertexPool(FltHeader *header) : FltRecord(header) { 00034 } 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Function: FltLocalVertexPool::extract_record 00038 // Access: Protected, Virtual 00039 // Description: Fills in the information in this bead based on the 00040 // information given in the indicated datagram, whose 00041 // opcode has already been read. Returns true on 00042 // success, false if the datagram is invalid. 00043 //////////////////////////////////////////////////////////////////// 00044 bool FltLocalVertexPool:: 00045 extract_record(FltRecordReader &reader) { 00046 if (!FltRecord::extract_record(reader)) { 00047 return false; 00048 } 00049 00050 nassertr(reader.get_opcode() == FO_local_vertex_pool, false); 00051 DatagramIterator &iterator = reader.get_iterator(); 00052 00053 int num_vertices = iterator.get_be_int32(); 00054 int attributes = iterator.get_be_int32(); 00055 00056 for (int i = 0; i < num_vertices; i++) { 00057 FltVertex *vertex = new FltVertex(_header); 00058 _vertices.push_back(vertex); 00059 00060 if ((attributes & AM_has_position) != 0) { 00061 vertex->_pos[0] = iterator.get_be_float64(); 00062 vertex->_pos[1] = iterator.get_be_float64(); 00063 vertex->_pos[2] = iterator.get_be_float64(); 00064 } 00065 00066 if ((attributes & AM_has_color_index) != 0) { 00067 vertex->_color_index = iterator.get_be_int32(); 00068 00069 } else if ((attributes & AM_has_packed_color) != 0) { 00070 if (!vertex->_packed_color.extract_record(reader)) { 00071 return false; 00072 } 00073 vertex->_flags |= FltVertex::F_packed_color; 00074 00075 } else { 00076 vertex->_flags |= FltVertex::F_no_color; 00077 } 00078 00079 if ((attributes & AM_has_normal) != 0) { 00080 vertex->_normal[0] = iterator.get_be_float32(); 00081 vertex->_normal[1] = iterator.get_be_float32(); 00082 vertex->_normal[2] = iterator.get_be_float32(); 00083 vertex->_has_normal = true; 00084 } 00085 00086 if ((attributes & AM_has_base_uv) != 0) { 00087 vertex->_uv[0] = iterator.get_be_float32(); 00088 vertex->_uv[1] = iterator.get_be_float32(); 00089 vertex->_has_uv = true; 00090 } 00091 00092 if ((attributes & AM_has_uv_1) != 0) { 00093 iterator.get_be_float32(); 00094 iterator.get_be_float32(); 00095 } 00096 00097 if ((attributes & AM_has_uv_2) != 0) { 00098 iterator.get_be_float32(); 00099 iterator.get_be_float32(); 00100 } 00101 00102 if ((attributes & AM_has_uv_3) != 0) { 00103 iterator.get_be_float32(); 00104 iterator.get_be_float32(); 00105 } 00106 00107 if ((attributes & AM_has_uv_4) != 0) { 00108 iterator.get_be_float32(); 00109 iterator.get_be_float32(); 00110 } 00111 00112 if ((attributes & AM_has_uv_5) != 0) { 00113 iterator.get_be_float32(); 00114 iterator.get_be_float32(); 00115 } 00116 00117 if ((attributes & AM_has_uv_6) != 0) { 00118 iterator.get_be_float32(); 00119 iterator.get_be_float32(); 00120 } 00121 00122 if ((attributes & AM_has_uv_7) != 0) { 00123 iterator.get_be_float32(); 00124 iterator.get_be_float32(); 00125 } 00126 } 00127 00128 check_remaining_size(iterator); 00129 return true; 00130 } 00131 00132 //////////////////////////////////////////////////////////////////// 00133 // Function: FltLocalVertexPool::build_record 00134 // Access: Protected, Virtual 00135 // Description: Fills up the current record on the FltRecordWriter with 00136 // data for this record, but does not advance the 00137 // writer. Returns true on success, false if there is 00138 // some error. 00139 //////////////////////////////////////////////////////////////////// 00140 bool FltLocalVertexPool:: 00141 build_record(FltRecordWriter &writer) const { 00142 if (!FltRecord::build_record(writer)) { 00143 return false; 00144 } 00145 00146 writer.set_opcode(FO_local_vertex_pool); 00147 Datagram &datagram = writer.update_datagram(); 00148 00149 // Determine what kind of vertices we have. 00150 int attributes = AM_has_position; 00151 00152 Vertices::const_iterator vi; 00153 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { 00154 FltVertex *vertex = (*vi); 00155 if ((vertex->_flags & FltVertex::F_no_color) != 0) { 00156 // No color. 00157 00158 } else if ((vertex->_flags & FltVertex::F_packed_color) != 0) { 00159 // Packed color. 00160 attributes |= AM_has_packed_color; 00161 00162 } else { 00163 // Indexed color. 00164 attributes |= AM_has_color_index; 00165 } 00166 00167 if (vertex->_has_normal) { 00168 attributes |= AM_has_normal; 00169 } 00170 00171 if (vertex->_has_uv) { 00172 attributes |= AM_has_base_uv; 00173 } 00174 } 00175 00176 if ((attributes & AM_has_packed_color) != 0 && 00177 (attributes & AM_has_color_index) != 0) { 00178 // We cannot have both a packed color and a color index. If we 00179 // want both, used packed color. 00180 attributes &= ~AM_has_color_index; 00181 } 00182 00183 datagram.add_be_int32(_vertices.size()); 00184 datagram.add_be_int32(attributes); 00185 00186 // Now write out each vertex. 00187 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { 00188 FltVertex *vertex = (*vi); 00189 00190 if ((attributes & AM_has_position) != 0) { 00191 datagram.add_be_float64(vertex->_pos[0]); 00192 datagram.add_be_float64(vertex->_pos[1]); 00193 datagram.add_be_float64(vertex->_pos[2]); 00194 } 00195 00196 if ((attributes & AM_has_color_index) != 0) { 00197 if ((vertex->_flags & (FltVertex::F_no_color | FltVertex::F_packed_color)) != 0) { 00198 // This particular vertex does not have a color index. 00199 // Make it white. 00200 datagram.add_be_int32(_header->get_closest_rgb(RGBColorf(1.0, 1.0, 1.0))); 00201 } else { 00202 datagram.add_be_int32(vertex->_color_index); 00203 } 00204 00205 } else if ((attributes & AM_has_packed_color) != 0) { 00206 // We extract our own FltPackedColor instead of writing out the 00207 // vertex's _packed_color directly, just in case the vertex is 00208 // actually index colored. This bit of code will work 00209 // regardless of the kind of color the vertex has. 00210 00211 FltPackedColor color; 00212 if (vertex->has_color()) { 00213 color.set_color(vertex->get_color()); 00214 } else { 00215 // An uncolored vertex. Make it white. 00216 color.set_color(Colorf(1.0, 1.0, 1.0, 1.0)); 00217 } 00218 00219 if (!color.build_record(writer)) { 00220 return false; 00221 } 00222 } 00223 00224 if ((attributes & AM_has_normal) != 0) { 00225 if (!vertex->_has_normal) { 00226 datagram.add_be_float32(0.0); 00227 datagram.add_be_float32(0.0); 00228 datagram.add_be_float32(0.0); 00229 } else { 00230 datagram.add_be_float32(vertex->_normal[0]); 00231 datagram.add_be_float32(vertex->_normal[1]); 00232 datagram.add_be_float32(vertex->_normal[2]); 00233 } 00234 } 00235 00236 if ((attributes & AM_has_base_uv) != 0) { 00237 if (!vertex->_has_uv) { 00238 datagram.add_be_float32(0.0); 00239 datagram.add_be_float32(0.0); 00240 } else { 00241 datagram.add_be_float32(vertex->_uv[0]); 00242 datagram.add_be_float32(vertex->_uv[1]); 00243 } 00244 } 00245 } 00246 00247 return true; 00248 }