/
opengles2rendertexture.go
313 lines (274 loc) · 9.22 KB
/
opengles2rendertexture.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package renderer
import (
"github.com/PucklaMotzer09/GoHomeEngine/src/gohome"
gl "github.com/PucklaMotzer09/android-go/gles2"
"image"
"image/color"
)
var currentlyBoundRT *OpenGLES2RenderTexture
var screenFramebuffer int32
type OpenGLES2RenderTexture struct {
Name string
fbo uint32
rbo uint32
depthBuffer bool
shadowMap bool
cubeMap bool
textures []gohome.Texture
prevViewport gohome.Viewport
viewport gohome.Viewport
prevRT *OpenGLES2RenderTexture
}
func CreateOpenGLES2RenderTexture(name string, width, height, textures int, depthBuffer, multiSampled, shadowMap, cubeMap bool) *OpenGLES2RenderTexture {
rt := &OpenGLES2RenderTexture{}
rt.Create(name, width, height, textures, depthBuffer, multiSampled, shadowMap, cubeMap)
return rt
}
func (this *OpenGLES2RenderTexture) loadTextures(width, height, textures int, cubeMap bool) {
for i := 0; i < textures; i++ {
var ogltex *OpenGLES2Texture
var oglcubemap *OpenGLES2CubeMap
var texture gohome.Texture
if cubeMap {
oglcubemap = CreateOpenGLES2CubeMap(this.Name)
texture = oglcubemap
} else {
ogltex = CreateOpenGLES2Texture(this.Name)
texture = ogltex
}
texture.Load(nil, width, height, this.shadowMap)
if cubeMap {
gl.GetError()
gl.BindTexture(gl.TEXTURE_CUBE_MAP, oglcubemap.oglName)
handleOpenGLError("RenderTexture", this.Name, "Binding cubemap")
} else {
gl.GetError()
gl.BindTexture(ogltex.bindingPoint(), ogltex.oglName)
handleOpenGLError("RenderTexture", this.Name, "Binding texture 2d")
}
if this.shadowMap {
if cubeMap {
gl.GetError()
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_CUBE_MAP_POSITIVE_X, oglcubemap.oglName, 0)
handleOpenGLError("RenderTexture", this.Name, "glFramebufferTexture2D with depthBuffer and CubeMap")
} else {
gl.GetError()
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, ogltex.bindingPoint(), ogltex.oglName, 0)
handleOpenGLError("RenderTexture", this.Name, "glFramebufferTexture2D with depthBuffer and TEXTURE2D")
}
} else {
if cubeMap {
gl.GetError()
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+uint32(i), gl.TEXTURE_CUBE_MAP_POSITIVE_X, oglcubemap.oglName, 0)
handleOpenGLError("RenderTexture", this.Name, "glFramebufferTexture2D with CubeMap")
} else {
gl.GetError()
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+uint32(i), ogltex.bindingPoint(), ogltex.oglName, 0)
handleOpenGLError("RenderTexture", this.Name, "glFramebufferTexture2D with TEXTURE2D")
}
}
if !cubeMap {
texture.SetFiltering(gohome.FILTERING_LINEAR)
}
if cubeMap {
gl.GetError()
gl.BindTexture(gl.TEXTURE_CUBE_MAP, 0)
handleOpenGLError("RenderTexture", this.Name, "glBindTexture with CubeMap")
} else {
gl.GetError()
gl.BindTexture(ogltex.bindingPoint(), 0)
handleOpenGLError("RenderTexture", this.Name, "glBindTexture with TEXTURE2D")
}
this.textures = append(this.textures, texture)
}
}
func (this *OpenGLES2RenderTexture) loadRenderBuffer(width, height int) {
if this.depthBuffer {
gl.GetError()
var buf [1]uint32
gl.GenRenderbuffers(1, buf[:])
this.rbo = buf[0]
handleOpenGLError("RenderTexture", this.Name, "glGenRenderbuffers")
gl.BindRenderbuffer(gl.RENDERBUFFER, this.rbo)
handleOpenGLError("RenderTexture", this.Name, "glBindRenderbuffer")
gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(width), int32(height))
handleOpenGLError("RenderTexture", this.Name, "glRenderbufferStorage")
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.rbo)
handleOpenGLError("RenderTexture", this.Name, "glFramebufferRenderbuffer")
gl.BindRenderbuffer(gl.RENDERBUFFER, 0)
handleOpenGLError("RenderTexture", this.Name, "glBindRenderbuffer with 0")
}
}
func (this *OpenGLES2RenderTexture) Create(name string, width, height, textures int, depthBuffer, multiSampled, shadowMap, cubeMap bool) {
if textures == 0 {
textures = 1
}
this.Name = name
this.shadowMap = shadowMap
this.depthBuffer = depthBuffer && !shadowMap
this.cubeMap = cubeMap
gl.GetError()
var buf [1]uint32
gl.GenFramebuffers(1, buf[:])
this.fbo = buf[0]
handleOpenGLError("RenderTexture", this.Name, "glGenFramebuffers")
gl.BindFramebuffer(gl.FRAMEBUFFER, this.fbo)
handleOpenGLError("RenderTexture", this.Name, "glBindFramebuffer")
this.loadRenderBuffer(width, height)
this.loadTextures(width, height, textures, cubeMap)
if gl.CheckFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE {
handleOpenGLError("RenderTexture", this.Name, "glCheckFramebufferStatus")
gohome.ErrorMgr.Message(gohome.ERROR_LEVEL_ERROR, "RenderTexture", this.Name, "Framebuffer is not complete")
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(screenFramebuffer))
currentlyBoundRT = this.prevRT
return
}
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
handleOpenGLError("RenderTexture", this.Name, "glBindFramebuffer with 0")
this.viewport = gohome.Viewport{
0,
0, 0,
int(width), int(height),
false,
}
this.SetAsTarget()
gohome.Render.ClearScreen(gohome.Color{0, 0, 0, 0})
this.UnsetAsTarget()
}
func (this *OpenGLES2RenderTexture) Load(data []byte, width, height int, shadowMap bool) {
}
func (ogltex *OpenGLES2RenderTexture) LoadFromImage(img image.Image) {
}
func (this *OpenGLES2RenderTexture) GetName() string {
return this.Name
}
func (this *OpenGLES2RenderTexture) SetAsTarget() {
if currentlyBoundRT == nil {
var buf [1]int32
gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, buf[:])
screenFramebuffer = buf[0]
} else {
}
this.prevRT = currentlyBoundRT
currentlyBoundRT = this
gl.BindFramebuffer(gl.FRAMEBUFFER, this.fbo)
handleOpenGLError("RenderTexture", this.Name, "glBindFramebuffer in SetAsTarget")
this.prevViewport = gohome.Render.GetViewport()
gohome.Render.SetViewport(this.viewport)
}
func (this *OpenGLES2RenderTexture) UnsetAsTarget() {
if this.prevRT != nil {
gl.BindFramebuffer(gl.FRAMEBUFFER, this.prevRT.fbo)
currentlyBoundRT = this.prevRT
} else {
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(screenFramebuffer))
currentlyBoundRT = nil
}
handleOpenGLError("RenderTexture", this.Name, "glBindFramebuffer in UnsetAsTarget")
gohome.Render.SetViewport(this.prevViewport)
}
func (this *OpenGLES2RenderTexture) Blit(rtex gohome.RenderTexture) {
gohome.ErrorMgr.Error("RenderTexture", this.Name, "BlitFramebuffer does not work in OpenGLES2 2.0")
}
func (this *OpenGLES2RenderTexture) Bind(unit uint32) {
this.BindIndex(0, unit)
}
func (this *OpenGLES2RenderTexture) Unbind(unit uint32) {
this.UnbindIndex(0, unit)
}
func (this *OpenGLES2RenderTexture) BindIndex(index int, unit uint32) {
if index < len(this.textures) {
this.textures[index].Bind(unit)
}
}
func (this *OpenGLES2RenderTexture) UnbindIndex(index int, unit uint32) {
if index < len(this.textures) {
this.textures[index].Unbind(unit)
}
}
func (this *OpenGLES2RenderTexture) GetWidth() int {
if len(this.textures) == 0 {
return 0
} else {
return this.textures[0].GetWidth()
}
}
func (this *OpenGLES2RenderTexture) GetHeight() int {
if len(this.textures) == 0 {
return 0
} else {
return this.textures[0].GetHeight()
}
}
func (this *OpenGLES2RenderTexture) Terminate() {
var fbuf [1]uint32
fbuf[0] = this.fbo
defer gl.DeleteFramebuffers(1, fbuf[:])
if this.depthBuffer {
var rbuf [1]uint32
rbuf[0] = this.rbo
defer gl.DeleteRenderbuffers(1, rbuf[:])
}
for i := 0; i < len(this.textures); i++ {
defer this.textures[i].Terminate()
}
this.textures = append(this.textures[:0], this.textures[len(this.textures):]...)
}
func (this *OpenGLES2RenderTexture) ChangeSize(width, height int) {
if this.GetWidth() != width || this.GetHeight() != height {
textures := len(this.textures)
this.Terminate()
this.Create(this.Name, width, height, textures, this.depthBuffer, false, this.shadowMap, this.cubeMap)
}
}
func (this *OpenGLES2RenderTexture) SetFiltering(filtering int) {
for i := 0; i < len(this.textures); i++ {
this.textures[i].SetFiltering(filtering)
}
}
func (this *OpenGLES2RenderTexture) SetWrapping(wrapping int) {
for i := 0; i < len(this.textures); i++ {
this.textures[i].SetWrapping(wrapping)
}
}
func (this *OpenGLES2RenderTexture) SetBorderColor(col color.Color) {
for i := 0; i < len(this.textures); i++ {
this.textures[i].SetBorderColor(col)
}
}
func (this *OpenGLES2RenderTexture) SetBorderDepth(depth float32) {
for i := 0; i < len(this.textures); i++ {
this.textures[i].SetBorderDepth(depth)
}
}
func (this *OpenGLES2RenderTexture) GetKeyColor() color.Color {
if len(this.textures) == 0 {
return nil
}
return this.textures[0].GetKeyColor()
}
func (this *OpenGLES2RenderTexture) GetModColor() color.Color {
if len(this.textures) == 0 {
return nil
}
return this.textures[0].GetModColor()
}
func (this *OpenGLES2RenderTexture) SetKeyColor(col color.Color) {
for i := 0; i < len(this.textures); i++ {
this.textures[i].SetKeyColor(col)
}
}
func (this *OpenGLES2RenderTexture) SetModColor(col color.Color) {
for i := 0; i < len(this.textures); i++ {
this.textures[i].SetModColor(col)
}
}
func (this *OpenGLES2RenderTexture) GetData() ([]byte, int, int) {
if len(this.textures) == 0 {
return nil, 0, 0
}
if tex, ok := this.textures[0].(*OpenGLES2Texture); ok {
return tex.GetData()
}
return this.textures[0].GetData()
}