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