From 0f5022d9bf8447c4a2e7dcd475df1b9a4cea1c49 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 2 Dec 2021 07:08:30 +1100 Subject: [PATCH] WebGL/OpenGL ES: Use dummy 1x1 pure white instead of pure black texture when no custom texture is bound Fixes world appearing black instead of shaded white when e.g. default.zip is missing, also missing skin, specifying a block face texture ID past end of terrain.png, etc --- src/Graphics_GL1.c | 8 ++++++++ src/Graphics_GL2.c | 21 +++++++++++++++++++++ src/_GLShared.h | 4 ---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Graphics_GL1.c b/src/Graphics_GL1.c index e604a9c98..da57956e9 100644 --- a/src/Graphics_GL1.c +++ b/src/Graphics_GL1.c @@ -341,6 +341,14 @@ void Gfx_SetDynamicVbData(GfxResourceID vb, void* vertices, int vCount) { #endif +/*########################################################################################################################* +*---------------------------------------------------------Textures--------------------------------------------------------* +*#########################################################################################################################*/ +void Gfx_BindTexture(GfxResourceID texId) { + glBindTexture(GL_TEXTURE_2D, (GLuint)texId); +} + + /*########################################################################################################################* *-----------------------------------------------------State management----------------------------------------------------* *#########################################################################################################################*/ diff --git a/src/Graphics_GL2.c b/src/Graphics_GL2.c index 4ef2de28d..a89d4f001 100644 --- a/src/Graphics_GL2.c +++ b/src/Graphics_GL2.c @@ -28,6 +28,7 @@ #include "_GLShared.h" /* Current format and size of vertices */ static int gfx_stride, gfx_format = -1; +static GfxResourceID white_square; /*########################################################################################################################* @@ -386,6 +387,19 @@ static void SwitchProgram(void) { } +/*########################################################################################################################* +*---------------------------------------------------------Textures--------------------------------------------------------* +*#########################################################################################################################*/ +void Gfx_BindTexture(GfxResourceID texId) { + /* Texture 0 has different behaviour depending on backend */ + /* Desktop OpenGL - pure white 1x1 texture */ + /* WebGL/OpenGL ES - pure black 1x1 texture */ + /* So for consistency, always use a 1x1 pure white texture */ + if (!texId) texId = white_square; + glBindTexture(GL_TEXTURE_2D, (GLuint)texId); +} + + /*########################################################################################################################* *-----------------------------------------------------State management----------------------------------------------------* *#########################################################################################################################*/ @@ -483,6 +497,7 @@ static void Gfx_FreeState(void) { glDeleteProgram(shaders[i].program); shaders[i].program = 0; } + Gfx_DeleteTexture(&white_square); } static void Gfx_RestoreState(void) { @@ -495,6 +510,12 @@ static void Gfx_RestoreState(void) { GL_ClearColor(gfx_clearColor); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthFunc(GL_LEQUAL); + + /* 1x1 dummy white texture */ + struct Bitmap bmp; + BitmapCol pixels[1] = { BITMAPCOL_WHITE }; + Bitmap_Init(bmp, 1, 1, pixels); + Gfx_RecreateTexture(&white_square, &bmp, 0, false); } cc_bool Gfx_WarnIfNecessary(void) { return false; } diff --git a/src/_GLShared.h b/src/_GLShared.h index 231744a60..18b80234f 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -152,10 +152,6 @@ void Gfx_UpdateTexturePart(GfxResourceID texId, int x, int y, struct Bitmap* par Gfx_UpdateTexture(texId, x, y, part, part->width, mipmaps); } -void Gfx_BindTexture(GfxResourceID texId) { - glBindTexture(GL_TEXTURE_2D, (GLuint)texId); -} - void Gfx_DeleteTexture(GfxResourceID* texId) { GLuint id = (GLuint)(*texId); if (!id) return;