2013-06-27 23:05:11 +00:00
|
|
|
|
2014-02-10 10:56:14 +00:00
|
|
|
#include "baselayer.h"
|
2013-05-15 02:17:17 +00:00
|
|
|
#include "build.h"
|
2014-02-10 10:55:49 +00:00
|
|
|
#include "lz4.h"
|
2013-05-15 02:17:17 +00:00
|
|
|
#include "hightile.h"
|
|
|
|
#include "polymost.h"
|
|
|
|
#include "scriptfile.h"
|
2014-03-22 09:26:39 +00:00
|
|
|
#include "xxhash.h"
|
2014-09-30 04:18:43 +00:00
|
|
|
#include "kplib.h"
|
2013-05-15 02:17:17 +00:00
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#include "vfs.h"
|
2019-10-10 21:29:13 +00:00
|
|
|
#include "textures.h"
|
|
|
|
#include "bitmap.h"
|
|
|
|
#include "../../glbackend/glbackend.h"
|
2019-03-01 08:51:50 +00:00
|
|
|
|
2019-10-18 09:37:07 +00:00
|
|
|
// External CVARs.
|
2019-10-17 22:20:27 +00:00
|
|
|
extern int r_detailmapping, r_glowmapping, usehightile, r_useindexedcolortextures;
|
|
|
|
extern int fixpalette, fixpalswap;
|
|
|
|
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
template<class T>
|
|
|
|
void FlipNonSquareBlock(T* dst, const T* src, int x, int y, int srcpitch)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < x; ++i)
|
2019-10-17 19:44:34 +00:00
|
|
|
{
|
2019-10-17 22:20:27 +00:00
|
|
|
for (int j = 0; j < y; ++j)
|
2019-10-17 19:44:34 +00:00
|
|
|
{
|
2019-10-17 22:20:27 +00:00
|
|
|
dst[i * y + j] = src[i + j * srcpitch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Create an indexed version of the requested texture
|
|
|
|
//
|
|
|
|
//===========================================================================
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-18 09:37:07 +00:00
|
|
|
FHardwareTexture* GLInstance::CreateIndexedTexture(FTexture* tex)
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
|
|
|
auto siz = tex->GetSize();
|
|
|
|
bool npoty = false;
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
const uint8_t* p = tex->Get8BitPixels();
|
|
|
|
TArray<uint8_t> store(siz.x * siz.y, true);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
tex->Create8BitPixels(store.Data());
|
|
|
|
p = store.Data();
|
2019-10-17 19:44:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
auto glpic = GLInterface.NewTexture();
|
|
|
|
glpic->CreateTexture(siz.x, siz.y, true, false);
|
|
|
|
|
|
|
|
TArray<uint8_t> flipped(siz.x * siz.y, true);
|
|
|
|
FlipNonSquareBlock(flipped.Data(), p, siz.y, siz.x, siz.y);
|
|
|
|
glpic->LoadTexture(flipped.Data());
|
|
|
|
return glpic;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
2019-10-18 09:37:07 +00:00
|
|
|
//
|
|
|
|
// Create a true color version of the requested tile
|
2019-10-17 22:20:27 +00:00
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2019-10-18 09:37:07 +00:00
|
|
|
FHardwareTexture* GLInstance::CreateTrueColorTexture(FTexture* tex, int palid, bool checkfulltransparency)
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
2019-10-18 09:37:07 +00:00
|
|
|
auto siz = tex->GetSize();
|
|
|
|
bool npoty = false;
|
|
|
|
|
|
|
|
auto palette = palid < 0? nullptr : palmanager.GetPaletteData(palid);
|
|
|
|
auto texbuffer = tex->CreateTexBuffer(palette, CTF_ProcessData);
|
|
|
|
// Check if the texture is fully transparent. When creating a brightmap such textures can be discarded.
|
|
|
|
if (checkfulltransparency)
|
2019-10-17 19:44:34 +00:00
|
|
|
{
|
2019-10-18 09:37:07 +00:00
|
|
|
int siz = texbuffer.mWidth * texbuffer.mHeight * 4;
|
|
|
|
bool found = false;
|
|
|
|
for (int i = 3; i < siz; i+=4)
|
2019-10-17 19:44:34 +00:00
|
|
|
{
|
2019-10-18 09:37:07 +00:00
|
|
|
if (texbuffer.mBuffer[i] > 0)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-17 19:44:34 +00:00
|
|
|
}
|
2019-10-18 09:37:07 +00:00
|
|
|
if (!found) return nullptr;
|
2019-10-17 19:44:34 +00:00
|
|
|
}
|
2019-10-18 09:37:07 +00:00
|
|
|
|
|
|
|
auto glpic = GLInterface.NewTexture();
|
|
|
|
glpic->CreateTexture(texbuffer.mWidth, texbuffer.mHeight, false, true);
|
|
|
|
glpic->LoadTexture(texbuffer.mBuffer);
|
|
|
|
return glpic;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Retrieve the texture to be used.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
FHardwareTexture* GLInstance::LoadTexture(FTexture* tex, int textype, int palid)
|
|
|
|
{
|
|
|
|
if (textype == TT_INDEXED) palid = -1;
|
|
|
|
auto phwtex = tex->GetHardwareTexture(palid);
|
|
|
|
if (phwtex) return *phwtex;
|
|
|
|
|
|
|
|
FHardwareTexture *hwtex;
|
|
|
|
if (textype == TT_INDEXED)
|
2019-10-18 17:06:57 +00:00
|
|
|
hwtex = CreateIndexedTexture(tex);
|
2019-10-18 09:37:07 +00:00
|
|
|
else
|
2019-10-18 17:06:57 +00:00
|
|
|
hwtex = CreateTrueColorTexture(tex, textype == TT_HICREPLACE? -1 : palid, textype == TT_BRIGHTMAP);
|
2019-10-18 09:37:07 +00:00
|
|
|
|
|
|
|
tex->SetHardwareTexture(palid, hwtex);
|
|
|
|
return hwtex;
|
2019-10-17 22:20:27 +00:00
|
|
|
}
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
2019-10-18 12:04:32 +00:00
|
|
|
// Sets a texture for rendering. This should be the ONLY place to bind in-game textures
|
2019-10-17 22:20:27 +00:00
|
|
|
//
|
|
|
|
//===========================================================================
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-18 12:04:32 +00:00
|
|
|
bool GLInstance::SetTextureInternal(FTexture* tex, int palette, int method, int sampleroverride, float xpanning, float ypanning, FTexture *det, float detscale, FTexture *glow)
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
2019-10-18 17:29:35 +00:00
|
|
|
if (tex->GetWidth() <= 0 || tex->GetHeight() <= 0) return false;
|
2019-10-17 22:20:27 +00:00
|
|
|
int usepalette = fixpalette >= 1 ? fixpalette - 1 : curbasepal;
|
|
|
|
int usepalswap = fixpalswap >= 1 ? fixpalswap - 1 : palette;
|
|
|
|
GLInterface.SetPalette(usepalette);
|
|
|
|
GLInterface.SetPalswap(usepalswap);
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
TextureType = r_useindexedcolortextures? TT_INDEXED : TT_TRUECOLOR;
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
int lookuppal = 0;
|
|
|
|
VSMatrix texmat;
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-18 17:06:57 +00:00
|
|
|
auto rep = usehightile? tex->FindReplacement(palette) : nullptr;
|
2019-10-17 22:20:27 +00:00
|
|
|
if (rep)
|
|
|
|
{
|
|
|
|
// Hightile replacements have only one texture representation and it is always the base.
|
|
|
|
tex = rep->faces[0];
|
|
|
|
TextureType = TT_HICREPLACE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Only look up the palette if we really want to use it (i.e. when creating a true color texture of an ART tile.)
|
2019-10-18 09:37:07 +00:00
|
|
|
if (!r_useindexedcolortextures) lookuppal = palmanager.LookupPalette(usepalette, usepalswap, false);
|
2019-10-17 22:20:27 +00:00
|
|
|
}
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
// Load the main texture
|
|
|
|
auto mtex = LoadTexture(tex, TextureType, lookuppal);
|
|
|
|
if (mtex)
|
|
|
|
{
|
|
|
|
auto sampler = (method & DAMETH_CLAMPED) ? (sampleroverride != -1 ? sampleroverride : SamplerClampXY) : SamplerRepeat;
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
BindTexture(0, mtex, sampler);
|
2019-10-18 12:04:32 +00:00
|
|
|
if (rep && (rep->scale.x != 1.0f || rep->scale.y != 1.0f || xpanning != 0 || ypanning != 0))
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
|
|
|
texmat.loadIdentity();
|
2019-10-18 12:04:32 +00:00
|
|
|
texmat.translate(xpanning, ypanning, 0);
|
2019-10-17 22:20:27 +00:00
|
|
|
texmat.scale(rep->scale.x, rep->scale.y, 1.0f);
|
|
|
|
GLInterface.SetMatrix(Matrix_Texture, &texmat);
|
|
|
|
MatrixChange |= 1;
|
|
|
|
}
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
// Also load additional layers needed for this texture.
|
|
|
|
if (r_detailmapping && usehightile)
|
|
|
|
{
|
2019-10-18 12:04:32 +00:00
|
|
|
float detscalex = detscale, detscaley = detscale;
|
|
|
|
if (!(method & DAMETH_MODEL))
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
2019-10-18 17:06:57 +00:00
|
|
|
auto drep = tex->FindReplacement(DETAILPAL);
|
2019-10-18 12:04:32 +00:00
|
|
|
if (drep)
|
|
|
|
{
|
|
|
|
det = drep->faces[0];
|
|
|
|
detscalex = drep->scale.x;
|
|
|
|
detscaley = drep->scale.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (det)
|
|
|
|
{
|
|
|
|
auto htex = LoadTexture(det, TT_HICREPLACE, 0);
|
2019-10-17 22:20:27 +00:00
|
|
|
UseDetailMapping(true);
|
|
|
|
BindTexture(3, htex, SamplerRepeat);
|
|
|
|
|
2019-10-18 12:04:32 +00:00
|
|
|
// Q: Pass the scale factor as a separate uniform to get rid of the additional matrix?
|
|
|
|
if (MatrixChange & 1) MatrixChange |= 2;
|
|
|
|
else texmat.loadIdentity();
|
2019-10-17 22:20:27 +00:00
|
|
|
|
2019-10-18 12:04:32 +00:00
|
|
|
if ((detscalex != 1.0f) || (detscaley != 1.0f))
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
2019-10-18 12:04:32 +00:00
|
|
|
texmat.scale(detscalex, detscaley, 1.0f);
|
2019-10-17 22:20:27 +00:00
|
|
|
MatrixChange |= 2;
|
|
|
|
}
|
|
|
|
if (MatrixChange & 2) GLInterface.SetMatrix(Matrix_Detail, &texmat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r_glowmapping && usehightile)
|
|
|
|
{
|
2019-10-18 12:04:32 +00:00
|
|
|
if (!(method & DAMETH_MODEL))
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
2019-10-18 17:06:57 +00:00
|
|
|
auto drep = tex->FindReplacement(DETAILPAL);
|
2019-10-18 12:04:32 +00:00
|
|
|
if (drep)
|
|
|
|
{
|
|
|
|
glow = drep->faces[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (glow)
|
|
|
|
{
|
|
|
|
auto htex = LoadTexture(glow, TT_HICREPLACE, 0);
|
2019-10-17 22:20:27 +00:00
|
|
|
UseGlowMapping(true);
|
|
|
|
BindTexture(4, htex, SamplerRepeat);
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 09:37:07 +00:00
|
|
|
if (!(tex->PicAnim.sf & PICANM_NOFULLBRIGHT_BIT) && !(globalflags & GLOBAL_NO_GL_FULLBRIGHT))
|
2019-10-17 22:20:27 +00:00
|
|
|
{
|
2019-10-18 09:37:07 +00:00
|
|
|
if (TextureType == TT_HICREPLACE)
|
|
|
|
{
|
2019-10-18 17:06:57 +00:00
|
|
|
auto brep = tex->FindReplacement(BRIGHTPAL);
|
2019-10-18 09:37:07 +00:00
|
|
|
if (brep)
|
|
|
|
{
|
|
|
|
auto htex = LoadTexture(brep->faces[0], TT_HICREPLACE, 0);
|
|
|
|
// UseBrightmapping(true);
|
|
|
|
BindTexture(5, mtex, sampler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (TextureType == TT_TRUECOLOR)
|
|
|
|
{
|
|
|
|
lookuppal = palmanager.LookupPalette(usepalette, usepalswap, true);
|
|
|
|
if (lookuppal >= 0)
|
|
|
|
{
|
|
|
|
auto htex = LoadTexture(tex, TT_BRIGHTMAP, lookuppal);
|
|
|
|
// UseBrightmapping(true);
|
|
|
|
BindTexture(5, mtex, sampler);
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 22:20:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-18 17:06:57 +00:00
|
|
|
else return false;
|
2019-10-17 19:44:34 +00:00
|
|
|
|
2019-10-17 22:20:27 +00:00
|
|
|
float al = 0;
|
|
|
|
if (TextureType == TT_HICREPLACE)
|
|
|
|
{
|
|
|
|
al = /*alphahackarray[globalpicnum] != 0 ? alphahackarray[globalpicnum] * (1.f / 255.f) :*/
|
|
|
|
(tex->alphaThreshold >= 0.f ? tex->alphaThreshold : 0.f);
|
|
|
|
}
|
2019-10-17 19:44:34 +00:00
|
|
|
GLInterface.SetAlphaThreshold(al);
|
2019-10-18 17:06:57 +00:00
|
|
|
return true;
|
2019-10-17 19:44:34 +00:00
|
|
|
}
|
2017-06-27 02:24:34 +00:00
|
|
|
|
2013-05-15 02:18:27 +00:00
|
|
|
|