forked from pyalot/webgl-heatmap
-
Notifications
You must be signed in to change notification settings - Fork 6
/
framebuffer.js
57 lines (48 loc) · 1.54 KB
/
framebuffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
function Framebuffer(gl) {
this.gl = gl;
this.buffer = this.gl.createFramebuffer();
}
Framebuffer.prototype.destroy = function() {
return this.gl.deleteFRamebuffer(this.buffer);
};
Framebuffer.prototype.bind = function() {
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.buffer);
return this;
};
Framebuffer.prototype.unbind = function() {
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null);
return this;
};
Framebuffer.prototype.check = function() {
var result;
result = this.gl.checkFramebufferStatus(this.gl.FRAMEBUFFER);
switch (result) {
case this.gl.FRAMEBUFFER_UNSUPPORTED:
throw 'Framebuffer is unsupported';
break;
case this.gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
throw 'Framebuffer incomplete attachment';
break;
case this.gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
throw 'Framebuffer incomplete dimensions';
break;
case this.gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
throw 'Framebuffer incomplete missing attachment';
}
return this;
};
Framebuffer.prototype.color = function(texture) {
this.gl.framebufferTexture2D(this.gl.FRAMEBUFFER, this.gl.COLOR_ATTACHMENT0, texture.target, texture.handle, 0);
this.check();
return this;
};
Framebuffer.prototype.depth = function(buffer) {
this.gl.framebufferRenderbuffer(this.gl.FRAMEBUFFER, this.gl.DEPTH_ATTACHMENT, this.gl.RENDERBUFFER, buffer.id);
this.check();
return this;
};
Framebuffer.prototype.destroy = function() {
return this.gl.deleteFramebuffer(this.buffer);
};
module.exports = Framebuffer;