rust/lib-hedgewars-engine/src/render/gl.rs
author alfadur
Fri, 22 Mar 2019 23:45:59 +0300
changeset 14719 4df1aac5f090
parent 14712 5237b4c44d11
child 14724 abc6aaf481c4
permissions -rw-r--r--
a bit of cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
     1
use integral_geometry::Rect;
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
     2
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
     3
use std::{ffi, ffi::CString, mem, ptr, slice};
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     4
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     5
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     6
pub struct Texture2D {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     7
    pub handle: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     8
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     9
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    10
impl Drop for Texture2D {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    11
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    12
        if self.handle != 0 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    13
            unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    14
                gl::DeleteTextures(1, &self.handle);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    15
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    16
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    17
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    18
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    19
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    20
impl Texture2D {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    21
    pub fn with_data(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    22
        data: &[u8],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    23
        data_stride: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    24
        width: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    25
        height: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    26
        internal_format: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    27
        format: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    28
        ty: u32,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    29
        filter: u32,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    30
    ) -> Self {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    31
        let mut handle = 0;
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    32
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    33
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    34
            gl::GenTextures(1, &mut handle);
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    35
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    36
            gl::BindTexture(gl::TEXTURE_2D, handle);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    37
            gl::PixelStorei(gl::UNPACK_ROW_LENGTH, data_stride as i32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    38
            gl::TexImage2D(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    39
                gl::TEXTURE_2D,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    40
                0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    41
                internal_format as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    42
                width as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    43
                height as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    44
                0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    45
                format as u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    46
                ty,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    47
                data.as_ptr() as *const _,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    48
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    49
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    50
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    51
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    52
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, filter as i32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    53
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, filter as i32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    54
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    55
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    56
        Texture2D { handle }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    57
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    58
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    59
    pub fn update(&self, region: Rect, data: &[u8], data_stride: u32, format: u32, ty: u32) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    60
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    61
            gl::BindTexture(gl::TEXTURE_2D, self.handle);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    62
            gl::PixelStorei(gl::UNPACK_ROW_LENGTH, data_stride as i32);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    63
            gl::TexSubImage2D(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    64
                gl::TEXTURE_2D,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    65
                0,             // texture level
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    66
                region.left(), // texture region
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    67
                region.top(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    68
                region.width() as i32 - 1,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    69
                region.height() as i32 - 1,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    70
                format,                    // data format
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    71
                ty,                        // data type
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    72
                data.as_ptr() as *const _, // data ptr
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    73
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    74
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    75
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    76
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    77
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    78
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    79
pub struct Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    80
    pub handle: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    81
    pub ty: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    82
    pub usage: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    83
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    84
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    85
impl Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    86
    pub fn empty(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    87
        ty: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    88
        usage: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    89
        //size: isize
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    90
    ) -> Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    91
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    92
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    93
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    94
            gl::GenBuffers(1, &mut buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    95
            gl::BindBuffer(ty, buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    96
            //gl::BufferData(ty, size, ptr::null_mut(), usage);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    97
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    98
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    99
        Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   100
            handle: buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   101
            ty,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   102
            usage,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   103
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   104
    }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   105
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   106
    fn with_data(ty: u32, usage: u32, data: &[u8]) -> Buffer {
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   107
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   108
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   109
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   110
            gl::GenBuffers(1, &mut buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   111
            gl::BindBuffer(ty, buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   112
            gl::BufferData(ty, data.len() as isize, data.as_ptr() as _, usage);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   113
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   114
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   115
        Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   116
            handle: buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   117
            ty,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   118
            usage,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   119
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   120
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   121
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   122
    pub fn ty(&self) -> u32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   123
        self.ty
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   124
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   125
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   126
    pub fn handle(&self) -> u32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   127
        self.handle
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   128
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   129
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   130
    pub fn write_typed<T>(&self, data: &[T]) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   131
        unsafe {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   132
            let data =
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   133
                slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * mem::size_of::<T>());
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   134
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   135
            gl::BindBuffer(self.ty, self.handle);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   136
            gl::BufferData(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   137
                self.ty,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   138
                data.len() as isize,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   139
                data.as_ptr() as *const _ as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   140
                self.usage,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   141
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   142
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   143
    }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   144
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   145
    pub fn write(&self, data: &[u8]) {
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   146
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   147
            gl::BindBuffer(self.ty, self.handle);
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   148
            gl::BufferData(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   149
                self.ty,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   150
                data.len() as isize,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   151
                data.as_ptr() as *const _ as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   152
                self.usage,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   153
            );
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   154
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   155
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   156
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   157
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   158
impl Drop for Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   159
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   160
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   161
            gl::DeleteBuffers(1, &self.handle);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   162
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   163
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   164
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   165
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   166
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   167
pub enum VariableBinding<'a> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   168
    Attribute(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   169
    Uniform(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   170
    UniformBlock(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   171
    Sampler(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   172
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   173
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   174
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   175
pub struct Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   176
    pub program: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   177
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   178
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   179
impl Drop for Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   180
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   181
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   182
            gl::DeleteProgram(self.program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   183
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   184
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   185
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   186
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   187
impl Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   188
    pub fn new<'a>(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   189
        vs: &str,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   190
        ps: Option<&str>,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   191
        bindings: &[VariableBinding<'a>],
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   192
    ) -> Result<Self, String> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   193
        unsafe fn compile_shader(ty: u32, shdr: &str) -> Result<u32, String> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   194
            let shader = gl::CreateShader(ty);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   195
            let len = shdr.len() as i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   196
            let shdr = shdr.as_ptr() as *const i8;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   197
            gl::ShaderSource(shader, 1, &shdr, &len);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   198
            gl::CompileShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   199
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   200
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   201
            gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   202
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   203
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   204
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   205
                gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   206
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   207
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   208
                gl::GetShaderInfoLog(shader, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   209
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   210
                gl::DeleteShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   211
                Err(String::from_utf8_unchecked(log))
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   212
            } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   213
                Ok(shader)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   214
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   215
        }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   216
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   217
        let vs = unsafe { compile_shader(gl::VERTEX_SHADER, vs)? };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   218
        let ps = if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   219
            Some(unsafe { compile_shader(gl::FRAGMENT_SHADER, ps)? })
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   220
        } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   221
            None
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   222
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   223
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   224
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   225
            let program = gl::CreateProgram();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   226
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   227
            gl::AttachShader(program, vs);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   228
            if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   229
                gl::AttachShader(program, ps);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   230
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   231
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   232
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   233
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   234
                    &VariableBinding::Attribute(ref name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   235
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   236
                        gl::BindAttribLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   237
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   238
                            id,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   239
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   240
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   241
                    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   242
                    _ => {}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   243
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   244
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   245
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   246
            gl::LinkProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   247
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   248
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   249
            gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   250
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   251
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   252
                gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   253
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   254
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   255
                gl::GetProgramInfoLog(program, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   256
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   257
                gl::DeleteProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   258
                return Err(String::from_utf8_unchecked(log));
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   259
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   260
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   261
            //gl::DetachShader(program, vs);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   262
            if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   263
                //gl::DetachShader(program, ps);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   264
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   265
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   266
            gl::UseProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   267
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   268
            // after linking we setup sampler bindings as specified in the shader
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   269
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   270
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   271
                    VariableBinding::Uniform(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   272
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   273
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   274
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   275
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   276
                        );
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   277
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   278
                        // TODO: impl for block?
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   279
                    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   280
                    VariableBinding::UniformBlock(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   281
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   282
                        let index = gl::GetUniformBlockIndex(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   283
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   284
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   285
                        );
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   286
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   287
                        gl::UniformBlockBinding(program, index, *id);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   288
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   289
                    VariableBinding::Sampler(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   290
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   291
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   292
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   293
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   294
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   295
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   296
                        gl::Uniform1i(index, *id as i32);
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   297
                    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   298
                    _ => {}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   299
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   300
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   301
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   302
            Ok(Shader { program })
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   303
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   304
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   305
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   306
    pub fn bind(&self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   307
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   308
            gl::UseProgram(self.program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   309
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   310
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   311
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   312
    pub fn set_matrix(&self, name: &str, matrix: *const f32) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   313
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   314
            let c_str = CString::new(name).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   315
            let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   316
                self.program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   317
                c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   318
            );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   319
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   320
            gl::UniformMatrix4fv(index, 1, gl::FALSE, matrix);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   321
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   322
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   323
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   324
    pub fn bind_texture_2d(&self, index: u32, texture: &Texture2D) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   325
        self.bind();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   326
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   327
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   328
            gl::ActiveTexture(gl::TEXTURE0 + index);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   329
            gl::BindTexture(gl::TEXTURE_2D, texture.handle);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   330
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   331
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   332
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   333
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   334
pub enum InputFormat {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   335
    Float(u32, bool),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   336
    Integer(u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   337
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   338
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   339
pub struct InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   340
    pub shader_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   341
    pub buffer_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   342
    pub format: InputFormat,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   343
    pub components: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   344
    pub stride: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   345
    pub offset: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   346
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   347
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   348
// TODO:
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   349
pub struct InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   350
    pub elements: Vec<InputElement>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   351
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   352
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   353
pub struct LayoutGuard {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   354
    vao: u32,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   355
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   356
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   357
impl Drop for LayoutGuard {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   358
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   359
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   360
            gl::DeleteVertexArrays(1, [self.vao].as_ptr());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   361
            gl::BindVertexArray(0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   362
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   363
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   364
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   365
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   366
impl InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   367
    pub fn new(elements: Vec<InputElement>) -> Self {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   368
        InputLayout { elements }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   369
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   370
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   371
    pub fn bind(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   372
        &mut self,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   373
        buffers: &[(u32, &Buffer)],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   374
        index_buffer: Option<&Buffer>,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   375
    ) -> LayoutGuard {
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   376
        let mut vao = 0;
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   377
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   378
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   379
            gl::GenVertexArrays(1, &mut vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   380
            gl::BindVertexArray(vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   381
        }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   382
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   383
        for &(slot, ref buffer) in buffers {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   384
            unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   385
                gl::BindBuffer(buffer.ty(), buffer.handle());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   386
            }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   387
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   388
            for attr in self.elements.iter().filter(|a| a.buffer_slot == slot) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   389
                unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   390
                    gl::EnableVertexAttribArray(attr.shader_slot);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   391
                    match attr.format {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   392
                        InputFormat::Float(fmt, normalized) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   393
                            gl::VertexAttribPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   394
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   395
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   396
                                fmt,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   397
                                if normalized { gl::TRUE } else { gl::FALSE },
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   398
                                attr.stride as i32,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   399
                                attr.offset as *const _,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   400
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   401
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   402
                        InputFormat::Integer(fmt) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   403
                            gl::VertexAttribIPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   404
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   405
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   406
                                fmt,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   407
                                attr.stride as i32,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   408
                                attr.offset as *const _,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   409
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   410
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   411
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   412
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   413
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   414
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   415
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   416
        if let Some(buf) = index_buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   417
            unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   418
                gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, buf.handle());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   419
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   420
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   421
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   422
        LayoutGuard { vao }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   423
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   424
}