-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobj.c
More file actions
60 lines (56 loc) · 1.86 KB
/
obj.c
File metadata and controls
60 lines (56 loc) · 1.86 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
#include "obj.h"
#include "data/format/scanner/scanner.h"
#include "files/helpers.h"
void push_idx(mesh *m, int trig){
chunk_array_push(m->segments, &trig);
}
int handle_trig(mesh *mesh, string_slice sl){
char *s1 = (char*)seek_to(sl.data, '/');
int segment = (parse_int64(sl.data,s1-sl.data-1) & 0xFFFFFFFF) - 1;
push_idx(mesh, segment);
//TODO: extra trig data
return segment;
}
void handle_obj_line(void *ctx, string_slice line){
mesh *m = (mesh *)ctx;
Scanner s = scanner_make(line.data, line.length);
char first = scan_next(&s);
if (first == 'v'){
if (scan_next(&s) != ' ') return;//TODO: other options
vector3 vector = {};
string_slice v1 = scan_to(&s, ' ');
if (!v1.length) return;
vector.x = parse_float(v1.data, v1.length-1);
string_slice v2 = scan_to(&s, ' ');
if (!v2.length) return;
vector.y = parse_float(v2.data, v2.length-1);
string_slice v3 = scan_to(&s, ' ');
if (!v3.length) return;
vector.z = parse_float(v3.data, v3.length);
chunk_array_push(m->vertices, &vector);
}
if (first == 'f'){
if (scan_next(&s) != ' ') return;
int first_s = -1;
int last_s = -1;
for (int i = 0; !scan_eof(&s); i++){
string_slice sl = scan_to(&s, ' ');
if (sl.length == 0) break;
if (i > 2){
push_idx(m, first_s);
push_idx(m, last_s);
}
int seg = handle_trig(m, sl);
if (i == 0) first_s = seg;
last_s = seg;
};
}
}
mesh parse_obj(void* obj, size_t size, primitives prim_type){
mesh m = {};
m.vertices = chunk_array_create(sizeof(vector3), 1000);
m.segments = chunk_array_create(sizeof(int), 1000);
m.primitive_type = prim_type;
read_lines(obj, &m, handle_obj_line);
return m;
}