-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_geometry.ml
85 lines (77 loc) · 2.31 KB
/
gl_geometry.ml
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
open Bigarray
open Tgl4
open Gl_utils
type int_bigarray = (int, int8_unsigned_elt, c_layout) Array1.t
type float_bigarray = (float, float32_elt, c_layout) Array1.t
type raw =
{ vertices: float_bigarray
; frags: float_bigarray * [`Colored | `Textured]
; indices: int_bigarray
; mode: int
; texture: string option }
type t =
{ gid: int
; bids: int list
; length: int
; mode: int
; texture: Gl_texture.t option }
let create raw =
let gid = get_int (Gl.gen_vertex_arrays 1) in
let iid = create_buffer raw.indices in
let vid = create_buffer raw.vertices in
let cid = create_buffer (fst raw.frags) in
let mode = raw.mode in
let bind_attrib id loc dim typ =
Gl.bind_buffer Gl.array_buffer id ;
Gl.enable_vertex_attrib_array loc ;
Gl.vertex_attrib_pointer loc dim typ false 0 (`Offset 0)
in
Gl.bind_vertex_array gid ;
Gl.bind_buffer Gl.element_array_buffer iid ;
bind_attrib vid 0 3 Gl.float ;
( match snd raw.frags with
| `Colored ->
bind_attrib cid 1 3 Gl.float
| `Textured ->
bind_attrib cid 1 2 Gl.float ) ;
Gl.bind_vertex_array 0 ;
Gl.bind_buffer Gl.array_buffer 0 ;
Gl.bind_buffer Gl.element_array_buffer 0 ;
let* texture =
match raw.texture with
| Some s ->
let* texture = Gl_texture.create_from_bmp s in
Result.ok (Some texture)
| None ->
Result.ok None
in
let result =
{ gid
; bids= [iid; vid; cid]
; length= Bigarray.Array1.dim raw.indices
; mode
; texture }
in
Result.ok result
let of_arrays ?texture ?(frag_kind = `Colored) (mode, v, c, i) =
let raw =
{ vertices= bigarray_of Bigarray.float32 v
; frags= (bigarray_of Bigarray.float32 c, frag_kind)
; indices= bigarray_of Bigarray.int8_unsigned i
; mode
; texture }
in
create raw
let delete t =
set_int (Gl.delete_vertex_arrays 1) t.gid ;
List.iter delete_buffer t.bids ;
ignore @@ Option.map Gl_texture.delete t.texture
let draw ?(trans = Matrix.identity) pid t =
(match t.texture with Some tex -> Gl_texture.bind tex | None -> ()) ;
Gl.use_program pid ;
let matid = Gl.get_uniform_location pid "model" in
Gl.uniform_matrix4fv matid 1 false (Matrix.raw trans) ;
Gl.bind_vertex_array t.gid ;
Gl.draw_elements t.mode t.length Gl.unsigned_byte (`Offset 0) ;
Gl.bind_vertex_array 0 ;
Gl.bind_texture Gl.texture_2d 0