Revert part of r49504 by Genscher:
[blender.git] / intern / bsp / test / BSP_GhostTest / BSP_PlyLoader.cpp
1 /**
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "BSP_PlyLoader.h"
29
30 #include "MT_Vector3.h"
31 #include "ply.h"
32
33 struct LoadVertex {
34   float x,y,z;             /* the usual 3-space position of a vertex */
35 };
36
37 struct LoadFace {
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 */
41 };
42
43
44         MEM_SmartPtr<BSP_TMesh>
45 BSP_PlyLoader::
46 NewMeshFromFile(
47         char * file_name,
48         MT_Vector3 &min,
49         MT_Vector3 &max
50
51 ) {
52
53         min = MT_Vector3(MT_INFINITY,MT_INFINITY,MT_INFINITY);
54         max = MT_Vector3(-MT_INFINITY,-MT_INFINITY,-MT_INFINITY);
55         
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},
60         };
61
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)},
65         };
66
67         MEM_SmartPtr<BSP_TMesh> mesh = new BSP_TMesh;
68
69         if (mesh == NULL) return NULL;
70
71         int i,j;
72         PlyFile *ply;
73         int nelems;
74         char **elist;
75         int file_type;
76         float version;
77         int nprops;
78         int num_elems;
79         PlyProperty **plist;
80
81         char *elem_name;
82
83         LoadVertex load_vertex;
84         LoadFace load_face;
85
86         /* open a PLY file for reading */
87         ply = ply_open_for_reading(
88                 file_name,
89                 &nelems,
90                 &elist, 
91                 &file_type, 
92                 &version
93         );
94
95         if (ply == NULL) return NULL;
96
97         /* go through each kind of element that we learned is in the file */
98         /* and read them */
99
100         for (i = 0; i < nelems; i++) {
101
102                 /* get the description of the first element */
103
104                 elem_name = elist[i];
105                 plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
106
107                 /* print the name of the element, for debugging */
108
109                 /* if we're on vertex elements, read them in */
110
111                 if (equal_strings ("vertex", elem_name)) {
112
113                         /* set up for getting vertex elements */
114
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]);
118
119                         // make some memory for the vertices            
120                         mesh->VertexSet().reserve(num_elems);
121
122                         /* grab all the vertex elements */
123                         for (j = 0; j < num_elems; j++) {
124
125                                 /* grab and element from the file */
126                                 ply_get_element (ply, (void *)&load_vertex);
127                                 // pass the vertex into the mesh builder.
128                                         
129                                 if (load_vertex.x < min.x()) {
130                                         min.x() = load_vertex.x;
131                                 } else
132                                 if (load_vertex.x > max.x()) {
133                                         max.x()= load_vertex.x;
134                                 }
135
136                                 if (load_vertex.y < min.y()) {
137                                         min.y() = load_vertex.y;
138                                 } else
139                                 if (load_vertex.y > max.y()) {
140                                         max.y()= load_vertex.y;
141                                 }
142
143                                 if (load_vertex.z < min.z()) {
144                                         min.z() = load_vertex.z;
145                                 } else
146                                 if (load_vertex.z > max.z()) {
147                                         max.z()= load_vertex.z;
148                                 }
149
150                                 BSP_TVertex my_vert;
151                                 my_vert.m_pos = MT_Vector3(load_vertex.x,load_vertex.y,load_vertex.z);
152                                 mesh->VertexSet().push_back(my_vert);
153                         }
154                 
155
156                 }
157
158                 /* if we're on face elements, read them in */
159                 if (equal_strings ("face", elem_name)) {
160
161                         /* set up for getting face elements */
162
163                         ply_get_property (ply, elem_name, &face_props[0]);
164
165                         /* grab all the face elements */
166                         for (j = 0; j < num_elems; j++) {
167
168                                 ply_get_element (ply, (void *)&load_face);
169
170                                 int v;
171                                 for (v = 2; v< load_face.nverts; v++) {
172
173                                         BSP_TFace f;
174
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];
178
179                                         mesh->BuildNormal(f);   
180                                         mesh->FaceSet().push_back(f);
181                                 }
182                                 // free up the memory this pile of shit used to allocate the polygon's vertices
183                                 free (load_face.verts);
184                         }
185
186                 }
187         }
188   /* close the PLY file */
189   ply_close (ply);
190
191  return mesh;
192 }