summaryrefslogtreecommitdiff
path: root/webhogg/wasm/src/context/graphics.rs
blob: 6af35a2cad1fad69558de297d0ada946a56dcaa1 (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
use crate::error::WasmError;
use wasm_bindgen::JsCast;
use web_sys::WebGl2RenderingContext as GlContext;

use super::webgl;
use super::webgl::{Color4, ShaderType, WebGl2};
use super::shader::{MAIN_VERTEX_SHADER, MAIN_FRAGMENT_SHADER};
use super::shader::ShaderProgram;

pub struct GraphicsContext {
    gl: WebGl2,
    frame_nr: u64,
    shader: ShaderProgram,
    vao: webgl::WebGlVertexArrayObject,
    buffer: webgl::WebGlBuffer,
}

impl GraphicsContext {
    pub fn from_canvas(canvas: web_sys::OffscreenCanvas) -> Result<Self, WasmError> {
        let context = canvas.get_context("webgl2")
            .map_err(|_| WasmError::WebGl2ContextCreation(
                    format!("context cration failed: getContext returned an exception")))?
            .ok_or_else(|| WasmError::WebGl2ContextCreation(
                    format!("context cration failed: getContext returned nothing")))?;
        let context = context
            .dyn_into::<GlContext>()
            .map_err(|_| WasmError::WebGl2ContextCreation(
                    format!("context object is not a context")))?;

        let gl = WebGl2::from_context(context);
        let shader = ShaderProgram::from_sources(&gl, &[
            (ShaderType::Vertex, MAIN_VERTEX_SHADER.to_string()),
            (ShaderType::Fragment, MAIN_FRAGMENT_SHADER.to_string()),
        ])?;

        let vao = gl.create_vertex_array()
            .map_err(|_| WasmError::WebGlBuffer(
                    format!("glGenVertexArrays failed")))?;
        gl.bind_vertex_array(&vao);

        let buffer = gl.create_buffer()
            .map_err(|_| WasmError::WebGlBuffer(
                    format!("glCreateBuffer failed")))?;
        gl.bind_array_buffer(&buffer);
        gl.array_buffer_data_f32(&[
            0.0, 0.0,
            1.0, 0.0,
            0.0, 1.0,
            1.0, 1.0,
            0.0, 1.0,
            1.0, 0.0,
        ]);
        gl.enable_vertex_attrib_array(0);
            
        Ok(Self {
            gl, frame_nr: 0,
            shader, vao, buffer
        })
    }

    pub fn update(&mut self) -> Result<(), WasmError> {
        let light = 0.5;
        let speed = 60.0;

        let a = (self.frame_nr as f32) / speed;
        let a = f32::abs(f32::sin(a));
        let b = f32::abs(f32::cos(a));
        let (a, b) = (a * light, b * light);

        self.gl.set_viewport();
        self.gl.clear(&Color4::new(a, light - a, b, 1.0));

        self.shader.run(&self.gl);
        self.gl.vertex_attrib_f32_pointer(0, 2);
        self.gl.draw_triangle_arrays(6);

        self.frame_nr += 1;

        Ok(())
    }
}