UNPKG

1.27 kBPlain TextView Raw
1import {ColorAttachment, DepthAttachment} from './value';
2import assert from 'assert';
3
4import type Context from './context';
5
6class Framebuffer {
7 context: Context;
8 width: number;
9 height: number;
10 framebuffer: WebGLFramebuffer;
11 colorAttachment: ColorAttachment;
12 depthAttachment: DepthAttachment;
13
14 constructor(context: Context, width: number, height: number, hasDepth: boolean) {
15 this.context = context;
16 this.width = width;
17 this.height = height;
18 const gl = context.gl;
19 const fbo = this.framebuffer = gl.createFramebuffer();
20
21 this.colorAttachment = new ColorAttachment(context, fbo);
22 if (hasDepth) {
23 this.depthAttachment = new DepthAttachment(context, fbo);
24 }
25 assert(gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE);
26 }
27
28 destroy() {
29 const gl = this.context.gl;
30
31 const texture = this.colorAttachment.get();
32 if (texture) gl.deleteTexture(texture);
33
34 if (this.depthAttachment) {
35 const renderbuffer = this.depthAttachment.get();
36 if (renderbuffer) gl.deleteRenderbuffer(renderbuffer);
37 }
38
39 gl.deleteFramebuffer(this.framebuffer);
40 }
41}
42
43export default Framebuffer;