grafix-toolbox 0.8.33

Personal collection of opengl and rust tools, also serving as an functional gui crate. See ./gui/elements for premade gui elements
Documentation
use super::*;

pub struct Rect {
	pub pos: Vec2,
	pub size: Vec2,
	pub color: Color,
}
impl Rect {
	pub fn compare(&self, crop: &Geom, r: &RectImpl) -> State {
		let Self { pos, size, color } = *self;
		let xyzw = State::XYZW.or_def(geom_cmp(pos, size, crop, &r.base));
		let rgba = State::RGBA.or_def(color != r.base.color);
		let ord = State::MISMATCH.or_def(!rgba.is_empty() && ordered(color) != r.ordered());
		ord | xyzw | rgba
	}
	pub fn obj(self, crop: Geom) -> RectImpl {
		let Self { pos, size, color } = self;
		RectImpl { base: Base { pos, size, crop, color } }
	}
}
pub struct RectImpl {
	base: Base,
}
impl RectImpl {
	pub fn batchable(&self, r: &Self) -> bool {
		self.ordered() == r.ordered()
	}
}
impl Primitive for RectImpl {
	fn base(&self) -> &Base {
		&self.base
	}
	fn write_mesh(&self, aspect: Vec2, BatchedObj { z, state, xyzw, rgba, .. }: BatchedObj) {
		if state.contains(State::XYZW) {
			let ((x1, y1), (x2, y2)) = mat2({
				let (p1, p2) = self.base.bound_box();
				(p1.mul(aspect), p2.mul(aspect))
			});
			let O = f16::ZERO;

			xyzw[..16].copy_from_slice(&[x1, y1, z, O, x2, y1, z, O, x2, y2, z, O, x1, y2, z, O]);
		}

		if state.contains(State::RGBA) {
			let (r, g, b, a) = vec4(self.base.color.mul(255).clmp(0, 255).round());

			rgba[..16].copy_from_slice(&[r, g, b, a, r, g, b, a, r, g, b, a, r, g, b, a]);
		}
	}
	fn batch_draw(&self, b: &VaoBind<u16>, (offset, num): (u16, u16)) {
		let s = LeakyStatic!(Shader, { [vs_gui__pos_col, ps_gui__col].pipe(Shader::pure) });

		let _ = s.Bind();
		b.Draw((num, offset, gl::TRIANGLES));
	}

	fn ordered(&self) -> bool {
		ordered(self.base().color)
	}
}

SHADER!(
	vs_gui__pos_col,
	r"layout(location = 0) in vec4 Position;
	layout(location = 1) in vec4 Color;
	out vec4 glColor;

	void main() {
		gl_Position = vec4(Position.xyz, 1);
		glColor = Color;
	}"
);
SHADER!(
	ps_gui__col,
	r"in vec4 glColor;
	layout(location = 0) out vec4 glFragColor;

	void main() { glFragColor = glColor; }"
);