-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbook.odin
70 lines (49 loc) · 1.67 KB
/
book.odin
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
#+private file
package example
import glm "core:math/linalg/glsl"
import gl "../wasm/webgl"
import "../obj"
@private
State_Book :: struct {
using locations: Input_Locations_Boxes,
vao: VAO,
vertices: Vertices,
rotation: mat4,
}
@private
setup_book :: proc(s: ^State_Book, program: gl.Program) {
gl.Enable(gl.CULL_FACE) // don't draw back faces
gl.Enable(gl.DEPTH_TEST) // draw only closest faces
obj_data := obj.parse_file(#load("./public/book.obj", string), context.temp_allocator)
s.vao = gl.CreateVertexArray()
o := &obj_data.objects[0]
s.vertices = convert_obj_vertices(o.vertices[:])
extent_min, extent_max := get_extents(obj_data.positions[:])
correct_extents(s.vertices.position[:len(s.vertices)], extent_min, extent_max, -140, 140)
gl.BindVertexArray(s.vao)
input_locations_boxes(&s.locations, program)
attribute(s.a_position, gl.CreateBuffer(), s.vertices.position[:len(s.vertices)])
attribute(s.a_color, gl.CreateBuffer(), s.vertices.color[:len(s.vertices)])
/* Init rotation */
s.rotation = 1
}
@private
frame_book :: proc(s: ^State_Book, delta: f32) {
gl.Viewport(0, 0, canvas_res.x, canvas_res.y)
gl.ClearColor(0, 0, 0, 0)
// Clear the canvas AND the depth buffer.
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
rotation := -0.01 * delta * mouse_rel.yx
s.rotation = mat4_rotate_x(rotation.x) * mat4_rotate_y(rotation.y) * s.rotation
mat: mat4 = 1
mat *= glm.mat4PerspectiveInfinite(
fovy = radians(80),
aspect = aspect_ratio,
near = 1,
)
mat *= mat4_translate({0, 0, -900 + scale * 720})
mat *= s.rotation
gl.BindVertexArray(s.vao)
uniform(s.u_matrix, mat)
gl.DrawArrays(gl.TRIANGLES, 0, len(s.vertices))
}