uni_gl/
lib.rs

1#![recursion_limit = "512"]
2
3#[cfg(not(target_arch = "wasm32"))]
4extern crate gl;
5
6#[cfg(target_arch = "wasm32")]
7#[path = "webgl.rs"]
8pub mod webgl;
9
10#[cfg(not(target_arch = "wasm32"))]
11#[path = "webgl_native.rs"]
12mod webgl;
13
14#[cfg(not(target_arch = "wasm32"))]
15/// whether current OpenGL context is OpenGL ES (Embedded System)
16pub const IS_GL_ES: bool = false;
17
18#[cfg(target_arch = "wasm32")]
19pub const IS_GL_ES: bool = true;
20
21mod glenum;
22
23pub use glenum::*;
24pub use webgl::{GLContext, WebGLContext};
25
26pub mod common {
27    use std::ops::{Deref, DerefMut};
28
29    type Reference = super::webgl::Reference;
30    type GLContext = super::GLContext;
31
32    #[derive(Debug, Clone)]
33    /// The OpenGL rendering context. This is the struct providing most of the OpenGL API.
34    pub struct WebGLRenderingContext {
35        pub common: GLContext,
36    }
37
38    impl Deref for WebGLRenderingContext {
39        type Target = GLContext;
40        fn deref(&self) -> &GLContext {
41            &self.common
42        }
43    }
44
45    impl DerefMut for WebGLRenderingContext {
46        fn deref_mut(&mut self) -> &mut GLContext {
47            &mut self.common
48        }
49    }
50
51    #[derive(Debug)]
52    /// an OpenGL buffer created with [`GLContext::create_buffer`].
53    ///
54    /// Buffers are used to store vertex attributes
55    /// (position, normal, uv coordinates) and indexes for indexed rendering,
56    pub struct WebGLBuffer(pub Reference);
57
58    impl Deref for WebGLBuffer {
59        type Target = Reference;
60        fn deref(&self) -> &Self::Target {
61            &self.0
62        }
63    }
64
65    #[derive(Debug)]
66    /// an OpenGL shader created with [`GLContext::create_shader`]
67    pub struct WebGLShader(pub Reference);
68    impl Deref for WebGLShader {
69        type Target = Reference;
70        fn deref(&self) -> &Self::Target {
71            &self.0
72        }
73    }
74
75    #[derive(Debug, PartialEq)]
76    /// an OpenGL shader created with [`GLContext::create_shader`].
77    ///
78    /// There are two kinds of shaders ([`ShaderKind`]) : vertex and fragment
79    pub struct WebGLProgram(pub Reference);
80    impl Deref for WebGLProgram {
81        type Target = Reference;
82        fn deref(&self) -> &Self::Target {
83            &self.0
84        }
85    }
86
87    #[derive(Debug, PartialEq)]
88    /// an OpenGL program created with [`GLContext::create_program`].
89    ///
90    /// It is built with a vertex shader and a fragment shader.
91    pub struct WebGLTexture(pub Reference);
92    impl Deref for WebGLTexture {
93        type Target = Reference;
94        fn deref(&self) -> &Self::Target {
95            &self.0
96        }
97    }
98
99    #[derive(Debug)]
100    /// an OpenGL vertex array object created with [`GLContext::create_vertex_array`].
101    ///
102    /// Vertex array objects store all the state needed to supply vertex data.
103    pub struct WebGLVertexArray(pub Reference);
104    impl Deref for WebGLVertexArray {
105        type Target = Reference;
106        fn deref(&self) -> &Self::Target {
107            &self.0
108        }
109    }
110
111    #[derive(Debug, PartialEq)]
112    /// the reference to a uniform (global GLSL variable) inside a shader, obtained with [`GLContext::get_uniform_location`].
113    pub struct WebGLUniformLocation {
114        pub reference: Reference,
115        pub name: String,
116    }
117    impl Deref for WebGLUniformLocation {
118        type Target = Reference;
119        fn deref(&self) -> &Reference {
120            &self.reference
121        }
122    }
123
124    #[derive(Debug)]
125    /// an OpenGL Framebuffer created with [`GLContext::create_framebuffer`].
126    ///
127    /// This is a special type of buffer that can be used as destination for rendering.
128    pub struct WebGLFrameBuffer(pub Reference);
129    impl Deref for WebGLFrameBuffer {
130        type Target = Reference;
131        fn deref(&self) -> &Self::Target {
132            &self.0
133        }
134    }
135
136    /// Utility function to print messages to stdout (native) or the js console (web)
137    pub fn print(s: &str) {
138        GLContext::print(s);
139    }
140}
141
142pub use self::common::*;