From 0a07f4c144f29f7a7de48c0d7b1ec2cc0a50d1a2 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 22 Mar 2018 23:57:14 +0100 Subject: [PATCH] - fixed alpha texture generation for OpenGL rendering. The old logic used a translation table that does not work with color images, it was designed to handle 8 bit grayscale images. So now, it creates a true color buffer and then turns it into a texture with R,G,B = 255 and the alpha channel set to the grayscale value. This was also the reason why crosshairs made from 32 bit PNGs did not show correctly. --- src/gl/renderer/gl_renderstate.h | 2 +- src/gl/textures/gl_material.cpp | 51 ++++++++++++++++---------------- src/textures/pcxtexture.cpp | 1 - 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/gl/renderer/gl_renderstate.h b/src/gl/renderer/gl_renderstate.h index 4e844c9795..34a030f8a0 100644 --- a/src/gl/renderer/gl_renderstate.h +++ b/src/gl/renderer/gl_renderstate.h @@ -145,7 +145,7 @@ public: void SetMaterial(FMaterial *mat, int clampmode, int translation, int overrideshader, bool alphatexture) { // alpha textures need special treatment in the legacy renderer because withouz shaders they need a different texture. - if (alphatexture && gl.legacyMode) translation = TRANSLATION(TRANSLATION_Standard, 8); + if (alphatexture && gl.legacyMode) translation = INT_MAX; if (mat->tex->bHasCanvas) { diff --git a/src/gl/textures/gl_material.cpp b/src/gl/textures/gl_material.cpp index 98f0559d94..6ac7eb05ac 100644 --- a/src/gl/textures/gl_material.cpp +++ b/src/gl/textures/gl_material.cpp @@ -213,31 +213,27 @@ unsigned char * FGLTexture::CreateTexBuffer(int translation, int & w, int & h, F FBitmap bmp(buffer, W*4, W, H); - if (translation <= 0) + if (translation <= 0 || alphatrans) { - // Q: Is this special treatment still needed? Needs to be checked. - if (tex->bComplex) + int trans = tex->CopyTrueColorPixels(&bmp, exx, exx); + tex->CheckTrans(buffer, W*H, trans); + isTransparent = tex->gl_info.mIsTransparent; + if (bIsTransparent == -1) bIsTransparent = isTransparent; + // alpha texture for legacy mode + if (alphatrans) { - FBitmap imgCreate; - - // The texture contains special processing so it must be fully composited before being converted as a whole. - if (imgCreate.Create(W, H)) + for (int i = 0; i < W*H; i++) { - memset(imgCreate.GetPixels(), 0, W * H * 4); - int trans = tex->CopyTrueColorPixels(&imgCreate, exx, exx); - bmp.CopyPixelDataRGB(0, 0, imgCreate.GetPixels(), W, H, 4, W * 4, 0, CF_BGRA); - tex->CheckTrans(buffer, W*H, trans); - isTransparent = tex->gl_info.mIsTransparent; - if (bIsTransparent == -1) bIsTransparent = isTransparent; + int b = buffer[4 * i]; + int g = buffer[4 * i + 1]; + int r = buffer[4 * i + 2]; + int gray = Luminance(r, g, b); + buffer[4 * i] = 255; + buffer[4 * i + 1] = 255; + buffer[4 * i + 2] = 255; + buffer[4 * i + 3] = (buffer[4 * i + 3] * gray) >> 8; } } - else - { - int trans = tex->CopyTrueColorPixels(&bmp, exx, exx); - tex->CheckTrans(buffer, W*H, trans); - isTransparent = tex->gl_info.mIsTransparent; - if (bIsTransparent == -1) bIsTransparent = isTransparent; - } } else { @@ -282,13 +278,18 @@ FHardwareTexture *FGLTexture::CreateHwTexture() const FHardwareTexture *FGLTexture::Bind(int texunit, int clampmode, int translation, FTexture *hirescheck) { int usebright = false; - bool alphatrans = false; + bool alphatrans = translation == INT_MAX; // This is only needed for legacy mode because no texture combine setting allows using the color as alpha. - if (translation <= 0) translation = -translation; - else + if (!alphatrans) { - alphatrans = (gl.legacyMode && uint32_t(translation) == TRANSLATION(TRANSLATION_Standard, 8)); - translation = GLTranslationPalette::GetInternalTranslation(translation); + if (translation <= 0) + { + translation = -translation; + } + else + { + translation = GLTranslationPalette::GetInternalTranslation(translation); + } } bool needmipmap = (clampmode <= CLAMP_XY); diff --git a/src/textures/pcxtexture.cpp b/src/textures/pcxtexture.cpp index 4d7e9d94f2..1a9f73841b 100644 --- a/src/textures/pcxtexture.cpp +++ b/src/textures/pcxtexture.cpp @@ -126,7 +126,6 @@ FTexture * PCXTexture_TryCreate(FileReader & file, int lumpnum) if (hdr.version != 0 && hdr.version != 2 && hdr.version != 3 && hdr.version != 4 && hdr.version != 5) return NULL; if (hdr.bitsPerPixel != 1 && hdr.bitsPerPixel != 8 && hdr.bitsPerPixel != 4) return NULL; if (hdr.bitsPerPixel == 1 && hdr.numColorPlanes !=1 && hdr.numColorPlanes != 4) return NULL; - if (hdr.bitsPerPixel == 4 && hdr.numColorPlanes != 1) return NULL; if (hdr.bitsPerPixel == 8 && hdr.bytesPerScanLine != ((hdr.xmax - hdr.xmin + 2)&~1)) return NULL; for (int i = 0; i < 54; i++)