summaryrefslogtreecommitdiff
path: root/engine/src/renderer/mod.rs
blob: 4f9d7e6c502896056c2847a1242fa89b881260af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::collections::HashMap;
use std::ffi::c_void;
use std::process::abort;

use cstr::cstr;
use ecs::component::local::Local;
use ecs::sole::Single;
use ecs::system::{Into as _, System};
use ecs::{Component, Query};

use crate::camera::Camera;
use crate::color::Color;
use crate::data_types::dimens::Dimens;
use crate::event::{Present as PresentEvent, Start as StartEvent};
use crate::lighting::{GlobalLight, LightSource};
use crate::material::{Flags as MaterialFlags, Material};
use crate::matrix::Matrix;
use crate::mesh::Mesh;
use crate::opengl::buffer::{Buffer, Usage as BufferUsage};
#[cfg(feature = "debug")]
use crate::opengl::debug::{MessageSeverity, MessageSource, MessageType};
use crate::opengl::shader::{
    Error as GlShaderError,
    Program as GlShaderProgram,
    Shader as GlShader,
};
use crate::opengl::texture::{
    set_active_texture_unit,
    Texture as GlTexture,
    TextureUnit,
};
use crate::opengl::vertex_array::{
    DataType as VertexArrayDataType,
    PrimitiveKind,
    VertexArray,
};
use crate::opengl::{clear_buffers, enable, BufferClearMask, Capability};
use crate::projection::{new_perspective_matrix, Projection};
use crate::shader::Program as ShaderProgram;
use crate::texture::{Id as TextureId, Texture};
use crate::transform::Transform;
use crate::vector::{Vec2, Vec3};
use crate::vertex::{AttributeComponentType, Vertex};
use crate::window::Window;

#[derive(Debug, Default)]
pub struct Extension {}

