Skip to main content

hao_il2cpp_unity/
gl.rs

1//! Immediate-mode drawing with `UnityEngine.GL`, for use inside an
2//! [`on_render`](crate::on_render) callback (it's only valid during rendering).
3//!
4//! ```no_run
5//! use hao_il2cpp_unity::{gl, on_render, Color, Vector3};
6//!
7//! on_render(|_camera| {
8//!     gl::load_pixel_matrix(0.0, 1920.0, 0.0, 1080.0);
9//!     gl::draw(gl::Mode::Triangles, || {
10//!         gl::color(Color::rgb(1.0, 0.0, 0.0));
11//!         gl::vertex(16.0, 16.0);
12//!         gl::vertex(216.0, 16.0);
13//!         gl::vertex(16.0, 116.0);
14//!     });
15//! })?;
16//! # Ok::<(), hao_il2cpp_unity::runtime::Error>(())
17//! ```
18//!
19//! An egui (or other GPU UI) backend would translate its tessellated output into
20//! these calls, or hook the native graphics present for a full GPU surface — that
21//! part is renderer-specific and up to the mod.
22
23use hao_il2cpp_runtime::Result;
24
25use crate::{
26    bindings::{
27        GL_BEGIN, GL_CLEAR, GL_COLOR, GL_END, GL_LOAD_ORTHO, GL_LOAD_PIXEL_MATRIX, GL_POP_MATRIX,
28        GL_PUSH_MATRIX, GL_TEXCOORD2, GL_VERTEX3,
29    },
30    Color,
31};
32
33/// The primitive topology passed to [`draw`]/[`begin`], matching Unity's `GL` modes.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub enum Mode {
36    Lines = 1,
37    LineStrip = 2,
38    Triangles = 4,
39    TriangleStrip = 5,
40    Quads = 7,
41}
42
43/// Begins a primitive of `mode`. Pair with [`end`]; prefer [`draw`].
44pub fn begin(mode: Mode) -> Result<()> {
45    GL_BEGIN.call((mode as i32,))
46}
47
48pub fn end() -> Result<()> {
49    GL_END.call(())
50}
51
52/// Draws a primitive: calls [`begin`], runs `body` (which emits [`color`] and
53/// [`vertex`]/[`vertex3`]), then [`end`], even if `body` returns an error.
54pub fn draw(mode: Mode, body: impl FnOnce()) -> Result<()> {
55    begin(mode)?;
56    body();
57    end()
58}
59
60/// Sets the current vertex colour.
61pub fn color(color: Color) -> Result<()> {
62    GL_COLOR.call((color,))
63}
64
65/// Emits a 3D vertex.
66pub fn vertex3(x: f32, y: f32, z: f32) -> Result<()> {
67    GL_VERTEX3.call((x, y, z))
68}
69
70/// Emits a 2D vertex at z = 0.
71pub fn vertex(x: f32, y: f32) -> Result<()> {
72    vertex3(x, y, 0.0)
73}
74
75/// Sets the texture coordinate for the next vertex.
76pub fn tex_coord(u: f32, v: f32) -> Result<()> {
77    GL_TEXCOORD2.call((u, v))
78}
79
80/// Sets up a pixel-space projection: `(left, bottom)` to `(right, top)` in pixels.
81pub fn load_pixel_matrix(left: f32, right: f32, bottom: f32, top: f32) -> Result<()> {
82    GL_LOAD_PIXEL_MATRIX.call((left, right, bottom, top))
83}
84
85/// Sets up an orthographic projection with coordinates from (0, 0) to (1, 1).
86pub fn load_ortho() -> Result<()> {
87    GL_LOAD_ORTHO.call(())
88}
89
90/// Clears the active render target to `color` (and optionally the depth buffer).
91pub fn clear(clear_depth: bool, color: Color) -> Result<()> {
92    GL_CLEAR.call((clear_depth, true, color, 1.0))
93}
94
95/// Saves the projection/model-view matrix; restore with [`pop_matrix`].
96pub fn push_matrix() -> Result<()> {
97    GL_PUSH_MATRIX.call(())
98}
99
100pub fn pop_matrix() -> Result<()> {
101    GL_POP_MATRIX.call(())
102}