2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 #include "BSP_PlyLoader.h"
30 #include "MT_Vector3.h"
34 float x,y,z; /* the usual 3-space position of a vertex */
38 unsigned char intensity; /* this user attaches intensity to faces */
39 unsigned char nverts; /* number of vertex indices in list */
40 int *verts; /* vertex index list */
44 MEM_SmartPtr<BSP_TMesh>
53 min = MT_Vector3(MT_INFINITY,MT_INFINITY,MT_INFINITY);
54 max = MT_Vector3(-MT_INFINITY,-MT_INFINITY,-MT_INFINITY);
56 PlyProperty vert_props[] = { /* list of property information for a vertex */
57 {"x", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,x), 0, 0, 0, 0},
58 {"y", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,y), 0, 0, 0, 0},
59 {"z", PLY_FLOAT, PLY_FLOAT, offsetof(LoadVertex,z), 0, 0, 0, 0},
62 PlyProperty face_props[] = { /* list of property information for a vertex */
63 {"vertex_indices", PLY_INT, PLY_INT, offsetof(LoadFace,verts),
64 1, PLY_UCHAR, PLY_UCHAR, offsetof(LoadFace,nverts)},
67 MEM_SmartPtr<BSP_TMesh> mesh = new BSP_TMesh;
69 if (mesh == NULL) return NULL;
83 LoadVertex load_vertex;
86 /* open a PLY file for reading */
87 ply = ply_open_for_reading(
95 if (ply == NULL) return NULL;
97 /* go through each kind of element that we learned is in the file */
100 for (i = 0; i < nelems; i++) {
102 /* get the description of the first element */
104 elem_name = elist[i];
105 plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
107 /* print the name of the element, for debugging */
109 /* if we're on vertex elements, read them in */
111 if (equal_strings ("vertex", elem_name)) {
113 /* set up for getting vertex elements */
115 ply_get_property (ply, elem_name, &vert_props[0]);
116 ply_get_property (ply, elem_name, &vert_props[1]);
117 ply_get_property (ply, elem_name, &vert_props[2]);
119 // make some memory for the vertices
120 mesh->VertexSet().reserve(num_elems);
122 /* grab all the vertex elements */
123 for (j = 0; j < num_elems; j++) {
125 /* grab and element from the file */
126 ply_get_element (ply, (void *)&load_vertex);
127 // pass the vertex into the mesh builder.
129 if (load_vertex.x < min.x()) {
130 min.x() = load_vertex.x;
132 if (load_vertex.x > max.x()) {
133 max.x()= load_vertex.x;
136 if (load_vertex.y < min.y()) {
137 min.y() = load_vertex.y;
139 if (load_vertex.y > max.y()) {
140 max.y()= load_vertex.y;
143 if (load_vertex.z < min.z()) {
144 min.z() = load_vertex.z;
146 if (load_vertex.z > max.z()) {
147 max.z()= load_vertex.z;
151 my_vert.m_pos = MT_Vector3(load_vertex.x,load_vertex.y,load_vertex.z);
152 mesh->VertexSet().push_back(my_vert);
158 /* if we're on face elements, read them in */
159 if (equal_strings ("face", elem_name)) {
161 /* set up for getting face elements */
163 ply_get_property (ply, elem_name, &face_props[0]);
165 /* grab all the face elements */
166 for (j = 0; j < num_elems; j++) {
168 ply_get_element (ply, (void *)&load_face);
171 for (v = 2; v< load_face.nverts; v++) {
175 f.m_verts[0] = load_face.verts[0];
176 f.m_verts[1] = load_face.verts[v-1];
177 f.m_verts[2] = load_face.verts[v];
179 mesh->BuildNormal(f);
180 mesh->FaceSet().push_back(f);
182 // free up the memory this pile of shit used to allocate the polygon's vertices
183 free (load_face.verts);
188 /* close the PLY file */