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
// "asi_opengl" - Aldaron's System Interface - OpenGL
//
// Copyright Jeron A. Lau 2018.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

use Program;
use types::*;

/// Uniform Data handle for a GPU Program
pub struct UniformData(pub(crate) GLint, Program);

impl UniformData {
	/// Get uniform from a shader.
	pub fn new(program: &Program, name: &[u8]) -> Self {
		// Last character in slice needs to null for it to be safe.
		assert_eq!(name[name.len() -1], b'\0');
		let opengl = program.opengl();
		let r = gl!(opengl, (opengl.get().uniform)(program.get(),
			name.as_ptr() as *const _));
		UniformData(r, program.clone())
	}

	/// If there is no such VertexData handle.
	pub fn is_none(&self) -> bool {
		self.0 == -1
	}

	/// Set a mat4 uniform
	pub fn set_mat4(&self, mat4: [f32; 16]) -> () {
		self.1.bind(); // bind the program attached to this uniform.
		let opengl = self.1.opengl();
		gl!(opengl, (opengl.get().uniform_mat4)(self.0, 1,
			0 /*bool: transpose*/, mat4.as_ptr()));
	}

	/// Set an int uniform 
	pub fn set_int1(&self, int1: i32) -> () {
		self.1.bind(); // bind the program attached to this uniform.
		let opengl = self.1.opengl();
		gl!(opengl, (opengl.get().uniform_int1)(self.0, int1));
	}

	/// Set a float uniform
	pub fn set_vec1(&self, vec1: f32) -> () {
		self.1.bind(); // bind the program attached to this uniform.
		let opengl = self.1.opengl();
		gl!(opengl, (opengl.get().uniform_vec1)(self.0, vec1));
	}

	/// Set a vec2 uniform
	pub fn set_vec2(&self, vec: &[f32; 2]) -> () {
		self.1.bind(); // bind the program attached to this uniform.
		let opengl = self.1.opengl();
		gl!(opengl, (opengl.get().uniform_vec2)(self.0, vec[0],
			vec[1]));
	}

	/// Set a vec3 uniform
	pub fn set_vec3(&self, vec: &[f32; 3]) -> () {
		self.1.bind(); // bind the program attached to this uniform.
		let opengl = self.1.opengl();
		gl!(opengl, (opengl.get().uniform_vec3)(self.0, vec[0],
			vec[1], vec[2]));
	}

	/// Set a vec4 uniform
	pub fn set_vec4(&self, vec: &[f32; 4]) -> () {
		self.1.bind(); // bind the program attached to this uniform.
		let opengl = self.1.opengl();
		gl!(opengl, (opengl.get().uniform_vec4)(self.0, vec[0],
			vec[1], vec[2], vec[3]));
	}
}