grafix_toolbox/kit/opengl/frame/
framebuff.rs

1use super::{super::texture::*, *};
2
3pub type Framebuffer = Object<Framebuff>;
4pub type Renderbuffer = Object<Renderbuff>;
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>) -> Binding<Framebuff> {
26		let TexParam { w, h, .. } = tex.param;
27		GL::Viewport::Set((0, 0, w, h));
28		Binding::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 = Fbo::new(args);
43		let TexParam { w, h, .. } = fbo.tex.param;
44		let depth = Renderbuffer::new();
45		GL!(glRenderbuffStorage(depth.obj, 1, gl::DEPTH_COMPONENT, w, h));
46		fbo.fb = fbo.fb.attach((&depth, gl::DEPTH_ATTACHMENT));
47		Self { fbo, depth }
48	}
49}
50impl<S: TexSize, F: TexFmt> Frame for RenderTgt<S, F> {
51	fn ClearColor(&self, args: impl ClearArgs) {
52		self.fbo.ClearColor(args);
53	}
54	fn ClearDepth<T>(&self, d: T)
55	where
56		f32: Cast<T>,
57	{
58		GL!(glClearFramebuff(self.fbo.fb.obj, gl::DEPTH, 0, &f32(d) as *const f32));
59	}
60	fn size(&self) -> uVec2 {
61		self.fbo.size()
62	}
63	fn bind(&self) -> Binding<Framebuff> {
64		self.fbo.bind()
65	}
66}