rust/lib-hedgewars-engine/src/render/gl.rs
author alfadur
Sat, 14 Nov 2020 03:46:01 +0300
changeset 15764 b10bbfb2b354
parent 15762 84c07aa94b61
permissions -rw-r--r--
add texturing to gear renderer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
     1
use integral_geometry::{Rect, Size};
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
     2
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
     3
use std::{ffi, ffi::CString, mem, num::NonZeroU32, ptr, slice};
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     4
14719
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
     5
#[derive(Default)]
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
     6
pub struct PipelineState {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
     7
    blending: bool,
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
     8
}
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
     9
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    10
impl PipelineState {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    11
    pub fn new() -> Self {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    12
        Self::default()
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    13
    }
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    14
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    15
    pub fn with_blend(mut self) -> Self {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    16
        unsafe {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    17
            gl::Enable(gl::BLEND);
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    18
            gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    19
        }
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    20
        self.blending = true;
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    21
        self
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    22
    }
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    23
}
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    24
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    25
impl Drop for PipelineState {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    26
    fn drop(&mut self) {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    27
        if self.blending {
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    28
            unsafe { gl::Disable(gl::BLEND) }
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    29
        }
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    30
    }
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    31
}
abc6aaf481c4 add blending to map
alfadur
parents: 14707
diff changeset
    32
