rust/lib-hedgewars-engine/src/render/map.rs
author alfadur
Fri, 22 Mar 2019 20:38:50 +0300
changeset 14707 5237b4c44d11
parent 14705 19122a329774
child 14714 4df1aac5f090
permissions -rw-r--r--
reeneble render from frontend
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     1
use integral_geometry::{Point, Rect, Size};
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
     2
use land2d::Land2D;
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
     3
use vec2d::Vec2D;
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     4
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     5
use super::gl::{
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
     6
    Buffer, InputElement, InputFormat, InputLayout, Shader, Texture2D, VariableBinding,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     7
};
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     8
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     9
// TODO: temp
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    10
const VERTEX_SHADER: &'static str = r#"
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    11
#version 150
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    12
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    13
in vec2 Position;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    14
in vec3 Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    15
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    16
out vec3 a_Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    17
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    18
//uniform Common {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    19
uniform mat4 Projection;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    20
//};
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    21
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    22
void main()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    23
{
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    24
	a_Uv = Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    25
	gl_Position = Projection * vec4(Position, 0.0, 1.0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    26
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    27
"#;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    28
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    29
const PIXEL_SHADER: &'static str = r#"
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    30
#version 150
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    31
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    32
in vec3 a_Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    33
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    34
uniform sampler2D Texture;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    35
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    36
out vec4 Target;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    37
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    38
void main()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    39
{
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    40
	 Target = texture2D(Texture, a_Uv.xy);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    41
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    42
"#;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    43
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    44
pub struct MapTile {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    45
    // either index into GL texture array or emulated [Texture; N]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    46
    texture_index: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    47
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    48
    width: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    49
    height: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    50
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    51
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    52
#[repr(C)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    53
#[derive(Copy, Clone)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    54
pub struct TileVertex {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    55
    pos: [f32; 2],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    56
    // doesn't hurt to include another float, just in case..
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    57
    uv: [f32; 3],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    58
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    59
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    60
pub struct DrawTile {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    61
    texture_index: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    62
    index_len: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    63
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    64
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    65
pub struct MapRenderer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    66
    tiles: Vec<MapTile>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    67
    textures: Vec<Texture2D>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    68
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    69
    tile_vertex_buffer: Buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    70
    tile_index_buffer: Buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    71
    tile_vertices: Vec<TileVertex>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    72
    tile_indices: Vec<u16>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    73
    tile_draw_calls: Vec<DrawTile>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    74
    index_offset: u16,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    75
    tile_shader: Shader,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    76
    tile_layout: InputLayout,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
    77
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    78
    tile_width: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    79
    tile_height: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    80
    num_tile_x: i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    81
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    82
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    83
impl MapRenderer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    84
    pub fn new(tile_width: u32, tile_height: u32) -> Self {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    85
        let tile_shader = Shader::new(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    86
            VERTEX_SHADER,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    87
            Some(PIXEL_SHADER),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    88
            &[
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    89
                VariableBinding::Attribute("Position", 0),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    90
                VariableBinding::Attribute("Uv", 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    91
                VariableBinding::Sampler("Texture", 0),
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
    92
            ],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
    93
        )
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
    94
        .unwrap();
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    95
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    96
        let tile_layout = InputLayout::new(vec![
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    97
            // position
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    98
            InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    99
                shader_slot: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   100
                buffer_slot: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   101
                format: InputFormat::Float(gl::FLOAT, false),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   102
                components: 2,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   103
                stride: 20,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   104
                offset: 0,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   105
            },
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   106
            // uv
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   107
            InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   108
                shader_slot: 1,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   109
                buffer_slot: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   110
                format: InputFormat::Float(gl::FLOAT, false),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   111
                components: 3,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   112
                stride: 20,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   113
                offset: 8,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   114
            },
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   115
        ]);
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   116
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   117
        MapRenderer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   118
            tiles: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   119
            textures: Vec::new(),
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   120
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   121
            tile_vertex_buffer: Buffer::empty(gl::ARRAY_BUFFER, gl::DYNAMIC_DRAW),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   122
            tile_index_buffer: Buffer::empty(gl::ELEMENT_ARRAY_BUFFER, gl::DYNAMIC_DRAW),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   123
            tile_vertices: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   124
            tile_indices: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   125
            index_offset: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   126
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   127
            tile_draw_calls: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   128
            tile_shader,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   129
            tile_layout,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   130
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   131
            tile_width,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   132
            tile_height,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   133
            num_tile_x: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   134
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   135
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   136
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   137
    pub fn init(&mut self, land: &Vec2D<u32>) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   138
        // clear tiles, but keep our textures for potential re-use
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   139
        self.tiles.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   140
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   141
        let tw = self.tile_width as usize;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   142
        let th = self.tile_height as usize;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   143
        let lw = land.width();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   144
        let lh = land.height();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   145
        let num_tile_x = lw / tw + if lw % tw != 0 { 1 } else { 0 };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   146
        let num_tile_y = lh / th + if lh % th != 0 { 1 } else { 0 };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   147
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   148
        self.num_tile_x = num_tile_x as i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   149
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   150
        for y in 0..num_tile_y {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   151
            for x in 0..num_tile_x {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   152
                let idx = x + y * num_tile_x;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   153
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   154
                let (data, stride) = {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   155
                    let bpp = 4;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   156
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   157
                    let offset = x * tw * bpp + y * th * lw * bpp;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   158
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   159
                    let data = unsafe { &land.as_bytes()[offset..] };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   160
                    let stride = land.width();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   161
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   162
                    (data, stride as u32)
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   163
                };
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   164
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   165
                let texture_index = if idx >= self.textures.len() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   166
                    let texture = Texture2D::with_data(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   167
                        data,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   168
                        stride,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   169
                        self.tile_width,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   170
                        self.tile_height,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   171
                        gl::RGBA8,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   172
                        gl::RGBA,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   173
                        gl::UNSIGNED_BYTE,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   174
                        gl::NEAREST,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   175
                    );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   176
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   177
                    let texture_index = self.textures.len();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   178
                    self.textures.push(texture);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   179
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   180
                    texture_index
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   181
                } else {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   182
                    let texture_region = Rect::new(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   183
                        Point::new(0, 0),
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   184
                        Point::new(self.tile_width as i32, self.tile_height as i32),
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   185
                    );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   186
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   187
                    self.textures[idx].update(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   188
                        texture_region,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   189
                        data,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   190
                        stride,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   191
                        gl::RGBA,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   192
                        gl::UNSIGNED_BYTE,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   193
                    );
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   194
                    idx
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   195
                };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   196
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   197
                let tile = MapTile {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   198
                    texture_index: texture_index as u32,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   199
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   200
                    // TODO: are there ever non-power of two textures?
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   201
                    width: self.tile_width,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   202
                    height: self.tile_height,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   203
                };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   204
                self.tiles.push(tile);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   205
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   206
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   207
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   208
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   209
    pub fn update(&mut self, land: &Land2D<u32>, region: Rect) {}
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   210
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   211
    pub fn render(&mut self, viewport: Rect) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   212
        self.tile_vertices.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   213
        self.tile_indices.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   214
        self.tile_draw_calls.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   215
        self.index_offset = 0;
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   216
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   217
        for (idx, tile) in self.tiles.iter().enumerate() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   218
            let tile_x = idx as i32 % self.num_tile_x;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   219
            let tile_y = idx as i32 / self.num_tile_x;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   220
            let tile_w = self.tile_width as i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   221
            let tile_h = self.tile_height as i32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   222
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   223
            let origin = Point::new(tile_x * tile_w, tile_y * tile_h);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   224
            let tile_rect = Rect::new(origin, origin + Point::new(tile_w, tile_h));
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   225
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   226
            if viewport.intersects(&tile_rect) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   227
                // lazy
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   228
                //dbg!(origin);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   229
                let tile_x = origin.x as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   230
                let tile_y = origin.y as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   231
                let tile_w = tile_x + tile_w as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   232
                let tile_h = tile_y + tile_h as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   233
                let uv_depth = tile.texture_index as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   234
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   235
                //dbg!(tile_x);
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   236
                let tl = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   237
                    pos: [tile_x, tile_y],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   238
                    uv: [0f32, 0f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   239
                };
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   240
                let bl = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   241
                    pos: [tile_x, tile_h],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   242
                    uv: [0f32, 1f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   243
                };
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   244
                let br = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   245
                    pos: [tile_w, tile_h],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   246
                    uv: [1f32, 1f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   247
                };
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   248
                let tr = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   249
                    pos: [tile_w, tile_y],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   250
                    uv: [1f32, 0f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   251
                };
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   252
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   253
                self.tile_vertices.extend(&[tl, bl, br, tr]);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   254
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   255
                let i = self.index_offset;
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   256
                self.tile_indices
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   257
                    .extend(&[i + 0, i + 1, i + 2, i + 2, i + 3, i + 0]);
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   258
                self.index_offset += 4;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   259
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   260
                self.tile_draw_calls.push(DrawTile {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   261
                    texture_index: tile.texture_index,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   262
                    index_len: 6,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   263
                });
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   264
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   265
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   266
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   267
        self.tile_vertex_buffer.write_typed(&self.tile_vertices);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   268
        self.tile_index_buffer.write_typed(&self.tile_indices);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   269
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   270
        let _g = self.tile_layout.bind(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   271
            &[(0, &self.tile_vertex_buffer)],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   272
            Some(&self.tile_index_buffer),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   273
        );
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   274
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   275
        let ortho = {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   276
            let l = viewport.left() as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   277
            let r = viewport.right() as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   278
            let b = viewport.bottom() as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   279
            let t = viewport.top() as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   280
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   281
            [
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   282
                2f32 / (r - l),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   283
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   284
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   285
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   286
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   287
                2f32 / (t - b),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   288
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   289
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   290
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   291
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   292
                0.5f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   293
                0f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   294
                (r + l) / (l - r),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   295
                (t + b) / (b - t),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   296
                0.5f32,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   297
                1f32,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   298
            ]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   299
        };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   300
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   301
        self.tile_shader.bind();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   302
        self.tile_shader.set_matrix("Projection", ortho.as_ptr());
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   303
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   304
        let mut draw_offset = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   305
        for draw_call in &self.tile_draw_calls {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   306
            unsafe {
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   307
                self.tile_shader
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   308
                    .bind_texture_2d(0, &self.textures[draw_call.texture_index as usize]);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   309
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   310
                gl::DrawElements(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   311
                    gl::TRIANGLES,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   312
                    draw_call.index_len as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   313
                    gl::UNSIGNED_SHORT,
14705
19122a329774 make world renderer optional + fmt
alfadur
parents: 14702
diff changeset
   314
                    draw_offset as *const _,
14702
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   315
                );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   316
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   317
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   318
            draw_offset += draw_call.index_len * 2;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   319
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   320
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   321
}