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)]
35#[cfg_attr(feature = "reflect", derive(facet::Facet))]
36#[cfg_attr(feature = "reflect", repr(C))]
37pub enum Mode {
38 /// Each pair of vertices is a separate line (`GL.LINES`).
39 Lines = 1,
40 /// A connected line through every vertex (`GL.LINE_STRIP`).
41 LineStrip = 2,
42 /// Each three vertices are a separate triangle (`GL.TRIANGLES`).
43 Triangles = 4,
44 /// Each vertex after the first two adds a triangle with the previous two
45 /// (`GL.TRIANGLE_STRIP`).
46 TriangleStrip = 5,
47 /// Each four vertices are a separate quad (`GL.QUADS`).
48 Quads = 7,
49}
50
51/// Begins a primitive of `mode`. Pair with [`end`]; prefer [`draw`].
52pub fn begin(mode: Mode) -> Result<()> {
53 GL_BEGIN.call((mode as i32,))
54}
55
56/// Ends the primitive started by [`begin`].
57pub fn end() -> Result<()> {
58 GL_END.call(())
59}
60
61/// Draws a primitive: calls [`begin`], runs `body` (which emits [`color`] and
62/// [`vertex`]/[`vertex3`]), then [`end`], even if `body` returns an error.
63pub fn draw(mode: Mode, body: impl FnOnce()) -> Result<()> {
64 begin(mode)?;
65 body();
66 end()
67}
68
69/// Sets the current vertex colour.
70pub fn color(color: Color) -> Result<()> {
71 GL_COLOR.call((color,))
72}
73
74/// Emits a 3D vertex.
75pub fn vertex3(x: f32, y: f32, z: f32) -> Result<()> {
76 GL_VERTEX3.call((x, y, z))
77}
78
79/// Emits a 2D vertex at z = 0.
80pub fn vertex(x: f32, y: f32) -> Result<()> {
81 vertex3(x, y, 0.0)
82}
83
84/// Sets the texture coordinate for the next vertex.
85pub fn tex_coord(u: f32, v: f32) -> Result<()> {
86 GL_TEXCOORD2.call((u, v))
87}
88
89/// Sets up a pixel-space projection: `(left, bottom)` to `(right, top)` in pixels.
90pub fn load_pixel_matrix(left: f32, right: f32, bottom: f32, top: f32) -> Result<()> {
91 GL_LOAD_PIXEL_MATRIX.call((left, right, bottom, top))
92}
93
94/// Sets up an orthographic projection with coordinates from (0, 0) to (1, 1).
95pub fn load_ortho() -> Result<()> {
96 GL_LOAD_ORTHO.call(())
97}
98
99/// Clears the active render target to `color` (and optionally the depth buffer).
100pub fn clear(clear_depth: bool, color: Color) -> Result<()> {
101 GL_CLEAR.call((clear_depth, true, color, 1.0))
102}
103
104/// Saves the projection/model-view matrix; restore with [`pop_matrix`].
105pub fn push_matrix() -> Result<()> {
106 GL_PUSH_MATRIX.call(())
107}
108
109/// Restores the matrices saved by [`push_matrix`].
110pub fn pop_matrix() -> Result<()> {
111 GL_POP_MATRIX.call(())
112}