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.
|
|
|
|
*/
|
2023-05-30 22:55:16 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include "Base.h"
|
2011-08-03 20:05:33 +00:00
|
|
|
#include "Texture.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
|
|
|
|
2023-08-09 00:41:04 +00:00
|
|
|
bool TexCoordBox::isStandard() const
|
|
|
|
{
|
|
|
|
return u1 == 0 && v1 == 0 && u2 == 1 && v2 == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TexCoordBox::setStandard()
|
|
|
|
{
|
|
|
|
u1 = 0;
|
|
|
|
v1 = 0;
|
|
|
|
u2 = 1;
|
|
|
|
v2 = 1;
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
Texture::Texture()
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2023-05-30 22:55:16 +00:00
|
|
|
gltexid = 0;
|
2011-08-03 20:05:33 +00:00
|
|
|
width = height = 0;
|
|
|
|
|
|
|
|
ow = oh = -1;
|
2022-03-31 19:03:40 +00:00
|
|
|
_mipmap = false;
|
2023-05-30 22:55:16 +00:00
|
|
|
success = false;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Texture::~Texture()
|
|
|
|
{
|
2023-05-30 22:55:16 +00:00
|
|
|
unload();
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:19:33 +00:00
|
|
|
void Texture::readRGBA(unsigned char *pixels) const
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2023-05-30 22:55:16 +00:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gltexid);
|
|
|
|
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 22:55:16 +00:00
|
|
|
void Texture::writeRGBA(int tx, int ty, int w, int h, const unsigned char *pixels)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2023-05-30 22:55:16 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gltexid);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0,
|
|
|
|
tx,
|
|
|
|
ty,
|
|
|
|
w,
|
|
|
|
h,
|
|
|
|
GL_RGBA,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
pixels
|
|
|
|
);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::unload()
|
|
|
|
{
|
2023-05-30 22:55:16 +00:00
|
|
|
if (gltexid)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
ow = width;
|
|
|
|
oh = height;
|
|
|
|
|
2023-05-30 22:55:16 +00:00
|
|
|
glDeleteTextures(1, &gltexid);
|
|
|
|
gltexid = 0;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:38:36 +00:00
|
|
|
void Texture::apply() const
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2023-05-30 22:55:16 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, gltexid);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-05-30 22:55:16 +00:00
|
|
|
bool Texture::upload(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
|
|
|
|
2023-05-30 22:55:16 +00:00
|
|
|
if(!gltexid)
|
|
|
|
glGenTextures(1, &gltexid);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, gltexid);
|
2023-08-09 00:41:04 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
|
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;
|
|
|
|
return true;
|
2012-06-15 02:12:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:19:33 +00:00
|
|
|
unsigned char * Texture::getBufferAndSize(int *wparam, int *hparam, size_t *sizeparam) const
|
2012-06-15 02:12:20 +00:00
|
|
|
{
|
2023-05-30 22:55:16 +00:00
|
|
|
const size_t bytes = size_t(width) * size_t(height) * 4;
|
|
|
|
unsigned char *data = (unsigned char*)malloc(bytes);
|
2012-06-15 02:12:20 +00:00
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
2023-05-30 22:55:16 +00:00
|
|
|
os << "Game::getBufferAndSize allocation failure, bytes = " << bytes;
|
2012-06-15 02:12:20 +00:00
|
|
|
errorLog(os.str());
|
2023-05-30 22:55:16 +00:00
|
|
|
return NULL;
|
2012-06-15 02:12:20 +00:00
|
|
|
}
|
2023-05-30 22:55:16 +00:00
|
|
|
this->readRGBA(data);
|
2012-06-15 02:12:20 +00:00
|
|
|
|
2023-05-30 22:55:16 +00:00
|
|
|
*wparam = width;
|
|
|
|
*hparam = height;
|
|
|
|
*sizeparam = bytes;
|
2012-06-15 02:12:20 +00:00
|
|
|
return data;
|
|
|
|
}
|