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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use super::*;

pub type UniformLocation = gl::types::GLint;

#[derive(Debug)]
pub struct ActiveInfo {
    pub name: String,
    pub size: Int,
    pub typ: Enum,
}

impl Context {
    pub fn disable_vertex_attrib_array(&self, index: UInt) {
        unsafe {
            gl::DisableVertexAttribArray(index);
        }
    }

    pub fn enable_vertex_attrib_array(&self, index: UInt) {
        unsafe {
            gl::EnableVertexAttribArray(index);
        }
    }

    pub fn get_active_attrib(&self, program: &Program, index: UInt) -> ActiveInfo {
        let mut max_length = std::mem::MaybeUninit::uninit();
        unsafe {
            gl::GetProgramiv(
                *program,
                gl::ACTIVE_ATTRIBUTE_MAX_LENGTH,
                max_length.as_mut_ptr(),
            );
        }
        let max_length = unsafe { max_length.assume_init() } as usize;
        let mut buf = vec![std::mem::MaybeUninit::<u8>::uninit(); max_length];
        let mut length = std::mem::MaybeUninit::uninit();
        let mut size = std::mem::MaybeUninit::uninit();
        let mut typ = std::mem::MaybeUninit::uninit();
        unsafe {
            gl::GetActiveAttrib(
                *program,
                index,
                buf.len() as SizeI,
                length.as_mut_ptr(),
                size.as_mut_ptr(),
                typ.as_mut_ptr(),
                buf.as_mut_ptr() as *mut Char,
            );
        }
        let length = unsafe { length.assume_init() } as usize;
        let size = unsafe { size.assume_init() };
        let typ = unsafe { typ.assume_init() };
        buf.truncate(length);
        let name = String::from_utf8(unsafe { std::mem::transmute(buf) }).unwrap();
        ActiveInfo { name, size, typ }
    }

    pub fn get_active_uniform(&self, program: &Program, index: UInt) -> ActiveInfo {
        let mut max_length = std::mem::MaybeUninit::uninit();
        unsafe {
            gl::GetProgramiv(
                *program,
                gl::ACTIVE_UNIFORM_MAX_LENGTH,
                max_length.as_mut_ptr(),
            );
        }
        let max_length = unsafe { max_length.assume_init() } as usize;
        let mut buf = vec![std::mem::MaybeUninit::<u8>::uninit(); max_length];
        let mut length = std::mem::MaybeUninit::uninit();
        let mut size = std::mem::MaybeUninit::uninit();
        let mut typ = std::mem::MaybeUninit::uninit();
        unsafe {
            gl::GetActiveUniform(
                *program,
                index,
                buf.len() as SizeI,
                length.as_mut_ptr(),
                size.as_mut_ptr(),
                typ.as_mut_ptr(),
                buf.as_mut_ptr() as *mut Char,
            );
        }
        let length = unsafe { length.assume_init() } as usize;
        let size = unsafe { size.assume_init() };
        let typ = unsafe { typ.assume_init() };
        buf.truncate(length);
        let name = String::from_utf8(unsafe { std::mem::transmute(buf) }).unwrap();
        ActiveInfo { name, size, typ }
    }

    pub fn get_attrib_location(&self, program: &Program, name: &str) -> Int {
        unsafe { gl::GetAttribLocation(*program, std::ffi::CString::new(name).unwrap().as_ptr()) }
    }

    pub fn get_uniform_location(&self, program: &Program, name: &str) -> Option<UniformLocation> {
        unsafe {
            let location =
                gl::GetUniformLocation(*program, std::ffi::CString::new(name).unwrap().as_ptr());
            if location < 0 {
                None
            } else {
                Some(location)
            }
        }
    }

    pub fn uniform_1i(&self, location: &UniformLocation, v: Int) {
        unsafe {
            gl::Uniform1i(*location, v);
        }
    }

    pub fn uniform_1f(&self, location: &UniformLocation, v: Float) {
        unsafe {
            gl::Uniform1f(*location, v);
        }
    }

    pub fn uniform_2i(&self, location: &UniformLocation, v0: Int, v1: Int) {
        unsafe {
            gl::Uniform2i(*location, v0, v1);
        }
    }

    pub fn uniform_2f(&self, location: &UniformLocation, v0: Float, v1: Float) {
        unsafe {
            gl::Uniform2f(*location, v0, v1);
        }
    }

    pub fn uniform_3i(&self, location: &UniformLocation, v0: Int, v1: Int, v2: Int) {
        unsafe {
            gl::Uniform3i(*location, v0, v1, v2);
        }
    }

    pub fn uniform_3f(&self, location: &UniformLocation, v0: Float, v1: Float, v2: Float) {
        unsafe {
            gl::Uniform3f(*location, v0, v1, v2);
        }
    }

    pub fn uniform_4i(&self, location: &UniformLocation, v0: Int, v1: Int, v2: Int, v3: Int) {
        unsafe {
            gl::Uniform4i(*location, v0, v1, v2, v3);
        }
    }

    pub fn uniform_4f(
        &self,
        location: &UniformLocation,
        v0: Float,
        v1: Float,
        v2: Float,
        v3: Float,
    ) {
        unsafe {
            gl::Uniform4f(*location, v0, v1, v2, v3);
        }
    }

    pub fn uniform_matrix4fv(
        &self,
        location: &UniformLocation,
        count: SizeI,
        transpose: Bool,
        v: &[Float],
    ) {
        debug_assert_eq!(v.len(), count as usize * 4 * 4);
        unsafe {
            gl::UniformMatrix4fv(*location, count, transpose, v.as_ptr());
        }
    }

    pub fn vertex_attrib_divisor(&self, index: UInt, divisor: UInt) {
        unsafe {
            gl::VertexAttribDivisor(index, divisor);
        }
    }

    pub fn vertex_attrib_pointer(
        &self,
        index: UInt,
        size: Int,
        typ: Enum,
        normalized: Bool,
        stride: SizeI,
        offset: IntPtr,
    ) {
        unsafe {
            gl::VertexAttribPointer(index, size, typ, normalized, stride, offset as _);
        }
    }
}