-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobj.odin
More file actions
105 lines (83 loc) · 2.23 KB
/
obj.odin
File metadata and controls
105 lines (83 loc) · 2.23 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
package main
import "core:log"
import "core:os/os2"
import "core:strconv"
import "core:strings"
load_obj :: proc(path: string) -> ([dynamic]Vertex, [dynamic]int) {
data, err := os2.read_entire_file_from_path(path, context.allocator)
defer delete(data)
if err != nil {panic("err")}
vert_norm := make([dynamic][3]f32)
vert_pos := make([dynamic][3]f32)
vert_tex := make([dynamic][2]f32)
defer delete(vert_norm)
defer delete(vert_pos)
defer delete(vert_tex)
v_map := make(map[string]int)
defer delete(v_map)
f_indx := make([dynamic]int)
defer delete(f_indx)
verts := make([dynamic]Vertex)
indicies := make([dynamic]int)
data_str := string(data)
for line in strings.split_lines_iterator(&data_str) {
trimmed := strings.trim_space(line)
if len(trimmed) == 0 || trimmed[0] == '#' {
continue
}
parts, _ := strings.split(trimmed, " ", context.allocator)
switch parts[0] {
case "v":
v1, _ := strconv.parse_f32(parts[1])
v2, _ := strconv.parse_f32(parts[2])
v2 = 1 - v2
v3, _ := strconv.parse_f32(parts[3])
append(&vert_pos, [3]f32{v1, v2, v3})
case "vt":
t1, _ := strconv.parse_f32(parts[1])
t2, _ := strconv.parse_f32(parts[2])
append(&vert_tex, [2]f32{t1, t2})
case "vn":
n1, _ := strconv.parse_f32(parts[1])
n2, _ := strconv.parse_f32(parts[2])
n3, _ := strconv.parse_f32(parts[3])
append(&vert_norm, [3]f32{n1, n2, n3})
case "f":
defer clear(&f_indx)
for p in parts[1:] {
if index, ok := v_map[p]; ok {
append(&f_indx, index)
continue
}
f_parts := strings.split(p, "/")
v_i, _ := strconv.parse_int(f_parts[0])
v_t: int = -1
v_n: int = -1
if len(f_parts) > 1 {
v_t, _ = strconv.parse_int(f_parts[1])
}
if len(f_parts) > 2 {
v_n, _ = strconv.parse_int(f_parts[2])
}
v_i -= 1
v_t -= 1
v_n -= 1
vert := Vertex {
pos = vert_pos[v_i],
uv = v_t > 0 ? vert_tex[v_t] : 0,
col = v_n > 0 ? vert_norm[v_n] : 0,
}
index := len(verts)
v_map[p] = index
append(&verts, vert)
append(&f_indx, index)
}
for i := 1; i < len(f_indx) - 1; i += 1 {
append(&indicies, f_indx[0])
append(&indicies, f_indx[i])
append(&indicies, f_indx[i + 1])
}
}
}
return verts, indicies
}