2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module egfx
.backgl
/*is aliced*/;
20 import arsd
.simpledisplay
;
24 //import iv.glbinds : glTexParameterfv; // rdmd hack
30 // ////////////////////////////////////////////////////////////////////////// //
31 /*public*/ enum GLTexType
= GL_BGRA
;
32 //public enum GLTexType = GL_RGBA;
35 // ////////////////////////////////////////////////////////////////////////// //
36 public __gshared
uint vglTexId
; // OpenGL texture id
37 public __gshared
uint vArrowTextureId
= 0;
40 // ////////////////////////////////////////////////////////////////////////// //
41 shared static this () {
42 import core
.stdc
.stdlib
: malloc
;
43 vglTexBuf
= cast(uint*)malloc((VBufWidth
*VBufHeight
+4)*4);
44 if (vglTexBuf
is null) { import core
.exception
: onOutOfMemoryErrorNoGC
; onOutOfMemoryErrorNoGC(); }
45 vglTexBuf
[0..VBufWidth
*VBufHeight
+4] = 0;
49 // ////////////////////////////////////////////////////////////////////////// //
50 public void vglCreateArrowTexture () {
53 enum wrapOpt
= GL_REPEAT
;
54 enum filterOpt
= GL_NEAREST
; //GL_LINEAR;
55 enum ttype
= GL_UNSIGNED_BYTE
;
57 if (vArrowTextureId
) glDeleteTextures(1, &vArrowTextureId
);
59 glGenTextures(1, &vArrowTextureId
);
60 if (vArrowTextureId
== 0) assert(0, "can't create arrow texture");
62 //GLint gltextbinding;
63 //glGetIntegerv(GL_TEXTURE_BINDING_2D, &gltextbinding);
64 //scope(exit) glBindTexture(GL_TEXTURE_2D, gltextbinding);
66 glBindTexture(GL_TEXTURE_2D
, vArrowTextureId
);
67 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapOpt
);
68 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapOpt
);
69 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, filterOpt
);
70 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, filterOpt
);
71 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
72 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
74 //GLfloat[4] bclr = 0.0;
75 //glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bclr.ptr);
77 uint[16*8] pmap
= 0x00_000000U;
78 // sprite,sprite,mask,mask
79 static immutable ushort[$] spx
= [
80 0b11111111_10000000, 0b00000000_01111111,
81 0b01000000_10000000, 0b10000000_01111111,
82 0b00100000_10000000, 0b11000000_01111111,
83 0b00010000_01100000, 0b11100000_00011111,
84 0b00001001_10011000, 0b11110000_00000111,
85 0b00000110_01100110, 0b11111001_10000001,
86 0b00000000_00011001, 0b11111111_11100000,
87 0b00000000_00000110, 0b11111111_11111001,
90 foreach (immutable dy
; 0..8) {
91 ushort spr
= spx
[dy
*2+0];
92 ushort msk
= spx
[dy
*2+1];
93 foreach (immutable dx
; 0..16) {
94 if ((msk
&0x8000) == 0) {
95 pmap
[dy
*16+dx
] = (spr
&0x8000 ?
0xff_ffffffU
: 0xff_000000U);
101 //pmap = 0xff_ff0000U;
104 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 16, 8, 0, GLTexType
, GL_UNSIGNED_BYTE
, pmap
.ptr
);
108 // ////////////////////////////////////////////////////////////////////////// //
109 public void vglBlitArrow (int px
, int py
) {
110 if (vArrowTextureId
!= 0) {
111 glMatrixMode(GL_PROJECTION
); // for ortho camera
113 // left, right, bottom, top, near, far
114 //glViewport(0, 0, w*vbufEffScale, h*vbufEffScale);
115 //glOrtho(0, w, h, 0, -1, 1); // top-to-bottom
116 glViewport(0, 0, VBufWidth
*vbufEffScale
, VBufHeight
*vbufEffScale
);
117 glOrtho(0, VBufWidth
, VBufHeight
, 0, -1, 1); // top-to-bottom
118 glMatrixMode(GL_MODELVIEW
);
121 glEnable(GL_TEXTURE_2D
);
122 glDisable(GL_LIGHTING
);
123 glDisable(GL_DITHER
);
124 glDisable(GL_DEPTH_TEST
);
127 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
128 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
129 //glColor4f(1, 1, 1, 1);
130 glBindTexture(GL_TEXTURE_2D
, vArrowTextureId
);
131 //scope(exit) glBindTexture(GL_TEXTURE_2D, 0);
133 glTexCoord2f(0.0f, 0.0f); glVertex2i(px
, py
); // top-left
134 glTexCoord2f(1.0f, 0.0f); glVertex2i(px
+16*2, py
); // top-right
135 glTexCoord2f(1.0f, 1.0f); glVertex2i(px
+16*2, py
+8*2); // bottom-right
136 glTexCoord2f(0.0f, 1.0f); glVertex2i(px
, py
+8*2); // bottom-left
142 // ////////////////////////////////////////////////////////////////////////// //
143 // resize buffer, reinitialize OpenGL texture
144 public void vglResizeBuffer (int wdt
, int hgt
, int ascale
=1) {
147 if (wdt
< 1) wdt
= 1;
148 if (hgt
< 1) hgt
= 1;
150 if (wdt
> 8192) wdt
= 8192;
151 if (hgt
> 8192) hgt
= 8192;
153 bool sizeChanged
= (wdt
!= VBufWidth || hgt
!= VBufHeight
);
157 if (vglTexBuf
is null || sizeChanged
) {
158 import core
.stdc
.stdlib
: realloc
;
159 vglTexBuf
= cast(uint*)realloc(vglTexBuf
, (VBufWidth
*VBufHeight
+4)*vglTexBuf
[0].sizeof
);
160 if (vglTexBuf
is null) { import core
.exception
: onOutOfMemoryErrorNoGC
; onOutOfMemoryErrorNoGC(); }
161 vglTexBuf
[0..VBufWidth
*VBufHeight
+4] = 0;
164 if (vglTexId
== 0 || sizeChanged
) {
165 enum wrapOpt
= GL_REPEAT
;
166 enum filterOpt
= GL_NEAREST
; //GL_LINEAR;
167 enum ttype
= GL_UNSIGNED_BYTE
;
169 if (vglTexId
) glDeleteTextures(1, &vglTexId
);
171 glGenTextures(1, &vglTexId
);
172 if (vglTexId
== 0) assert(0, "can't create OpenGL texture");
174 //GLint gltextbinding;
175 //glGetIntegerv(GL_TEXTURE_BINDING_2D, &gltextbinding);
176 //scope(exit) glBindTexture(GL_TEXTURE_2D, gltextbinding);
178 glBindTexture(GL_TEXTURE_2D
, vglTexId
);
179 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, wrapOpt
);
180 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, wrapOpt
);
181 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, filterOpt
);
182 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, filterOpt
);
183 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
184 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
186 //GLfloat[4] bclr = 0.0;
187 //glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bclr.ptr);
189 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, VBufWidth
, VBufHeight
, 0, GLTexType
, GL_UNSIGNED_BYTE
, vglTexBuf
);
192 if (ascale
< 1) ascale
= 1;
193 if (ascale
> 32) ascale
= 32;
194 vbufEffScale
= cast(ubyte)ascale
;
198 // ////////////////////////////////////////////////////////////////////////// //
199 public void vglUpdateTexture () {
201 glBindTexture(GL_TEXTURE_2D
, vglTexId
);
202 glTexSubImage2D(GL_TEXTURE_2D
, 0, 0/*x*/, 0/*y*/, VBufWidth
, VBufHeight
, GLTexType
, GL_UNSIGNED_BYTE
, vglTexBuf
);
203 //glBindTexture(GL_TEXTURE_2D, 0);
208 // ////////////////////////////////////////////////////////////////////////// //
209 public void vglBlitTexture () {
211 glMatrixMode(GL_PROJECTION
); // for ortho camera
213 // left, right, bottom, top, near, far
214 //glViewport(0, 0, w*vbufEffScale, h*vbufEffScale);
215 //glOrtho(0, w, h, 0, -1, 1); // top-to-bottom
216 glViewport(0, 0, VBufWidth
*vbufEffScale
, VBufHeight
*vbufEffScale
);
217 glOrtho(0, VBufWidth
, VBufHeight
, 0, -1, 1); // top-to-bottom
218 glMatrixMode(GL_MODELVIEW
);
221 glEnable(GL_TEXTURE_2D
);
222 glDisable(GL_LIGHTING
);
223 glDisable(GL_DITHER
);
224 //glDisable(GL_BLEND);
225 glDisable(GL_DEPTH_TEST
);
226 //glEnable(GL_BLEND);
227 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
228 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
230 //glDisable(GL_STENCIL_TEST);
233 immutable w
= VBufWidth
;
234 immutable h
= VBufHeight
;
236 glColor4f(1, 1, 1, 1);
237 glBindTexture(GL_TEXTURE_2D
, vglTexId
);
238 //scope(exit) glBindTexture(GL_TEXTURE_2D, 0);
240 glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0); // top-left
241 glTexCoord2f(1.0f, 0.0f); glVertex2i(w
, 0); // top-right
242 glTexCoord2f(1.0f, 1.0f); glVertex2i(w
, h
); // bottom-right
243 glTexCoord2f(0.0f, 1.0f); glVertex2i(0, h
); // bottom-left