00001 // Filename: fltMeshPrimitive.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 "fltMeshPrimitive.h" 00020 #include "fltRecordReader.h" 00021 #include "fltRecordWriter.h" 00022 #include "fltHeader.h" 00023 #include "fltMaterial.h" 00024 00025 TypeHandle FltMeshPrimitive::_type_handle; 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: FltMeshPrimitive::Constructor 00029 // Access: Public 00030 // Description: 00031 //////////////////////////////////////////////////////////////////// 00032 FltMeshPrimitive:: 00033 FltMeshPrimitive(FltHeader *header) : FltBead(header) { 00034 _primitive_type = PT_tristrip; 00035 } 00036 00037 //////////////////////////////////////////////////////////////////// 00038 // Function: FltMeshPrimitive::extract_record 00039 // Access: Protected, Virtual 00040 // Description: Fills in the information in this bead based on the 00041 // information given in the indicated datagram, whose 00042 // opcode has already been read. Returns true on 00043 // success, false if the datagram is invalid. 00044 //////////////////////////////////////////////////////////////////// 00045 bool FltMeshPrimitive:: 00046 extract_record(FltRecordReader &reader) { 00047 if (!FltBead::extract_record(reader)) { 00048 return false; 00049 } 00050 00051 nassertr(reader.get_opcode() == FO_mesh_primitive, false); 00052 DatagramIterator &iterator = reader.get_iterator(); 00053 00054 _primitive_type = (PrimitiveType)iterator.get_be_int16(); 00055 00056 int vertex_width = iterator.get_be_int16(); 00057 int num_vertices = iterator.get_be_int32(); 00058 00059 if (vertex_width == 1) { 00060 for (int i = 0; i < num_vertices; i++) { 00061 _vertices.push_back(iterator.get_uint8()); 00062 } 00063 00064 } else if (vertex_width == 2) { 00065 for (int i = 0; i < num_vertices; i++) { 00066 _vertices.push_back(iterator.get_be_uint16()); 00067 } 00068 00069 } else if (vertex_width == 4) { 00070 for (int i = 0; i < num_vertices; i++) { 00071 _vertices.push_back(iterator.get_be_int32()); 00072 } 00073 00074 } else { 00075 nout << "Invalid vertex width in mesh primitive: " << vertex_width 00076 << "\n"; 00077 return false; 00078 } 00079 00080 check_remaining_size(iterator); 00081 return true; 00082 } 00083 00084 //////////////////////////////////////////////////////////////////// 00085 // Function: FltMeshPrimitive::build_record 00086 // Access: Protected, Virtual 00087 // Description: Fills up the current record on the FltRecordWriter with 00088 // data for this record, but does not advance the 00089 // writer. Returns true on success, false if there is 00090 // some error. 00091 //////////////////////////////////////////////////////////////////// 00092 bool FltMeshPrimitive:: 00093 build_record(FltRecordWriter &writer) const { 00094 if (!FltBead::build_record(writer)) { 00095 return false; 00096 } 00097 00098 writer.set_opcode(FO_mesh_primitive); 00099 Datagram &datagram = writer.update_datagram(); 00100 00101 datagram.add_be_int16(_primitive_type); 00102 00103 // Determine the optimum index width, based on the largest vertex 00104 // index. 00105 int max_index = 0; 00106 Vertices::const_iterator vi; 00107 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { 00108 max_index = max(max_index, (*vi)); 00109 } 00110 00111 int vertex_width; 00112 if (max_index < 0x100) { 00113 vertex_width = 1; 00114 } else if (max_index < 0x10000) { 00115 vertex_width = 2; 00116 } else { 00117 vertex_width = 4; 00118 } 00119 00120 datagram.add_be_int16(vertex_width); 00121 datagram.add_be_int32(_vertices.size()); 00122 00123 if (vertex_width == 1) { 00124 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { 00125 datagram.add_uint8(*vi); 00126 } 00127 00128 } else if (vertex_width == 2) { 00129 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { 00130 datagram.add_be_uint16(*vi); 00131 } 00132 00133 } else { 00134 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { 00135 datagram.add_be_int32(*vi); 00136 } 00137 } 00138 00139 return true; 00140 }