-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvertex.c
More file actions
324 lines (268 loc) · 8.71 KB
/
vertex.c
File metadata and controls
324 lines (268 loc) · 8.71 KB
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
#include <assert.h>
#include <stdbool.h>
#include <vulkan/vulkan.h>
#include <talloc.h>
#include "trtl_loader.h"
#include "turtle.h"
#include "../../tinyobjloader-c/tinyobj_loader_c.h"
#include "blobby.h"
#include "helpers.h"
#include "vertex.h"
struct vhash;
static int32_t vhash_find(struct vhash *vhash, uint32_t vertex_index, uint32_t texture_index,
int32_t material_idx, bool *new);
static struct vhash *vhash_init(int32_t size);
static int vhash_netries(struct vhash *vhash, int *lookups);
#define DEBUGTHIS 1
// FIXME: This doesn't need to a function
trtl_pure VkVertexInputBindingDescription
vertex_binding_description_get(void)
{
VkVertexInputBindingDescription bindingDescription = {};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(struct vertex);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return bindingDescription;
}
VkVertexInputAttributeDescription *
get_attribute_description_pair(uint32_t *nentries)
{
VkVertexInputAttributeDescription *descriptions;
descriptions = talloc_zero_array(NULL, VkVertexInputAttributeDescription, 3);
descriptions[0].binding = 0;
descriptions[0].location = 0;
descriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
descriptions[0].offset = offsetof(struct vertex, pos);
descriptions[1].binding = 0;
descriptions[1].location = 1;
descriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
descriptions[1].offset = offsetof(struct vertex, color);
descriptions[2].binding = 0;
descriptions[2].location = 2;
descriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
descriptions[2].offset = offsetof(struct vertex, tex_coord);
if (nentries) {
*nentries = 3;
}
return descriptions;
}
struct file_reader_context {
struct turtle *turtle;
struct blobby *root;
char *basepath;
};
static void
tinyobj_file_reader(void *contextv, const char *filename, int is_mtl, const char *obj_filename,
char **buf, size_t *len)
{
struct blobby *blobby;
char *extra_path = NULL;
struct file_reader_context *context = contextv;
printf("**** File Reader: %s %d %s\n", filename, is_mtl, obj_filename);
if (is_mtl) {
// Need to build the path:
char *lastsep = rindex(context->root->source, '/');
printf("loast sep from %s is %s\n", context->root->source, lastsep);
if (!lastsep) {
// Not relative - just use filename.
} else {
// clean up when the blobby goes.
filename = talloc_asprintf(context, "%.*s/%s",
(int)(lastsep - context->root->source),
context->root->source, filename);
}
printf("Is MTL: %s\n", extra_path);
blobby = blobby_from_file_ctx(context, filename);
} else {
blobby = context->turtle->loader->load(context->turtle->loader, obj_filename);
context->root = talloc_steal(context, blobby);
}
if (!blobby) {
*len = 0;
*buf = 0;
return;
}
// FIXME: tinyobj file reader should take const char
*buf = (char *)(uintptr_t)blobby->data;
*len = blobby->len;
}
// extern int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
// size_t *num_shapes, tinyobj_material_t **materials,
// size_t *num_materials, const char *file_name, file_reader_callback
// file_reader,
// unsigned int flags);
/*
* Loads the given model from the given path.
*
* Returns a model structure, that should be freed with talloc_free.
*/
struct trtl_model *
load_model(struct turtle *turtle, const char *basename)
{
struct trtl_model *model;
tinyobj_attrib_t attrib;
tinyobj_shape_t *shapes;
tinyobj_material_t *materials;
struct pos3d *vertices;
struct pos2d *texcoords;
size_t num_shapes;
size_t num_materials;
{
struct file_reader_context *context;
context = talloc_zero(NULL, struct file_reader_context);
context->turtle = turtle;
int ret = tinyobj_parse_obj(&attrib, &shapes, &num_shapes, &materials,
&num_materials, basename, tinyobj_file_reader, context,
TINYOBJ_FLAG_TRIANGULATE);
talloc_free(context);
if (ret != 0) {
printf("parse object failed\n");
exit(1);
}
}
if (DEBUGTHIS) {
printf("We have %d Vertices\n", attrib.num_vertices);
printf("Model Details:\n Vertices: %d\n Faces: %d\n Face N Verts: %d\n",
attrib.num_vertices, attrib.num_faces, attrib.num_face_num_verts);
}
if (DEBUGTHIS > 1) {
for (uint32_t i = 0; i < num_shapes; i++) {
printf("Shape %d %d %d %s\n", i, shapes[i].face_offset, shapes[i].length,
shapes[i].name);
}
}
// Some typed helpers
vertices = (struct pos3d *)attrib.vertices;
texcoords = (struct pos2d *)attrib.texcoords;
// First; work out our scale factor. Run through the array, generate the bounding,
// and work the scale factor
struct boundingbox3d bbox = BOUNDINGBOX_INIT;
for (size_t i = 0; i < attrib.num_vertices; i++) {
bbox.min.x = MIN(bbox.min.x, vertices[i].x);
bbox.min.y = MIN(bbox.min.y, vertices[i].y);
bbox.min.z = MIN(bbox.min.z, vertices[i].z);
bbox.max.x = MAX(bbox.max.x, vertices[i].x);
bbox.max.y = MAX(bbox.max.y, vertices[i].y);
bbox.max.z = MAX(bbox.max.z, vertices[i].z);
}
// Now work out scale factor x/y
printf("Min: %f %f %f\n", bbox.min.x, bbox.min.y, bbox.min.z);
printf("Max: %f %f %f\n", bbox.max.x, bbox.max.y, bbox.max.z);
float scale = bbox.max.x - bbox.min.x;
scale = MAX(scale, bbox.max.y - bbox.min.y);
scale = MAX(scale, bbox.max.z - bbox.min.z);
scale = 1 / scale;
printf("Scaling by %f\n", scale);
model = talloc_zero(NULL, struct trtl_model);
model->vertices = talloc_array(model, struct vertex, attrib.num_faces);
model->nvertices = attrib.num_faces;
// Load our indices
model->indices = talloc_array(model, uint32_t, attrib.num_faces);
model->nindices = attrib.num_faces;
struct vhash *vhash = vhash_init(attrib.num_faces);
int maxn = -1;
for (uint32_t i = 0; i < attrib.num_faces; i++) {
bool new = true;
tinyobj_vertex_index_t idx = attrib.faces[i];
int n = vhash_find(vhash, idx.v_idx, idx.vt_idx, attrib.material_ids[i / 3], &new);
if (n > maxn) maxn = n;
// Already seen it
model->indices[i] = n;
if (!new) {
continue;
}
struct vertex *v = model->vertices + n;
v->pos.x = vertices[idx.v_idx].x * scale;
v->pos.y = vertices[idx.v_idx].y * scale;
v->pos.z = vertices[idx.v_idx].z * scale;
// FIXME: Number of text coords shold be range checked.
if (attrib.num_texcoords) {
v->tex_coord.x = texcoords[idx.vt_idx].x;
v->tex_coord.y = 1.0f - texcoords[idx.vt_idx].y;
}
// Note the '3' is assuming all triangles here
int material_idx = attrib.material_ids[i / 3];
assert(material_idx < (int)num_materials);
if (material_idx >= 0) {
v->color.r = materials[material_idx].diffuse[0];
v->color.g = materials[material_idx].diffuse[1];
v->color.b = materials[material_idx].diffuse[2];
}
}
int lookups;
vhash_netries(vhash, &lookups);
talloc_free(vhash);
printf("Max N is %d (of %d)\n", maxn, model->nvertices);
if (DEBUGTHIS > 1) {
for (uint32_t j = 0; j < model->nindices; j++) {
struct vertex *v = model->vertices + model->indices[j];
printf("Vertex %4d: %lf %lf %lf / %lf %lf\n", j, v->pos.x, v->pos.y,
v->pos.z, v->tex_coord.x, v->tex_coord.y);
}
}
return model;
}
struct vhash {
int32_t size;
int32_t next;
struct vhash_node **nodes;
// stats:
int lookups;
};
struct vhash_node {
struct vhash_node *next;
uint32_t vertex_index;
uint32_t texture_index;
int32_t material_idx;
int32_t vindex;
};
static uint32_t
hash(uint32_t vertex_index, uint32_t texture_index, int32_t material_idx, uint32_t size)
{
return ((vertex_index + texture_index) ^ material_idx) % size;
}
// Returns the index to use for this vertex.
static int32_t
vhash_find(struct vhash *vhash, uint32_t vertex_index, uint32_t texture_index, int32_t material_idx,
bool *created)
{
struct vhash_node *node;
uint32_t key = hash(vertex_index, texture_index, material_idx, vhash->size);
vhash->lookups++;
node = vhash->nodes[key];
while (node) {
if (node->vertex_index == vertex_index && node->texture_index == texture_index &&
node->material_idx == material_idx) {
if (created) *created = false;
return node->vindex;
}
node = node->next;
}
// Didn't find it
node = talloc(vhash, struct vhash_node);
node->texture_index = texture_index;
node->vertex_index = vertex_index;
node->vindex = vhash->next++;
node->material_idx = material_idx;
node->next = vhash->nodes[key];
vhash->nodes[key] = node;
if (created) *created = true;
return node->vindex;
}
static struct vhash *
vhash_init(int32_t size)
{
struct vhash *vhash;
vhash = talloc(NULL, struct vhash);
vhash->size = size;
vhash->next = 0;
vhash->nodes = talloc_zero_array(vhash, struct vhash_node *, size);
vhash->lookups = 0;
return vhash;
}
static int
vhash_netries(struct vhash *vhash, int *lookups)
{
if (lookups) *lookups = vhash->lookups;
return vhash->next;
}