Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/flt/fltVertex.cxx

Go to the documentation of this file.
00001 // Filename: fltVertex.cxx
00002 // Created by:  drose (25Aug00)
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 "fltVertex.h"
00020 #include "fltRecordReader.h"
00021 #include "fltRecordWriter.h"
00022 #include "fltHeader.h"
00023 
00024 TypeHandle FltVertex::_type_handle;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: FltVertex::Constructor
00028 //       Access: Public
00029 //  Description:
00030 ////////////////////////////////////////////////////////////////////
00031 FltVertex::
00032 FltVertex(FltHeader *header) : FltRecord(header) {
00033   _color_name_index = 0;
00034   _flags = 0;
00035   _pos.set(0.0, 0.0, 0.0);
00036   _normal.set(0.0, 0.0, 0.0);
00037   _uv.set(0.0, 0.0);
00038   _color_index = 0;
00039 
00040   _has_normal = false;
00041   _has_uv = false;
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: FltVertex::get_opcode
00046 //       Access: Public
00047 //  Description: Returns the opcode that this record will be written
00048 //               as.
00049 ////////////////////////////////////////////////////////////////////
00050 FltOpcode FltVertex::
00051 get_opcode() const {
00052   if (_has_normal) {
00053     if (_has_uv) {
00054       return FO_vertex_cnu;
00055     } else {
00056       return FO_vertex_cn;
00057     }
00058   } else {
00059     if (_has_uv) {
00060       return FO_vertex_cu;
00061     } else {
00062       return FO_vertex_c;
00063     }
00064   }
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: FltVertex::get_record_length
00069 //       Access: Public
00070 //  Description: Returns the length of this record in bytes as it will
00071 //               be written to the flt file.
00072 ////////////////////////////////////////////////////////////////////
00073 int FltVertex::
00074 get_record_length() const {
00075   if (_header->get_flt_version() < 1520) {
00076     // Version 14.2
00077     switch (get_opcode()) {
00078     case FO_vertex_c:
00079       return 36;
00080 
00081     case FO_vertex_cn:
00082       return 48;
00083 
00084     case FO_vertex_cnu:
00085       return 56;
00086 
00087     case FO_vertex_cu:
00088       return 44;
00089 
00090     default:
00091       nassertr(false, 0);
00092     }
00093 
00094   } else {
00095     // Version 15.2 and higher
00096     switch (get_opcode()) {
00097     case FO_vertex_c:
00098       return 40;
00099 
00100     case FO_vertex_cn:
00101       return 56;
00102 
00103     case FO_vertex_cnu:
00104       return 64;
00105 
00106     case FO_vertex_cu:
00107       return 48;
00108 
00109     default:
00110       nassertr(false, 0);
00111     }
00112   }
00113 
00114   return 0;
00115 }
00116 
00117 ////////////////////////////////////////////////////////////////////
00118 //     Function: FltVertex::get_color
00119 //       Access: Public
00120 //  Description: If has_color() indicates true, returns the primary
00121 //               color of the face, as a four-component value.  In the
00122 //               case of a vertex, the alpha channel will always be
00123 //               1.0, as MultiGen does not store transparency
00124 //               per-vertex.
00125 ////////////////////////////////////////////////////////////////////
00126 Colorf FltVertex::
00127 get_color() const {
00128   nassertr(has_color(), Colorf(0.0, 0.0, 0.0, 0.0));
00129 
00130   return _header->get_color(_color_index, (_flags & F_packed_color) != 0,
00131                             _packed_color, 0);
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: FltVertex::get_rgb
00136 //       Access: Public
00137 //  Description: If has_color() indicates true, returns the primary
00138 //               color of the face, as a three-component value
00139 //               ignoring transparency.
00140 ////////////////////////////////////////////////////////////////////
00141 RGBColorf FltVertex::
00142 get_rgb() const {
00143   nassertr(has_color(), RGBColorf(0.0, 0.0, 0.0));
00144 
00145   return _header->get_rgb(_color_index, (_flags & F_packed_color) != 0,
00146                           _packed_color);
00147 }
00148 
00149 ////////////////////////////////////////////////////////////////////
00150 //     Function: FltVertex::extract_record
00151 //       Access: Protected, Virtual
00152 //  Description: Fills in the information in this record based on the
00153 //               information given in the indicated datagram, whose
00154 //               opcode has already been read.  Returns true on
00155 //               success, false if the datagram is invalid.
00156 ////////////////////////////////////////////////////////////////////
00157 bool FltVertex::
00158 extract_record(FltRecordReader &reader) {
00159   if (!FltRecord::extract_record(reader)) {
00160     return false;
00161   }
00162 
00163   switch (reader.get_opcode()) {
00164   case FO_vertex_c:
00165     _has_normal = false;
00166     _has_uv = false;
00167     break;
00168 
00169   case FO_vertex_cn:
00170     _has_normal = true;
00171     _has_uv = false;
00172     break;
00173 
00174   case FO_vertex_cnu:
00175     _has_normal = true;
00176     _has_uv = true;
00177     break;
00178 
00179   case FO_vertex_cu:
00180     _has_normal = false;
00181     _has_uv = true;
00182     break;
00183 
00184   default:
00185     nassertr(false, false);
00186   }
00187 
00188   DatagramIterator &iterator = reader.get_iterator();
00189 
00190   _color_name_index = iterator.get_be_int16();
00191   _flags = iterator.get_be_uint16();
00192   _pos[0] = iterator.get_be_float64();
00193   _pos[1] = iterator.get_be_float64();
00194   _pos[2] = iterator.get_be_float64();
00195 
00196   if (_has_normal) {
00197     _normal[0] = iterator.get_be_float32();
00198     _normal[1] = iterator.get_be_float32();
00199     _normal[2] = iterator.get_be_float32();
00200   }
00201   if (_has_uv) {
00202     _uv[0] = iterator.get_be_float32();
00203     _uv[1] = iterator.get_be_float32();
00204   }
00205 
00206   if (iterator.get_remaining_size() > 0) {
00207     if (!_packed_color.extract_record(reader)) {
00208       return false;
00209     }
00210     if (_header->get_flt_version() >= 1520) {
00211       _color_index = iterator.get_be_int32();
00212       
00213       if (_has_normal && iterator.get_remaining_size() > 0) {
00214         // If we extracted a normal, our double-word alignment is off; now
00215         // we have a few extra bytes to ignore.
00216         iterator.skip_bytes(4);
00217       }
00218     }
00219   }
00220 
00221   check_remaining_size(iterator);
00222   return true;
00223 }
00224 
00225 ////////////////////////////////////////////////////////////////////
00226 //     Function: FltVertex::build_record
00227 //       Access: Protected, Virtual
00228 //  Description: Fills up the current record on the FltRecordWriter with
00229 //               data for this record, but does not advance the
00230 //               writer.  Returns true on success, false if there is
00231 //               some error.
00232 ////////////////////////////////////////////////////////////////////
00233 bool FltVertex::
00234 build_record(FltRecordWriter &writer) const {
00235   if (!FltRecord::build_record(writer)) {
00236     return false;
00237   }
00238 
00239   writer.set_opcode(get_opcode());
00240   Datagram &datagram = writer.update_datagram();
00241 
00242   datagram.add_be_int16(_color_name_index);
00243   datagram.add_be_uint16(_flags);
00244   datagram.add_be_float64(_pos[0]);
00245   datagram.add_be_float64(_pos[1]);
00246   datagram.add_be_float64(_pos[2]);
00247 
00248   if (_has_normal) {
00249     datagram.add_be_float32(_normal[0]);
00250     datagram.add_be_float32(_normal[1]);
00251     datagram.add_be_float32(_normal[2]);
00252   }
00253   if (_has_uv) {
00254     datagram.add_be_float32(_uv[0]);
00255     datagram.add_be_float32(_uv[1]);
00256   }
00257 
00258   if (!_packed_color.build_record(writer)) {
00259     return false;
00260   }
00261 
00262   if (_header->get_flt_version() >= 1520) {
00263     // New with 15.2
00264     datagram.add_be_uint32(_color_index);
00265 
00266     if (_has_normal) {
00267       // If we added a normal, our double-word alignment is off; now we
00268       // have a few extra bytes to add.
00269       datagram.pad_bytes(4);
00270     }
00271   }
00272 
00273   nassertr((int)datagram.get_length() == get_record_length() - 4, true);
00274   return true;
00275 }

Generated on Fri May 2 03:19:49 2003 for Panda-Tool by doxygen1.3