-
Notifications
You must be signed in to change notification settings - Fork 0
/
framebuffer.go
executable file
·59 lines (49 loc) · 1.4 KB
/
framebuffer.go
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
package gfx
import (
"github.com/go-gl/gl/v2.1/gl"
)
// FrameBuffer wraps an OpenGL framebuffer.
type FrameBuffer struct {
id uint32
tex Texture
}
// ErrFrameBuffer indicates that a program failed to link.
const ErrFrameBuffer constErr = "incomplete framebuffer"
// NewFrameBuffer creates an FBO of the specified size that renders to
// a texture.
func NewFrameBuffer(width, height int32) (FrameBuffer, error) {
var fb FrameBuffer
var err error
gl.GenFramebuffers(1, &fb.id)
fb.Bind()
bufs := uint32(gl.COLOR_ATTACHMENT0)
gl.DrawBuffers(1, &bufs)
fb.tex, err = NewTexture(width, height, nil, gl.RGBA, 4, 4)
if err != nil {
fb.Unbind()
return FrameBuffer{}, err
}
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, fb.tex.id, 0)
status := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
fb.Unbind()
if status != gl.FRAMEBUFFER_COMPLETE {
return FrameBuffer{}, ErrFrameBuffer
}
return fb, nil
}
// GetTexture returns the texture associated with the frame buffer.
func (fb FrameBuffer) GetTexture() Texture {
return fb.tex
}
// Bind sets this framebuffer to the current framebuffer.
func (fb FrameBuffer) Bind() {
gl.BindFramebuffer(gl.FRAMEBUFFER, fb.id)
}
// Unbind unsets the current framebuffer.
func (fb FrameBuffer) Unbind() {
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
}
// Destroy frees external resources.
func (fb FrameBuffer) Destroy() {
gl.DeleteFramebuffers(1, &fb.id)
}