Blender V2.61 - r43446
|
00001 00028 #include "BSP_PlyLoader.h" 00029 00030 #include "MT_Vector3.h" 00031 #include "ply.h" 00032 00033 struct LoadVertex { 00034 float x,y,z; /* the usual 3-space position of a vertex */ 00035 }; 00036 00037 struct LoadFace { 00038 unsigned char intensity; /* this user attaches intensity to faces */ 00039 unsigned char nverts; /* number of vertex indices in list */ 00040 int *verts; /* vertex index list */ 00041 }; 00042 00043 00044 MEM_SmartPtr<BSP_TMesh> 00045 BSP_PlyLoader:: 00046 NewMeshFromFile( 00047 char * file_name, 00048 MT_Vector3 &min, 00049 MT_Vector3 &max 00050 00051 ) { 00052 00053 min = MT_Vector3(MT_INFINITY,MT_INFINITY,MT_INFINITY); 00054 max = MT_Vector3(-MT_INFINITY,-MT_INFINITY,-MT_INFINITY); 00055 00056 PlyProperty vert_props[] = { /* list of property information for a vertex */ 00057 {"x", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,x), 0, 0, 0, 0}, 00058 {"y", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,y), 0, 0, 0, 0}, 00059 {"z", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,z), 0, 0, 0, 0}, 00060 }; 00061 00062 PlyProperty face_props[] = { /* list of property information for a vertex */ 00063 {"vertex_indices", PLY_INT, PLY_INT, offsetof(LoadFace,verts), 00064 1, PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,nverts)}, 00065 }; 00066 00067 MEM_SmartPtr<BSP_TMesh> mesh = new BSP_TMesh; 00068 00069 if (mesh == NULL) return NULL; 00070 00071 int i,j; 00072 PlyFile *ply; 00073 int nelems; 00074 char **elist; 00075 int file_type; 00076 float version; 00077 int nprops; 00078 int num_elems; 00079 PlyProperty **plist; 00080 00081 char *elem_name; 00082 00083 LoadVertex load_vertex; 00084 LoadFace load_face; 00085 00086 /* open a PLY file for reading */ 00087 ply = ply_open_for_reading( 00088 file_name, 00089 &nelems, 00090 &elist, 00091 &file_type, 00092 &version 00093 ); 00094 00095 if (ply == NULL) return NULL; 00096 00097 /* go through each kind of element that we learned is in the file */ 00098 /* and read them */ 00099 00100 for (i = 0; i < nelems; i++) { 00101 00102 /* get the description of the first element */ 00103 00104 elem_name = elist[i]; 00105 plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops); 00106 00107 /* print the name of the element, for debugging */ 00108 00109 /* if we're on vertex elements, read them in */ 00110 00111 if (equal_strings ("vertex", elem_name)) { 00112 00113 /* set up for getting vertex elements */ 00114 00115 ply_get_property (ply, elem_name, &vert_props[0]); 00116 ply_get_property (ply, elem_name, &vert_props[1]); 00117 ply_get_property (ply, elem_name, &vert_props[2]); 00118 00119 // make some memory for the vertices 00120 mesh->VertexSet().reserve(num_elems); 00121 00122 /* grab all the vertex elements */ 00123 for (j = 0; j < num_elems; j++) { 00124 00125 /* grab and element from the file */ 00126 ply_get_element (ply, (void *)&load_vertex); 00127 // pass the vertex into the mesh builder. 00128 00129 if (load_vertex.x < min.x()) { 00130 min.x() = load_vertex.x; 00131 } else 00132 if (load_vertex.x > max.x()) { 00133 max.x()= load_vertex.x; 00134 } 00135 00136 if (load_vertex.y < min.y()) { 00137 min.y() = load_vertex.y; 00138 } else 00139 if (load_vertex.y > max.y()) { 00140 max.y()= load_vertex.y; 00141 } 00142 00143 if (load_vertex.z < min.z()) { 00144 min.z() = load_vertex.z; 00145 } else 00146 if (load_vertex.z > max.z()) { 00147 max.z()= load_vertex.z; 00148 } 00149 00150 BSP_TVertex my_vert; 00151 my_vert.m_pos = MT_Vector3(load_vertex.x,load_vertex.y,load_vertex.z); 00152 mesh->VertexSet().push_back(my_vert); 00153 } 00154 00155 00156 } 00157 00158 /* if we're on face elements, read them in */ 00159 if (equal_strings ("face", elem_name)) { 00160 00161 /* set up for getting face elements */ 00162 00163 ply_get_property (ply, elem_name, &face_props[0]); 00164 00165 /* grab all the face elements */ 00166 for (j = 0; j < num_elems; j++) { 00167 00168 ply_get_element (ply, (void *)&load_face); 00169 00170 int v; 00171 for (v = 2; v< load_face.nverts; v++) { 00172 00173 BSP_TFace f; 00174 00175 f.m_verts[0] = load_face.verts[0]; 00176 f.m_verts[1] = load_face.verts[v-1]; 00177 f.m_verts[2] = load_face.verts[v]; 00178 00179 mesh->BuildNormal(f); 00180 mesh->FaceSet().push_back(f); 00181 } 00182 // free up the memory this pile of shit used to allocate the polygon's vertices 00183 free (load_face.verts); 00184 } 00185 00186 } 00187 } 00188 /* close the PLY file */ 00189 ply_close (ply); 00190 00191 return mesh; 00192 }