Debian Codegraph @

RecastDemo/Source/MeshLoaderObj.cpp

raw symbols SCIP
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 // 1. The origin of this software must not be misrepresented; you must not
11 // claim that you wrote the original software. If you use this software
12 // in a product, an acknowledgment in the product documentation would be
13 // appreciated but is not required.
14 // 2. Altered source versions must be plainly marked as such, and must not be
15 // misrepresented as being the original software.
16 // 3. This notice may not be removed or altered from any source distribution.
17 //
18
19 #include "MeshLoaderObj.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <cstring>
23 #include <math.h>
24
25 rcMeshLoaderObj::rcMeshLoaderObj() :
26 m_scale(1.0f),
27 m_verts(0),
28 m_tris(0),
29 m_normals(0),
30 m_vertCount(0),
31 m_triCount(0)
32 {
33 }
34
35 rcMeshLoaderObj::~rcMeshLoaderObj()
36 {
37 delete [] m_verts;
38 delete [] m_normals;
39 delete [] m_tris;
40 }
41
42 void rcMeshLoaderObj::addVertex(float x, float y, float z, int& cap)
43 {
44 if (m_vertCount+1 > cap)
45 {
46 cap = !cap ? 8 : cap*2;
47 float* nv = new float[cap*3];
48 if (m_vertCount)
49 memcpy(nv, m_verts, m_vertCount*3*sizeof(float));
50 delete [] m_verts;
51 m_verts = nv;
52 }
53 float* dst = &m_verts[m_vertCount*3];
54 *dst++ = x*m_scale;
55 *dst++ = y*m_scale;
56 *dst++ = z*m_scale;
57 m_vertCount++;
58 }
59
60 void rcMeshLoaderObj::addTriangle(int a, int b, int c, int& cap)
61 {
62 if (m_triCount+1 > cap)
63 {
64 cap = !cap ? 8 : cap*2;
65 int* nv = new int[cap*3];
66 if (m_triCount)
67 memcpy(nv, m_tris, m_triCount*3*sizeof(int));
68 delete [] m_tris;
69 m_tris = nv;
70 }
71 int* dst = &m_tris[m_triCount*3];
72 *dst++ = a;
73 *dst++ = b;
74 *dst++ = c;
75 m_triCount++;
76 }
77
78 static char* parseRow(char* buf, char* bufEnd, char* row, int len)
79 {
80 bool start = true;
81 bool done = false;
82 int n = 0;
83 while (!done && buf < bufEnd)
84 {
85 char c = *buf;
86 buf++;
87 // multirow
88 switch (c)
89 {
90 case '\\':
91 break;
92 case '\n':
93 if (start) break;
94 done = true;
95 break;
96 case '\r':
97 break;
98 case '\t':
99 case ' ':
100 if (start) break;
101 // else falls through
102 default:
103 start = false;
104 row[n++] = c;
105 if (n >= len-1)
106 done = true;
107 break;
108 }
109 }
110 row[n] = '\0';
111 return buf;
112 }
113
114 static int parseFace(char* row, int* data, int n, int vcnt)
115 {
116 int j = 0;
117 while (*row != '\0')
118 {
119 // Skip initial white space
120 while (*row != '\0' && (*row == ' ' || *row == '\t'))
121 row++;
122 char* s = row;
123 // Find vertex delimiter and terminated the string there for conversion.
124 while (*row != '\0' && *row != ' ' && *row != '\t')
125 {
126 if (*row == '/') *row = '\0';
127 row++;
128 }
129 if (*s == '\0')
130 continue;
131 int vi = atoi(s);
132 data[j++] = vi < 0 ? vi+vcnt : vi-1;
133 if (j >= n) return j;
134 }
135 return j;
136 }
137
138 bool rcMeshLoaderObj::load(const std::string& filename)
139 {
140 char* buf = 0;
141 FILE* fp = fopen(filename.c_str(), "rb");
142 if (!fp)
143 return false;
144 if (fseek(fp, 0, SEEK_END) != 0)
145 {
146 fclose(fp);
147 return false;
148 }
149 long bufSize = ftell(fp);
150 if (bufSize < 0)
151 {
152 fclose(fp);
153 return false;
154 }
155 if (fseek(fp, 0, SEEK_SET) != 0)
156 {
157 fclose(fp);
158 return false;
159 }
160 buf = new char[bufSize];
161 if (!buf)
162 {
163 fclose(fp);
164 return false;
165 }
166 size_t readLen = fread(buf, bufSize, 1, fp);
167 fclose(fp);
168
169 if (readLen != 1)
170 {
171 delete[] buf;
172 return false;
173 }
174
175 char* src = buf;
176 char* srcEnd = buf + bufSize;
177 char row[512];
178 int face[32];
179 float x,y,z;
180 int nv;
181 int vcap = 0;
182 int tcap = 0;
183
184 while (src < srcEnd)
185 {
186 // Parse one row
187 row[0] = '\0';
188 src = parseRow(src, srcEnd, row, sizeof(row)/sizeof(char));
189 // Skip comments
190 if (row[0] == '#') continue;
191 if (row[0] == 'v' && row[1] != 'n' && row[1] != 't')
192 {
193 // Vertex pos
194 sscanf(row+1, "%f %f %f", &x, &y, &z);
195 addVertex(x, y, z, vcap);
196 }
197 if (row[0] == 'f')
198 {
199 // Faces
200 nv = parseFace(row+1, face, 32, m_vertCount);
201 for (int i = 2; i < nv; ++i)
202 {
203 const int a = face[0];
204 const int b = face[i-1];
205 const int c = face[i];
206 if (a < 0 || a >= m_vertCount || b < 0 || b >= m_vertCount || c < 0 || c >= m_vertCount)
207 continue;
208 addTriangle(a, b, c, tcap);
209 }
210 }
211 }
212
213 delete [] buf;
214
215 // Calculate normals.
216 m_normals = new float[m_triCount*3];
217 for (int i = 0; i < m_triCount*3; i += 3)
218 {
219 const float* v0 = &m_verts[m_tris[i]*3];
220 const float* v1 = &m_verts[m_tris[i+1]*3];
221 const float* v2 = &m_verts[m_tris[i+2]*3];
222 float e0[3], e1[3];
223 for (int j = 0; j < 3; ++j)
224 {
225 e0[j] = v1[j] - v0[j];
226 e1[j] = v2[j] - v0[j];
227 }
228 float* n = &m_normals[i];
229 n[0] = e0[1]*e1[2] - e0[2]*e1[1];
230 n[1] = e0[2]*e1[0] - e0[0]*e1[2];
231 n[2] = e0[0]*e1[1] - e0[1]*e1[0];
232 float d = sqrtf(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
233 if (d > 0)
234 {
235 d = 1.0f/d;
236 n[0] *= d;
237 n[1] *= d;
238 n[2] *= d;
239 }
240 }
241
242 m_filename = filename;
243 return true;
244 }