rust/lib-hedgewars-engine/src/render/gl.rs
author Wuzzy <Wuzzy2@mail.ru>
Fri, 11 Oct 2019 18:56:50 +0200
changeset 15472 e1467bced64d
parent 15294 fd20e0a134af
permissions -rw-r--r--
Dynamite handler: Exit early when drowning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
     1
use integral_geometry::{Rect, Size};
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
     2
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
     3
use std::{ffi, ffi::CString, mem, num::NonZeroU32, 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 {
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    35
    pub handle: Option<NonZeroU32>,
14707
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) {
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    40
        if let Some(handle) = self.handle {
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    41
            unsafe {
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    42
                gl::DeleteTextures(1, &handle.get());
14707
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
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    48
fn new_texture() -> Option<NonZeroU32> {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    49
    let mut handle = 0;
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    50
    unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    51
        gl::GenTextures(1, &mut handle);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    52
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    53
    NonZeroU32::new(handle)
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    54
}
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    55
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    56
fn tex_params(filter: u32) {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    57
    unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    58
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    59
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    60
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, filter as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    61
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, filter as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    62
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    63
}
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    64
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    65
impl Texture2D {
15291
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15290
diff changeset
    66
    pub fn new(size: Size, internal_format: u32, filter: u32) -> Self {
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    67
        if let Some(handle) = new_texture() {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    68
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    69
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    70
                gl::TexImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    71
                    gl::TEXTURE_2D,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    72
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    73
                    internal_format as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    74
                    size.width as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    75
                    size.height as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    76
                    0,
15291
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15290
diff changeset
    77
                    gl::RGBA,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15290
diff changeset
    78
                    gl::UNSIGNED_BYTE,
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    79
                    std::ptr::null(),
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    80
                )
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    81
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    82
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    83
            tex_params(filter);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    84
            Self {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    85
                handle: Some(handle),
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    86
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    87
        } else {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    88
            Self { handle: None }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    89
        }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    90
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    91
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    92
    pub fn with_data(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    93
        data: &[u8],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    94
        data_stride: u32,
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
    95
        size: Size,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    96
        internal_format: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    97
        format: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    98
        ty: u32,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
    99
        filter: u32,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   100
    ) -> Self {
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   101
        if let Some(handle) = new_texture() {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   102
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   103
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   104
                gl::PixelStorei(gl::UNPACK_ROW_LENGTH, data_stride as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   105
                gl::TexImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   106
                    gl::TEXTURE_2D,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   107
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   108
                    internal_format as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   109
                    size.width as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   110
                    size.height as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   111
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   112
                    format as u32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   113
                    ty,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   114
                    data.as_ptr() as *const _,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   115
                )
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   116
            }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   117
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   118
            tex_params(filter);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   119
            Self {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   120
                handle: Some(handle),
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   121
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   122
        } else {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   123
            Self { handle: None }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   124
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   125
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   126
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   127
    pub fn update(&self, region: Rect, data: &[u8], data_stride: u32, format: u32, ty: u32) {
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   128
        if let Some(handle) = self.handle {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   129
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   130
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   131
                gl::PixelStorei(gl::UNPACK_ROW_LENGTH, data_stride as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   132
                gl::TexSubImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   133
                    gl::TEXTURE_2D,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   134
                    0,             // texture level
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   135
                    region.left(), // texture region
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   136
                    region.top(),
15294
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   137
                    region.width() as i32,
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   138
                    region.height() as i32,
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   139
                    format,                    // data format
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   140
                    ty,                        // data type
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   141
                    data.as_ptr() as *const _, // data ptr
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   142
                );
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   143
            }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   144
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   145
    }
15294
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   146
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   147
    pub fn retrieve(&self, data: &mut [u8]) {
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   148
        if let Some(handle) = self.handle {
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   149
            unsafe {
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   150
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   151
                gl::GetTexImage(
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   152
                    gl::TEXTURE_2D,
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   153
                    0,                           // texture level
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   154
                    gl::RGBA,                    // data format
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   155
                    gl::UNSIGNED_BYTE,           // data type
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   156
                    data.as_mut_ptr() as *mut _, // data ptr
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   157
                );
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   158
            }
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   159
        }
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15291
diff changeset
   160
    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   161
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   162
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   163
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   164
pub struct Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   165
    pub handle: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   166
    pub ty: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   167
    pub usage: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   168
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   169
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   170
impl Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   171
    pub fn empty(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   172
        ty: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   173
        usage: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   174
        //size: isize
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   175
    ) -> Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   176
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   177
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   178
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   179
            gl::GenBuffers(1, &mut buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   180
            gl::BindBuffer(ty, buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   181
            //gl::BufferData(ty, size, ptr::null_mut(), usage);
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
        Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   185
            handle: buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   186
            ty,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   187
            usage,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   188
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   189
    }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   190
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   191
    fn with_data(ty: u32, usage: u32, data: &[u8]) -> Buffer {
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   192
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   193
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   194
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   195
            gl::GenBuffers(1, &mut buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   196
            gl::BindBuffer(ty, buffer);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   197
            gl::BufferData(ty, data.len() as isize, data.as_ptr() as _, usage);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   198
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   199
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   200
        Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   201
            handle: buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   202
            ty,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   203
            usage,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   204
        }
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
    pub fn ty(&self) -> u32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   208
        self.ty
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   209
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   210
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   211
    pub fn handle(&self) -> u32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   212
        self.handle
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
    pub fn write_typed<T>(&self, data: &[T]) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   216
        unsafe {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   217
            let data =
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   218
                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
   219
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   220
            gl::BindBuffer(self.ty, self.handle);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   221
            gl::BufferData(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   222
                self.ty,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   223
                data.len() as isize,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   224
                data.as_ptr() as *const _ as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   225
                self.usage,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   226
            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   227
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   228
    }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   229
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   230
    pub fn write(&self, data: &[u8]) {
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   231
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   232
            gl::BindBuffer(self.ty, self.handle);
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   233
            gl::BufferData(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   234
                self.ty,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   235
                data.len() as isize,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   236
                data.as_ptr() as *const _ as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   237
                self.usage,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   238
            );
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   239
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   240
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   241
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   242
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   243
impl Drop for Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   244
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   245
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   246
            gl::DeleteBuffers(1, &self.handle);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   247
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   248
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   249
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   250
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   251
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   252
pub enum VariableBinding<'a> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   253
    Attribute(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   254
    Uniform(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   255
    UniformBlock(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   256
    Sampler(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   257
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   258
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   259
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   260
pub struct Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   261
    pub program: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   262
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   263
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   264
impl Drop for Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   265
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   266
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   267
            gl::DeleteProgram(self.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
    }
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
impl Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   273
    pub fn new<'a>(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   274
        vs: &str,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   275
        ps: Option<&str>,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   276
        bindings: &[VariableBinding<'a>],
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   277
    ) -> Result<Self, String> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   278
        unsafe fn compile_shader(ty: u32, shdr: &str) -> Result<u32, String> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   279
            let shader = gl::CreateShader(ty);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   280
            let len = shdr.len() as i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   281
            let shdr = shdr.as_ptr() as *const i8;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   282
            gl::ShaderSource(shader, 1, &shdr, &len);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   283
            gl::CompileShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   284
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   285
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   286
            gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   287
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   288
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   289
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   290
                gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   291
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   292
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   293
                gl::GetShaderInfoLog(shader, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   294
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   295
                gl::DeleteShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   296
                Err(String::from_utf8_unchecked(log))
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   297
            } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   298
                Ok(shader)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   299
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   300
        }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   301
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   302
        let vs = unsafe { compile_shader(gl::VERTEX_SHADER, vs)? };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   303
        let ps = if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   304
            Some(unsafe { compile_shader(gl::FRAGMENT_SHADER, ps)? })
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   305
        } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   306
            None
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   307
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   308
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   309
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   310
            let program = gl::CreateProgram();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   311
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   312
            gl::AttachShader(program, vs);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   313
            if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   314
                gl::AttachShader(program, ps);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   315
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   316
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   317
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   318
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   319
                    &VariableBinding::Attribute(ref name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   320
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   321
                        gl::BindAttribLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   322
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   323
                            id,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   324
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   325
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   326
                    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   327
                    _ => {}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   328
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   329
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   330
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   331
            gl::LinkProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   332
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   333
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   334
            gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   335
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   336
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   337
                gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   338
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   339
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   340
                gl::GetProgramInfoLog(program, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   341
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   342
                gl::DeleteProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   343
                return Err(String::from_utf8_unchecked(log));
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   344
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   345
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   346
            //gl::DetachShader(program, vs);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   347
            if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   348
                //gl::DetachShader(program, ps);
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
            gl::UseProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   352
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   353
            // after linking we setup sampler bindings as specified in the shader
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   354
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   355
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   356
                    VariableBinding::Uniform(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   357
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   358
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   359
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   360
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   361
                        );
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   362
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   363
                        // TODO: impl for block?
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   364
                    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   365
                    VariableBinding::UniformBlock(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   366
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   367
                        let index = gl::GetUniformBlockIndex(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   368
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   369
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   370
                        );
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   371
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   372
                        gl::UniformBlockBinding(program, index, *id);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   373
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   374
                    VariableBinding::Sampler(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   375
                        let c_str = CString::new(name.as_bytes()).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   376
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   377
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   378
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   379
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   380
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   381
                        gl::Uniform1i(index, *id as i32);
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   382
                    }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   383
                    _ => {}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   384
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   385
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   386
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   387
            Ok(Shader { program })
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   388
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   389
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   390
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   391
    pub fn bind(&self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   392
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   393
            gl::UseProgram(self.program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   394
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   395
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   396
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   397
    pub fn set_matrix(&self, name: &str, matrix: *const f32) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   398
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   399
            let c_str = CString::new(name).unwrap();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   400
            let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   401
                self.program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   402
                c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   403
            );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   404
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   405
            gl::UniformMatrix4fv(index, 1, gl::FALSE, matrix);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   406
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   407
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   408
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   409
    pub fn bind_texture_2d(&self, index: u32, texture: &Texture2D) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   410
        self.bind();
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   411
15290
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   412
        if let Some(handle) = texture.handle {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   413
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   414
                gl::ActiveTexture(gl::TEXTURE0 + index);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   415
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14724
diff changeset
   416
            }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   417
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   418
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   419
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   420
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   421
pub enum InputFormat {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   422
    Float(u32, bool),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   423
    Integer(u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   424
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   425
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   426
pub struct InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   427
    pub shader_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   428
    pub buffer_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   429
    pub format: InputFormat,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   430
    pub components: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   431
    pub stride: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   432
    pub offset: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   433
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   434
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   435
// TODO:
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   436
pub struct InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   437
    pub elements: Vec<InputElement>,
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
pub struct LayoutGuard {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   441
    vao: u32,
14707
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
impl Drop for LayoutGuard {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   445
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   446
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   447
            gl::DeleteVertexArrays(1, [self.vao].as_ptr());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   448
            gl::BindVertexArray(0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   449
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   450
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   451
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   452
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   453
impl InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   454
    pub fn new(elements: Vec<InputElement>) -> Self {
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   455
        InputLayout { elements }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   456
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   457
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   458
    pub fn bind(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   459
        &mut self,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   460
        buffers: &[(u32, &Buffer)],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   461
        index_buffer: Option<&Buffer>,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   462
    ) -> LayoutGuard {
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   463
        let mut vao = 0;
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   464
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   465
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   466
            gl::GenVertexArrays(1, &mut vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   467
            gl::BindVertexArray(vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   468
        }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   469
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   470
        for &(slot, ref buffer) in buffers {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   471
            unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   472
                gl::BindBuffer(buffer.ty(), buffer.handle());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   473
            }
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   474
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   475
            for attr in self.elements.iter().filter(|a| a.buffer_slot == slot) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   476
                unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   477
                    gl::EnableVertexAttribArray(attr.shader_slot);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   478
                    match attr.format {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   479
                        InputFormat::Float(fmt, normalized) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   480
                            gl::VertexAttribPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   481
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   482
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   483
                                fmt,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   484
                                if normalized { gl::TRUE } else { gl::FALSE },
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   485
                                attr.stride as i32,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   486
                                attr.offset as *const _,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   487
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   488
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   489
                        InputFormat::Integer(fmt) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   490
                            gl::VertexAttribIPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   491
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   492
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   493
                                fmt,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   494
                                attr.stride as i32,
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   495
                                attr.offset as *const _,
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   496
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   497
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   498
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   499
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   500
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   501
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   502
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   503
        if let Some(buf) = index_buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   504
            unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   505
                gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, buf.handle());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   506
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   507
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   508
14710
19122a329774 make world renderer optional + fmt
alfadur
parents: 14707
diff changeset
   509
        LayoutGuard { vao }
14707
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   510
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   511
}