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