You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
11 KiB
Rust

use crate::texture::TextureSlot;
use crate::GlContext;
use nalgebra_glm::{Mat3, Mat4};
use std::convert::From;
use std::ptr::null;
pub trait UpdateUniform<T> {
fn update_uniform(&self, location: i32, value: T);
fn update_uniform_by_index(&self, gl: &GlContext, program_id: u32, index: usize, value: T) {
self.update_uniform(gl.get_uniform_location_by_index(program_id, index), value)
}
}
impl UpdateUniform<i32> for GlContext {
fn update_uniform(&self, location: i32, value: i32) {
unsafe { gl::Uniform1i(location, value) }
}
}
impl UpdateUniform<&i32> for GlContext {
fn update_uniform(&self, location: i32, value: &i32) {
unsafe { gl::Uniform1i(location, *value) }
}
}
impl UpdateUniform<&Vec<i32>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<i32>) {
unsafe { gl::Uniform1iv(location, value.len() as i32, value.as_ptr()) }
}
}
impl UpdateUniform<&nalgebra_glm::TVec2<i32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec2<i32>) {
unsafe { gl::Uniform2i(location, value.x, value.y) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec2<i32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec2<i32>>) {
unsafe {
gl::Uniform2iv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec3<i32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec3<i32>) {
unsafe { gl::Uniform3i(location, value.x, value.y, value.z) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec3<i32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec3<i32>>) {
unsafe {
gl::Uniform3iv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec4<i32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec4<i32>) {
unsafe { gl::Uniform4i(location, value.x, value.y, value.z, value.w) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec4<i32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec4<i32>>) {
unsafe {
gl::Uniform4iv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<u32> for GlContext {
fn update_uniform(&self, location: i32, value: u32) {
unsafe { gl::Uniform1ui(location, value) }
}
}
impl UpdateUniform<&u32> for GlContext {
fn update_uniform(&self, location: i32, value: &u32) {
unsafe { gl::Uniform1ui(location, *value) }
}
}
impl UpdateUniform<&Vec<u32>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<u32>) {
unsafe { gl::Uniform1uiv(location, value.len() as i32, value.as_ptr()) }
}
}
impl UpdateUniform<&nalgebra_glm::TVec2<u32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec2<u32>) {
unsafe { gl::Uniform2ui(location, value.x, value.y) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec2<u32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec2<u32>>) {
unsafe {
gl::Uniform2uiv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec3<u32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec3<u32>) {
unsafe { gl::Uniform3ui(location, value.x, value.y, value.z) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec3<u32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec3<u32>>) {
unsafe {
gl::Uniform3uiv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec4<u32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec4<u32>) {
unsafe { gl::Uniform4ui(location, value.x, value.y, value.z, value.w) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec4<u32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec4<u32>>) {
unsafe {
gl::Uniform4uiv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<f32> for GlContext {
fn update_uniform(&self, location: i32, value: f32) {
unsafe { gl::Uniform1f(location, value) }
}
}
impl UpdateUniform<&f32> for GlContext {
fn update_uniform(&self, location: i32, value: &f32) {
unsafe { gl::Uniform1f(location, *value) }
}
}
impl UpdateUniform<&Vec<f32>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<f32>) {
unsafe { gl::Uniform1fv(location, value.len() as i32, value.as_ptr()) }
}
}
impl UpdateUniform<&nalgebra_glm::TVec1<f32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec1<f32>) {
unsafe { gl::Uniform1f(location, value.x) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec1<f32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec1<f32>>) {
unsafe {
gl::Uniform1fv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec2<f32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec2<f32>) {
unsafe { gl::Uniform2f(location, value.x, value.y) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec2<f32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec2<f32>>) {
unsafe {
gl::Uniform2fv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec3<f32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec3<f32>) {
unsafe { gl::Uniform3f(location, value.x, value.y, value.z) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec3<f32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec3<f32>>) {
unsafe {
gl::Uniform3fv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec4<f32>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec4<f32>) {
unsafe { gl::Uniform4f(location, value.x, value.y, value.z, value.w) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec4<f32>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec4<f32>>) {
unsafe {
gl::Uniform4fv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<f64> for GlContext {
fn update_uniform(&self, location: i32, value: f64) {
unsafe { gl::Uniform1d(location, value) }
}
}
impl UpdateUniform<&f64> for GlContext {
fn update_uniform(&self, location: i32, value: &f64) {
unsafe { gl::Uniform1d(location, *value) }
}
}
impl UpdateUniform<&Vec<f64>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<f64>) {
unsafe { gl::Uniform1dv(location, value.len() as i32, value.as_ptr()) }
}
}
impl UpdateUniform<&nalgebra_glm::TVec1<f64>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec1<f64>) {
unsafe { gl::Uniform1d(location, value.x) }
}
}
impl UpdateUniform<&nalgebra_glm::TVec2<f64>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec2<f64>) {
unsafe { gl::Uniform2d(location, value.x, value.y) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec2<f64>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec2<f64>>) {
unsafe {
gl::Uniform2dv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec3<f64>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec3<f64>) {
unsafe { gl::Uniform3d(location, value.x, value.y, value.z) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec3<f64>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec3<f64>>) {
unsafe {
gl::Uniform3dv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&nalgebra_glm::TVec4<f64>> for GlContext {
fn update_uniform(&self, location: i32, value: &nalgebra_glm::TVec4<f64>) {
unsafe { gl::Uniform4d(location, value.x, value.y, value.z, value.w) }
}
}
impl UpdateUniform<&Vec<nalgebra_glm::TVec4<f64>>> for GlContext {
fn update_uniform(&self, location: i32, value: &Vec<nalgebra_glm::TVec4<f64>>) {
unsafe {
gl::Uniform4dv(
location,
value.len() as i32,
value.first().map(|x| x.as_ptr()).unwrap_or(null()),
)
}
}
}
impl UpdateUniform<&Mat3> for GlContext {
fn update_uniform(&self, location: i32, value: &Mat3) {
unsafe { gl::UniformMatrix3fv(location, 1, u8::from(false), value.as_ptr()) }
}
}
impl UpdateUniform<&Mat4> for GlContext {
fn update_uniform(&self, location: i32, value: &Mat4) {
unsafe { gl::UniformMatrix4fv(location, 1, u8::from(false), value.as_ptr()) }
}
}
impl UpdateUniform<&TextureSlot> for GlContext {
fn update_uniform(&self, location: i32, value: &TextureSlot) {
unsafe { gl::Uniform1i(location, (*value as i32) - 0x84C0) }
}
}