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
This commit is contained in:
UnknownShadow200 2021-12-02 07:08:30 +11:00
parent c942d507d1
commit 0f5022d9bf
3 changed files with 29 additions and 4 deletions

View File

@ -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----------------------------------------------------*
*#########################################################################################################################*/

View File

@ -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; }

View File

@ -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;