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"
|
|
|
|
#include "../ExternalLibs/glpng.h"
|
2012-02-09 23:10:50 +00:00
|
|
|
#include "ByteBuffer.h"
|
2016-07-09 02:18:40 +00:00
|
|
|
#include "RenderBase.h"
|
2011-08-03 20:05:33 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#if defined(BBGE_BUILD_UNIX)
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
pngSetStandardOrientation(0);
|
|
|
|
ow = oh = -1;
|
2016-03-13 00:37:43 +00:00
|
|
|
loadResult = TEX_FAILED;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
load(loadName);
|
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
debugLog("DONE");
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
bool Texture::load(std::string file)
|
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;
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
if (post == "png")
|
|
|
|
{
|
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
return loadPNG(file);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (post == "zga")
|
|
|
|
{
|
2015-03-23 23:06:51 +00:00
|
|
|
return loadZGA(file);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
else if (post == "tga")
|
|
|
|
{
|
2015-03-23 23:06:51 +00:00
|
|
|
return loadTGA(file);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debugLog("unknown image file type: " + file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// load default image / leave white
|
|
|
|
if (core->debugLogTextures)
|
|
|
|
debugLog("***Could not find texture: " + file);
|
|
|
|
}
|
2015-03-23 23:06:51 +00:00
|
|
|
return false;
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
bool Texture::loadPNG(const std::string &file)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2015-03-23 23:06:51 +00:00
|
|
|
if (file.empty()) return false;
|
|
|
|
bool good = false;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
pngInfo info;
|
|
|
|
|
2014-04-07 00:10:05 +00:00
|
|
|
unsigned long memsize = 0;
|
|
|
|
const char *memptr = readFile(file, &memsize);
|
|
|
|
if(!memptr || !memsize)
|
|
|
|
goto fail;
|
[vfs, #3] All file reading code goes through the VFS now, new mod downloader & mod selector in place. Also a bunch of other stuff. (...)
- HTTP networking support, mods can be downloaded via the builtin downloader.
All network activity runs in a seperate thread, which is started
as soon as any network activity is requested.
- The master server is hard-coded to fg.wzff.de/aqmods/ if not specified otherwise;
this setting can be overridden in the config file.
- The mod selector screen is now a grid-view for much better navigation;
also works with joystick.
- VFS code is functionally similar to the old molebox-packed release
for win32. The game could also have its data shipped in a Zip file
or any other kind of archive.
- It is still possible to build without VFS support, but then the mod
downloader and soft-patching will not be available.
The full commit history can be found here:
https://github.com/fgenesis/Aquaria_clean/compare/master...vfs
The most important commit messages follow:
[...]
This replaces all std::ifstream with InStream, and fopen(), ... with vfopen(), ...
Some code is #ifdef'd for better performance and less memory-copying.
VFILE is defined to whatever type of file is in use:
- FILE if BBGE_BUILD_VFS is not defined
- tttvfs::VFSFile if it is.
Other changes:
- [un]packFile() is now unused and obsolete. That code has not been adjusted to use VFILE.
- glpng can now load from a memory buffer.
- TinyXML uses the VFS for reading operations now.
- The rather clunky binary stream loading of glfont2 got replaced with ByteBuffer,
which gets its data in one block (necessary to use the VFS without implementing
a somewhat STL-compliant std::ifstream replacement.)
-------------
Implement loading mods from zip files.
-------------
Implement soft-patching game data files. (Replacing textures/audio/... on the fly)
-------------
Misc bits:
- Extended GUI focus handling a bit
- Fixed weirdness in texture loading... not sure but this seems more correct to me.
Actually, considering that the texture will have its native size after restarting the game,
the lines removed with this commit seem pretty useless.
2012-06-01 15:52:19 +00:00
|
|
|
|
2016-07-09 02:18:40 +00:00
|
|
|
textures[0] = pngBindMem(memptr, memsize, PNG_BUILDMIPMAPS, PNG_ALPHA, &info, GL_CLAMP_TO_EDGE, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
if (textures[0] != 0)
|
|
|
|
{
|
|
|
|
width = info.Width;
|
|
|
|
height = info.Height;
|
2015-03-23 23:06:51 +00:00
|
|
|
good = true;
|
2016-03-13 00:37:43 +00:00
|
|
|
loadResult = TEX_SUCCESS;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-07 00:10:05 +00:00
|
|
|
fail:
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
debugLog("Can't load PNG file: " + file);
|
|
|
|
}
|
2011-09-15 16:33:13 +00:00
|
|
|
|
2014-04-07 00:10:05 +00:00
|
|
|
if(memptr)
|
|
|
|
delete [] memptr;
|
2011-09-15 17:18:53 +00:00
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
return good;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// internal load functions
|
2015-03-23 23:06:51 +00:00
|
|
|
bool Texture::loadTGA(const std::string &file)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2015-03-23 23:06:51 +00:00
|
|
|
return loadTGA(TGAload(file.c_str()));
|
2012-02-09 23:10:50 +00:00
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
bool Texture::loadZGA(const std::string &file)
|
2012-02-09 23:10:50 +00:00
|
|
|
{
|
|
|
|
unsigned long size = 0;
|
|
|
|
char *buf = readCompressedFile(file, &size);
|
|
|
|
ImageTGA *tga = TGAloadMem(buf, size);
|
|
|
|
if (!tga)
|
|
|
|
{
|
|
|
|
debugLog("Can't load ZGA File: " + file);
|
2015-03-23 23:06:51 +00:00
|
|
|
return false;
|
2012-02-09 23:10:50 +00:00
|
|
|
}
|
2015-03-23 23:06:51 +00:00
|
|
|
return loadTGA(tga);
|
2012-02-09 23:10:50 +00:00
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2015-03-23 23:06:51 +00:00
|
|
|
bool Texture::loadTGA(ImageTGA *imageTGA)
|
2012-02-09 23:10:50 +00:00
|
|
|
{
|
|
|
|
if (!imageTGA)
|
2015-03-23 23:06:51 +00:00
|
|
|
return false;
|
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]);
|
2016-07-09 02:18:40 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
|
|
|
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2012-02-09 23:10:50 +00:00
|
|
|
if (imageTGA->channels==3)
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, 3, imageTGA->sizeX, imageTGA->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, imageTGA->data);
|
|
|
|
else if (imageTGA->channels==4)
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, 4,imageTGA->sizeX, imageTGA->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageTGA->data);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
width = imageTGA->sizeX;
|
|
|
|
height = imageTGA->sizeY;
|
|
|
|
|
2012-02-09 23:10:50 +00:00
|
|
|
if (imageTGA->data)
|
|
|
|
delete[] (imageTGA->data);
|
|
|
|
free (imageTGA);
|
2015-03-23 23:06:51 +00:00
|
|
|
|
2016-03-13 00:37:43 +00:00
|
|
|
loadResult = TEX_SUCCESS;
|
2015-03-23 23:06:51 +00:00
|
|
|
return true;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define TGA_RGB 2 // This tells us it's a normal RGB (really BGR) file
|
|
|
|
#define TGA_A 3 // This tells us it's a ALPHA file
|
|
|
|
#define TGA_RLE 10 // This tells us that the targa is Run-Length Encoded (RLE)
|
|
|
|
|
|
|
|
#if defined(BBGE_BUILD_UNIX)
|
|
|
|
typedef uint8_t byte;
|
|
|
|
typedef uint16_t WORD;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef BBGE_BUILD_WINDOWS
|
|
|
|
#define byte char
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ImageTGA *Texture::TGAload(const char *filename)
|
|
|
|
{
|
2012-02-09 23:10:50 +00:00
|
|
|
unsigned long size = 0;
|
|
|
|
char *rawbuf = readFile(filename, &size);
|
|
|
|
ImageTGA *tga = TGAloadMem(rawbuf, size);
|
|
|
|
if (rawbuf)
|
|
|
|
delete [] rawbuf;
|
|
|
|
if (!tga)
|
|
|
|
{
|
|
|
|
debugLog("Can't load TGA File!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return tga;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageTGA *Texture::TGAloadMem(void *mem, int size)
|
|
|
|
{
|
|
|
|
if (!mem || size < 20)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ByteBuffer bb(mem, size, ByteBuffer::REUSE);
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
ImageTGA *pImageData = NULL; // This stores our important image data
|
2016-07-09 02:18:40 +00:00
|
|
|
unsigned short width = 0, height = 0;// The dimensions of the image
|
2011-08-03 20:05:33 +00:00
|
|
|
byte length = 0; // The length in bytes to the pixels
|
|
|
|
byte imageType = 0; // The image type (RLE, RGB, Alpha...)
|
|
|
|
byte bits = 0; // The bits per pixel for the image (16, 24, 32)
|
2017-01-14 17:10:20 +00:00
|
|
|
uint32_t channels = 0; // The channels of the image (3 = RGA : 4 = RGBA)
|
|
|
|
uint32_t stride = 0; // The stride (channels * width)
|
|
|
|
size_t i = 0; // A counter
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// This function loads in a TARGA (.TGA) file and returns its data to be
|
|
|
|
// used as a texture or what have you. This currently loads in a 16, 24
|
|
|
|
// and 32-bit targa file, along with RLE compressed files. Eventually you
|
|
|
|
// will want to do more error checking to make it more robust. This is
|
|
|
|
// also a perfect start to go into a modular class for an engine.
|
|
|
|
// Basically, how it works is, you read in the header information, then
|
|
|
|
// move your file pointer to the pixel data. Before reading in the pixel
|
|
|
|
// data, we check to see the if it's an RLE compressed image. This is because
|
|
|
|
// we will handle it different. If it isn't compressed, then we need another
|
|
|
|
// check to see if we need to convert it from 16-bit to 24 bit. 24-bit and
|
|
|
|
// 32-bit textures are very similar, so there's no need to do anything special.
|
|
|
|
// We do, however, read in an extra bit for each color.
|
|
|
|
|
|
|
|
|
|
|
|
// Allocate the structure that will hold our eventual image data (must free it!)
|
|
|
|
pImageData = (ImageTGA*)malloc(sizeof(ImageTGA));
|
|
|
|
|
|
|
|
// Read in the length in bytes from the header to the pixel data
|
2012-02-09 23:10:50 +00:00
|
|
|
bb >> length;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Jump over one byte
|
2012-02-09 23:10:50 +00:00
|
|
|
bb.skipRead(1);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Read in the imageType (RLE, RGB, etc...)
|
|
|
|
//fread(&imageType, sizeof(byte), 1, pFile);
|
2012-02-09 23:10:50 +00:00
|
|
|
bb >> imageType;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Skip past general information we don't care about
|
2012-02-09 23:10:50 +00:00
|
|
|
bb.skipRead(9);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Read the width, height and bits per pixel (16, 24 or 32)
|
2012-02-09 23:10:50 +00:00
|
|
|
bb >> width >> height >> bits;
|
2012-07-12 03:46:18 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
/*
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "TGALoad: width: " << width << " height: " << height << " bits: " << bits;
|
|
|
|
debugLog(os.str());
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Now we move the file pointer to the pixel data
|
2012-02-09 23:10:50 +00:00
|
|
|
bb.skipRead(length + 1);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Check if the image is RLE compressed or not
|
|
|
|
if(imageType != TGA_RLE)
|
|
|
|
{
|
|
|
|
// Check if the image is a 24 or 32-bit image
|
|
|
|
if(bits == 24 || bits == 32)
|
|
|
|
{
|
|
|
|
// Calculate the channels (3 or 4) - (use bits >> 3 for more speed).
|
|
|
|
// Next, we calculate the stride and allocate enough memory for the pixels.
|
|
|
|
channels = bits / 8;
|
|
|
|
stride = channels * width;
|
|
|
|
pImageData->data = new unsigned char[stride * height];
|
|
|
|
|
|
|
|
// Load in all the pixel data line by line
|
|
|
|
for(int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
// Store a pointer to the current line of pixels
|
|
|
|
unsigned char *pLine = &(pImageData->data[stride * y]);
|
|
|
|
|
|
|
|
// Read in the current line of pixels
|
2012-02-09 23:10:50 +00:00
|
|
|
if (bb.readable() < stride)
|
2011-08-03 20:05:33 +00:00
|
|
|
break;
|
2012-02-09 23:10:50 +00:00
|
|
|
bb.read(pLine, stride);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Go through all of the pixels and swap the B and R values since TGA
|
|
|
|
// files are stored as BGR instead of RGB (or use GL_BGR_EXT verses GL_RGB)
|
|
|
|
for(i = 0; i < stride; i += channels)
|
|
|
|
{
|
2017-01-12 21:51:46 +00:00
|
|
|
int temp = pLine[i];
|
|
|
|
pLine[i] = pLine[i + 2];
|
2011-08-03 20:05:33 +00:00
|
|
|
pLine[i + 2] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check if the image is a 16 bit image (RGB stored in 1 unsigned short)
|
|
|
|
else if(bits == 16)
|
|
|
|
{
|
|
|
|
unsigned short pixels = 0;
|
|
|
|
int r=0, g=0, b=0;
|
|
|
|
|
|
|
|
// Since we convert 16-bit images to 24 bit, we hardcode the channels to 3.
|
|
|
|
// We then calculate the stride and allocate memory for the pixels.
|
|
|
|
channels = 3;
|
|
|
|
stride = channels * width;
|
|
|
|
pImageData->data = new unsigned char[stride * height];
|
|
|
|
|
|
|
|
// Load in all the pixel data pixel by pixel
|
|
|
|
for(int i = 0; i < width*height; i++)
|
|
|
|
{
|
|
|
|
// Read in the current pixel
|
2012-02-09 23:10:50 +00:00
|
|
|
if (bb.readable() < sizeof(unsigned char))
|
2011-08-03 20:05:33 +00:00
|
|
|
break;
|
2012-02-09 23:10:50 +00:00
|
|
|
bb >> pixels;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// To convert a 16-bit pixel into an R, G, B, we need to
|
|
|
|
// do some masking and such to isolate each color value.
|
|
|
|
// 0x1f = 11111 in binary, so since 5 bits are reserved in
|
|
|
|
// each unsigned short for the R, G and B, we bit shift and mask
|
|
|
|
// to find each value. We then bit shift up by 3 to get the full color.
|
|
|
|
b = (pixels & 0x1f) << 3;
|
|
|
|
g = ((pixels >> 5) & 0x1f) << 3;
|
|
|
|
r = ((pixels >> 10) & 0x1f) << 3;
|
|
|
|
|
|
|
|
// This essentially assigns the color to our array and swaps the
|
|
|
|
// B and R values at the same time.
|
|
|
|
pImageData->data[i * 3 + 0] = r;
|
|
|
|
pImageData->data[i * 3 + 1] = g;
|
|
|
|
pImageData->data[i * 3 + 2] = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Else return a NULL for a bad or unsupported pixel format
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Else, it must be Run-Length Encoded (RLE)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// First, let me explain real quickly what RLE is.
|
|
|
|
// For further information, check out Paul Bourke's intro article at:
|
|
|
|
// http://astronomy.swin.edu.au/~pbourke/dataformats/rle/
|
|
|
|
//
|
|
|
|
// Anyway, we know that RLE is a basic type compression. It takes
|
|
|
|
// colors that are next to each other and then shrinks that info down
|
|
|
|
// into the color and a integer that tells how much of that color is used.
|
|
|
|
// For instance:
|
|
|
|
// aaaaabbcccccccc would turn into a5b2c8
|
|
|
|
// Well, that's fine and dandy and all, but how is it down with RGB colors?
|
|
|
|
// Simple, you read in an color count (rleID), and if that number is less than 128,
|
|
|
|
// it does NOT have any optimization for those colors, so we just read the next
|
|
|
|
// pixels normally. Say, the color count was 28, we read in 28 colors like normal.
|
|
|
|
// If the color count is over 128, that means that the next color is optimized and
|
|
|
|
// we want to read in the same pixel color for a count of (colorCount - 127).
|
|
|
|
// It's 127 because we add 1 to the color count, as you'll notice in the code.
|
|
|
|
|
|
|
|
// Create some variables to hold the rleID, current colors read, channels, & stride.
|
|
|
|
byte rleID = 0;
|
|
|
|
int colorsRead = 0;
|
|
|
|
channels = bits / 8;
|
|
|
|
stride = channels * width;
|
|
|
|
|
|
|
|
// Next we want to allocate the memory for the pixels and create an array,
|
|
|
|
// depending on the channel count, to read in for each pixel.
|
|
|
|
pImageData->data = new unsigned char[stride * height];
|
|
|
|
byte *pColors = new byte [channels];
|
|
|
|
|
|
|
|
// Load in all the pixel data
|
|
|
|
while(i < width*height)
|
|
|
|
{
|
|
|
|
// Read in the current color count + 1
|
2012-02-09 23:10:50 +00:00
|
|
|
bb >> rleID;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Check if we don't have an encoded string of colors
|
|
|
|
if(rleID < 128)
|
|
|
|
{
|
|
|
|
// Increase the count by 1
|
|
|
|
rleID++;
|
|
|
|
|
|
|
|
// Go through and read all the unique colors found
|
|
|
|
while(rleID)
|
|
|
|
{
|
|
|
|
// Read in the current color
|
2012-02-09 23:10:50 +00:00
|
|
|
if (bb.readable() < channels)
|
2011-08-03 20:05:33 +00:00
|
|
|
break;
|
2012-02-09 23:10:50 +00:00
|
|
|
bb.read(pColors, channels);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Store the current pixel in our image array
|
|
|
|
pImageData->data[colorsRead + 0] = pColors[2];
|
|
|
|
pImageData->data[colorsRead + 1] = pColors[1];
|
|
|
|
pImageData->data[colorsRead + 2] = pColors[0];
|
|
|
|
|
|
|
|
// If we have a 4 channel 32-bit image, assign one more for the alpha
|
|
|
|
if(bits == 32)
|
|
|
|
pImageData->data[colorsRead + 3] = pColors[3];
|
|
|
|
|
|
|
|
// Increase the current pixels read, decrease the amount
|
|
|
|
// of pixels left, and increase the starting index for the next pixel.
|
|
|
|
i++;
|
|
|
|
rleID--;
|
|
|
|
colorsRead += channels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Else, let's read in a string of the same character
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Minus the 128 ID + 1 (127) to get the color count that needs to be read
|
|
|
|
rleID -= 127;
|
|
|
|
|
|
|
|
// Read in the current color, which is the same for a while
|
2012-02-09 23:10:50 +00:00
|
|
|
if (bb.readable() < channels)
|
2011-08-03 20:05:33 +00:00
|
|
|
break;
|
2012-02-09 23:10:50 +00:00
|
|
|
bb.read(pColors, channels);
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Go and read as many pixels as are the same
|
|
|
|
while(rleID)
|
|
|
|
{
|
|
|
|
// Assign the current pixel to the current index in our pixel array
|
|
|
|
pImageData->data[colorsRead + 0] = pColors[2];
|
|
|
|
pImageData->data[colorsRead + 1] = pColors[1];
|
|
|
|
pImageData->data[colorsRead + 2] = pColors[0];
|
|
|
|
|
|
|
|
// If we have a 4 channel 32-bit image, assign one more for the alpha
|
|
|
|
if(bits == 32)
|
|
|
|
pImageData->data[colorsRead + 3] = pColors[3];
|
|
|
|
|
|
|
|
// Increase the current pixels read, decrease the amount
|
|
|
|
// of pixels left, and increase the starting index for the next pixel.
|
|
|
|
i++;
|
|
|
|
rleID--;
|
|
|
|
colorsRead += channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free up pColors
|
|
|
|
delete[] pColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in our tImageTGA structure to pass back
|
|
|
|
pImageData->channels = channels;
|
2017-01-12 21:51:46 +00:00
|
|
|
pImageData->sizeX = width;
|
|
|
|
pImageData->sizeY = height;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
// Return the TGA data (remember, you must free this data after you are done)
|
|
|
|
return pImageData;
|
|
|
|
}
|
|
|
|
|
2012-06-15 02:12:20 +00:00
|
|
|
// ceil to next power of 2
|
|
|
|
static unsigned int clp2(unsigned int x)
|
|
|
|
{
|
|
|
|
--x;
|
|
|
|
x |= (x >> 1);
|
|
|
|
x |= (x >> 2);
|
|
|
|
x |= (x >> 4);
|
|
|
|
x |= (x >> 8);
|
|
|
|
x |= (x >> 16);
|
|
|
|
return x + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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.
|
2012-06-15 02:19:03 +00:00
|
|
|
tw = clp2(width); // known to be > 0.
|
|
|
|
th = 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;
|
|
|
|
}
|