forked from unitoftime/glitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
131 lines (105 loc) · 2.91 KB
/
state.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package glitch
// TODO - maybe push this up into internal/gl?
// TODO - this might lock us up into a single window? That doesn't seem like too bad of a requirement though
import (
"github.com/unitoftime/glitch/internal/gl"
"github.com/unitoftime/glitch/internal/mainthread"
)
type stateTracker struct {
// FBO
fbo gl.Framebuffer
fboBounds Rect
fboBinder func()
// Depth Test
depthTest bool
enableDepthFunc func()
depthFunc gl.Enum
depthFuncBinder func()
// Texture
texture *Texture
textureBinder func()
// BlendFunc
blendSrc, blendDst gl.Enum
blendFuncBinder func()
// Vert Buffer
vertBuf *VertexBuffer
vertBufDrawer func()
clearColor RGBA
clearMode gl.Enum
clearFunc func()
}
var state *stateTracker
func init() {
state = &stateTracker{}
state.enableDepthFunc = func() {
if state.depthTest {
gl.Enable(gl.DEPTH_TEST)
} else {
gl.Disable(gl.DEPTH_TEST)
}
}
state.depthFuncBinder = func() {
gl.DepthFunc(state.depthFunc)
}
state.fboBinder = func() {
// TODO - Note: I set the viewport when I bind the framebuffer. Is this okay?
gl.Viewport(0, 0, int(state.fboBounds.W()), int(state.fboBounds.H()))
gl.BindFramebuffer(gl.FRAMEBUFFER, state.fbo)
}
state.textureBinder = func() {
gl.ActiveTexture(gl.TEXTURE0)
// gl.ActiveTexture(gl.TEXTURE0 + position); // TODO - include position
gl.BindTexture(gl.TEXTURE_2D, state.texture.texture)
}
state.blendFuncBinder = func() {
gl.BlendFunc(state.blendSrc, state.blendDst)
}
state.vertBufDrawer = func() {
state.vertBuf.mainthreadDraw()
}
state.clearFunc = func() {
gl.ClearColor(float32(state.clearColor.R), float32(state.clearColor.G), float32(state.clearColor.B), float32(state.clearColor.A))
// gl.Clear(gl.COLOR_BUFFER_BIT) // Make configurable?
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
}
}
func (s *stateTracker) bindTexture(texture *Texture) {
// TODO: Check if changed
s.texture = texture
mainthread.Call(state.textureBinder)
}
func (s *stateTracker) bindFramebuffer(fbo gl.Framebuffer, bounds Rect) {
if s.fbo.Equal(fbo) && s.fboBounds == bounds {
return
}
state.fbo = fbo
state.fboBounds = bounds
mainthread.Call(s.fboBinder)
}
func (s *stateTracker) enableDepthTest(enable bool) {
if s.depthTest == enable {
return // Skip if state already matches
}
s.depthTest = enable
mainthread.Call(s.enableDepthFunc)
}
func (s *stateTracker) setDepthFunc(depthFunc gl.Enum) {
if s.depthFunc == depthFunc {
return // Skip if already enabled and depth functions match
}
s.depthFunc = depthFunc
mainthread.Call(s.depthFuncBinder)
}
func (s *stateTracker) setBlendFunc(src, dst gl.Enum) {
s.blendSrc = src
s.blendDst = dst
mainthread.Call(s.blendFuncBinder)
}
func (s *stateTracker) drawVertBuffer(vb *VertexBuffer) {
s.vertBuf = vb
mainthread.Call(s.vertBufDrawer)
}
func (s *stateTracker) clearTarget(color RGBA) {
s.clearColor = color
mainthread.Call(s.clearFunc)
}