"r_vsync" console var; slightly faster light tracer
[dd2d.git] / glutils.d
blob1e2f5e811e27cca06763261879e78a8bc3e6b024
1 /* DooM2D: Midnight on the Firing Line
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, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 module glutils is aliced;
19 private:
20 import iv.glbinds;
21 import iv.vfs;
22 import arsd.color;
23 import arsd.png;
24 import iv.jpeg;
26 import wadarc;
29 // ////////////////////////////////////////////////////////////////////////// //
30 __gshared bool glutilsShowShaderWarnings = false; // shut up!
33 __gshared GLuint glLastUsedTexture = 0;
35 public void useTexture (GLuint tid) {
36 pragma(inline, true);
37 if (glLastUsedTexture != tid) {
38 glLastUsedTexture = tid;
39 glBindTexture(GL_TEXTURE_2D, tid);
43 public void useTexture (Texture tex) { pragma(inline, true); useTexture(tex !is null ? tex.tid : 0); }
47 public TrueColorImage loadPngFile (string fname) {
48 auto fl = VFile(fname);
49 auto sz = fl.size;
50 if (sz < 4 || sz > 32*1024*1024) throw new Exception("invalid png file size: '"~fname~"'");
51 if (sz == 0) return null;
52 auto res = new ubyte[](cast(uint)sz);
53 if (fl.rawRead(res[]).length != res.length) throw new Exception("error reading png file '"~fname~"'");
54 return imageFromPng(readPng(res)).getAsTrueColorImage;
58 // ////////////////////////////////////////////////////////////////////////// //
59 class OpenGLObject {
60 abstract @property uint id () const pure nothrow @nogc;
62 final void activate () nothrow @nogc {
63 if (gloSP >= gloStack.length) assert(0, "glo stack overflow");
64 gloStack.ptr[gloSP++] = this;
65 activateObj();
68 final void deactivate () nothrow @nogc {
69 foreach_reverse (usize idx; 0..gloStack.length) {
70 if (gloStack.ptr[idx] is this) {
71 // find previous object of this type
72 foreach_reverse (usize pidx; 0..idx) {
73 if (typeid(gloStack.ptr[pidx]) is typeid(gloStack.ptr[idx])) {
74 gloStack.ptr[pidx].activateObj();
75 removeFromStack(pidx);
76 return;
79 deactivateObj();
80 removeFromStack(idx);
81 return;
84 assert(0, "trying to deactivate inactive object");
87 protected:
88 abstract void activateObj () nothrow @nogc;
89 abstract void deactivateObj () nothrow @nogc;
93 __gshared OpenGLObject[1024] gloStack;
94 __gshared uint gloSP = 0;
97 public void gloStackClear () nothrow @nogc {
98 bindTexture(0);
99 //glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
100 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
101 glUseProgram(0);
105 void removeFromStack (usize idx) nothrow @nogc {
106 if (idx >= gloSP) return;
107 if (idx != gloSP-1) {
108 import core.stdc.string : memmove;
109 memmove(gloStack.ptr+idx, gloStack.ptr+idx+1, (gloSP-idx-1)*gloStack[0].sizeof);
111 --gloSP;
115 // ////////////////////////////////////////////////////////////////////////// //
116 public final class Texture : OpenGLObject {
117 GLuint tid;
118 int width, height;
120 override @property uint id () const pure nothrow @nogc => tid;
122 // default: repeat, linear
123 enum Option : int {
124 Repeat,
125 Clamp,
126 ClampBorder,
127 Linear,
128 Nearest,
129 UByte,
130 Float, // create floating point texture
131 Depth, // FBO: attach depth buffer
134 this (string fname, in Option[] opts...) { loadImage(fname, opts); }
135 this (int w, int h, in Option[] opts...) { createIntr(w, h, null, opts); }
136 this (TrueColorImage aimg, Option[] opts...) { createIntr(aimg.width, aimg.height, aimg, opts); }
137 ~this () { clear(); }
140 void clear () {
141 if (tid) {
142 //useTexture(tid);
143 bindTexture(tid);
144 glDeleteTextures(1, &tid);
145 //useTexture(0);
146 bindTexture(0);
147 tid = 0;
148 width = 0;
149 height = 0;
153 private static void processOpt (GLuint* wrapOpt, GLuint* filterOpt, GLuint* ttype, in Option[] opts...) {
154 foreach (immutable opt; opts) {
155 switch (opt) with (Option) {
156 case Repeat: *wrapOpt = GL_REPEAT; break;
157 case Clamp: *wrapOpt = GL_CLAMP_TO_EDGE; break;
158 case ClampBorder: *wrapOpt = GL_CLAMP_TO_BORDER; break;
159 case Linear: *filterOpt = GL_LINEAR; break;
160 case Nearest: *filterOpt = GL_NEAREST; break;
161 case UByte: *ttype = GL_UNSIGNED_BYTE; break;
162 case Float: *ttype = GL_FLOAT; break;
163 default:
168 void createIntr (int w, int h, TrueColorImage img, in Option[] opts...) {
169 import core.stdc.stdlib : malloc, free;
170 assert(w > 0);
171 assert(h > 0);
172 clear();
174 GLuint wrapOpt = GL_REPEAT;
175 GLuint filterOpt = GL_LINEAR;
176 GLuint ttype = GL_UNSIGNED_BYTE;
177 processOpt(&wrapOpt, &filterOpt, &ttype, opts);
179 glGenTextures(1, &tid);
180 //useTexture(tid);
181 auto oldtid = boundTexture;
182 bindTexture(tid);
183 scope(exit) bindTexture(oldtid);
184 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapOpt);
185 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapOpt);
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterOpt);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterOpt);
188 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
189 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
190 GLfloat[4] bclr = 0.0;
191 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, bclr.ptr);
192 if (img !is null && img.width == w && img.height == h) {
193 // create straight from image
194 glTexImage2D(GL_TEXTURE_2D, 0, (ttype == GL_FLOAT ? GL_RGBA16F : GL_RGBA), w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.imageData.bytes.ptr);
195 } else {
196 // create empty texture
197 ubyte* ptr = null;
198 scope(exit) if (ptr !is null) free(ptr);
200 import core.stdc.string : memset;
201 ptr = cast(ubyte*)malloc(w*h*4);
202 if (ptr !is null) memset(ptr, 0, w*h*4);
204 glTexImage2D(GL_TEXTURE_2D, 0, (ttype == GL_FLOAT ? GL_RGBA16F : GL_RGBA), w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
205 if (img !is null && img.width > 0 && img.height > 0) {
206 // setup from image
207 //TODO: dunno if it's ok to use images bigger than texture here
208 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, img.imageData.bytes.ptr);
209 // the following is ok too
210 //bindTexture(0);
211 //glTextureSubImage2D(tid, 0, 0, 0, img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, img.imageData.bytes.ptr);
214 width = w;
215 height = h;
218 void setFromImage (TrueColorImage img, int x=0, int y=0) {
219 if (img is null || !tid || img.height < 1 || img.width < 1) return;
220 if (x >= width || y >= height) return;
221 if (x+img.width <= 0 || y+img.height <= 0) return; //TODO: overflow
222 if (x >= 0 && y >= 0 && x+img.width <= width && y+img.height <= height) {
223 // easy case, just copy it
224 glTextureSubImage2D(tid, 0, x, y, img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, img.imageData.bytes.ptr);
225 } else {
226 import core.stdc.stdlib : malloc, free;
227 import core.stdc.string : memset, memcpy;
228 // hard case, have to build the temp region
229 uint* src = cast(uint*)img.imageData.bytes.ptr;
230 // calc x skip and effective width
231 int rwdt = img.width;
232 if (x < 0) {
233 rwdt += x;
234 src -= x; // as `x` is negative here
235 x = 0;
237 if (x+rwdt > width) rwdt = width-x;
238 // calc y skip and effective height
239 int rhgt = img.height;
240 if (y < 0) {
241 rhgt += y;
242 src -= y*img.width; // as `y` is negative here
243 y = 0;
245 if (y+rhgt > height) rhgt = height-y;
246 assert(rwdt > 0 && rhgt > 0);
247 uint* ptr = null;
248 scope(exit) if (ptr !is null) free(ptr);
249 ptr = cast(uint*)malloc(rwdt*rhgt*4);
250 if (ptr is null) assert(0, "out of memory in `Texture.setFromImage()`");
251 // now copy
252 auto d = ptr;
253 foreach (immutable _; 0..rhgt) {
254 memcpy(d, src, rwdt*4);
255 src += img.width;
256 d += rwdt;
258 glTextureSubImage2D(tid, 0, x, y, rwdt, rhgt, GL_RGBA, GL_UNSIGNED_BYTE, ptr);
262 void loadPng (VFile fl, in Option[] opts...) {
263 auto flsize = fl.size-fl.tell;
264 if (flsize < 8 || flsize > 1024*1024*32) throw new Exception("png image too big");
265 auto data = new ubyte[](cast(uint)flsize);
266 fl.rawReadExact(data);
267 auto png = readPng(data);
268 auto ximg = imageFromPng(png).getAsTrueColorImage;
269 if (ximg is null) throw new Exception("png: wtf?!");
270 if (ximg.width < 1 || ximg.height < 1) throw new Exception("png image too small");
271 createIntr(ximg.width, ximg.height, ximg, opts);
274 void loadJpeg (VFile fl, in Option[] opts...) {
275 auto jpg = readJpeg(fl);
276 if (jpg.width < 1 || jpg.width > 32760) throw new Exception("invalid image width");
277 if (jpg.height < 1 || jpg.height > 32760) throw new Exception("invalid image height");
278 createIntr(jpg.width, jpg.height, jpg, opts);
281 void loadImage (string fname, in Option[] opts...) {
282 scope(failure) clear;
283 auto fl = VFile(fname);
284 scope(failure) fl.seek(0);
285 char[8] sign;
286 fl.seek(0);
287 fl.rawReadExact(sign[]);
288 fl.seek(0);
289 // png?
290 if (sign == "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
291 loadPng(fl, opts);
292 return;
294 // jpeg?
295 if (sign[0..2] == "\xff\xd8" && detectJpeg(fl)) {
297 fl.seek(-2, Seek.End);
298 fl.rawReadExact(sign[0..2]);
299 fl.seek(0);
300 if (sign[0..2] == "\xff\xd9") {
301 loadJpeg(fl, opts);
302 return;
305 loadJpeg(fl, opts);
306 return;
308 throw new Exception("invalid texture image format");
311 protected:
312 override void activateObj () nothrow @nogc { /*if (tid)*/ bindTexture(tid); }
313 override void deactivateObj () nothrow @nogc { /*if (tid)*/ bindTexture(0); }
317 // ////////////////////////////////////////////////////////////////////////// //
318 public final class FBO : OpenGLObject {
319 int width;
320 int height;
321 GLuint fbo;
322 Texture tex;
323 Texture texdepth;
324 //Texture.Option[] xopts;
326 override @property uint id () const pure nothrow @nogc => fbo;
328 this (Texture atex) { createWithTexture(atex); }
330 this (int wdt, int hgt, Texture.Option[] opts...) {
331 //xopts = opts.dup;
332 createWithTexture(new Texture(wdt, hgt, opts), opts);
335 ~this () {
336 //FIXME: this may be wrong, as texture may be already destroyed (and it's wrong too); we need refcount for textures
337 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0);
338 glDeleteFramebuffersEXT(1, &fbo);
339 fbo = 0;
342 void clear () {
343 if (fbo) {
344 // detach texture
345 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0);
346 glDeleteFramebuffersEXT(1, &fbo);
347 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
348 glDeleteFramebuffersEXT(1, &fbo);
349 fbo = 0;
351 if (tex !is null) tex.clear();
352 tex = null;
353 if (texdepth !is null) texdepth.clear();
354 texdepth = null;
357 protected:
358 override void activateObj () nothrow @nogc { /*if (fbo)*/ glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); }
359 override void deactivateObj () nothrow @nogc { glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); }
361 // this will deactivate current FBO!
363 void replaceTexture (Texture ntex) {
364 if (tex !is null) {
365 if (ntex !is null && ntex.tid == tex.tid) return;
366 } else {
367 if (ntex is null) return;
369 glGenFramebuffersEXT(1, &fbo);
370 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
371 scope(exit) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
372 // detach texture
373 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0);
374 glDeleteFramebuffersEXT(1, &fbo);
375 fbo = 0;
376 tex = ntex;
377 // attach texture
378 if (tex !is null) {
379 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex.tid, 0);
381 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
382 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) assert(0, "framebuffer fucked!");
388 private:
389 void createWithTexture (Texture atex, Texture.Option[] opts...) {
390 assert(atex !is null && atex.tid);
392 tex = atex;
393 glGenFramebuffersEXT(1, &fbo);
394 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
395 scope(exit) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
396 // attach 2D texture to this FBO
397 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex.tid, 0);
398 // GL_COLOR_ATTACHMENT0_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_STENCIL_ATTACHMENT_EXT
400 void createDepth () {
401 uint fboDepthId;
402 glGenRenderbuffersEXT(1, &fboDepthId);
403 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboDepthId);
404 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT/*24*/, atex.width, atex.height);
405 // attach depth buffer to FBO
406 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fboDepthId);
407 // kill it (we don't need it anymore)
408 glDeleteRenderbuffersEXT(1, &fboDepthId);
412 foreach (Texture.Option opt; opts) {
413 if (opt == Texture.Option.Depth) {
414 createDepth();
415 break;
419 //createDepth();
422 GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
423 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) assert(0, "framebuffer fucked!");
425 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
427 width = tex.width;
428 height = tex.height;
433 // ////////////////////////////////////////////////////////////////////////// //
434 public struct SVec2I { int x, y; }
435 public struct SVec3I { int x, y, z; alias r = x; alias g = y; alias b = z; }
436 public struct SVec4I { int x, y, z, w; alias r = x; alias g = y; alias b = z; alias a = w; }
438 public struct SVec2F { float x, y; }
439 public struct SVec3F { float x, y, z; alias r = x; alias g = y; alias b = z; }
440 public struct SVec4F { float x, y, z, w; alias r = x; alias g = y; alias b = z; alias a = w; }
443 public final class Shader : OpenGLObject {
444 string shaderName;
445 GLuint prg = 0;
446 GLint[string] vars;
448 override @property uint id () const pure nothrow @nogc => prg;
450 this (string ashaderName, const(char)[] src) {
451 shaderName = ashaderName;
452 if (src.length > int.max) {
453 import core.stdc.stdio : printf;
454 printf("shader '%.*s' code too long!", cast(uint)ashaderName.length, ashaderName.ptr);
455 assert(0);
457 auto shaderId = glCreateShader(GL_FRAGMENT_SHADER);
458 auto sptr = src.ptr;
459 GLint slen = cast(int)src.length;
460 glShaderSource(shaderId, 1, &sptr, &slen);
461 glCompileShader(shaderId);
462 GLint success = 0;
463 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
464 if (!success || glutilsShowShaderWarnings) {
465 import core.stdc.stdio : printf;
466 import core.stdc.stdlib : malloc, free;
467 GLint logSize = 0;
468 glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logSize);
469 if (logSize > 0) {
470 auto logStrZ = cast(GLchar*)malloc(logSize);
471 glGetShaderInfoLog(shaderId, logSize, null, logStrZ);
472 printf("shader '%.*s' compilation messages:\n%s\n", cast(uint)ashaderName.length, ashaderName.ptr, logStrZ);
473 free(logStrZ);
476 if (!success) assert(0);
477 prg = glCreateProgram();
478 glAttachShader(prg, shaderId);
479 glLinkProgram(prg);
482 GLint varId(NT) (NT vname) if (is(NT == char[]) || is(NT == const(char)[]) || is(NT == immutable(char)[])) {
483 GLint id = -1;
484 if (vname.length > 0 && vname.length <= 128) {
485 if (auto vi = vname in vars) {
486 id = *vi;
487 } else {
488 char[129] buf = void;
489 buf[0..vname.length] = vname[];
490 buf[vname.length] = 0;
491 id = glGetUniformLocation(prg, buf.ptr);
492 //{ import core.stdc.stdio; printf("[%.*s.%s]=%i\n", cast(uint)shaderName.length, shaderName.ptr, buf.ptr, id); }
493 static if (is(NT == immutable(char)[])) {
494 vars[vname.idup] = id;
495 } else {
496 vars[vname.idup] = id;
498 if (id < 0) {
499 import core.stdc.stdio : printf;
500 printf("shader '%.*s': unknown variable '%.*s'\n", cast(uint)shaderName.length, shaderName.ptr, cast(uint)vname.length, vname.ptr);
504 return id;
507 // get unified var id
508 GLint opIndex(NT) (NT vname) if (is(NT == char[]) || is(NT == const(char)[]) || is(NT == immutable(char)[])) {
509 auto id = varId(vname);
510 if (id < 0) {
511 import core.stdc.stdio : printf;
512 printf("shader '%.*s': unknown variable '%.*s'\n", cast(uint)shaderName.length, shaderName.ptr, cast(uint)vname.length, vname.ptr);
513 assert(0);
515 return id;
518 private import std.traits;
519 void opIndexAssign(T, NT) (in auto ref T v, NT vname)
520 if (((isIntegral!T && T.sizeof <= 4) || (isFloatingPoint!T && T.sizeof == float.sizeof) || isBoolean!T ||
521 is(T : SVec2I) || is(T : SVec3I) || is(T : SVec4I) ||
522 is(T : SVec2F) || is(T : SVec3F) || is(T : SVec4F)) &&
523 (is(NT == char[]) || is(NT == const(char)[]) || is(NT == immutable(char)[])))
525 auto id = varId(vname);
526 if (id < 0) return;
527 //{ import core.stdc.stdio; printf("setting '%.*s' (%d)\n", cast(uint)vname.length, vname.ptr, id); }
528 static if (isIntegral!T || isBoolean!T) glUniform1i(id, cast(int)v);
529 else static if (isFloatingPoint!T) glUniform1f(id, cast(float)v);
530 else static if (is(SVec2I : T)) glUniform2i(id, cast(int)v.x, cast(int)v.y);
531 else static if (is(SVec3I : T)) glUniform3i(id, cast(int)v.x, cast(int)v.y, cast(int)v.z);
532 else static if (is(SVec4I : T)) glUniform4i(id, cast(int)v.x, cast(int)v.y, cast(int)v.z, cast(int)v.w);
533 else static if (is(SVec2F : T)) glUniform2f(id, cast(float)v.x, cast(float)v.y);
534 else static if (is(SVec3F : T)) glUniform3f(id, cast(float)v.x, cast(float)v.y, cast(float)v.z);
535 else static if (is(SVec4F : T)) glUniform4f(id, cast(float)v.x, cast(float)v.y, cast(float)v.z, cast(float)v.w);
536 else static assert(0, "wtf?!");
539 protected:
540 override void activateObj () nothrow @nogc { /*if (prg)*/ glUseProgram(prg); }
541 override void deactivateObj () nothrow @nogc { glUseProgram(0); }
545 // ////////////////////////////////////////////////////////////////////////// //
546 //private import std.traits;
548 public:
549 void exec(TO) (TO obj, scope void delegate () dg) if (is(typeof(() { obj.activate(); obj.deactivate(); }))) {
550 obj.activate();
551 scope(exit) obj.deactivate();
552 dg();
555 void exec(TO, TG) (TO obj, scope TG dg) if (is(typeof((TO obj) { dg(obj); })) && is(typeof(() { obj.activate(); obj.deactivate(); }))) {
556 obj.activate();
557 scope(exit) obj.deactivate();
558 dg(obj);
562 // ////////////////////////////////////////////////////////////////////////// //
563 void orthoCamera (int wdt, int hgt) {
564 glMatrixMode(GL_PROJECTION); // for ortho camera
565 glLoadIdentity();
566 // left, right, bottom, top, near, far
567 //glOrtho(0, wdt, 0, hgt, -1, 1); // bottom-to-top
568 glOrtho(0, wdt, hgt, 0, -1, 1); // top-to-bottom
569 glViewport(0, 0, wdt, hgt);
571 //glTranslatef(-cx, -cy, 0.0f);
574 // origin is texture left top
575 void drawAtXY (GLuint tid, int x, int y, int w, int h, bool mirrorX=false, bool mirrorY=false) {
576 if (!tid || w < 1 || h < 1) return;
577 w += x;
578 h += y;
579 if (mirrorX) { int tmp = x; x = w; w = tmp; }
580 if (mirrorY) { int tmp = y; y = h; h = tmp; }
581 bindTexture(tid);
582 glBegin(GL_QUADS);
583 glTexCoord2f(0.0f, 0.0f); glVertex2i(x, y); // top-left
584 glTexCoord2f(1.0f, 0.0f); glVertex2i(w, y); // top-right
585 glTexCoord2f(1.0f, 1.0f); glVertex2i(w, h); // bottom-right
586 glTexCoord2f(0.0f, 1.0f); glVertex2i(x, h); // bottom-left
587 glEnd();
591 // origin is texture center
592 void drawAtXYC (Texture tex, int x, int y, bool mirrorX=false, bool mirrorY=false) {
593 if (tex is null || !tex.tid) return;
594 x -= tex.width/2;
595 y -= tex.height/2;
596 drawAtXY(tex.tid, x, y, tex.width, tex.height, mirrorX, mirrorY);
600 // origin is texture left top
601 void drawAtXY (Texture tex, int x, int y, bool mirrorX=false, bool mirrorY=false) {
602 if (tex is null || !tex.tid) return;
603 drawAtXY(tex.tid, x, y, tex.width, tex.height, mirrorX, mirrorY);
607 private __gshared GLuint boundTexture = 0;
609 // make sure that texture unit 0 is active!
610 void bindTexture (GLuint tid) nothrow @trusted @nogc {
611 if (tid != boundTexture) {
612 boundTexture = tid;
613 glBindTexture(GL_TEXTURE_2D, tid);