/
framebuffer.go
106 lines (84 loc) · 2.71 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gl
import (
"github.com/thinkofdeath/gl/v3.2-core/gl"
"github.com/thinkofdeath/steven/console"
)
type Attachment uint32
const (
ColorAttachment0 Attachment = gl.COLOR_ATTACHMENT0
ColorAttachment1 Attachment = gl.COLOR_ATTACHMENT1
ColorAttachment2 Attachment = gl.COLOR_ATTACHMENT2
DepthAttachment Attachment = gl.DEPTH_ATTACHMENT
)
type Framebuffer struct {
internal uint32
}
func NewFramebuffer() Framebuffer {
f := Framebuffer{}
gl.GenFramebuffers(1, &f.internal)
return f
}
func BindFragDataLocation(p Program, cn int, name string) {
n := gl.Str(name + "\x00")
gl.BindFragDataLocation(uint32(p), uint32(cn), n)
}
func (f Framebuffer) Bind() {
gl.BindFramebuffer(gl.FRAMEBUFFER, f.internal)
}
func (f Framebuffer) BindRead() {
gl.BindFramebuffer(gl.READ_FRAMEBUFFER, f.internal)
}
func (f Framebuffer) BindDraw() {
gl.BindFramebuffer(gl.DRAW_FRAMEBUFFER, f.internal)
}
func UnbindFramebuffer() {
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
}
func UnbindFramebufferRead() {
gl.BindFramebuffer(gl.READ_FRAMEBUFFER, 0)
}
func UnbindFramebufferDraw() {
gl.BindFramebuffer(gl.DRAW_FRAMEBUFFER, 0)
}
func DrawBuffers(bufs []Attachment) {
gl.DrawBuffers(int32(len(bufs)), (*uint32)(gl.Ptr(bufs)))
}
func (f Framebuffer) Texture2D(attachment Attachment, texTarget TextureTarget, tex Texture, level int) {
gl.FramebufferTexture2D(gl.FRAMEBUFFER, uint32(attachment), uint32(texTarget), tex.internal, int32(level))
}
func (f Framebuffer) Delete() {
gl.DeleteFramebuffers(1, &f.internal)
}
func (f Framebuffer) Check() {
console.Text("%04X", gl.CheckFramebufferStatus(gl.FRAMEBUFFER))
}
func BlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1 int, mask ClearFlags, filter TextureValue) {
gl.BlitFramebuffer(
int32(sx0), int32(sy0), int32(sx1), int32(sy1),
int32(dx0), int32(dy0), int32(dx1), int32(dy1),
uint32(mask), uint32(filter),
)
}
func (f Framebuffer) ReadBuffer(a Attachment) {
gl.ReadBuffer(uint32(a))
}
type TargetBuffer uint32
const (
Color TargetBuffer = gl.COLOR
)
func ClearBuffer(buffer TargetBuffer, drawBuffer int, values []float32) {
gl.ClearBufferfv(uint32(buffer), int32(drawBuffer), (*float32)(gl.Ptr(values)))
}