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