impl ecs::extension::Extension for Extension
{
    fn collect(self, mut collector: ecs::extension::Collector<'_>)
    {
        collector.add_system(StartEvent, initialize);

        collector.add_system(
            PresentEvent,
            render.into_system().initialize((GlObjects::default(),)),
        );
    }
}

fn initialize(window: Single<Window>)
{
    window
        .make_context_current()
        .expect("Failed to make window context current");

    gl::load_with(|symbol| match window.get_proc_address(symbol) {
        Ok(addr) => addr as *const c_void,
        Err(err) => {
            println!(
                "FATAL ERROR: Failed to get adress of OpenGL function {symbol}: {err}",
            );

            abort();
        }
    });

    #[cfg(feature = "debug")]
    initialize_debug();

    let window_size = window.size().expect("Failed to get window size");

    set_viewport(Vec2 { x: 0, y: 0 }, window_size);

    window.set_framebuffer_size_callback(|new_window_size| {
        set_viewport(Vec2::ZERO, new_window_size);
    });

    enable(Capability::DepthTest);
}

fn render(
    query: Query<(
        Mesh,
        ShaderProgram,
        Material,
        Option<MaterialFlags>,
        Transform,
    )>,
    light_source_query: Query<(LightSource, Transform)>,
    camera_query: Query<(Camera,)>,
    window: Single<Window>,
    global_light: Single<GlobalLight>,
    mut gl_objects: Local<GlObjects>,
)
{
    let Some(camera) = camera_query.iter().find_map(|(camera,)| {
        if !camera.current {
            return None;
        }

        Some(camera)
    }) else {
        #[cfg(feature = "debug")]
        tracing::warn!("No current camera. Nothing will be rendered");
        return;
    };

    // TODO: Maybe find a way to not clone here? Cloning here is needed since a light
    // source transform can be in both the object query and the light source query
    let light_source = light_source_query
        .iter()
        .next()
        .map(|(light_source, transform)| (light_source.clone(), transform.clone()));

    let GlObjects {
        shader_programs: gl_shader_programs,
        textures: gl_textures,
    } = &mut *gl_objects;

    clear_buffers(BufferClearMask::COLOR | BufferClearMask::DEPTH);

    for (mesh, shader_program, material, material_flags, transform) in &query {
        let material_flags = material_flags
            .map(|material_flags| material_flags.clone())
            .unwrap_or_default();

        let shader_program = gl_shader_programs
            .entry(shader_program.u64_hash())
            .or_insert_with(|| create_gl_shader_program(&shader_program).unwrap());

        apply_transformation_matrices(
            &transform,
            shader_program,
            &camera,
            window.size().expect("Failed to get window size"),
        );

        apply_light(
            &material,
            &material_flags,
            &global_light,
            shader_program,
            light_source
                .as_ref()
                .map(|(light_source, light_source_transform)| {
                    (light_source, light_source_transform)
                }),
            &camera,
        );

        for texture in &material.textures {
            let gl_texture = gl_textures
                .entry(texture.id())
                .or_insert_with(|| create_gl_texture(texture));

            let texture_unit =
                TextureUnit::from_texture_id(texture.id()).unwrap_or_else(|| {
                    panic!("Texture id {} is a invalid texture unit", texture.id());
                });

            set_active_texture_unit(texture_unit);

            gl_texture.bind();
        }

        shader_program.activate();

        draw_mesh(&mesh);
    }
}

#[derive(Debug, Default, Component)]
struct GlObjects
{
    shader_programs: HashMap<u64, GlShaderProgram>,
    textures: HashMap<TextureId, GlTexture>,
}

fn set_viewport(position: Vec2<u32>, size: Dimens<u32>)
{
    crate::opengl::set_viewport(position, size);
}

#[cfg(feature = "debug")]
fn initialize_debug()
{
    use crate::opengl::debug::{
        enable_debug_output,
        set_debug_message_callback,
        set_debug_message_control,
        MessageIdsAction,
    };

    enable_debug_output();

    set_debug_message_callback(opengl_debug_message_cb);

    set_debug_message_control(None, None, None, &[], MessageIdsAction::Disable);
}

fn draw_mesh(mesh: &Mesh)
{
    // TODO: Creating a new vertex buffer each draw is really dumb and slow this
    // should be rethinked
    let renderable = Renderable::new(mesh.vertices(), mesh.indices());

    renderable.vertex_arr.bind();

    if let Some(index_info) = &renderable.index_info {
        VertexArray::draw_elements(PrimitiveKind::Triangles, 0, index_info.cnt);
    } else {
        VertexArray::draw_arrays(PrimitiveKind::Triangles, 0, 3);
    }
}

fn create_gl_texture(texture: &Texture) -> GlTexture
{
    let mut gl_texture = GlTexture::new();

    gl_texture.generate(
        *texture.dimensions(),
        texture.image().as_bytes(),
        texture.pixel_data_format(),
    );

    gl_texture.apply_properties(texture.properties());

    gl_texture
}

fn create_gl_shader_program(
    shader_program: &ShaderProgram,
) -> Result<GlShaderProgram, GlShaderError>
{
    let gl_shaders = shader_program
        .shaders()
        .iter()
        .map(|shader| {
            let gl_shader = GlShader::new(shader.kind());

            gl_shader.set_source(shader.source())?;
            gl_shader.compile()?;

            Ok(gl_shader)
        })
        .collect::<Result<Vec<_>, _>>()?;

    let gl_shader_program = GlShaderProgram::new();

    for gl_shader in &gl_shaders {
        gl_shader_program.attach(gl_shader);
    }

    gl_shader_program.link()?;

    Ok(gl_shader_program)
}

#[derive(Debug)]
struct Renderable
{
    vertex_arr: VertexArray,

