2011-08-03 20:05:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2007, 2010 - Bit-Blot
|
|
|
|
|
|
|
|
This file is part of Aquaria.
|
|
|
|
|
|
|
|
Aquaria is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
#include "Texture.h"
|
|
|
|
#include "Core.h"
|
2022-03-31 19:03:40 +00:00
|
|
|
#include "Image.h"
|
2012-02-09 23:10:50 +00:00
|
|
|
#include "ByteBuffer.h"
|
2016-07-09 02:18:40 +00:00
|
|
|
#include "RenderBase.h"
|
2022-03-31 19:03:40 +00:00
|
|
|
#include "bithacks.h"
|
2011-08-03 20:05:33 +00:00
|
|
|
#include <assert.h>
|
2022-03-31 19:03:40 +00:00
|
|
|
#include "GLLoad.h"
|
|
|
|
#include "stb_image_resize.h"
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
Texture::Texture()
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
textures[0] = 0;
|
|
|
|
width = height = 0;
|
|
|
|
|
|
|
|
repeat = false;
|
2012-02-19 03:57:04 +00:00
|
|
|
repeating = false;
|
2011-08-03 20:05:33 +00:00
|
|
|
ow = oh = -1;
|
2016-03-13 00:37:43 +00:00
|
|
|
loadResult = TEX_FAILED;
|
2022-03-31 19:03:40 +00:00
|
|
|
_mipmap = false;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Texture::~Texture()
|
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::read(int tx, int ty, int w, int h, unsigned char *pixels)
|
|
|
|
{
|
|
|
|
if (tx == 0 && ty == 0 && w == this->width && h == this->height)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[0]);
|
|
|
|
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "Unable to read a texture subimage (size = "
|
|
|
|
<< this->width << "x" << this->height << ", requested = "
|
|
|
|
<< tx << "," << ty << "+" << w << "x" << h << ")";
|
|
|
|
debugLog(os.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::write(int tx, int ty, int w, int h, const unsigned char *pixels)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[0]);
|
|
|
|
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0,
|
|
|
|
tx,
|
|
|
|
ty,
|
|
|
|
w,
|
|
|
|
h,
|
|
|
|
GL_RGBA,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
pixels
|
|
|
|
);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
/*
|
|
|
|
target Specifies the target texture. Must be
|
|
|
|
GL_TEXTURE_2D.
|
|
|
|
|
|
|
|
level Specifies the level-of-detail number. Level 0 is
|
|
|
|
the base image level. Level n is the nth mipmap
|
|
|
|
reduction image.
|
|
|
|
|
|
|
|
xoffset Specifies a texel offset in the x direction within
|
|
|
|
the texture array.
|
|
|
|
|
|
|
|
yoffset Specifies a texel offset in the y direction within
|
|
|
|
the texture array.
|
|
|
|
|
|
|
|
width Specifies the width of the texture subimage.
|
|
|
|
|
|
|
|
height Specifies the height of the texture subimage.
|
|
|
|
|
|
|
|
format Specifies the format of the pixel data. The
|
|
|
|
following symbolic values are accepted:
|
|
|
|
GL_COLOR_INDEX, GL_RED, GL_GREEN, GL_BLUE,
|
|
|
|
GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, and
|
|
|
|
GL_LUMINANCE_ALPHA.
|
|
|
|
|
|
|
|
type Specifies the data type of the pixel data. The
|
|
|
|
following symbolic values are accepted:
|
|
|
|
GL_UNSIGNED_BYTE, GL_BYTE, GL_BITMAP,
|
|
|
|
GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT,
|
|
|
|
GL_INT, and GL_FLOAT.
|
|
|
|
|
|
|
|
pixels Specifies a pointer to the image data in memory.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::unload()
|
|
|
|
{
|
|
|
|
if (textures[0])
|
|
|
|
{
|
|
|
|
ow = width;
|
|
|
|
oh = height;
|
|
|
|
|
|
|
|
if (core->debugLogTextures)
|
|
|
|
{
|
|
|
|
debugLog("UNLOADING TEXTURE: " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glDeleteTextures(1, &textures[0]);
|
|
|
|
textures[0] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::destroy()
|
|
|
|
{
|
|
|
|
unload();
|
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
core->removeTexture(this);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
// FIXME: this should be recorded at load-time -- fg
|
2011-08-03 20:05:33 +00:00
|
|
|
int Texture::getPixelWidth()
|
|
|
|
{
|
2012-06-14 15:40:01 +00:00
|
|
|
int w = 0, h = 0;
|
2012-06-15 02:12:20 +00:00
|
|
|
unsigned int size = 0;
|
|
|
|
unsigned char *data = getBufferAndSize(&w, &h, &size);
|
2012-06-14 15:40:01 +00:00
|
|
|
if (!data)
|
2011-08-03 20:05:33 +00:00
|
|
|
return 0;
|
2012-06-14 15:40:01 +00:00
|
|
|
|
2017-01-14 18:22:37 +00:00
|
|
|
size_t smallestx = -1, largestx = 0;
|
2012-06-14 15:40:01 +00:00
|
|
|
for (unsigned int x = 0; x < unsigned(w); x++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2012-06-14 15:40:01 +00:00
|
|
|
for (unsigned int y = 0; y < unsigned(h); y++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2012-06-14 15:40:01 +00:00
|
|
|
unsigned int p = (y*unsigned(w)*4) + (x*4) + 3;
|
|
|
|
if (p < size && data[p] >= 254)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
if (x < smallestx)
|
2011-08-03 20:05:33 +00:00
|
|
|
smallestx = x;
|
2017-01-14 17:10:20 +00:00
|
|
|
if (x > largestx)
|
2011-08-03 20:05:33 +00:00
|
|
|
largestx = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
return largestx - smallestx;
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
// FIXME: same as above
|
2011-08-03 20:05:33 +00:00
|
|
|
int Texture::getPixelHeight()
|
|
|
|
{
|
2012-06-14 15:40:01 +00:00
|
|
|
int w = 0, h = 0;
|
2012-06-15 02:12:20 +00:00
|
|
|
unsigned int size = 0;
|
|
|
|
unsigned char *data = getBufferAndSize(&w, &h, &size);
|
2012-06-14 15:40:01 +00:00
|
|
|
if (!data)
|
2011-08-03 20:05:33 +00:00
|
|
|
return 0;
|
2012-06-15 02:12:20 +00:00
|
|
|
|
2017-01-14 18:22:37 +00:00
|
|
|
size_t smallesty = -1, largesty = 0;
|
2012-06-14 15:40:01 +00:00
|
|
|
for (unsigned int x = 0; x < unsigned(w); x++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2012-06-14 15:40:01 +00:00
|
|
|
for (unsigned int y = 0; y < unsigned(h); y++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
size_t p = (y*unsigned(w)*4) + (x*4) + 3;
|
2012-06-14 15:40:01 +00:00
|
|
|
if (p < size && data[p] >= 254)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
if (y < smallesty)
|
2011-08-03 20:05:33 +00:00
|
|
|
smallesty = y;
|
2017-01-14 17:10:20 +00:00
|
|
|
if (y > largesty)
|
2011-08-03 20:05:33 +00:00
|
|
|
largesty = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 15:40:01 +00:00
|
|
|
free(data);
|
2011-08-03 20:05:33 +00:00
|
|
|
return largesty - smallesty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::reload()
|
|
|
|
{
|
|
|
|
debugLog("RELOADING TEXTURE: " + name + " with loadName " + loadName + "...");
|
|
|
|
|
2012-02-19 03:57:04 +00:00
|
|
|
unload();
|
2022-03-31 19:03:40 +00:00
|
|
|
load(loadName, _mipmap);
|
2012-02-19 03:57:04 +00:00
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
debugLog("DONE");
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
bool Texture::load(std::string file, bool mipmap)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2016-03-13 00:37:43 +00:00
|
|
|
loadResult = TEX_FAILED;
|
2011-08-03 20:05:33 +00:00
|
|
|
if (file.size()<4)
|
|
|
|
{
|
|
|
|
errorLog("Texture Name is Empty or Too Short");
|
2015-03-23 23:06:51 +00:00
|
|
|
return false;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2012-07-12 03:46:18 +00:00
|
|
|
stringToLowerUserData(file);
|
2016-07-09 02:18:40 +00:00
|
|
|
file = adjustFilenameCase(file);
|
2012-07-12 03:46:18 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
loadName = file;
|
2012-04-01 19:24:31 +00:00
|
|
|
repeating = false;
|
2022-03-31 19:03:40 +00:00
|
|
|
_mipmap = mipmap;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
size_t pos = file.find_last_of('.');
|
|
|
|
|
2017-01-12 21:51:46 +00:00
|
|
|
if (pos != std::string::npos)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
// make sure this didn't catch the '.' in /home/username/.Aquaria/* --ryan.
|
|
|
|
const std::string userdata = core->getUserDataFolder();
|
|
|
|
const size_t len = userdata.length();
|
|
|
|
if (pos < len)
|
|
|
|
pos = std::string::npos;
|
|
|
|
}
|
|
|
|
|
2012-07-10 20:16:48 +00:00
|
|
|
bool found = exists(file);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2012-07-10 20:16:48 +00:00
|
|
|
if(!found && exists(file + ".png"))
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
file += ".png";
|
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2012-07-10 20:16:48 +00:00
|
|
|
// .tga/.zga are never used as game graphics anywhere except save slot thumbnails.
|
|
|
|
// if so, their file names are passed exact, not with a missing extension
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
bool ok = false;
|
2011-08-03 20:05:33 +00:00
|
|
|
if (found)
|
|
|
|
{
|
2012-07-14 13:00:37 +00:00
|
|
|
file = localisePathInternalModpath(file);
|
2016-07-09 02:18:40 +00:00
|
|
|
file = adjustFilenameCase(file);
|
2012-07-10 20:16:48 +00:00
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
std::string post = file.substr(file.size()-3, 3);
|
|
|
|
stringToLower(post);
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
ImageData img = {};
|
|
|
|
if (post == "zga")
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
img = imageLoadZGA(file.c_str());
|
|
|
|
if(img.pixels)
|
|
|
|
mipmap = false;
|
|
|
|
else
|
|
|
|
debugLog("Can't load ZGA File: " + file);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2022-03-31 19:03:40 +00:00
|
|
|
else
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
img = imageLoadGeneric(file.c_str(), false);
|
|
|
|
if(!img.pixels)
|
|
|
|
debugLog("unknown image file type: " + file);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2022-03-31 19:03:40 +00:00
|
|
|
|
|
|
|
if(img.pixels)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
ok = loadInternal(img, mipmap);
|
|
|
|
free(img.pixels);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// load default image / leave white
|
|
|
|
if (core->debugLogTextures)
|
|
|
|
debugLog("***Could not find texture: " + file);
|
|
|
|
}
|
2022-03-31 19:03:40 +00:00
|
|
|
return ok;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::apply(bool repeatOverride)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[0]);
|
|
|
|
if (repeat || repeatOverride)
|
|
|
|
{
|
2012-02-19 03:57:04 +00:00
|
|
|
if (!repeating)
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
repeating = true;
|
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-19 03:57:04 +00:00
|
|
|
if (repeating)
|
|
|
|
{
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
repeating = false;
|
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::unbind()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
struct GlTexFormat
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
int internalformat, format, type;
|
|
|
|
int alphachan; // for stb_image_resize; index of alpha channel
|
|
|
|
};
|
|
|
|
static const GlTexFormat formatLUT[] =
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
{ GL_LUMINANCE, GL_R, GL_UNSIGNED_BYTE, STBIR_ALPHA_CHANNEL_NONE },
|
|
|
|
{ GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 1 },
|
|
|
|
{ GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, STBIR_ALPHA_CHANNEL_NONE },
|
|
|
|
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 3 }
|
|
|
|
};
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
bool Texture::loadInternal(const ImageData& img, bool mipmap)
|
2012-02-09 23:10:50 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
if(!img.pixels || !img.channels || img.channels > 4 || !img.w || !img.h)
|
2015-03-23 23:06:51 +00:00
|
|
|
return false;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
//if(!bithacks::isPowerOf2(img.w))
|
|
|
|
// __debugbreak();
|
|
|
|
|
|
|
|
// work around bug in older ATI drivers that would cause glGenerateMipmapEXT() to fail otherwise
|
|
|
|
// via https://www.khronos.org/opengl/wiki/Common_Mistakes#Automatic_mipmap_generation
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
// no padding
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2012-02-09 23:10:50 +00:00
|
|
|
glGenTextures(1, &textures[0]);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[0]);
|
2022-03-31 19:03:40 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2015-03-23 23:06:51 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
const GlTexFormat& f = formatLUT[img.channels - 1];
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
int minfilter = GL_LINEAR;
|
|
|
|
int ismip = 0;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
// if our super old OpenGL supports it, request automatic mipmap generation
|
|
|
|
// but not if glGenerateMipmapEXT is present, as it's the much better choice
|
|
|
|
if(mipmap && !glGenerateMipmapEXT && g_has_GL_GENERATE_MIPMAP)
|
2012-02-09 23:10:50 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
|
|
|
glGetTexParameteriv(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, &ismip);
|
2012-02-09 23:10:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
// attach base level first
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, f.internalformat, img.w, img.h, 0, f.format, f.type, img.pixels);
|
2012-02-09 23:10:50 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
if(mipmap && !ismip)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
// now that the base is attached, generate mipmaps
|
|
|
|
if(glGenerateMipmapEXT)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
|
|
|
|
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
|
|
|
ismip = 1;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
if(!ismip)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
debugLog("Failed to mipmap in hardware, using software fallback");
|
|
|
|
ismip = 1;
|
|
|
|
unsigned mw = img.w;
|
|
|
|
unsigned mh = img.h;
|
|
|
|
unsigned char *pmip = img.pixels;
|
|
|
|
unsigned level = 0;
|
|
|
|
while(mw > 1 || mh > 1)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
const unsigned oldw = mw, oldh = mh;
|
|
|
|
mw = bithacks::prevPowerOf2(mw);
|
|
|
|
mh = bithacks::prevPowerOf2(mh);
|
|
|
|
assert(mw && mh);
|
|
|
|
++level;
|
|
|
|
unsigned char *out = (unsigned char*)malloc(mw * mh * img.channels);
|
|
|
|
// when we're on hardware old enough not to have glGenerateMipmapEXT we'll
|
|
|
|
// likely not want to spend too much time generating mipmaps,
|
|
|
|
// so something fast & cheap like a box filter is enough
|
|
|
|
int res = stbir_resize_uint8_generic(pmip, oldw, oldh, 0, out, mw, mh, 0, img.channels,
|
|
|
|
f.alphachan, 0, STBIR_EDGE_CLAMP, STBIR_FILTER_BOX, STBIR_COLORSPACE_LINEAR, NULL);
|
|
|
|
if(!res)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-03-31 19:03:40 +00:00
|
|
|
debugLog("Failed to calculate software mipmap");
|
|
|
|
free(out);
|
|
|
|
ismip = 0;
|
2011-08-03 20:05:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-03-31 19:03:40 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, level, f.internalformat, mw, mh, 0, f.format, f.type, out);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
if(pmip != img.pixels)
|
|
|
|
free(pmip);
|
|
|
|
pmip = out;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2022-03-31 19:03:40 +00:00
|
|
|
if(pmip != img.pixels)
|
|
|
|
free(pmip);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAX_LEVEL, level);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-05 08:02:33 +00:00
|
|
|
if(ismip)
|
|
|
|
minfilter = GL_LINEAR_MIPMAP_LINEAR;
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, minfilter);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
width = img.w;
|
|
|
|
height = img.h;
|
|
|
|
loadResult = TEX_SUCCESS;
|
|
|
|
return true;
|
2012-06-15 02:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char * Texture::getBufferAndSize(int *wparam, int *hparam, unsigned int *sizeparam)
|
|
|
|
{
|
2012-06-15 02:19:03 +00:00
|
|
|
unsigned char *data = NULL;
|
2012-06-15 15:58:18 +00:00
|
|
|
unsigned int size = 0;
|
2012-06-15 02:19:03 +00:00
|
|
|
int tw = 0, th = 0;
|
|
|
|
int w = 0, h = 0;
|
|
|
|
|
2012-06-15 02:12:20 +00:00
|
|
|
// This can't happen. If it does we're doomed.
|
|
|
|
if(width <= 0 || height <= 0)
|
|
|
|
goto fail;
|
|
|
|
|
2022-03-31 19:03:40 +00:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
2012-06-15 02:12:20 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, textures[0]);
|
|
|
|
|
|
|
|
// As returned by graphics driver
|
2012-06-15 02:19:03 +00:00
|
|
|
|
2012-06-15 02:12:20 +00:00
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
|
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
|
|
|
|
|
2012-06-15 15:58:18 +00:00
|
|
|
// As we know it - but round to nearest power of 2 - OpenGL does this internally anyways.
|
2022-03-31 19:03:40 +00:00
|
|
|
tw = bithacks::clp2(width); // known to be > 0.
|
|
|
|
th = bithacks::clp2(height);
|
2012-06-15 02:12:20 +00:00
|
|
|
|
|
|
|
if (w != tw || h != th)
|
|
|
|
{
|
2012-06-15 15:58:18 +00:00
|
|
|
std::ostringstream os;
|
|
|
|
os << "Texture::getBufferAndSize() WARNING: width/height disagree: ";
|
|
|
|
os << "Driver says (" << w << ", " << h << "); ";
|
|
|
|
os << "Texture says (" << width << ", " << height << "); ";
|
|
|
|
os << "Rounded to (" << tw << ", " << th << ")";
|
|
|
|
debugLog(os.str());
|
2012-06-15 02:12:20 +00:00
|
|
|
// choose max. for size calculation
|
|
|
|
w = w > tw ? w : tw;
|
|
|
|
h = h > th ? h : th;
|
|
|
|
}
|
|
|
|
|
2012-06-15 02:23:22 +00:00
|
|
|
size = w * h * 4;
|
2012-06-15 02:12:20 +00:00
|
|
|
if (!size)
|
|
|
|
goto fail;
|
|
|
|
|
2012-06-15 15:58:18 +00:00
|
|
|
data = (unsigned char*)malloc(size + 32);
|
2012-06-15 02:12:20 +00:00
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "Game::fillGridFromQuad allocation failure, size = " << size;
|
|
|
|
errorLog(os.str());
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-06-15 15:58:18 +00:00
|
|
|
memcpy(data + size, "SAFE", 5);
|
2012-06-15 02:12:20 +00:00
|
|
|
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
// Not sure but this might be the case with nouveau drivers on linux... still investigating. -- fg
|
2012-06-15 15:58:18 +00:00
|
|
|
if(memcmp(data + size, "SAFE", 5))
|
2012-06-15 02:12:20 +00:00
|
|
|
{
|
|
|
|
errorLog("Texture::getBufferAndSize(): Broken graphics driver! Wrote past end of buffer!");
|
|
|
|
free(data); // in case we are here, this will most likely cause a crash.
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*wparam = w;
|
|
|
|
*hparam = h;
|
|
|
|
*sizeparam = size;
|
|
|
|
return data;
|
|
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*wparam = 0;
|
|
|
|
*hparam = 0;
|
|
|
|
*sizeparam = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|