1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#include "mesh.h"
#define GENERATE_TANGNET
//#define GENERATE_NORMAL
//#define OPTIMIZE_MESH
#define LINE_SIZE 1024
#define darray_push(darray, value) \
do { \
(darray) = darray_hold((darray), 1, sizeof(*(darray))); \
(darray)[darray_size(darray) - 1] = (value); \
} while (0)
#define DARRAY_RAW_DATA(darray) ((int*)(darray) - 2)
#define DARRAY_CAPACITY(darray) (DARRAY_RAW_DATA(darray)[0])
#define DARRAY_OCCUPIED(darray) (DARRAY_RAW_DATA(darray)[1])
int darray_size(void *darray) {
return darray != NULL ? DARRAY_OCCUPIED(darray) : 0;
}
void darray_free(void *darray) {
if (darray != NULL) {
free(DARRAY_RAW_DATA(darray));
}
}
#define UNUSED_VAR(x) ((void)(x))
void *darray_hold(void *darray, int count, int itemsize) {
assert(count > 0 && itemsize > 0);
if (darray == NULL) {
int raw_size = sizeof(int) * 2 + itemsize * count;
int *base = (int*)malloc(raw_size);
base[0] = count; /* capacity */
base[1] = count; /* occupied */
return base + 2;
}
else if (DARRAY_OCCUPIED(darray) + count <= DARRAY_CAPACITY(darray)) {
DARRAY_OCCUPIED(darray) += count;
return darray;
}
else {
int needed_size = DARRAY_OCCUPIED(darray) + count;
int double_curr = DARRAY_CAPACITY(darray) * 2;
int capacity = needed_size > double_curr ? needed_size : double_curr;
int occupied = needed_size;
int raw_size = sizeof(int) * 2 + itemsize * capacity;
int *base = (int*)realloc(DARRAY_RAW_DATA(darray), raw_size);
base[0] = capacity;
base[1] = occupied;
return base + 2;
}
}
Vec3 internal_vec3_min(Vec3 a, Vec3 b) {
float x = min(a.x, b.x);
float y = min(a.y, b.y);
float z = min(a.z, b.z);
Vec3 r = { x, y, z };
return r;
}
Vec3 internal_vec3_max(Vec3 a, Vec3 b) {
float x = max(a.x, b.x);
float y = max(a.y, b.y);
float z = max(a.z, b.z);
Vec3 r = { x, y, z };
return r;
}
static Mesh *build_mesh(
Vec3 *positions, Vec2 *texcoords, Vec3 *normals,
Vec4 *tangents, Vec4 *joints, Vec4 *weights,
int *position_indices, int *texcoord_indices, int *normal_indices) {
Vec3 bbox_min = { +1e6, +1e6, +1e6 };
Vec3 bbox_max = { -1e6, -1e6, -1e6 };
int num_indices = darray_size(position_indices);
int num_faces = num_indices / 3;
Mesh *mesh;
int i;
assert(num_faces > 0 && num_faces * 3 == num_indices);
assert(darray_size(position_indices) == num_indices);
assert(darray_size(texcoord_indices) == num_indices);
assert(darray_size(normal_indices) == num_indices);
mesh = (Mesh*)malloc(sizeof(Mesh));
mesh->triangles = ssrM_newvector(int, 3 * num_faces);
int nvert = DARRAY_OCCUPIED(positions);
Vert* vertices = ssrM_newvector(Vert, nvert);
for (i = 0; i < num_indices; i++) {
int position_index = position_indices[i];
int texcoord_index = texcoord_indices[i];
int normal_index = normal_indices[i];
assert(position_index >= 0 && position_index < darray_size(positions));
assert(texcoord_index >= 0 && texcoord_index < darray_size(texcoords));
assert(normal_index >= 0 && normal_index < darray_size(normals));
vertices[position_index].index = position_index;
vertices[position_index].position = positions[position_index];
vertices[position_index].texcoord = texcoords[texcoord_index];
vertices[position_index].normal = normals[normal_index];
if (tangents) {
int tangent_index = position_index;
ssr_assert(tangent_index >= 0 && tangent_index < darray_size(tangents));
vertices[position_index].tangent = tangents[tangent_index];
}
mesh->triangles[i] = position_index;
//bbox_min = internal_vec3_min(bbox_min, vertices[i].position);
//bbox_max = internal_vec3_max(bbox_max, vertices[i].position);
}
mesh->tris_count = num_faces;
mesh->vert_count = nvert;
mesh->vertices = vertices;
//mesh->center = internal_vec3_div(internal_vec3_add(bbox_min, bbox_max), 2);
return mesh;
}
/************************************************************************/
/* obj loader */
/************************************************************************/
#define SIMPLE_OBJ_LOADER 0
#if SIMPLE_OBJ_LOADER
Mesh* mesh_loadfromobj(const char *filename) {
Vec3 *positions = NULL;
Vec2 *texcoords = NULL;
Vec3 *normals = NULL;
Vec4 *tangents = NULL;
Vec4 *joints = NULL;
Vec4 *weights = NULL;
int *position_indices = NULL;
int *texcoord_indices = NULL;
int *normal_indices = NULL;
char line[LINE_SIZE];
Mesh *mesh;
FILE *file;
file = fopen(filename, "rb");
assert(file != NULL);
while (1) {
int items;
if (fgets(line, LINE_SIZE, file) == NULL) {
break;
}
else if (strncmp(line, "v ", 2) == 0) { /* position */
Vec3 position;
items = sscanf(line, "v %f %f %f",
&position.x, &position.y, &position.z);
assert(items == 3);
darray_push(positions, position);
}
else if (strncmp(line, "vt ", 3) == 0) { /* texcoord */
Vec2 texcoord;
items = sscanf(line, "vt %f %f",
&texcoord.x, &texcoord.y);
assert(items == 2);
darray_push(texcoords, texcoord);
}
else if (strncmp(line, "vn ", 3) == 0) { /* normal */
Vec3 normal;
items = sscanf(line, "vn %f %f %f",
&normal.x, &normal.y, &normal.z);
assert(items == 3);
darray_push(normals, normal);
}
else if (strncmp(line, "f ", 2) == 0) { /* face */
int i;
int pos_indices[3], uv_indices[3], n_indices[3];
items = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d",
&pos_indices[0], &uv_indices[0], &n_indices[0],
&pos_indices[1], &uv_indices[1], &n_indices[1],
&pos_indices[2], &uv_indices[2], &n_indices[2]);
assert(items == 9);
for (i = 0; i < 3; i++) {
darray_push(position_indices, pos_indices[i] - 1);
darray_push(texcoord_indices, uv_indices[i] - 1);
darray_push(normal_indices, n_indices[i] - 1);
}
} else if (strncmp(line, "# ext.tangent ", 14) == 0) { /* tangent */
Vec4 tangent;
items = sscanf(line, "# ext.tangent %f %f %f %f",
&tangent.x, &tangent.y, &tangent.z, &tangent.w);
assert(items == 4);
darray_push(tangents, tangent);
}
}
fclose(file);
mesh = build_mesh(positions, texcoords, normals, tangents, joints, weights,
position_indices, texcoord_indices, normal_indices);
darray_free(positions);
darray_free(texcoords);
darray_free(normals);
darray_free(tangents);
darray_free(joints);
darray_free(weights);
darray_free(position_indices);
darray_free(texcoord_indices);
darray_free(normal_indices);
printf("%s: %d verts, %d tris\n", filename, mesh->vert_count, mesh->tris_count);
return mesh;
}
#else
typedef struct {
int normal_index;
int uv_index;
int index;
} VertexInfo;
typedef struct {
VertexInfo* vertices; // vertices list
} VertexListElement;
typedef struct {
int posIndex, normalIndex, uvIndex;
} FaceVertex;
typedef struct {
FaceVertex vertices[3];
} FaceInfo;
Mesh* mesh_loadfromobj(const char* path) {
Vec3* position_list = NULL;
Vec3* normal_list = NULL;
Vec2* uv_list = NULL;
VertexListElement* vertex_list = NULL;
FaceInfo* face_list = NULL;
FILE *file;
file = fopen(path, "rb");
assert(file != NULL);
char line[LINE_SIZE];
while (1) {
int items;
if (fgets(line, LINE_SIZE, file) == NULL) {
break;
}
else if (strncmp(line, "v ", 2) == 0) { /* position */
Vec3 position;
items = sscanf(line, "v %f %f %f",
&position.x, &position.y, &position.z);
assert(items == 3);
darray_push(position_list, position);
// init vertex_list
VertexListElement element = { 0 };
darray_push(vertex_list, element);
}
else if (strncmp(line, "vt ", 3) == 0) { /* texcoord */
Vec2 texcoord;
items = sscanf(line, "vt %f %f",
&texcoord.x, &texcoord.y);
assert(items == 2);
darray_push(uv_list, texcoord);
}
else if (strncmp(line, "vn ", 3) == 0) { /* normal */
Vec3 normal;
items = sscanf(line, "vn %f %f %f",
&normal.x, &normal.y, &normal.z);
assert(items == 3);
darray_push(normal_list, normal);
}
else if (strncmp(line, "f ", 2) == 0) { /* face */
FaceInfo face;
items = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d", // only support triangle
&face.vertices[0].posIndex, &face.vertices[0].uvIndex, &face.vertices[0].normalIndex,
&face.vertices[1].posIndex, &face.vertices[1].uvIndex, &face.vertices[1].normalIndex,
&face.vertices[2].posIndex, &face.vertices[2].uvIndex, &face.vertices[2].normalIndex);
assert(items == 9);
darray_push(face_list, face);
}
else
{
// skip
}
}
int vert_counts = darray_size(vertex_list);
int faceCount = darray_size(face_list);
int normalCount = darray_size(normal_list);
int uvCount = darray_size(uv_list);
int vert_count = 0;
Mesh temp_mesh = {0};
for (int i = 0; i < darray_size(face_list); ++i)
{
FaceInfo* face = &face_list[i];
for (int j = 0; j < 3; ++j)
{
FaceVertex face_vert = face->vertices[j];
int pos_index = face_vert.posIndex - 1;
int normal_index = face_vert.normalIndex - 1;
int uv_index = face_vert.uvIndex - 1;
VertexListElement* vertex = &vertex_list[pos_index];
if (vertex->vertices)
{
for (int j = 0; j < darray_size(vertex->vertices); ++j)
{
VertexInfo* vert = &vertex->vertices[j];
if (vert->normal_index == normal_index && vert->uv_index == uv_index)
{
darray_push(temp_mesh.triangles, vert->index);
goto next;
}
}
}
// create seam
VertexInfo new_vert;
new_vert.normal_index = normal_index;
new_vert.uv_index = uv_index;
new_vert.index = vert_count;
darray_push(vertex->vertices, new_vert);
Vert vert;
vert.position = position_list[pos_index];
vert.normal = normal_list[normal_index];
vert.texcoord = uv_list[uv_index];
vert.index = vert_count;
darray_push(temp_mesh.vertices, vert);
darray_push(temp_mesh.triangles, vert_count);
++temp_mesh.vert_count;
++vert_count;
next: continue;
}
++temp_mesh.tris_count;
}
Mesh* mesh = malloc(sizeof(Mesh));
mesh->triangles = temp_mesh.triangles;
mesh->vertices = temp_mesh.vertices;
mesh->tris_count = temp_mesh.tris_count;
mesh->vert_count = temp_mesh.vert_count;
return mesh;
}
#endif
|