2016-03-01 15:47:10 +00:00
|
|
|
/*
|
|
|
|
** texture.cpp
|
|
|
|
** The base texture class
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2004-2007 Randy Heit
|
2018-03-31 17:20:59 +00:00
|
|
|
** Copyright 2006-2018 Christoph Oelckers
|
2016-03-01 15:47:10 +00:00
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2020-04-11 17:10:22 +00:00
|
|
|
#include "printf.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "files.h"
|
2020-04-11 11:36:23 +00:00
|
|
|
#include "filesystem.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "templates.h"
|
2020-04-11 17:00:07 +00:00
|
|
|
#include "textures.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "bitmap.h"
|
|
|
|
#include "colormatcher.h"
|
|
|
|
#include "c_dispatch.h"
|
|
|
|
#include "m_fixed.h"
|
2018-12-08 19:44:28 +00:00
|
|
|
#include "imagehelpers.h"
|
2018-12-09 06:39:05 +00:00
|
|
|
#include "image.h"
|
2018-12-15 15:05:48 +00:00
|
|
|
#include "formats/multipatchtexture.h"
|
2020-04-11 17:00:07 +00:00
|
|
|
#include "texturemanager.h"
|
2020-04-14 21:19:11 +00:00
|
|
|
#include "c_cvars.h"
|
2020-04-11 17:00:07 +00:00
|
|
|
|
2020-04-16 19:36:14 +00:00
|
|
|
EXTERN_CVAR(Int, gl_texture_hqresize_targets)
|
|
|
|
|
2020-04-11 17:00:07 +00:00
|
|
|
// Wrappers to keep the definitions of these classes out of here.
|
|
|
|
void DeleteMaterial(FMaterial* mat);
|
2020-04-11 17:07:08 +00:00
|
|
|
IHardwareTexture* CreateHardwareTexture();
|
2020-04-11 17:00:07 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-12-09 06:39:05 +00:00
|
|
|
FTexture *CreateBrightmapTexture(FImageSource*);
|
2018-03-31 17:20:59 +00:00
|
|
|
|
2018-03-31 08:37:46 +00:00
|
|
|
// Make sprite offset adjustment user-configurable per renderer.
|
|
|
|
int r_spriteadjustSW, r_spriteadjustHW;
|
|
|
|
|
2018-03-31 17:20:59 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// Examines the lump contents to decide what type of texture to create,
|
|
|
|
// and creates the texture.
|
2020-04-17 17:48:24 +00:00
|
|
|
FTexture * FTexture::CreateTexture(const char *name, int lumpnum, bool allowflats)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-12-09 16:10:51 +00:00
|
|
|
if (lumpnum == -1) return nullptr;
|
|
|
|
|
2020-04-17 17:48:24 +00:00
|
|
|
auto image = FImageSource::GetImage(lumpnum, allowflats);
|
2018-12-09 16:10:51 +00:00
|
|
|
if (image != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-12-09 16:10:51 +00:00
|
|
|
FTexture *tex = new FImageTexture(image);
|
|
|
|
if (tex != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-12-09 16:10:51 +00:00
|
|
|
tex->Name = name;
|
|
|
|
tex->Name.ToUpper();
|
|
|
|
return tex;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-09 16:10:51 +00:00
|
|
|
return nullptr;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 17:20:59 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
FTexture::FTexture (const char *name, int lumpnum)
|
2018-03-31 08:37:46 +00:00
|
|
|
:
|
2018-12-07 01:13:11 +00:00
|
|
|
Scale(1,1), SourceLump(lumpnum),
|
2020-04-16 22:37:05 +00:00
|
|
|
bNoDecals(false), bNoRemap0(false), bWorldPanning(false),
|
2020-04-12 20:13:15 +00:00
|
|
|
bMasked(true), bAlphaTexture(false), bHasCanvas(false), bWarped(0), bComplex(false), bMultiPatch(false), bFullNameTexture(false),
|
2018-12-07 01:13:11 +00:00
|
|
|
Rotations(0xFFFF), SkyOffset(0), Width(0), Height(0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-03-31 17:20:59 +00:00
|
|
|
bBrightmapChecked = false;
|
|
|
|
bGlowing = false;
|
2018-04-02 11:00:33 +00:00
|
|
|
bAutoGlowing = false;
|
|
|
|
bFullbright = false;
|
|
|
|
bDisableFullbright = false;
|
|
|
|
bSkybox = false;
|
|
|
|
bNoCompress = false;
|
2018-03-31 17:20:59 +00:00
|
|
|
bTranslucent = -1;
|
|
|
|
|
|
|
|
|
2018-03-31 08:37:46 +00:00
|
|
|
_LeftOffset[0] = _LeftOffset[1] = _TopOffset[0] = _TopOffset[1] = 0;
|
2016-03-01 15:47:10 +00:00
|
|
|
if (name != NULL)
|
|
|
|
{
|
|
|
|
Name = name;
|
|
|
|
Name.ToUpper();
|
|
|
|
}
|
|
|
|
else if (lumpnum < 0)
|
|
|
|
{
|
|
|
|
Name = FString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-11 11:27:19 +00:00
|
|
|
fileSystem.GetFileShortName (Name, lumpnum);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FTexture::~FTexture ()
|
|
|
|
{
|
2018-03-31 17:20:59 +00:00
|
|
|
if (areas != nullptr) delete[] areas;
|
|
|
|
areas = nullptr;
|
2016-06-10 11:50:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
2018-12-10 01:35:10 +00:00
|
|
|
// FTexture::GetBgraBitmap
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
2018-12-10 01:35:10 +00:00
|
|
|
// Default returns just an empty bitmap. This needs to be overridden by
|
|
|
|
// any subclass that actually does return a software pixel buffer.
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-04-11 17:08:22 +00:00
|
|
|
FBitmap FTexture::GetBgraBitmap(const PalEntry *remap, int *ptrans)
|
2018-12-08 13:06:16 +00:00
|
|
|
{
|
|
|
|
FBitmap bmp;
|
2018-12-10 01:35:10 +00:00
|
|
|
bmp.Create(Width, Height);
|
2018-12-08 13:06:16 +00:00
|
|
|
return bmp;
|
|
|
|
}
|
|
|
|
|
2020-04-11 16:54:54 +00:00
|
|
|
void FTexture::SetDisplaySize(int fitwidth, int fitheight)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-03-26 12:37:44 +00:00
|
|
|
Scale.X = double(Width) / fitwidth;
|
|
|
|
Scale.Y =double(Height) / fitheight;
|
2016-03-01 15:47:10 +00:00
|
|
|
// compensate for roundoff errors
|
2016-03-26 12:37:44 +00:00
|
|
|
if (int(Scale.X * fitwidth) != Width) Scale.X += (1 / 65536.);
|
|
|
|
if (int(Scale.Y * fitheight) != Height) Scale.Y += (1 / 65536.);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 22:08:22 +00:00
|
|
|
//====================================================================
|
|
|
|
//
|
|
|
|
// CheckRealHeight
|
|
|
|
//
|
|
|
|
// Checks the posts in a texture and returns the lowest row (plus one)
|
|
|
|
// of the texture that is actually used.
|
|
|
|
//
|
|
|
|
//====================================================================
|
|
|
|
|
|
|
|
int FTexture::CheckRealHeight()
|
|
|
|
{
|
2018-12-08 11:42:35 +00:00
|
|
|
auto pixels = Get8BitPixels(false);
|
2018-12-06 00:11:04 +00:00
|
|
|
|
2020-04-11 16:20:23 +00:00
|
|
|
for(int h = GetTexelHeight()-1; h>= 0; h--)
|
2017-03-17 22:08:22 +00:00
|
|
|
{
|
2020-04-11 16:20:23 +00:00
|
|
|
for(int w = 0; w < GetTexelWidth(); w++)
|
2017-03-17 22:08:22 +00:00
|
|
|
{
|
2020-04-11 16:20:23 +00:00
|
|
|
if (pixels[h + w * GetTexelHeight()] != 0)
|
2017-03-17 22:08:22 +00:00
|
|
|
{
|
2018-12-06 00:11:04 +00:00
|
|
|
// Scale maxy before returning it
|
|
|
|
h = int((h * 2) / Scale.Y);
|
|
|
|
h = (h >> 1) + (h & 1);
|
|
|
|
return h;
|
2017-03-17 22:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 00:11:04 +00:00
|
|
|
return 0;
|
2017-03-17 22:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 17:20:59 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Search auto paths for extra material textures
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2020-04-16 23:03:21 +00:00
|
|
|
void FGameTexture::AddAutoMaterials()
|
2018-03-31 17:20:59 +00:00
|
|
|
{
|
|
|
|
struct AutoTextureSearchPath
|
|
|
|
{
|
|
|
|
const char *path;
|
2020-04-16 23:03:21 +00:00
|
|
|
RefCountedPtr<FTexture> FGameTexture::*pointer;
|
2018-03-31 17:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static AutoTextureSearchPath autosearchpaths[] =
|
|
|
|
{
|
2020-04-16 23:03:21 +00:00
|
|
|
{ "brightmaps/", &FGameTexture::Brightmap }, // For backwards compatibility, only for short names
|
|
|
|
{ "materials/brightmaps/", &FGameTexture::Brightmap },
|
|
|
|
{ "materials/normalmaps/", &FGameTexture::Normal },
|
|
|
|
{ "materials/specular/", &FGameTexture::Specular },
|
|
|
|
{ "materials/metallic/", &FGameTexture::Metallic },
|
|
|
|
{ "materials/roughness/", &FGameTexture::Roughness },
|
|
|
|
{ "materials/ao/", &FGameTexture::AmbientOcclusion }
|
2018-03-31 17:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-04-16 23:03:21 +00:00
|
|
|
int startindex = Base->bFullNameTexture ? 1 : 0;
|
|
|
|
FString searchname = GetName();
|
2018-03-31 17:20:59 +00:00
|
|
|
|
2020-04-16 23:03:21 +00:00
|
|
|
if (Base->bFullNameTexture)
|
2018-03-31 17:20:59 +00:00
|
|
|
{
|
|
|
|
auto dot = searchname.LastIndexOf('.');
|
|
|
|
auto slash = searchname.LastIndexOf('/');
|
|
|
|
if (dot > slash) searchname.Truncate(dot);
|
|
|
|
}
|
2017-03-17 22:08:22 +00:00
|
|
|
|
2018-04-08 09:53:41 +00:00
|
|
|
for (size_t i = 0; i < countof(autosearchpaths); i++)
|
2018-03-31 17:20:59 +00:00
|
|
|
{
|
|
|
|
auto &layer = autosearchpaths[i];
|
|
|
|
if (this->*(layer.pointer) == nullptr) // only if no explicit assignment had been done.
|
|
|
|
{
|
2020-04-16 23:03:21 +00:00
|
|
|
FStringf lookup("%s%s%s", layer.path, Base->bFullNameTexture ? "" : "auto/", searchname.GetChars());
|
2020-04-11 11:24:34 +00:00
|
|
|
auto lump = fileSystem.CheckNumForFullName(lookup, false, ns_global, true);
|
2018-03-31 17:20:59 +00:00
|
|
|
if (lump != -1)
|
|
|
|
{
|
2020-04-15 20:03:51 +00:00
|
|
|
auto bmtex = TexMan.FindGameTexture(fileSystem.GetFileFullName(lump), ETextureType::Any, FTextureManager::TEXMAN_TryAny);
|
2018-03-31 17:20:59 +00:00
|
|
|
if (bmtex != nullptr)
|
|
|
|
{
|
2020-04-15 20:03:51 +00:00
|
|
|
bmtex->GetTexture()->bMasked = false;
|
|
|
|
this->*(layer.pointer) = bmtex->GetTexture();
|
2018-03-31 17:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 22:08:22 +00:00
|
|
|
|
2018-03-31 17:20:59 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Checks if the texture has a default brightmap and creates it if so
|
|
|
|
//
|
|
|
|
//===========================================================================
|
2020-04-16 22:37:05 +00:00
|
|
|
void FGameTexture::CreateDefaultBrightmap()
|
2018-03-31 17:20:59 +00:00
|
|
|
{
|
2020-04-16 22:37:05 +00:00
|
|
|
auto tex = GetTexture();
|
|
|
|
if (!tex->bBrightmapChecked)
|
2018-03-31 17:20:59 +00:00
|
|
|
{
|
|
|
|
// Check for brightmaps
|
2020-04-16 22:37:05 +00:00
|
|
|
if (tex->GetImage() && tex->GetImage()->UseGamePalette() && GPalette.HasGlobalBrightmap &&
|
|
|
|
GetUseType() != ETextureType::Decal && GetUseType() != ETextureType::MiscPatch && GetUseType() != ETextureType::FontChar &&
|
2020-04-16 23:03:21 +00:00
|
|
|
Brightmap == nullptr)
|
2018-03-31 17:20:59 +00:00
|
|
|
{
|
|
|
|
// May have one - let's check when we use this texture
|
2020-04-16 22:37:05 +00:00
|
|
|
auto texbuf = tex->Get8BitPixels(false);
|
2018-03-31 17:20:59 +00:00
|
|
|
const int white = ColorMatcher.Pick(255, 255, 255);
|
|
|
|
|
2020-04-11 16:20:23 +00:00
|
|
|
int size = GetTexelWidth() * GetTexelHeight();
|
2018-03-31 17:20:59 +00:00
|
|
|
for (int i = 0; i<size; i++)
|
|
|
|
{
|
2020-04-11 16:53:56 +00:00
|
|
|
if (GPalette.GlobalBrightmap.Remap[texbuf[i]] == white)
|
2018-03-31 17:20:59 +00:00
|
|
|
{
|
|
|
|
// Create a brightmap
|
2020-04-16 22:37:05 +00:00
|
|
|
DPrintf(DMSG_NOTIFY, "brightmap created for texture '%s'\n", GetName().GetChars());
|
2020-04-16 23:03:21 +00:00
|
|
|
Brightmap = CreateBrightmapTexture(tex->GetImage());
|
2020-04-16 22:37:05 +00:00
|
|
|
tex->bBrightmapChecked = true;
|
|
|
|
//TexMan.AddGameTexture(MakeGameTexture(tex->Brightmap));
|
2018-03-31 17:20:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No bright pixels found
|
2020-04-16 22:37:05 +00:00
|
|
|
DPrintf(DMSG_SPAMMY, "No bright pixels found in texture '%s'\n", GetName().GetChars());
|
|
|
|
tex->bBrightmapChecked = true;
|
2018-03-31 17:20:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// does not have one so set the flag to 'done'
|
2020-04-16 22:37:05 +00:00
|
|
|
tex->bBrightmapChecked = true;
|
2018-03-31 17:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Calculates glow color for a texture
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FTexture::GetGlowColor(float *data)
|
|
|
|
{
|
|
|
|
if (bGlowing && GlowColor == 0)
|
|
|
|
{
|
2018-12-08 13:06:16 +00:00
|
|
|
auto buffer = GetBgraBitmap(nullptr);
|
|
|
|
GlowColor = averageColor((uint32_t*)buffer.GetPixels(), buffer.GetWidth() * buffer.GetHeight(), 153);
|
2018-03-31 17:20:59 +00:00
|
|
|
|
|
|
|
// Black glow equals nothing so switch glowing off
|
|
|
|
if (GlowColor == 0) bGlowing = false;
|
|
|
|
}
|
|
|
|
data[0] = GlowColor.r / 255.0f;
|
|
|
|
data[1] = GlowColor.g / 255.0f;
|
|
|
|
data[2] = GlowColor.b / 255.0f;
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:19:11 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
int FTexture::GetAreas(FloatRect** pAreas) const
|
|
|
|
{
|
|
|
|
if (shaderindex == SHADER_Default) // texture splitting can only be done if there's no attached effects
|
|
|
|
{
|
|
|
|
*pAreas = areas;
|
|
|
|
return areacount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-31 17:20:59 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Finds gaps in the texture which can be skipped by the renderer
|
|
|
|
// This was mainly added to speed up one area in E4M6 of 007LTSD
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
bool FTexture::FindHoles(const unsigned char * buffer, int w, int h)
|
|
|
|
{
|
|
|
|
const unsigned char * li;
|
|
|
|
int y, x;
|
|
|
|
int startdraw, lendraw;
|
|
|
|
int gaps[5][2];
|
|
|
|
int gapc = 0;
|
|
|
|
|
|
|
|
|
|
|
|
// already done!
|
|
|
|
if (areacount) return false;
|
|
|
|
areacount = -1; //whatever happens next, it shouldn't be done twice!
|
|
|
|
|
|
|
|
// large textures are excluded for performance reasons
|
|
|
|
if (h>512) return false;
|
|
|
|
|
|
|
|
startdraw = -1;
|
|
|
|
lendraw = 0;
|
|
|
|
for (y = 0; y<h; y++)
|
|
|
|
{
|
|
|
|
li = buffer + w * y * 4 + 3;
|
|
|
|
|
|
|
|
for (x = 0; x<w; x++, li += 4)
|
|
|
|
{
|
|
|
|
if (*li != 0) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x != w)
|
|
|
|
{
|
|
|
|
// non - transparent
|
|
|
|
if (startdraw == -1)
|
|
|
|
{
|
|
|
|
startdraw = y;
|
|
|
|
// merge transparent gaps of less than 16 pixels into the last drawing block
|
|
|
|
if (gapc && y <= gaps[gapc - 1][0] + gaps[gapc - 1][1] + 16)
|
|
|
|
{
|
|
|
|
gapc--;
|
|
|
|
startdraw = gaps[gapc][0];
|
|
|
|
lendraw = y - startdraw;
|
|
|
|
}
|
|
|
|
if (gapc == 4) return false; // too many splits - this isn't worth it
|
|
|
|
}
|
|
|
|
lendraw++;
|
|
|
|
}
|
|
|
|
else if (startdraw != -1)
|
|
|
|
{
|
|
|
|
if (lendraw == 1) lendraw = 2;
|
|
|
|
gaps[gapc][0] = startdraw;
|
|
|
|
gaps[gapc][1] = lendraw;
|
|
|
|
gapc++;
|
|
|
|
|
|
|
|
startdraw = -1;
|
|
|
|
lendraw = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (startdraw != -1)
|
|
|
|
{
|
|
|
|
gaps[gapc][0] = startdraw;
|
|
|
|
gaps[gapc][1] = lendraw;
|
|
|
|
gapc++;
|
|
|
|
}
|
|
|
|
if (startdraw == 0 && lendraw == h) return false; // nothing saved so don't create a split list
|
|
|
|
|
|
|
|
if (gapc > 0)
|
|
|
|
{
|
|
|
|
FloatRect * rcs = new FloatRect[gapc];
|
|
|
|
|
|
|
|
for (x = 0; x < gapc; x++)
|
|
|
|
{
|
|
|
|
// gaps are stored as texture (u/v) coordinates
|
|
|
|
rcs[x].width = rcs[x].left = -1.0f;
|
|
|
|
rcs[x].top = (float)gaps[x][0] / (float)h;
|
|
|
|
rcs[x].height = (float)gaps[x][1] / (float)h;
|
|
|
|
}
|
|
|
|
areas = rcs;
|
|
|
|
}
|
|
|
|
else areas = nullptr;
|
|
|
|
areacount = gapc;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void FTexture::CheckTrans(unsigned char * buffer, int size, int trans)
|
|
|
|
{
|
|
|
|
if (bTranslucent == -1)
|
|
|
|
{
|
|
|
|
bTranslucent = trans;
|
|
|
|
if (trans == -1)
|
|
|
|
{
|
|
|
|
uint32_t * dwbuf = (uint32_t*)buffer;
|
|
|
|
for (int i = 0; i<size; i++)
|
|
|
|
{
|
|
|
|
uint32_t alpha = dwbuf[i] >> 24;
|
|
|
|
|
|
|
|
if (alpha != 0xff && alpha != 0)
|
|
|
|
{
|
|
|
|
bTranslucent = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bTranslucent = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// smooth the edges of transparent fields in the texture
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
#define MSB 0
|
|
|
|
#define SOME_MASK 0xffffff00
|
|
|
|
#else
|
|
|
|
#define MSB 3
|
|
|
|
#define SOME_MASK 0x00ffffff
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CHKPIX(ofs) (l1[(ofs)*4+MSB]==255 ? (( ((uint32_t*)l1)[0] = ((uint32_t*)l1)[ofs]&SOME_MASK), trans=true ) : false)
|
|
|
|
|
|
|
|
bool FTexture::SmoothEdges(unsigned char * buffer, int w, int h)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
bool trans = buffer[MSB] == 0; // If I set this to false here the code won't detect textures
|
|
|
|
// that only contain transparent pixels.
|
|
|
|
bool semitrans = false;
|
|
|
|
unsigned char * l1;
|
|
|
|
|
|
|
|
if (h <= 1 || w <= 1) return false; // makes (a) no sense and (b) doesn't work with this code!
|
|
|
|
|
|
|
|
l1 = buffer;
|
|
|
|
|
|
|
|
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(1)) CHKPIX(w);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
l1 += 4;
|
|
|
|
for (x = 1; x<w - 1; x++, l1 += 4)
|
|
|
|
{
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-1) && !CHKPIX(1)) CHKPIX(w);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
}
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-1)) CHKPIX(w);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
l1 += 4;
|
|
|
|
|
|
|
|
for (y = 1; y<h - 1; y++)
|
|
|
|
{
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-w) && !CHKPIX(1)) CHKPIX(w);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
l1 += 4;
|
|
|
|
for (x = 1; x<w - 1; x++, l1 += 4)
|
|
|
|
{
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-w) && !CHKPIX(-1) && !CHKPIX(1) && !CHKPIX(-w - 1) && !CHKPIX(-w + 1) && !CHKPIX(w - 1) && !CHKPIX(w + 1)) CHKPIX(w);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
}
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-w) && !CHKPIX(-1)) CHKPIX(w);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
l1 += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-w)) CHKPIX(1);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
l1 += 4;
|
|
|
|
for (x = 1; x<w - 1; x++, l1 += 4)
|
|
|
|
{
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-w) && !CHKPIX(-1)) CHKPIX(1);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
}
|
|
|
|
if (l1[MSB] == 0 && !CHKPIX(-w)) CHKPIX(-1);
|
|
|
|
else if (l1[MSB]<255) semitrans = true;
|
|
|
|
|
|
|
|
return trans || semitrans;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Post-process the texture data after the buffer has been created
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
bool FTexture::ProcessData(unsigned char * buffer, int w, int h, bool ispatch)
|
|
|
|
{
|
|
|
|
if (bMasked)
|
|
|
|
{
|
|
|
|
bMasked = SmoothEdges(buffer, w, h);
|
|
|
|
if (bMasked && !ispatch) FindHoles(buffer, w, h);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-01 12:38:48 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Initializes the buffer for the texture data
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2018-12-11 19:26:33 +00:00
|
|
|
FTextureBuffer FTexture::CreateTexBuffer(int translation, int flags)
|
2018-04-01 12:38:48 +00:00
|
|
|
{
|
2018-12-11 19:26:33 +00:00
|
|
|
FTextureBuffer result;
|
|
|
|
|
2018-04-24 18:41:52 +00:00
|
|
|
unsigned char * buffer = nullptr;
|
2018-04-01 12:38:48 +00:00
|
|
|
int W, H;
|
|
|
|
int isTransparent = -1;
|
2018-12-11 23:46:58 +00:00
|
|
|
bool checkonly = !!(flags & CTF_CheckOnly);
|
2018-04-01 12:38:48 +00:00
|
|
|
|
2018-04-25 19:02:50 +00:00
|
|
|
int exx = !!(flags & CTF_Expand);
|
2018-04-01 12:38:48 +00:00
|
|
|
|
2020-04-11 16:20:23 +00:00
|
|
|
W = GetTexelWidth() + 2 * exx;
|
|
|
|
H = GetTexelHeight() + 2 * exx;
|
2018-04-01 12:38:48 +00:00
|
|
|
|
2018-12-11 23:46:58 +00:00
|
|
|
if (!checkonly)
|
|
|
|
{
|
|
|
|
buffer = new unsigned char[W*(H + 1) * 4];
|
|
|
|
memset(buffer, 0, W * (H + 1) * 4);
|
2018-04-01 12:38:48 +00:00
|
|
|
|
2020-04-11 16:19:05 +00:00
|
|
|
auto remap = translation <= 0 ? nullptr : GPalette.TranslationToTable(translation);
|
2020-04-13 12:55:16 +00:00
|
|
|
if (remap) translation = remap->Index;
|
2018-12-11 23:46:58 +00:00
|
|
|
FBitmap bmp(buffer, W * 4, W, H);
|
2018-04-24 18:41:52 +00:00
|
|
|
|
2018-12-11 23:46:58 +00:00
|
|
|
int trans;
|
2020-04-11 10:43:46 +00:00
|
|
|
auto Pixels = GetBgraBitmap(remap? remap->Palette : nullptr, &trans);
|
2018-12-11 23:46:58 +00:00
|
|
|
bmp.Blit(exx, exx, Pixels);
|
2018-12-08 16:23:15 +00:00
|
|
|
|
2018-12-11 23:46:58 +00:00
|
|
|
if (remap == nullptr)
|
|
|
|
{
|
|
|
|
CheckTrans(buffer, W*H, trans);
|
|
|
|
isTransparent = bTranslucent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isTransparent = 0;
|
|
|
|
// A translated image is not conclusive for setting the texture's transparency info.
|
|
|
|
}
|
2018-04-25 19:02:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 20:06:34 +00:00
|
|
|
if (GetImage())
|
|
|
|
{
|
|
|
|
FContentIdBuilder builder;
|
|
|
|
builder.id = 0;
|
|
|
|
builder.imageID = GetImage()->GetId();
|
|
|
|
builder.translation = MAX(0, translation);
|
|
|
|
builder.expand = exx;
|
|
|
|
result.mContentId = builder.id;
|
|
|
|
}
|
|
|
|
else result.mContentId = 0; // for non-image backed textures this has no meaning so leave it at 0.
|
2018-12-11 19:26:33 +00:00
|
|
|
|
|
|
|
result.mBuffer = buffer;
|
|
|
|
result.mWidth = W;
|
|
|
|
result.mHeight = H;
|
|
|
|
|
2018-12-11 20:06:34 +00:00
|
|
|
// Only do postprocessing for image-backed textures. (i.e. not for the burn texture which can also pass through here.)
|
|
|
|
if (GetImage() && flags & CTF_ProcessData)
|
2018-04-25 19:02:50 +00:00
|
|
|
{
|
2020-04-16 21:37:22 +00:00
|
|
|
if (flags & CTF_Upscale) CreateUpsampledTextureBuffer(result, !!isTransparent, checkonly);
|
2018-12-11 23:46:58 +00:00
|
|
|
if (!checkonly) ProcessData(result.mBuffer, result.mWidth, result.mHeight, false);
|
2018-06-08 17:12:06 +00:00
|
|
|
}
|
2018-04-01 12:38:48 +00:00
|
|
|
|
2018-12-11 19:26:33 +00:00
|
|
|
return result;
|
2018-04-01 12:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Dummy texture for the 0-entry.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-04-11 17:51:22 +00:00
|
|
|
bool FTexture::DetermineTranslucency()
|
2018-04-01 12:38:48 +00:00
|
|
|
{
|
2020-04-11 17:51:22 +00:00
|
|
|
if (!bHasCanvas)
|
2018-04-01 12:38:48 +00:00
|
|
|
{
|
2020-04-11 17:51:22 +00:00
|
|
|
// This will calculate all we need, so just discard the result.
|
|
|
|
CreateTexBuffer(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bTranslucent = 0;
|
2018-04-01 12:38:48 +00:00
|
|
|
}
|
|
|
|
return !!bTranslucent;
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:05:49 +00:00
|
|
|
|
2020-04-16 19:36:14 +00:00
|
|
|
void FTexture::CleanHardwareTextures(bool reallyclean)
|
2020-04-13 15:05:49 +00:00
|
|
|
{
|
2020-04-16 21:37:22 +00:00
|
|
|
SystemTextures.Clean(reallyclean);
|
2020-04-13 15:05:49 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:19:57 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
2018-12-08 11:42:35 +00:00
|
|
|
// the default just returns an empty texture.
|
2018-04-08 19:19:57 +00:00
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2018-12-08 11:42:35 +00:00
|
|
|
TArray<uint8_t> FTexture::Get8BitPixels(bool alphatex)
|
2018-04-08 19:19:57 +00:00
|
|
|
{
|
2018-12-08 11:42:35 +00:00
|
|
|
TArray<uint8_t> Pixels(Width * Height, true);
|
|
|
|
memset(Pixels.Data(), 0, Width * Height);
|
|
|
|
return Pixels;
|
2018-04-08 19:19:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 21:19:11 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Checks if a sprite may be expanded with an empty frame
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-04-16 23:03:21 +00:00
|
|
|
bool FGameTexture::ShouldExpandSprite()
|
2020-04-14 21:19:11 +00:00
|
|
|
{
|
2020-04-16 23:03:21 +00:00
|
|
|
if (Base->bExpandSprite != -1) return Base->bExpandSprite;
|
|
|
|
if (isWarped() || isHardwareCanvas() || GetShaderIndex() != SHADER_Default)
|
2020-04-14 21:19:11 +00:00
|
|
|
{
|
2020-04-16 23:03:21 +00:00
|
|
|
Base->bExpandSprite = false;
|
2020-04-14 21:19:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (Brightmap != NULL && (GetTexelWidth() != Brightmap->GetTexelWidth() || GetTexelHeight() != Brightmap->GetTexelHeight()))
|
|
|
|
{
|
|
|
|
// do not expand if the brightmap's size differs.
|
2020-04-16 23:03:21 +00:00
|
|
|
Base->bExpandSprite = false;
|
2020-04-14 21:19:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-16 23:03:21 +00:00
|
|
|
Base->bExpandSprite = true;
|
2020-04-14 21:19:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Finds empty space around the texture.
|
|
|
|
// Used for sprites that got placed into a huge empty frame.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
bool FTexture::TrimBorders(uint16_t* rect)
|
|
|
|
{
|
|
|
|
|
|
|
|
auto texbuffer = CreateTexBuffer(0);
|
|
|
|
int w = texbuffer.mWidth;
|
|
|
|
int h = texbuffer.mHeight;
|
|
|
|
auto Buffer = texbuffer.mBuffer;
|
|
|
|
|
|
|
|
if (texbuffer.mBuffer == nullptr)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (w != Width || h != Height)
|
|
|
|
{
|
|
|
|
// external Hires replacements cannot be trimmed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int size = w * h;
|
|
|
|
if (size == 1)
|
|
|
|
{
|
|
|
|
// nothing to be done here.
|
|
|
|
rect[0] = 0;
|
|
|
|
rect[1] = 0;
|
|
|
|
rect[2] = 1;
|
|
|
|
rect[3] = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int first, last;
|
|
|
|
|
|
|
|
for (first = 0; first < size; first++)
|
|
|
|
{
|
|
|
|
if (Buffer[first * 4 + 3] != 0) break;
|
|
|
|
}
|
|
|
|
if (first >= size)
|
|
|
|
{
|
|
|
|
// completely empty
|
|
|
|
rect[0] = 0;
|
|
|
|
rect[1] = 0;
|
|
|
|
rect[2] = 1;
|
|
|
|
rect[3] = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (last = size - 1; last >= first; last--)
|
|
|
|
{
|
|
|
|
if (Buffer[last * 4 + 3] != 0) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rect[1] = first / w;
|
|
|
|
rect[3] = 1 + last / w - rect[1];
|
|
|
|
|
|
|
|
rect[0] = 0;
|
|
|
|
rect[2] = w;
|
|
|
|
|
|
|
|
unsigned char* bufferoff = Buffer + (rect[1] * w * 4);
|
|
|
|
h = rect[3];
|
|
|
|
|
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < h; y++)
|
|
|
|
{
|
|
|
|
if (bufferoff[(x + y * w) * 4 + 3] != 0) goto outl;
|
|
|
|
}
|
|
|
|
rect[0]++;
|
|
|
|
}
|
|
|
|
outl:
|
|
|
|
rect[2] -= rect[0];
|
|
|
|
|
|
|
|
for (int x = w - 1; rect[2] > 1; x--)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < h; y++)
|
|
|
|
{
|
|
|
|
if (bufferoff[(x + y * w) * 4 + 3] != 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rect[2]--;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Sets up the sprite positioning data for this texture
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-04-16 23:03:21 +00:00
|
|
|
void FGameTexture::SetupSpriteData()
|
2020-04-16 16:46:31 +00:00
|
|
|
{
|
|
|
|
// Since this is only needed for real sprites it gets allocated on demand.
|
|
|
|
// It also allocates from the image memory arena because it has the same lifetime and to reduce maintenance.
|
2020-04-17 16:52:48 +00:00
|
|
|
if (spi == nullptr) spi = (SpritePositioningInfo*)ImageArena.Alloc(2 * sizeof(SpritePositioningInfo));
|
2020-04-16 16:46:31 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2020-04-17 16:52:48 +00:00
|
|
|
auto& spi = this->spi[i];
|
2020-04-16 16:46:31 +00:00
|
|
|
spi.mSpriteU[0] = spi.mSpriteV[0] = 0.f;
|
|
|
|
spi.mSpriteU[1] = spi.mSpriteV[1] = 1.f;
|
|
|
|
spi.spriteWidth = GetTexelWidth();
|
|
|
|
spi.spriteHeight = GetTexelHeight();
|
|
|
|
|
|
|
|
if (i == 1 && ShouldExpandSprite())
|
|
|
|
{
|
2020-04-16 23:03:21 +00:00
|
|
|
spi.mTrimResult = Base->TrimBorders(spi.trim); // get the trim size before adding the empty frame
|
2020-04-16 16:46:31 +00:00
|
|
|
spi.spriteWidth += 2;
|
|
|
|
spi.spriteHeight += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SetSpriteRect();
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:19:11 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
2020-04-16 16:46:31 +00:00
|
|
|
// Set the sprite rectangle. This is separate because it may be called by a CVAR, too.
|
2020-04-14 21:19:11 +00:00
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-04-16 23:03:21 +00:00
|
|
|
void FGameTexture::SetSpriteRect()
|
2020-04-14 21:19:11 +00:00
|
|
|
{
|
2020-04-16 16:46:31 +00:00
|
|
|
|
2020-04-17 16:52:48 +00:00
|
|
|
if (!spi) return;
|
2020-04-14 21:19:11 +00:00
|
|
|
auto leftOffset = GetLeftOffsetHW();
|
|
|
|
auto topOffset = GetTopOffsetHW();
|
|
|
|
|
2020-04-16 23:03:21 +00:00
|
|
|
float fxScale = (float)Base->Scale.X;
|
|
|
|
float fyScale = (float)Base->Scale.Y;
|
2020-04-14 21:19:11 +00:00
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
2020-04-14 21:19:11 +00:00
|
|
|
{
|
2020-04-17 16:52:48 +00:00
|
|
|
auto& spi = this->spi[i];
|
2020-04-16 16:46:31 +00:00
|
|
|
|
|
|
|
// mSpriteRect is for positioning the sprite in the scene.
|
|
|
|
spi.mSpriteRect.left = -leftOffset / fxScale;
|
|
|
|
spi.mSpriteRect.top = -topOffset / fyScale;
|
|
|
|
spi.mSpriteRect.width = spi.spriteWidth / fxScale;
|
|
|
|
spi.mSpriteRect.height = spi.spriteHeight / fyScale;
|
2020-04-14 21:19:11 +00:00
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
if (i == 1 && ShouldExpandSprite())
|
|
|
|
{
|
|
|
|
// a little adjustment to make sprites look better with texture filtering:
|
|
|
|
// create a 1 pixel wide empty frame around them.
|
2020-04-14 21:19:11 +00:00
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
int oldwidth = spi.spriteWidth - 2;
|
|
|
|
int oldheight = spi.spriteHeight - 2;
|
2020-04-14 21:19:11 +00:00
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
leftOffset += 1;
|
|
|
|
topOffset += 1;
|
2020-04-14 21:19:11 +00:00
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
// Reposition the sprite with the frame considered
|
|
|
|
spi.mSpriteRect.left = -(float)leftOffset / fxScale;
|
|
|
|
spi.mSpriteRect.top = -(float)topOffset / fyScale;
|
|
|
|
spi.mSpriteRect.width = (float)spi.spriteWidth / fxScale;
|
|
|
|
spi.mSpriteRect.height = (float)spi.spriteHeight / fyScale;
|
2020-04-14 21:19:11 +00:00
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
if (spi.mTrimResult > 0)
|
|
|
|
{
|
|
|
|
spi.mSpriteRect.left += (float)spi.trim[0] / fxScale;
|
|
|
|
spi.mSpriteRect.top += (float)spi.trim[1] / fyScale;
|
2020-04-14 21:19:11 +00:00
|
|
|
|
2020-04-16 16:46:31 +00:00
|
|
|
spi.mSpriteRect.width -= float(oldwidth - spi.trim[2]) / fxScale;
|
|
|
|
spi.mSpriteRect.height -= float(oldheight - spi.trim[3]) / fyScale;
|
|
|
|
|
|
|
|
spi.mSpriteU[0] = (float)spi.trim[0] / (float)spi.spriteWidth;
|
|
|
|
spi.mSpriteV[0] = (float)spi.trim[1] / (float)spi.spriteHeight;
|
|
|
|
spi.mSpriteU[1] -= float(oldwidth - spi.trim[0] - spi.trim[2]) / (float)spi.spriteWidth;
|
|
|
|
spi.mSpriteV[1] -= float(oldheight - spi.trim[1] - spi.trim[3]) / (float)spi.spriteHeight;
|
|
|
|
}
|
2020-04-14 21:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-15 22:21:13 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Create a hardware texture for this texture image.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2020-04-16 21:37:22 +00:00
|
|
|
IHardwareTexture* FTexture::GetHardwareTexture(int translation, int scaleflags)
|
2020-04-15 22:21:13 +00:00
|
|
|
{
|
2020-04-16 22:37:05 +00:00
|
|
|
//if (UseType != ETextureType::Null)
|
2020-04-15 22:21:13 +00:00
|
|
|
{
|
2020-04-16 21:37:22 +00:00
|
|
|
IHardwareTexture* hwtex = SystemTextures.GetHardwareTexture(translation, scaleflags);
|
2020-04-15 22:21:13 +00:00
|
|
|
if (hwtex == nullptr)
|
|
|
|
{
|
|
|
|
hwtex = CreateHardwareTexture();
|
2020-04-16 21:37:22 +00:00
|
|
|
SystemTextures.AddHardwareTexture(translation, scaleflags, hwtex);
|
2020-04-15 22:21:13 +00:00
|
|
|
}
|
|
|
|
return hwtex;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-17 17:24:14 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Coordinate helper.
|
|
|
|
// The only reason this is even needed is that many years ago someone
|
|
|
|
// was convinced that having per-texel panning on walls was a good idea.
|
|
|
|
// If it wasn't for this relatively useless feature the entire positioning
|
|
|
|
// code for wall textures could be a lot simpler.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
float FTexCoordInfo::RowOffset(float rowoffset) const
|
|
|
|
{
|
|
|
|
float scale = fabs(mScale.Y);
|
2018-12-14 21:34:28 +00:00
|
|
|
if (scale == 1.f || mWorldPanning) return rowoffset;
|
|
|
|
else return rowoffset / scale;
|
2018-11-17 17:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
float FTexCoordInfo::TextureOffset(float textureoffset) const
|
|
|
|
{
|
|
|
|
float scale = fabs(mScale.X);
|
2018-12-14 21:34:28 +00:00
|
|
|
if (scale == 1.f || mWorldPanning) return textureoffset;
|
|
|
|
else return textureoffset / scale;
|
2018-11-17 17:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Returns the size for which texture offset coordinates are used.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
float FTexCoordInfo::TextureAdjustWidth() const
|
|
|
|
{
|
|
|
|
if (mWorldPanning)
|
|
|
|
{
|
|
|
|
float tscale = fabs(mTempScale.X);
|
|
|
|
if (tscale == 1.f) return (float)mRenderWidth;
|
|
|
|
else return mWidth / fabs(tscale);
|
|
|
|
}
|
|
|
|
else return (float)mWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Retrieve texture coordinate info for per-wall scaling
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
2019-01-29 01:51:06 +00:00
|
|
|
void FTexCoordInfo::GetFromTexture(FTexture *tex, float x, float y, bool forceworldpanning)
|
2018-11-17 17:24:14 +00:00
|
|
|
{
|
|
|
|
if (x == 1.f)
|
|
|
|
{
|
2020-04-15 16:59:14 +00:00
|
|
|
mRenderWidth = tex->GetDisplayWidth();
|
2018-11-17 17:24:14 +00:00
|
|
|
mScale.X = (float)tex->Scale.X;
|
|
|
|
mTempScale.X = 1.f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float scale_x = x * (float)tex->Scale.X;
|
2020-04-11 16:20:23 +00:00
|
|
|
mRenderWidth = xs_CeilToInt(tex->GetTexelWidth() / scale_x);
|
2018-11-17 17:24:14 +00:00
|
|
|
mScale.X = scale_x;
|
|
|
|
mTempScale.X = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y == 1.f)
|
|
|
|
{
|
2020-04-15 16:59:14 +00:00
|
|
|
mRenderHeight = tex->GetDisplayHeight();
|
2018-11-17 17:24:14 +00:00
|
|
|
mScale.Y = (float)tex->Scale.Y;
|
|
|
|
mTempScale.Y = 1.f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float scale_y = y * (float)tex->Scale.Y;
|
2020-04-11 16:20:23 +00:00
|
|
|
mRenderHeight = xs_CeilToInt(tex->GetTexelHeight() / scale_y);
|
2018-11-17 17:24:14 +00:00
|
|
|
mScale.Y = scale_y;
|
|
|
|
mTempScale.Y = y;
|
|
|
|
}
|
|
|
|
if (tex->bHasCanvas)
|
|
|
|
{
|
|
|
|
mScale.Y = -mScale.Y;
|
|
|
|
mRenderHeight = -mRenderHeight;
|
|
|
|
}
|
2019-01-29 01:51:06 +00:00
|
|
|
mWorldPanning = tex->bWorldPanning || forceworldpanning;
|
2020-04-11 16:20:23 +00:00
|
|
|
mWidth = tex->GetTexelWidth();
|
2018-11-17 17:24:14 +00:00
|
|
|
}
|
2018-12-07 01:31:30 +00:00
|
|
|
|
2020-04-14 16:01:51 +00:00
|
|
|
void FTexCoordInfo::GetFromTexture(FGameTexture* tex, float x, float y, bool forceworldpanning)
|
|
|
|
{
|
|
|
|
GetFromTexture(tex->GetTexture(), x, y, forceworldpanning);
|
|
|
|
}
|
2018-12-07 02:35:10 +00:00
|
|
|
|
2020-04-11 17:07:08 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// this must be copied back to textures.cpp later.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FWrapperTexture::FWrapperTexture(int w, int h, int bits)
|
|
|
|
{
|
|
|
|
Width = w;
|
|
|
|
Height = h;
|
|
|
|
Format = bits;
|
|
|
|
bNoCompress = true;
|
|
|
|
auto hwtex = CreateHardwareTexture();
|
|
|
|
// todo: Initialize here.
|
|
|
|
SystemTextures.AddHardwareTexture(0, false, hwtex);
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:55:16 +00:00
|
|
|
|
2020-04-16 17:34:56 +00:00
|
|
|
FGameTexture::~FGameTexture()
|
|
|
|
{
|
|
|
|
FGameTexture* link = fileSystem.GetLinkedTexture(GetSourceLump());
|
|
|
|
if (link == this) fileSystem.SetLinkedTexture(GetSourceLump(), nullptr);
|
2020-04-17 16:52:48 +00:00
|
|
|
if (SoftwareTexture != nullptr)
|
|
|
|
{
|
|
|
|
delete SoftwareTexture;
|
|
|
|
SoftwareTexture = nullptr;
|
|
|
|
}
|
|
|
|
for (auto &mat : Material)
|
|
|
|
{
|
|
|
|
if (mat != nullptr) DeleteMaterial(mat);
|
|
|
|
mat = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:55:16 +00:00
|
|
|
bool FGameTexture::isUserContent() const
|
|
|
|
{
|
2020-04-16 23:03:21 +00:00
|
|
|
int filenum = fileSystem.GetFileContainer(Base->GetSourceLump());
|
2020-04-13 22:55:16 +00:00
|
|
|
return (filenum > fileSystem.GetMaxIwadNum());
|
|
|
|
}
|
2020-04-13 23:23:37 +00:00
|
|
|
|
2020-04-14 21:19:11 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Make sprite offset adjustment user-configurable per renderer.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
CUSTOM_CVAR(Int, r_spriteadjust, 2, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
|
|
|
{
|
|
|
|
r_spriteadjustHW = !!(self & 2);
|
|
|
|
r_spriteadjustSW = !!(self & 1);
|
|
|
|
for (int i = 0; i < TexMan.NumTextures(); i++)
|
|
|
|
{
|
|
|
|
auto tex = TexMan.GetGameTexture(FSetTextureID(i));
|
|
|
|
if (tex->GetTexelLeftOffset(0) != tex->GetTexelLeftOffset(1) || tex->GetTexelTopOffset(0) != tex->GetTexelTopOffset(1))
|
|
|
|
{
|
|
|
|
tex->SetSpriteRect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|