    /// Vertex and index buffer has to live as long as the vertex array
    _vertex_buffer: Buffer<Vertex>,
    index_info: Option<IndexInfo>,
}

impl Renderable
{
    fn new(vertices: &[Vertex], indices: Option<&[u32]>) -> Self
    {
        let mut vertex_arr = VertexArray::new();
        let mut vertex_buffer = Buffer::new();

        let mut index_info = None;

        vertex_buffer.store(vertices, BufferUsage::Static);

        vertex_arr.bind_vertex_buffer(0, &vertex_buffer, 0);

        let mut offset = 0u32;

        for attrib in Vertex::attrs() {
            vertex_arr.enable_attrib(attrib.index);

            vertex_arr.set_attrib_format(
                attrib.index,
                match attrib.component_type {
                    AttributeComponentType::Float => VertexArrayDataType::Float,
                },
                false,
                offset,
            );

            vertex_arr.set_attrib_vertex_buf_binding(attrib.index, 0);

            offset += attrib.component_size * attrib.component_cnt as u32;
        }

        if let Some(indices) = indices {
            let mut index_buffer = Buffer::new();

            index_buffer.store(indices, BufferUsage::Static);

            vertex_arr.bind_element_buffer(&index_buffer);

            index_info = Some(IndexInfo {
                _buffer: index_buffer,
                cnt: indices.len().try_into().unwrap(),
            });
        }

        Self {
            vertex_arr,
            _vertex_buffer: vertex_buffer,
            index_info,
        }
    }
}

fn apply_transformation_matrices(
    transform: &Transform,
    gl_shader_program: &mut GlShaderProgram,
    camera: &Camera,
    window_size: Dimens<u32>,
)
{
    gl_shader_program.set_uniform_matrix_4fv(cstr!("model"), &transform.as_matrix());

    let view = create_view(camera);

    gl_shader_program.set_uniform_matrix_4fv(cstr!("view"), &view);

    #[allow(clippy::cast_precision_loss)]
    let projection = match &camera.projection {
        Projection::Perspective(perspective) => new_perspective_matrix(
            perspective,
            window_size.width as f32 / window_size.height as f32,
        ),
    };

    gl_shader_program.set_uniform_matrix_4fv(cstr!("projection"), &projection);
}

fn apply_light(
    material: &Material,
    material_flags: &MaterialFlags,
    global_light: &GlobalLight,
    gl_shader_program: &mut GlShaderProgram,
    light_source: Option<(&LightSource, &Transform)>,
    camera: &Camera,
)
{
    gl_shader_program.set_uniform_vec_3fv(
        cstr!("light.position"),
        &light_source.map_or_else(Vec3::default, |(_, light_source_transform)| {
            light_source_transform.position
        }),
    );

    gl_shader_program.set_uniform_vec_3fv(
        cstr!("light.ambient"),
        // There cannot be both a material ambient color and a global ambient color
        &if material_flags.use_ambient_color {
            Color::WHITE_F32.into()
        } else {
            global_light.ambient.clone().into()
        },
    );

    gl_shader_program.set_uniform_vec_3fv(
        cstr!("light.diffuse"),
        &light_source
            .map_or(Color::WHITE_F32, |(light_source, _)| {
                light_source.diffuse.clone()
            })
            .into(),
    );

    gl_shader_program.set_uniform_vec_3fv(
        cstr!("light.specular"),
        &light_source
            .map_or(Color::WHITE_F32, |(light_source, _)| {
                light_source.specular.clone()
            })
            .into(),
    );

    gl_shader_program.set_uniform_vec_3fv(
        cstr!("material.ambient"),
        // When using the material ambient color is disabled, the ambient color has to be
        // white and not black since the material's ambient color is multiplied
        // with the global ambient light in the shader
        &if material_flags.use_ambient_color {
            material.ambient.clone()
        } else {
            Color::WHITE_F32
        }
        .into(),
    );

    gl_shader_program
        .set_uniform_vec_3fv(cstr!("material.diffuse"), &material.diffuse.clone().into());

    #[allow(clippy::cast_possible_wrap)]
    gl_shader_program.set_uniform_vec_3fv(
        cstr!("material.specular"),
        &material.specular.clone().into(),
    );

    #[allow(clippy::cast_possible_wrap)]
    gl_shader_program.set_uniform_1i(
        cstr!("material.ambient_map"),
        material.ambient_map.into_inner() as i32,
    );

    #[allow(clippy::cast_possible_wrap)]
    gl_shader_program.set_uniform_1i(
        cstr!("material.diffuse_map"),
        material.diffuse_map.into_inner() as i32,
    );

    #[allow(clippy::cast_possible_wrap)]
    gl_shader_program.set_uniform_1i(
        cstr!("material.specular_map"),
        material.specular_map.into_inner() as i32,
    );

    gl_shader_program.set_uniform_1fv(cstr!("material.shininess"), material.shininess);

    gl_shader_program.set_uniform_vec_3fv(cstr!("view_pos"), &camera.position);
}

fn create_view(camera: &Camera) -> Matrix<f32, 4, 4>
{
    let mut view = Matrix::new();

    view.look_at(&camera.position, &camera.target, &camera.global_up);

    view
}

#[cfg(feature = "debug")]
#[tracing::instrument(skip_all)]
fn opengl_debug_message_cb(
    source: MessageSource,
    ty: MessageType,
    id: u32,
    severity: MessageSeverity,
    message: &str,
)
{
    use std::backtrace::{Backtrace, BacktraceStatus};

    use tracing::{event, Level};

    macro_rules! create_event {
        ($level: expr) => {
            event!($level, ?source, ?ty, id, ?severity, message);
        };
    }

    if matches!(severity, MessageSeverity::Notification) {
        return;
    }

    match ty {
        MessageType::Error => {
            create_event!(Level::ERROR);

            let backtrace = Backtrace::capture();

            if matches!(backtrace.status(), BacktraceStatus::Captured) {
                event!(Level::TRACE, "{backtrace}");
            }
        }
        MessageType::Other => {
            create_event!(Level::INFO);
        }
        _ => {
            create_event!(Level::WARN);
        }
    };
}

#[derive(Debug)]
struct IndexInfo
{
    _buffer: Buffer<u32>,
    cnt: u32,
}