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
#![recursion_limit = "512"]

#[cfg(not(target_arch = "wasm32"))]
extern crate gl;

#[cfg(target_arch = "wasm32")]
#[path = "webgl.rs"]
pub mod webgl;

#[cfg(not(target_arch = "wasm32"))]
#[path = "webgl_native.rs"]
mod webgl;

#[cfg(not(target_arch = "wasm32"))]
/// whether current OpenGL context is OpenGL ES (Embedded System)
pub const IS_GL_ES: bool = false;

#[cfg(target_arch = "wasm32")]
pub const IS_GL_ES: bool = true;

mod glenum;

pub use glenum::*;
pub use webgl::{GLContext, WebGLContext};

pub mod common {
    use std::ops::{Deref, DerefMut};

    type Reference = super::webgl::Reference;
    type GLContext = super::GLContext;

    #[derive(Debug, Clone)]
    /// The OpenGL rendering context. This is the struct providing most of the OpenGL API.
    pub struct WebGLRenderingContext {
        pub common: GLContext,
    }

    impl Deref for WebGLRenderingContext {
        type Target = GLContext;
        fn deref(&self) -> &GLContext {
            &self.common
        }
    }

    impl DerefMut for WebGLRenderingContext {
        fn deref_mut(&mut self) -> &mut GLContext {
            &mut self.common
        }
    }

    #[derive(Debug)]
    /// an OpenGL buffer created with [`GLContext::create_buffer`].
    ///
    /// Buffers are used to store vertex attributes
    /// (position, normal, uv coordinates) and indexes for indexed rendering,
    pub struct WebGLBuffer(pub Reference);

    impl Deref for WebGLBuffer {
        type Target = Reference;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    #[derive(Debug)]
    /// an OpenGL shader created with [`GLContext::create_shader`]
    pub struct WebGLShader(pub Reference);
    impl Deref for WebGLShader {
        type Target = Reference;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    #[derive(Debug, PartialEq)]
    /// an OpenGL shader created with [`GLContext::create_shader`].
    ///
    /// There are two kinds of shaders ([`ShaderKind`]) : vertex and fragment
    pub struct WebGLProgram(pub Reference);
    impl Deref for WebGLProgram {
        type Target = Reference;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    #[derive(Debug, PartialEq)]
    /// an OpenGL program created with [`GLContext::create_program`].
    ///
    /// It is built with a vertex shader and a fragment shader.
    pub struct WebGLTexture(pub Reference);
    impl Deref for WebGLTexture {
        type Target = Reference;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    #[derive(Debug)]
    /// an OpenGL vertex array object created with [`GLContext::create_vertex_array`].
    ///
    /// Vertex array objects store all the state needed to supply vertex data.
    pub struct WebGLVertexArray(pub Reference);
    impl Deref for WebGLVertexArray {
        type Target = Reference;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    #[derive(Debug, PartialEq)]
    /// the reference to a uniform (global GLSL variable) inside a shader, obtained with [`GLContext::get_uniform_location`].
    pub struct WebGLUniformLocation {
        pub reference: Reference,
        pub name: String,
    }
    impl Deref for WebGLUniformLocation {
        type Target = Reference;
        fn deref(&self) -> &Reference {
            &self.reference
        }
    }

    #[derive(Debug)]
    /// an OpenGL Framebuffer created with [`GLContext::create_framebuffer`].
    ///
    /// This is a special type of buffer that can be used as destination for rendering.
    pub struct WebGLFrameBuffer(pub Reference);
    impl Deref for WebGLFrameBuffer {
        type Target = Reference;
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    /// Utility function to print messages to stdout (native) or the js console (web)
    pub fn print(s: &str) {
        GLContext::print(s);
    }
}

pub use self::common::*;