rust/lib-hedgewars-engine/src/render/map.rs
author alfadur
Thu, 27 Jan 2022 03:51:13 +0300
changeset 15850 44b49f255e31
parent 15784 84c07aa94b61
permissions -rw-r--r--
add type safe power of two sizes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
     1
use integral_geometry::{GridIndex, Point, Rect, Size};
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
     2
use land2d::Land2D;
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
     3
use vec2d::Vec2D;
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
     4
14739
5915a199cb81 move projection matrix into camera
alfadur
parents: 14735
diff changeset
     5
use super::{
5915a199cb81 move projection matrix into camera
alfadur
parents: 14735
diff changeset
     6
    camera::Camera,
5915a199cb81 move projection matrix into camera
alfadur
parents: 14735
diff changeset
     7
    gl::{
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
     8
        Buffer, BufferType, BufferUsage, InputElement, InputFormat, InputLayout, PipelineState,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
     9
        Shader, Texture2D, TextureDataType, TextureFilter, TextureFormat, TextureInternalFormat,
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
    10
        VariableBinding,
14740
abc6aaf481c4 add blending to map
alfadur
parents: 14739
diff changeset
    11
    },
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    12
};
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    13
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
    14
use std::num::NonZeroU32;
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
    15
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    16
const VERTEX_SHADER: &'static str = r#"
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    17
#version 150
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    18
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    19
in vec2 Position;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    20
in vec3 Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    21
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    22
out vec3 a_Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    23
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    24
//uniform Common {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    25
uniform mat4 Projection;
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
void main()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    29
{
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    30
	a_Uv = Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    31
	gl_Position = Projection * vec4(Position, 0.0, 1.0);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    32
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    33
"#;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    34
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    35
const PIXEL_SHADER: &'static str = r#"
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    36
#version 150
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    37
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    38
in vec3 a_Uv;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    39
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    40
uniform sampler2D Texture;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    41
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    42
out vec4 Target;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    43
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    44
void main()
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    45
{
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    46
	 Target = texture2D(Texture, a_Uv.xy);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    47
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    48
"#;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    49
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15783
diff changeset
    50
struct MapTile {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    51
    // either index into GL texture array or emulated [Texture; N]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    52
    texture_index: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    53
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    54
    width: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    55
    height: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    56
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    57
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    58
#[repr(C)]
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    59
#[derive(Copy, Clone)]
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15783
diff changeset
    60
struct TileVertex {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    61
    pos: [f32; 2],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    62
    // doesn't hurt to include another float, just in case..
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    63
    uv: [f32; 3],
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    64
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    65
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15783
diff changeset
    66
struct DrawTile {
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    67
    texture_index: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    68
    index_len: u32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    69
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    70
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    71
pub struct MapRenderer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    72
    tiles: Vec<MapTile>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    73
    textures: Vec<Texture2D>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    74
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    75
    tile_vertex_buffer: Buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    76
    tile_index_buffer: Buffer,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    77
    tile_vertices: Vec<TileVertex>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    78
    tile_indices: Vec<u16>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    79
    tile_draw_calls: Vec<DrawTile>,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    80
    index_offset: u16,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    81
    tile_shader: Shader,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    82
    tile_layout: InputLayout,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
    83
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
    84
    tile_size: Size,
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
    85
    num_tile_x: usize,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    86
}
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    87
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    88
impl MapRenderer {
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
    89
    pub fn new(tile_size: Size) -> Self {
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
    90
        debug_assert!(tile_size.is_power_of_two());
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
    91
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    92
        let tile_shader = Shader::new(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    93
            VERTEX_SHADER,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    94
            Some(PIXEL_SHADER),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    95
            &[
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    96
                VariableBinding::Attribute("Position", 0),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    97
                VariableBinding::Attribute("Uv", 1),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
    98
                VariableBinding::Sampler("Texture", 0),
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
    99
            ],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   100
        )
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   101
        .unwrap();
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   102
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   103
        let vertex_size = std::mem::size_of::<TileVertex>() as u32;
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   104
        let tile_layout = InputLayout::new(vec![
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   105
            // position
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   106
            InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   107
                shader_slot: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   108
                buffer_slot: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   109
                format: InputFormat::Float(gl::FLOAT, false),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   110
                components: 2,
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   111
                stride: vertex_size,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   112
                offset: 0,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   113
            },
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   114
            // uv
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   115
            InputElement {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   116
                shader_slot: 1,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   117
                buffer_slot: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   118
                format: InputFormat::Float(gl::FLOAT, false),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   119
                components: 3,
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   120
                stride: vertex_size,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   121
                offset: 8,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   122
            },
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   123
        ]);
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   124
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   125
        MapRenderer {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   126
            tiles: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   127
            textures: Vec::new(),
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   128
15783
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   129
            tile_vertex_buffer: Buffer::empty(BufferType::Array, BufferUsage::DynamicDraw),
e7eb0cd5b0e4 safetize buffer interface
alfadur
parents: 15782
diff changeset
   130
            tile_index_buffer: Buffer::empty(BufferType::ElementArray, BufferUsage::DynamicDraw),
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   131
            tile_vertices: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   132
            tile_indices: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   133
            index_offset: 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
            tile_draw_calls: Vec::new(),
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   136
            tile_shader,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   137
            tile_layout,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   138
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   139
            tile_size,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   140
            num_tile_x: 0,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   141
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   142
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   143
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   144
    pub fn init(&mut self, land: &Vec2D<u32>) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   145
        // clear tiles, but keep our textures for potential re-use
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   146
        self.tiles.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   147
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   148
        let tw = self.tile_size.width;
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   149
        let th = self.tile_size.height;
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   150
        let lw = land.width();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   151
        let lh = land.height();
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   152
        let num_tile_x = lw / tw;
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   153
        let num_tile_y = lh / th;
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   154
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   155
        self.num_tile_x = num_tile_x;
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   156
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   157
        for y in 0..num_tile_y {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   158
            for x in 0..num_tile_x {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   159
                let idx = x + y * num_tile_x;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   160
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   161
                let (data, stride) = {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   162
                    let bpp = 4;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   163
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   164
                    let offset = x * tw * bpp + y * th * lw * bpp;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   165
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   166
                    let data = unsafe { &land.as_bytes()[offset..] };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   167
                    let stride = land.width();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   168
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
   169
                    (data, NonZeroU32::new(stride as u32))
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   170
                };
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   171
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   172
                let texture_index = if idx >= self.textures.len() {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   173
                    let texture = Texture2D::with_data(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   174
                        data,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   175
                        stride,
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 14740
diff changeset
   176
                        self.tile_size,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
   177
                        TextureInternalFormat::Rgba8,
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
   178
                        TextureFormat::Rgba,
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
   179
                        TextureDataType::UnsignedByte,
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
   180
                        TextureFilter::Nearest,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   181
                    );
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   182
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   183
                    let texture_index = self.textures.len();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   184
                    self.textures.push(texture);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   185
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   186
                    texture_index
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   187
                } else {
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   188
                    let texture_region = Rect::at_origin(self.tile_size);
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   189
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   190
                    self.textures[idx].update(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   191
                        texture_region,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   192
                        data,
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   193
                        stride,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
   194
                        TextureFormat::Rgba,
ff1432e873bd safetize texture interface
alfadur
parents: 15307
diff changeset
   195
                        TextureDataType::UnsignedByte,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   196
                    );
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   197
                    idx
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
                let tile = MapTile {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   201
                    texture_index: texture_index as u32,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   202
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   203
                    // TODO: are there ever non-power of two textures?
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   204
                    width: self.tile_size.width as u32,
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   205
                    height: self.tile_size.height as u32,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   206
                };
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   207
                self.tiles.push(tile);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   208
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   209
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   210
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   211
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   212
    pub fn update(&mut self, land: &Land2D<u32>, region: Rect) {}
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   213
14739
5915a199cb81 move projection matrix into camera
alfadur
parents: 14735
diff changeset
   214
    pub fn render(&mut self, camera: &Camera) {
5915a199cb81 move projection matrix into camera
alfadur
parents: 14735
diff changeset
   215
        let viewport = camera.viewport();
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   216
        self.tile_vertices.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   217
        self.tile_indices.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   218
        self.tile_draw_calls.clear();
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   219
        self.index_offset = 0;
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   220
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   221
        for (idx, tile) in self.tiles.iter().enumerate() {
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   222
            let tile_x = idx % self.num_tile_x;
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   223
            let tile_y = idx / self.num_tile_x;
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   224
            let tile_w = self.tile_size.width;
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   225
            let tile_h = self.tile_size.width;
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   226
14735
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   227
            let origin = Point::new((tile_x * tile_w) as i32, (tile_y * tile_h) as i32);
4df1aac5f090 a bit of cleanup
alfadur
parents: 14726
diff changeset
   228
            let tile_rect = Rect::from_size(origin, self.tile_size);
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   229
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   230
            if viewport.intersects(&tile_rect) {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   231
                // lazy
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   232
                //dbg!(origin);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   233
                let tile_x = origin.x as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   234
                let tile_y = origin.y as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   235
                let tile_w = tile_x + tile_w as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   236
                let tile_h = tile_y + tile_h as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   237
                let uv_depth = tile.texture_index as f32;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   238
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   239
                //dbg!(tile_x);
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   240
                let tl = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   241
                    pos: [tile_x, tile_y],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   242
                    uv: [0f32, 0f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   243
                };
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   244
                let bl = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   245
                    pos: [tile_x, tile_h],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   246
                    uv: [0f32, 1f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   247
                };
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   248
                let br = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   249
                    pos: [tile_w, tile_h],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   250
                    uv: [1f32, 1f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   251
                };
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   252
                let tr = TileVertex {
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   253
                    pos: [tile_w, tile_y],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   254
                    uv: [1f32, 0f32, uv_depth],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   255
                };
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   256
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   257
                self.tile_vertices.extend(&[tl, bl, br, tr]);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   258
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   259
                let i = self.index_offset;
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   260
                self.tile_indices
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   261
                    .extend(&[i + 0, i + 1, i + 2, i + 2, i + 3, i + 0]);
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   262
                self.index_offset += 4;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   263
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   264
                self.tile_draw_calls.push(DrawTile {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   265
                    texture_index: tile.texture_index,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   266
                    index_len: 6,
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   267
                });
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   268
            }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   269
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   270
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   271
        self.tile_vertex_buffer.write_typed(&self.tile_vertices);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   272
        self.tile_index_buffer.write_typed(&self.tile_indices);
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   273
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   274
        let _g = self.tile_layout.bind(
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   275
            &[(0, &self.tile_vertex_buffer)],
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   276
            Some(&self.tile_index_buffer),
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   277
        );
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   278
14739
5915a199cb81 move projection matrix into camera
alfadur
parents: 14735
diff changeset
   279
        let projection = camera.projection();
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   280
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   281
        self.tile_shader.bind();
14740
abc6aaf481c4 add blending to map
alfadur
parents: 14739
diff changeset
   282
        self.tile_shader
abc6aaf481c4 add blending to map
alfadur
parents: 14739
diff changeset
   283
            .set_matrix("Projection", projection.as_ptr());
abc6aaf481c4 add blending to map
alfadur
parents: 14739
diff changeset
   284
abc6aaf481c4 add blending to map
alfadur
parents: 14739
diff changeset
   285
        let _state = PipelineState::new().with_blend();
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 mut draw_offset = 0;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   288
        for draw_call in &self.tile_draw_calls {
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   289
            unsafe {
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   290
                self.tile_shader
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   291
                    .bind_texture_2d(0, &self.textures[draw_call.texture_index as usize]);
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   292
14723
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   293
                gl::DrawElements(
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   294
                    gl::TRIANGLES,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   295
                    draw_call.index_len as i32,
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   296
                    gl::UNSIGNED_SHORT,
14726
19122a329774 make world renderer optional + fmt
alfadur
parents: 14723
diff changeset
   297
                    draw_offset as *const _,
14723
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
            draw_offset += draw_call.index_len * 2;
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   302
        }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   303
    }
29dbe9ce8b7d add basic map rendering with gl
fkaa
parents:
diff changeset
   304
}