-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlight.odin
114 lines (94 loc) · 2.72 KB
/
light.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
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
package zephr
import m "core:math/linalg/glsl"
import gl "vendor:OpenGL"
LightType :: enum u8 {
DIRECTIONAL,
POINT,
SPOT,
}
Light :: struct #packed {
type: LightType,
position: m.vec3,
diffuse: m.vec3,
using _: struct #raw_union {
direction: m.vec3,
point: struct {
constant: f32,
linear: f32,
quadratic: f32,
},
},
vao: u32,
vbo: u32,
ebo: u32,
}
new_dir_light :: proc(direction, diffuse: m.vec3) -> Light {
vao, vbo, ebo: u32
//odinfmt: disable
gizmo_verts := [2]m.vec3 {
{0, 0, 0},
m.normalize(direction) * 5,
}
gizmo_indices := [?]u32{0, 1}
//odinfmt: enable
gl.GenVertexArrays(1, &vao)
gl.GenBuffers(1, &vbo)
gl.GenBuffers(1, &ebo)
gl.BindVertexArray(vao)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, size_of(gizmo_verts), raw_data(&gizmo_verts), gl.STATIC_DRAW)
gl.EnableVertexAttribArray(0)
gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * size_of(f32), 0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size_of(gizmo_indices), raw_data(&gizmo_indices), gl.STATIC_DRAW)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindVertexArray(0)
return(
Light {
type = .DIRECTIONAL,
diffuse = diffuse,
direction = direction,
vao = vao,
vbo = vbo,
ebo = ebo,
} \
)
}
new_point_light :: proc(position, diffuse: m.vec3, constant: f32 = 1.0, linear: f32 = 0.09, quadratic: f32 = 0.032) -> Light {
vao, vbo, ebo: u32
//odinfmt: disable
gizmo_verts := [4]m.vec3 {
{1, 1, 0},
{1, -1, 0},
{-1, -1, 0},
{-1, 1, 0},
}
gizmo_indices := [?]u32{
0, 1, 3,
1, 2, 3,
}
//odinfmt: enable
gl.GenVertexArrays(1, &vao)
gl.GenBuffers(1, &vbo)
gl.GenBuffers(1, &ebo)
gl.BindVertexArray(vao)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, size_of(gizmo_verts), &gizmo_verts, gl.STATIC_DRAW)
gl.EnableVertexAttribArray(0)
gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * size_of(f32), 0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size_of(gizmo_indices), &gizmo_indices, gl.STATIC_DRAW)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindVertexArray(0)
return(
Light {
type = .POINT,
diffuse = diffuse,
position = position,
point = {constant = constant, linear = linear, quadratic = quadratic},
vao = vao,
vbo = vbo,
ebo = ebo,
} \
)
}