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