-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.odin
164 lines (132 loc) · 4.6 KB
/
utils.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import "core:os"
import "core:strings"
import "core:path/filepath"
import lalg "core:math/linalg"
import "core:fmt"
import gl "vendor:OpenGL"
import stbi "vendor:stb/image"
load_shader_separate :: proc(vs_name, fs_name: string) -> (program: u32, success: bool) {
vs_path := filepath.join({
SHADERS_BASE_PATH,
strings.concatenate({vs_name, ".", SHADERS_VERTEX_EXTENSION}),
})
fs_path := filepath.join({
SHADERS_BASE_PATH,
strings.concatenate({fs_name, ".", SHADERS_FRAGMENT_EXTENSION}),
})
return gl.load_shaders_file(vs_path, fs_path)
}
load_shader_unified :: proc(name: string) -> (program: u32, success: bool) {
file_name, concat_err := strings.concatenate({name, ".", SHADERS_EXTENSION})
if concat_err != nil {
return 0, false
}
defer delete(file_name)
file_path := filepath.join({SHADERS_BASE_PATH, file_name})
file := os.read_entire_file_from_filename(file_path) or_return
defer delete(file)
splits := [?]string {SHADERS_VERTEX_SEPARATOR, SHADERS_FRAGMENT_SEPARATOR}
sections, split_err := strings.split_multi(string(file), splits[:])
if split_err != nil {
return 0, false
}
defer delete(sections)
// See proc code in odin/vendor/OpenGL for details
return gl.load_shaders_source(sections[1], sections[2])
}
load_shader :: proc{
load_shader_separate,
load_shader_unified,
}
use_shader :: proc(id: u32) {
gl.UseProgram(id)
}
get_uniform_loc :: proc(program: u32, uniform: string) -> i32 {
return gl.GetUniformLocation(program, cstring(raw_data(uniform)))
}
set_uniform_bool :: proc(program: u32, uniform: string, value: bool) {
gl.Uniform1i(get_uniform_loc(program, uniform), value ? 1 : 0)
}
set_uniform_int :: proc(program: u32, uniform: string, value: int) {
gl.Uniform1i(get_uniform_loc(program, uniform), i32(value))
}
set_uniform_f32 :: proc(program: u32, uniform: string, value: f32) {
gl.Uniform1f(get_uniform_loc(program, uniform), value)
}
set_uniform_v3 :: proc(program: u32, uniform: string, value: v3) {
gl.Uniform3f(
get_uniform_loc(program, uniform),
value.x, value.y, value.z,
)
}
set_uniform_v4 :: proc(program: u32, uniform: string, value: v4) {
gl.Uniform4f(
get_uniform_loc(program, uniform),
value.x, value.y, value.z, value.w,
)
}
set_uniform_mat4 :: proc(program: u32, uniform: string, value: ^mat4) {
gl.UniformMatrix4fv(
get_uniform_loc(program, uniform), 1, gl.FALSE, &value[0][0],
)
}
set_uniform :: proc{
set_uniform_bool,
set_uniform_int,
set_uniform_f32,
set_uniform_v3,
set_uniform_v4,
set_uniform_mat4,
}
load_texture :: proc(file_name: string, wrap_s := gl.REPEAT, wrap_t := gl.REPEAT) -> u32 {
tex_id: u32
gl.GenTextures(1, &tex_id)
image_width, image_height, image_chans: i32
file_path_string := filepath.join({TEXTURES_BASE_PATH, file_name})
file_path := strings.clone_to_cstring(file_path_string)
defer delete(file_path)
image_bytes := stbi.load(file_path, &image_width, &image_height, &image_chans, 0)
defer stbi.image_free(image_bytes)
format := gl.RGB
if image_chans == 4 {
format = gl.RGBA
}
gl.BindTexture(gl.TEXTURE_2D, tex_id)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, i32(wrap_s))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, i32(wrap_t))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexImage2D(gl.TEXTURE_2D, 0, i32(format), i32(image_width), i32(image_height), 0, u32(format), gl.UNSIGNED_BYTE, image_bytes)
gl.GenerateMipmap(gl.TEXTURE_2D)
return tex_id
}
// +X (right)
// -X (left)
// +Y (top)
// -Y (bottom)
// +Z (front)
// -Z (back)
load_cubemap :: proc(name: string) -> u32 {
tex_id: u32
gl.GenTextures(1, &tex_id)
gl.BindTexture(gl.TEXTURE_CUBE_MAP, tex_id)
stbi.set_flip_vertically_on_load(0)
image_width, image_height, image_chans: i32
for file_name, i in CUBEMAP_FILE_NAMES {
file_path := strings.clone_to_cstring(
filepath.join({CUBEMAPS_BASE_PATH, name, file_name}),
context.temp_allocator,
)
image_bytes := stbi.load(file_path, &image_width, &image_height, &image_chans, 0)
defer stbi.image_free(image_bytes)
gl.TexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + u32(i), 0, gl.RGB, image_width, image_height, 0, gl.RGB, gl.UNSIGNED_BYTE, image_bytes)
}
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE)
stbi.set_flip_vertically_on_load(1)
return tex_id
}