summary refs log tree commit diff
path: root/src/shader/shader.odin
blob: cd798ff19454f8b79f28b2c972d0f75b93c2b330 (plain)
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
package shader

import "core:math/linalg"
import "core:os"
import "core:strings"
import gl "vendor:OpenGL"

Shader :: struct {
	id: u32,
}

SHADER_LOAD_ERROR :: -1
SHADER_OK :: 0

Vec3 :: linalg.Vector3f32
Mat4 :: linalg.Matrix4x4f32

shader_init :: proc(vsp, fsp: string) -> (^Shader, int) {
	assert(os.is_file_path(vsp))
	assert(os.is_file_path(fsp))

	program_id, ok := gl.load_shaders_file(vsp, fsp)

	if !ok {
		return nil, SHADER_LOAD_ERROR
	}

	shader := new(Shader)
	shader.id = program_id

	return shader, SHADER_OK
}

use :: proc(using shader: ^Shader) {
	gl.UseProgram(id)
}

set_bool :: proc(using shader: ^Shader, name: cstring, value: bool) {
	gl.Uniform1i(gl.GetUniformLocation(id, name), i32(value))
}

set_i32 :: proc(using shader: ^Shader, name: cstring, value: i32) {
	gl.Uniform1i(gl.GetUniformLocation(id, name), value)
}

set_f32 :: proc(using shader: ^Shader, name: cstring, value: f32) {
	gl.Uniform1f(gl.GetUniformLocation(id, name), value)
}

set_vec3 :: proc(using shader: ^Shader, name: cstring, value: ^Vec3) {
	gl.Uniform3fv(gl.GetUniformLocation(id, name), 1, &value[0])
}

set_mat4 :: proc(using shader: ^Shader, name: cstring, value: ^Mat4) {
	gl.UniformMatrix4fv(gl.GetUniformLocation(id, name), 1, gl.FALSE, &value[0][0])
}

set_value :: proc {
	set_i32,
	set_f32,
	set_bool,
}