Panda3D
 All Classes Functions Variables Enumerations
fltLocalVertexPool.cxx
00001 // Filename: fltLocalVertexPool.cxx
00002 // Created by:  drose (28Feb01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
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 //     Function: FltLocalVertexPool::Constructor
00025 //       Access: Public
00026 //  Description:
00027 ////////////////////////////////////////////////////////////////////
00028 FltLocalVertexPool::
00029 FltLocalVertexPool(FltHeader *header) : FltRecord(header) {
00030 }
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //     Function: FltLocalVertexPool::extract_record
00034 //       Access: Protected, Virtual
00035 //  Description: Fills in the information in this bead based on the
00036 //               information given in the indicated datagram, whose
00037 //               opcode has already been read.  Returns true on
00038 //               success, false if the datagram is invalid.
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 //     Function: FltLocalVertexPool::build_record
00130 //       Access: Protected, Virtual
00131 //  Description: Fills up the current record on the FltRecordWriter with
00132 //               data for this record, but does not advance the
00133 //               writer.  Returns true on success, false if there is
00134 //               some error.
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   // Determine what kind of vertices we have.
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       // No color.
00153 
00154     } else if ((vertex->_flags & FltVertex::F_packed_color) != 0) {
00155       // Packed color.
00156       attributes |= AM_has_packed_color;
00157 
00158     } else {
00159       // Indexed color.
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     // We cannot have both a packed color and a color index.  If we
00175     // want both, used packed color.
00176     attributes &= ~AM_has_color_index;
00177   }
00178 
00179   datagram.add_be_int32(_vertices.size());
00180   datagram.add_be_int32(attributes);
00181 
00182   // Now write out each vertex.
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         // This particular vertex does not have a color index.
00195         // Make it white.
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       // We extract our own FltPackedColor instead of writing out the
00203       // vertex's _packed_color directly, just in case the vertex is
00204       // actually index colored.  This bit of code will work
00205       // regardless of the kind of color the vertex has.
00206 
00207       FltPackedColor color;
00208       if (vertex->has_color()) {
00209         color.set_color(vertex->get_color());
00210       } else {
00211         // An uncolored vertex.  Make it white.
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 }
 All Classes Functions Variables Enumerations