From 0eecce227e4c0b1c7267b6213213fb8b399d5f0b Mon Sep 17 00:00:00 2001 From: Spoike Date: Tue, 5 Oct 2021 05:05:06 +0000 Subject: [PATCH] Try to fix alpha blend issues with the webgl port. git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@6065 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- engine/gl/gl_draw.c | 2 +- engine/web/ftejslib.h | 2 +- engine/web/ftejslib.js | 24 +++++++++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/engine/gl/gl_draw.c b/engine/gl/gl_draw.c index 71a2da6d5..031c8ea54 100644 --- a/engine/gl/gl_draw.c +++ b/engine/gl/gl_draw.c @@ -908,7 +908,7 @@ qboolean GL_LoadTextureMips(texid_t tex, const struct pendingtextureinfo *mips) #ifdef FTE_TARGET_WEB if (encoding == PTI_WHOLEFILE) { - emscriptenfte_gl_loadtexturefile(tex->num, &tex->width, &tex->height, mips->mip[0].data, mips->mip[0].datasize, tex->ident); + emscriptenfte_gl_loadtexturefile(tex->num, &tex->width, &tex->height, mips->mip[0].data, mips->mip[0].datasize, tex->ident, !!(tex->flags & IF_PREMULTIPLYALPHA), !(tex->flags & IF_NOMIPMAP)); return true; } #endif diff --git a/engine/web/ftejslib.h b/engine/web/ftejslib.h index 9aaf18e33..d6d35f540 100644 --- a/engine/web/ftejslib.h +++ b/engine/web/ftejslib.h @@ -36,7 +36,7 @@ NORETURN void emscriptenfte_abortmainloop(const char *caller); //we're trying to avoid including libpng+libjpeg+libogg in javascript due to it being redundant bloat. //to use such textures/sounds, we can just 'directly' load them via webgl -void emscriptenfte_gl_loadtexturefile(int gltexid, int *width, int *height, void *data, int datasize, const char *fname); +void emscriptenfte_gl_loadtexturefile(int gltexid, int *width, int *height, void *data, int datasize, const char *fname, int premul, int genmips); void emscriptenfte_al_loadaudiofile(int al_buf, void *data, int datasize); //avoid all of emscripten's sdl emulation. diff --git a/engine/web/ftejslib.js b/engine/web/ftejslib.js index c3a2f736e..0e09c9a1d 100644 --- a/engine/web/ftejslib.js +++ b/engine/web/ftejslib.js @@ -519,20 +519,21 @@ mergeInto(LibraryManager.library, emscriptenfte_abortmainloop : function(fname) { fname = Pointer_stringify(fname); + FTEC.aborted = true; throw 'oh noes! something bad happened in ' + fname + '!\n' + Module['stackTrace'](); }, emscriptenfte_setupmainloop : function(fnc) { Module['noExitRuntime'] = true; + FTEC.aborted = false; //Module.abort = abort = function(msg) {}; - - Module["sched"] = function() + function step(timestamp) { var dovsync = false; var vr = false; - if (ABORT) + if (FTE.aborted) return; if (FTEC.vrDisplay) @@ -554,15 +555,15 @@ mergeInto(LibraryManager.library, if (dovsync) { if (FTEC.vrDisplay) - FTEC.vrDisplay.requestAnimationFrame(Module["sched"]); + FTEC.vrDisplay.requestAnimationFrame(step); else - Browser.requestAnimationFrame(Module["sched"]); + Browser.requestAnimationFrame(step); } else - setTimeout(Module["sched"], 0); + setTimeout(step, 0); }; //don't start it instantly, so we can distinguish between types of errors (emscripten sucks!). - setTimeout(Module["sched"], 1); + setTimeout(step, 1); }, emscriptenfte_ticks_ms : function() @@ -1124,7 +1125,7 @@ console.log("onerror: " + _url); } }, - emscriptenfte_gl_loadtexturefile : function(texid, widthptr, heightptr, dataptr, datasize, fname) + emscriptenfte_gl_loadtexturefile : function(texid, widthptr, heightptr, dataptr, datasize, fname, dopremul, genmips) { function encode64(data) { var BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; @@ -1166,8 +1167,13 @@ console.log("onerror: " + _url); } var oldtex = GLctx.getParameter(GLctx.TEXTURE_BINDING_2D); //blurgh, try to avoid breaking anything in this unexpected event. GLctx.bindTexture(GLctx.TEXTURE_2D, gltex); + if (dopremul) + GLctx.pixelStorei(GLctx.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true); GLctx.texImage2D(GLctx.TEXTURE_2D, 0, GLctx.RGBA, GLctx.RGBA, GLctx.UNSIGNED_BYTE, img); - GLctx.generateMipmap(GLctx.TEXTURE_2D); + if (dopremul) + GLctx.pixelStorei(GLctx.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); + if (genmips) + GLctx.generateMipmap(GLctx.TEXTURE_2D); GLctx.bindTexture(GLctx.TEXTURE_2D, oldtex); }; img.crossorigin = true;