Skip to content

Commit

Permalink
feat(core): RenderBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sakitam-fdd committed Jun 2, 2022
1 parent ec0de76 commit 0ed53eb
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/core/RenderBuffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Resource from './Resource';
import Renderer from './Renderer';

export interface RenderBufferOptions {
width: number;
height: number;

format: GLenum;
}

export default class RenderBuffer extends Resource<RenderBufferOptions> {
public width: number;

public height: number;

#internalFormat: GLenum;

constructor (renderer: Renderer, options: RenderBufferOptions = {} as RenderBufferOptions) {
super(renderer, {
...options,
format: options.format || renderer.gl.DEPTH_COMPONENT16,
});

this.#internalFormat = this.options.format as GLenum;

this.width = this.options.width as number;
this.height = this.options.height as number;

console.assert(
this.width > 0 && this.height > 0,
'Renderbuffer object requires valid width and height greater than zero'
);
this.bind();
renderer.gl.renderbufferStorage(renderer.gl.RENDERBUFFER, this.#internalFormat, this.width, this.height);
}

resize (width, height) {
if (width === this.width && height === this.height) return;
this.bind();
this.gl.renderbufferStorage(this.gl.RENDERBUFFER, this.#internalFormat, width, height);
this.unbind();
}

bind () {
this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, this.handle);
}

unbind () {
this.gl.bindRenderbuffer(this.gl.RENDERBUFFER, null);
}

destroy () {
this.unbind();
this.deleteHandle();
}

createHandle () {
return this.gl.createRenderbuffer();
}

deleteHandle () {
this.handle && this.gl.deleteRenderbuffer(this.handle);
}
}

0 comments on commit 0ed53eb

Please sign in to comment.