14702
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 {
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    35
    handle: Option<NonZeroU32>,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    36
    size: Size,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    37
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    38
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    39
impl Drop for Texture2D {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    40
    fn drop(&mut self) {
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    41
        if let Some(handle) = self.handle {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    42
            unsafe {
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    43
                gl::DeleteTextures(1, &handle.get());
14702
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
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    49
fn new_texture() -> Option<NonZeroU32> {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    50
    let mut handle = 0;
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    51
    unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    52
        gl::GenTextures(1, &mut handle);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    53
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    54
    NonZeroU32::new(handle)
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    55
}
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    56
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    57
fn tex_params(filter: TextureFilter) {
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    58
    unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    59
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    60
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    61
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, filter as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    62
        gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, filter as i32);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    63
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    64
}
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
    65
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    66
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    67
pub enum TextureFormat {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    68
    Rgba = gl::RGBA as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    69
}
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    70
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    71
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    72
pub enum TextureInternalFormat {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    73
    Rgba8 = gl::RGBA as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    74
}
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    75
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    76
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    77
pub enum TextureDataType {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    78
    UnsignedByte = gl::UNSIGNED_BYTE as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    79
}
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    80
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    81
#[derive(Clone, Copy, Debug)]
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    82
pub enum TextureFilter {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    83
    Nearest = gl::NEAREST as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    84
    Linear = gl::LINEAR as isize,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    85
}
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    86
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    87
#[inline]
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    88
fn get_u32(value: Option<NonZeroU32>) -> u32 {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    89
    value.map_or(0, |v| v.get())
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    90
}
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    91
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    92
fn is_out_of_bounds(data: &[u8], data_stride: Option<NonZeroU32>, texture_size: Size) -> bool {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    93
    let data_stride = get_u32(data_stride);
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    94
    data_stride == 0 && texture_size.area() * 4 > data.len()
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    95
        || data_stride != 0
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    96
            && texture_size.width > data_stride as usize
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    97
            && (texture_size.height * data_stride as usize) * 4 > data.len()
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    98
}
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
    99
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   100
impl Texture2D {
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   101
    pub fn new(size: Size, internal_format: TextureInternalFormat, filter: TextureFilter) -> Self {
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   102
        if let Some(handle) = new_texture() {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   103
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   104
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   105
                gl::TexImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   106
                    gl::TEXTURE_2D,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   107
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   108
                    internal_format as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   109
                    size.width as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   110
                    size.height as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   111
                    0,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   112
                    TextureFormat::Rgba as u32,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   113
                    TextureDataType::UnsignedByte as u32,
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   114
                    std::ptr::null(),
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   115
                )
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   116
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   117
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   118
            tex_params(filter);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   119
            Self {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   120
                handle: Some(handle),
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   121
                size,
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   122
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   123
        } else {
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   124
            Self { handle: None, size }
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   125
        }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   126
    }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   127
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   128
    pub fn with_data(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   129
        data: &[u8],
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   130
        data_stride: Option<NonZeroU32>,
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   131
        size: Size,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   132
        internal_format: TextureInternalFormat,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   133
        format: TextureFormat,
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   134
        data_type: TextureDataType,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   135
        filter: TextureFilter,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   136
    ) -> Self {
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   137
        if is_out_of_bounds(data, data_stride, size) {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   138
            return Self { handle: None, size };
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   139
        }
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   140
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   141
        if let Some(handle) = new_texture() {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   142
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   143
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   144
                gl::PixelStorei(gl::UNPACK_ROW_LENGTH, get_u32(data_stride) as i32);
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   145
                gl::TexImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   146
                    gl::TEXTURE_2D,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   147
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   148
                    internal_format as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   149
                    size.width as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   150
                    size.height as i32,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   151
                    0,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   152
                    format as u32,
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   153
                    data_type as u32,
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   154
                    data.as_ptr() as *const _,
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   155
                )
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   156
            }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   157
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   158
            tex_params(filter);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   159
            Self {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   160
                handle: Some(handle),
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   161
                size,
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   162
            }
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   163
        } else {
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   164
            Self { handle: None, size }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   165
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   166
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   167
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   168
    pub fn update(
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   169
        &self,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   170
        region: Rect,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   171
        data: &[u8],
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   172
        data_stride: Option<NonZeroU32>,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   173
        format: TextureFormat,
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   174
        data_type: TextureDataType,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   175
    ) {
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   176
        if let Some(handle) = self.handle {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   177
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   178
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   179
                gl::PixelStorei(gl::UNPACK_ROW_LENGTH, get_u32(data_stride) as i32);
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   180
                gl::TexSubImage2D(
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   181
                    gl::TEXTURE_2D,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   182
                    0,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   183
                    region.left(),
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   184
                    region.top(),
15289
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   185
                    region.width() as i32,
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   186
                    region.height() as i32,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   187
                    format as u32,
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   188
                    data_type as u32,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   189
                    data.as_ptr() as *const _,
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   190
                );
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   191
            }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   192
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   193
    }
15289
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   194
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   195
    pub fn retrieve(&self, data: &mut [u8]) {
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   196
        if self.size.area() * 4 > data.len() {
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   197
            return;
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   198
        }
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   199
15289
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   200
        if let Some(handle) = self.handle {
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   201
            unsafe {
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   202
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   203
                gl::GetTexImage(
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   204
                    gl::TEXTURE_2D,
15760
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   205
                    0,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   206
                    TextureFormat::Rgba as u32,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   207
                    TextureDataType::UnsignedByte as u32,
ff1432e873bd safetize texture interface
alfadur
parents: 15289
diff changeset
   208
                    data.as_mut_ptr() as *mut _,
15289
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   209
                );
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   210
            }
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   211
        }
fd20e0a134af add function to retrieve texture data
alfadur
parents: 15286
diff changeset
   212
    }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   213
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   214
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   215
#[derive(Clone, Copy, Debug)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   216
#[repr(u32)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   217
pub enum BufferType {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   218
    Array = gl::ARRAY_BUFFER,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   219
    ElementArray = gl::ELEMENT_ARRAY_BUFFER,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   220
}
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   221
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   222
#[derive(Clone, Copy, Debug)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   223
#[repr(u32)]
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   224
pub enum BufferUsage {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   225
    DynamicDraw = gl::DYNAMIC_DRAW,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   226
}
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   227
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   228
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   229
pub struct Buffer {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   230
    pub handle: Option<NonZeroU32>,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   231
    pub buffer_type: BufferType,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   232
    pub usage: BufferUsage,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   233
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   234
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   235
impl Buffer {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   236
    pub fn empty(buffer_type: BufferType, usage: BufferUsage) -> Buffer {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   237
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   238
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   239
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   240
            gl::GenBuffers(1, &mut buffer);
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
        Buffer {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   244
            handle: NonZeroU32::new(buffer),
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   245
            buffer_type: buffer_type,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   246
            usage,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   247
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   248
    }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   249
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   250
    fn with_data(buffer_type: BufferType, usage: BufferUsage, data: &[u8]) -> Buffer {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   251
        let mut buffer = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   252
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   253
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   254
            gl::GenBuffers(1, &mut buffer);
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   255
            if buffer != 0 {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   256
                gl::BindBuffer(buffer_type as u32, buffer);
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   257
                gl::BufferData(
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   258
                    buffer_type as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   259
                    data.len() as isize,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   260
                    data.as_ptr() as _,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   261
                    usage as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   262
                );
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   263
            }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   264
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   265
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   266
        Buffer {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   267
            handle: NonZeroU32::new(buffer),
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   268
            buffer_type,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   269
            usage,
14702
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
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   273
    pub fn ty(&self) -> BufferType {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   274
        self.buffer_type
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   275
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   276
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   277
    pub fn handle(&self) -> Option<NonZeroU32> {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   278
        self.handle
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   279
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   280
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   281
    pub fn write_typed<T>(&self, data: &[T]) {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   282
        if let Some(handle) = self.handle {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   283
            unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   284
                gl::BindBuffer(self.buffer_type as u32, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   285
                gl::BufferData(
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   286
                    self.buffer_type as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   287
                    (data.len() * mem::size_of::<T>()) as isize,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   288
                    data.as_ptr() as *const _,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   289
                    self.usage as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   290
                );
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   291
            }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   292
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   293
    }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   294
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   295
    pub fn write(&self, data: &[u8]) {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   296
        if let Some(handle) = self.handle {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   297
            unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   298
                gl::BindBuffer(self.buffer_type as u32, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   299
                gl::BufferData(
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   300
                    self.buffer_type as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   301
                    data.len() as isize,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   302
                    data.as_ptr() as *const _,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   303
                    self.usage as u32,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   304
                );
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   305
            }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   306
        }
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
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   310
impl Drop for Buffer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   311
    fn drop(&mut self) {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   312
        if let Some(handle) = self.handle {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   313
            let handle = handle.get();
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   314
            unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   315
                gl::DeleteBuffers(1, &handle);
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   316
            }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   317
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   318
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   319
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   320
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   321
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   322
pub enum VariableBinding<'a> {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   323
    Attribute(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   324
    Uniform(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   325
    UniformBlock(&'a str, u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   326
    Sampler(&'a str, u32),
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
#[derive(Debug)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   330
pub struct Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   331
    pub program: u32,
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
impl Drop for Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   335
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   336
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   337
            gl::DeleteProgram(self.program);
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
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   341
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   342
impl Shader {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   343
    pub fn new<'a>(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   344
        vs: &str,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   345
        ps: Option<&str>,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   346
        bindings: &[VariableBinding<'a>],
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   347
    ) -> Result<Self, String> {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   348
        unsafe fn compile_shader(shader_type: u32, shader_code: &str) -> Result<u32, String> {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   349
            let shader = gl::CreateShader(shader_type);
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   350
            let len = shader_code.len() as i32;
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   351
            let code_strings = shader_code.as_ptr() as *const i8;
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   352
            gl::ShaderSource(shader, 1, &code_strings, &len);
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   353
            gl::CompileShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   354
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   355
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   356
            gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut success as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   357
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   358
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   359
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   360
                gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   361
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   362
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   363
                gl::GetShaderInfoLog(shader, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   364
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   365
                gl::DeleteShader(shader);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   366
                Err(String::from_utf8_unchecked(log))
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   367
            } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   368
                Ok(shader)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   369
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   370
        }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   371
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   372
        let vs = unsafe { compile_shader(gl::VERTEX_SHADER, vs)? };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   373
        let ps = if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   374
            Some(unsafe { compile_shader(gl::FRAGMENT_SHADER, ps)? })
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   375
        } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   376
            None
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   377
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   378
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   379
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   380
            let program = gl::CreateProgram();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   381
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   382
            gl::AttachShader(program, vs);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   383
            if let Some(ps) = ps {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   384
                gl::AttachShader(program, ps);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   385
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   386
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   387
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   388
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   389
                    &VariableBinding::Attribute(ref name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   390
                        let c_str = CString::new(name.as_bytes()).unwrap();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   391
                        gl::BindAttribLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   392
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   393
                            id,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   394
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   395
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   396
                    }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   397
                    _ => {}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   398
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   399
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   400
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   401
            gl::LinkProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   402
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   403
            let mut success = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   404
            gl::GetProgramiv(program, gl::LINK_STATUS, &mut success);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   405
            if success == gl::FALSE as i32 {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   406
                let mut log_size = 0i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   407
                gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut log_size as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   408
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   409
                let mut log = vec![0u8; log_size as usize];
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   410
                gl::GetProgramInfoLog(program, log_size, ptr::null_mut(), log.as_mut_ptr() as _);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   411
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   412
                gl::DeleteProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   413
                return Err(String::from_utf8_unchecked(log));
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   414
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   415
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   416
            gl::UseProgram(program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   417
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   418
            for bind in bindings {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   419
                match bind {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   420
                    VariableBinding::Uniform(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   421
                        let c_str = CString::new(name.as_bytes()).unwrap();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   422
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   423
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   424
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   425
                        );
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   426
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   427
                        // TODO: impl for block?
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   428
                    }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   429
                    VariableBinding::UniformBlock(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   430
                        let c_str = CString::new(name.as_bytes()).unwrap();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   431
                        let index = gl::GetUniformBlockIndex(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   432
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   433
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   434
                        );
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   435
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   436
                        gl::UniformBlockBinding(program, index, *id);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   437
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   438
                    VariableBinding::Sampler(name, id) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   439
                        let c_str = CString::new(name.as_bytes()).unwrap();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   440
                        let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   441
                            program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   442
                            c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   443
                        );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   444
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   445
                        gl::Uniform1i(index, *id as i32);
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   446
                    }
14702
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
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   450
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   451
            Ok(Shader { program })
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   452
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   453
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   454
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   455
    pub fn bind(&self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   456
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   457
            gl::UseProgram(self.program);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   458
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   459
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   460
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   461
    pub fn set_matrix(&self, name: &str, matrix: *const f32) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   462
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   463
            let c_str = CString::new(name).unwrap();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   464
            let index = gl::GetUniformLocation(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   465
                self.program,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   466
                c_str.to_bytes_with_nul().as_ptr() as *const _,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   467
            );
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   468
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   469
            gl::UniformMatrix4fv(index, 1, gl::FALSE, matrix);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   470
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   471
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   472
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   473
    pub fn bind_texture_2d(&self, index: u32, texture: &Texture2D) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   474
        self.bind();
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   475
15285
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   476
        if let Some(handle) = texture.handle {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   477
            unsafe {
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   478
                gl::ActiveTexture(gl::TEXTURE0 + index);
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   479
                gl::BindTexture(gl::TEXTURE_2D, handle.get());
6382a14c9e83 add an empty texture constructor
alfadur
parents: 14719
diff changeset
   480
            }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   481
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   482
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   483
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   484
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   485
pub enum InputFormat {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   486
    Float(u32, bool),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   487
    Integer(u32),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   488
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   489
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   490
pub struct InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   491
    pub shader_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   492
    pub buffer_slot: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   493
    pub format: InputFormat,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   494
    pub components: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   495
    pub stride: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   496
    pub offset: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   497
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   498
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   499
// TODO:
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   500
pub struct InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   501
    pub elements: Vec<InputElement>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   502
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   503
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   504
pub struct LayoutGuard {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   505
    vao: u32,
14702
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
impl Drop for LayoutGuard {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   509
    fn drop(&mut self) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   510
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   511
            gl::DeleteVertexArrays(1, [self.vao].as_ptr());
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   512
            gl::BindVertexArray(0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   513
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   514
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   515
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   516
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   517
impl InputLayout {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   518
    pub fn new(elements: Vec<InputElement>) -> Self {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   519
        InputLayout { elements }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   520
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   521
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   522
    pub fn bind(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   523
        &mut self,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   524
        buffers: &[(u32, &Buffer)],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   525
        index_buffer: Option<&Buffer>,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   526
    ) -> LayoutGuard {
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   527
        let mut vao = 0;
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   528
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   529
        unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   530
            gl::GenVertexArrays(1, &mut vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   531
            gl::BindVertexArray(vao);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   532
        }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   533
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   534
        for &(slot, ref buffer) in buffers {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   535
            if let Some(handle) = buffer.handle() {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   536
                unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   537
                    gl::BindBuffer(buffer.ty() as u32, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   538
                }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   539
            }
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   540
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   541
            for attr in self.elements.iter().filter(|a| a.buffer_slot == slot) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   542
                unsafe {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   543
                    gl::EnableVertexAttribArray(attr.shader_slot);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   544
                    match attr.format {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   545
                        InputFormat::Float(fmt, normalized) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   546
                            gl::VertexAttribPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   547
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   548
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   549
                                fmt,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   550
                                if normalized { gl::TRUE } else { gl::FALSE },
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   551
                                attr.stride as i32,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   552
                                attr.offset as *const _,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   553
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   554
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   555
                        InputFormat::Integer(fmt) => {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   556
                            gl::VertexAttribIPointer(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   557
                                attr.shader_slot,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   558
                                attr.components as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   559
                                fmt,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   560
                                attr.stride as i32,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   561
                                attr.offset as *const _,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   562
                            );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   563
                        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   564
                    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   565
                }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   566
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   567
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   568
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   569
        if let Some(buf) = index_buffer {
15761
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   570
            if let Some(handle) = buf.handle() {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   571
                unsafe {
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   572
                    gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, handle.get());
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15760
diff changeset
   573
                }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   574
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   575
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   576
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   577
        LayoutGuard { vao }
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   578
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   579
}