-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframebuffer.js
71 lines (57 loc) · 2.19 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// OpenGL framebuffer prototype
// Authored by Michael Becher
// Last edit 22.03.2015
function Framebuffer(width, height)
{
this.handle = gl.createFramebuffer();
this.width = width;
this.height = height;
this.color_attachments = [];
this.depth_buffer = null;
}
// Apperently, gl.RGB or gl.FLOAT are currently not allowed for (internal-)format
// internalFormat e.g. gl.RGBA
// format e.g. gl.RGBA
// type e.g. gl.FLOAT
Framebuffer.prototype.addColorAttachment = function(internalFormat,format,type)
{
var handle = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, handle);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, this.width, this.height, 0, format, type, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.handle);
var attach_point = this.color_attachments.length;
gl.framebufferTexture2D(gl.FRAMEBUFFER, drawbuffersExt.COLOR_ATTACHMENT0_WEBGL + attach_point, gl.TEXTURE_2D, handle, 0);
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE)
{
console.log("Framebuffer incomplete after adding color attachment "+attach_point+".");
return;
}
this.color_attachments.push(handle);
}
Framebuffer.prototype.addDepthBuffer = function()
{
gl.bindFramebuffer(gl.FRAMEBUFFER, this.handle);
this.depth_buffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, this.depth_buffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, this.width, this.height);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.depth_buffer);
if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE)
{
console.log("Framebuffer incomplete after adding depth attachment.");
return;
}
}
Framebuffer.prototype.bind = function()
{
gl.bindFramebuffer(gl.FRAMEBUFFER, this.handle);
}
Framebuffer.prototype.getColorAttachment = function(index)
{
if(index >= this.color_attachments.length)
return;
return this.color_attachments[index];
}