-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglrenderimage.bmx
150 lines (109 loc) · 3.71 KB
/
glrenderimage.bmx
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
Strict
Import Pub.GLEW
Import BRL.GLMax2D
Import "renderimageinterface.bmx"
Type TGLRenderImageFrame Extends TGLImageFrame
Field _fbo:Int
Method Delete()
DeleteFramebuffer
EndMethod
Method DeleteFramebuffer()
If _fbo
glDeleteFramebuffers(1, Varptr _fbo)
_fbo = -1 '???
EndIf
EndMethod
Method CreateRenderTarget:TGLRenderImageFrame(width, height, UseImageFiltering:Int, pixmap:TPixmap)
If pixmap pixmap = ConvertPixmap(pixmap, PF_RGBA)
glDisable(GL_SCISSOR_TEST)
glGenTextures(1, Varptr name)
glBindTexture(GL_TEXTURE_2D, name)
If pixmap
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixmap.pixels)
Else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Null)
EndIf
If UseImageFiltering
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR
Else
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST
EndIf
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE
glTexParameteri GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE
glGenFramebuffers(1,Varptr _fbo)
glBindFramebuffer GL_FRAMEBUFFER,_fbo
glBindTexture GL_TEXTURE_2D,name
glFramebufferTexture2D GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,name,0
If Not pixmap
' set and clear to a default colour
glClearColor 0, 0, 0, 0
glClear(GL_COLOR_BUFFER_BIT)
EndIf
uscale = 1.0 / width
vscale = 1.0 / height
Return Self
EndMethod
Method DestroyRenderTarget()
DeleteFramebuffer()
EndMethod
Method ToPixmap:TPixmap(width:Int, height:Int)
Local prevTexture:Int
Local prevFBO:Int
glGetIntegerv(GL_TEXTURE_BINDING_2D,Varptr prevTexture)
glBindTexture(GL_TEXTURE_2D,name)
Local pixmap:TPixmap = CreatePixmap(width, height, PF_RGBA8888)
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixmap.pixels)
glBindTexture(GL_TEXTURE_2D,prevTexture)
Return pixmap
EndMethod
EndType
Type TGLRenderImage Extends TRenderImage
Field _matrix:Float[16]
Method CreateRenderImage:TGLRenderImage(width:Int, height:Int)
Self.width = width ' TImage.width
Self.height = height ' TImage.height
_matrix = [ 2.0/width, 0.0, 0.0, 0.0,..
0.0, 2.0/height, 0.0, 0.0,..
0.0, 0.0, 1.0, 0.0,..
-1-(1.0/width), -1-(1.0/height), 1.0, 1.0 ]
Return Self
EndMethod
Method DestroyRenderImage()
TGLRenderImageFrame(frames[0]).DestroyRenderTarget()
EndMethod
Method Init(UseImageFiltering:Int, pixmap:TPixmap)
Local prevFBO:Int
Local prevTexture:Int
Local prevScissorTest:Int
glGetIntegerv(GL_FRAMEBUFFER_BINDING, Varptr prevFBO)
glGetIntegerv(GL_TEXTURE_BINDING_2D,Varptr prevTexture)
glGetIntegerv(GL_SCISSOR_TEST, Varptr prevScissorTest)
frames = New TGLRenderImageFrame[1]
frames[0] = New TGLRenderImageFrame.CreateRenderTarget(width, height, UseImageFiltering, pixmap)
If prevScissorTest glEnable(GL_SCISSOR_TEST)
glBindTexture GL_TEXTURE_2D,prevTexture
glBindFramebuffer GL_FRAMEBUFFER,prevFBO
EndMethod
Method Frame:TImageFrame(index=0)
Return frames[0]
EndMethod
Method SetRenderImage()
glBindFrameBuffer(GL_FRAMEBUFFER, TGLRenderImageFrame(frames[0])._fbo)
glMatrixMode(GL_PROJECTION)
glLoadMatrixf(_matrix)
glViewport 0,0,width,height
EndMethod
Method ToPixmap:TPixmap()
Return TGLRenderImageFrame(frames[0]).ToPixmap(width, height)
EndMethod
Method SetViewport(x:Int, y:Int, width:Int, height)
If x = 0 And y = 0 And width = Self.width And height = Self.height
glDisable GL_SCISSOR_TEST
Else
glEnable GL_SCISSOR_TEST
glScissor x, y, width, height
EndIf
EndMethod
EndType