Skip to main content

grafix_toolbox/kit/opengl/frame/
framebuff.rs

1use super::{super::texture::*, *};
2
3pub type Framebuffer = Obj<FramebuffT>;
4pub type Renderbuffer = Obj<RenderbuffT>;
5
6pub trait FramebuffArg {
7	fn apply(self, _: u32);
8}
9impl<S, F, T: TexType> FramebuffArg for (&Tex<S, F, T>, GLenum) {
10	fn apply(self, obj: u32) {
11		GL!(glFramebuffTex(obj, self.0.obj(), self.1));
12	}
13}
14impl FramebuffArg for (&Renderbuffer, GLenum) {
15	fn apply(self, obj: u32) {
16		GL!(glFramebuffRenderbuff(obj, self.0.obj, self.1));
17	}
18}
19
20impl Framebuffer {
21	pub fn attach(self, args: impl FramebuffArg) -> Self {
22		args.apply(self.obj);
23		self
24	}
25	pub fn Bind<S, F>(&self, tex: &Tex<S, F, impl TexType>) -> Bind<FramebuffT> {
26		let (w, h) = tex.whdl().xy();
27		GL::Viewport::Set((0, 0, w, h));
28		Bind::new(self)
29	}
30	pub fn Clear(&self, typ: GLenum, args: impl ClearArgs) {
31		let (attach, c) = args.get();
32		GL!(glClearFramebuff(self.obj, typ, attach, c.as_ptr()));
33	}
34}
35
36pub struct RenderTgt<S, F> {
37	pub fbo: Fbo<S, F>,
38	pub depth: Renderbuffer,
39}
40impl<S: TexSize, F: TexFmt> RenderTgt<S, F> {
41	pub fn new(args: impl FboArgs) -> Self {
42		let (mut fbo, depth) = (Fbo::new(args), Renderbuffer::new());
43		let (w, h) = fbo.tex.whdl().xy();
44		GL!(glRenderbuffStorage(depth.obj, 1, gl::DEPTH_COMPONENT, w, h));
45		fbo.fb = fbo.fb.attach((&depth, gl::DEPTH_ATTACHMENT));
46		Self { fbo, depth }
47	}
48}
49impl<S: TexSize, F: TexFmt> Frame for RenderTgt<S, F> {
50	fn ClearColor(&self, args: impl ClearArgs) {
51		self.fbo.ClearColor(args);
52	}
53	fn ClearDepth<A>(&self, d: A)
54	where
55		f32: Cast<A>,
56	{
57		GL!(glClearFramebuff(self.fbo.fb.obj, gl::DEPTH, 0, &f32(d) as *const f32));
58	}
59	fn size(&self) -> uVec2 {
60		self.fbo.size()
61	}
62	fn bind(&self) -> Bind<FramebuffT> {
63		self.fbo.bind()
64	}
65}