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