1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "System/mmgr.h"
#include "TextureAtlas.h"
#include "Bitmap.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GL/myGL.h"
#include "Rendering/GL/PBO.h"
#include "System/FileSystem/FileHandler.h"
#include "System/Log/ILog.h"
#include "System/Util.h"
#include "System/Exceptions.h"
#include "System/Vec2.h"
#include <list>
#include <cstring>
CR_BIND(AtlasedTexture, );
CR_BIND_DERIVED(GroundFXTexture, AtlasedTexture, );
// texture spacing in the atlas (in pixels)
#define TEXMARGIN 2
bool CTextureAtlas::debug;
CTextureAtlas::CTextureAtlas(int maxxsize, int maxysize)
: gltex(0)
, freeTexture(true)
, xsize(4)
, ysize(4)
, maxxsize(maxxsize)
, maxysize(maxysize)
, usedPixels(0)
, initialized(false)
{
}
CTextureAtlas::~CTextureAtlas()
{
if (initialized && freeTexture) {
glDeleteTextures(1, &gltex);
}
}
void* CTextureAtlas::AddTex(std::string name, int xsize, int ysize, TextureType texType)
{
const int gpp = GetBPP(texType);
const size_t data_size = xsize * ysize * gpp / 8;
MemTex* memtex = new MemTex;
memtex->xsize = xsize;
memtex->ysize = ysize;
memtex->xpos = 0;
memtex->ypos = 0;
memtex->texType = texType;
memtex->data = new char[data_size];
StringToLowerInPlace(name);
memtex->names.push_back(name);
memtextures.push_back(memtex);
return memtex->data;
}
int CTextureAtlas::AddTexFromMem(std::string name, int xsize, int ysize, TextureType texType, void* data)
{
const int gpp = GetBPP(texType);
const size_t data_size = xsize * ysize * gpp / 8;
MemTex* memtex = new MemTex;
memtex->xsize = xsize;
memtex->ysize = ysize;
memtex->xpos = 0;
memtex->ypos = 0;
memtex->texType = texType;
memtex->data = new char[data_size];
StringToLowerInPlace(name);
memtex->names.push_back(name);
std::memcpy(memtex->data, data, data_size);
memtextures.push_back(memtex);
return 1;
}
int CTextureAtlas::AddTexFromFile(std::string name, std::string file)
{
StringToLowerInPlace(name);
// if the file is already loaded, use that instead
std::string lcFile = StringToLower(file);
std::map<std::string, MemTex*>::iterator it = files.find(lcFile);
if (it != files.end()) {
MemTex* memtex = it->second;
memtex->names.push_back(name);
return 1;
}
CBitmap bitmap;
if (!bitmap.Load(file)) {
throw content_error("Could not load texture from file " + file);
}
if (bitmap.type != CBitmap::BitmapTypeStandardRGBA) {
// only suport RGBA for now
throw content_error("Unsupported bitmap format in file " + file);
}
const int ret = AddTexFromMem(name, bitmap.xsize, bitmap.ysize, RGBA32, bitmap.mem);
if (ret == 1) {
files[lcFile] = memtextures.back();
}
return ret;
}
bool CTextureAtlas::Finalize()
{
sort(memtextures.begin(), memtextures.end(), CTextureAtlas::CompareTex);
bool success = true;
int maxx = 0;
int curx = 0;
int maxy = 0;
int cury = 0;
std::list<int2> nextSub;
std::list<int2> thisSub;
bool recalc = false;
for (int a = 0; a < static_cast<int>(memtextures.size()); ++a) {
MemTex* curtex = memtextures[a];
bool done = false;
while (!done) {
if (thisSub.empty()) {
if (nextSub.empty()) {
maxx = std::max(maxx, curx);
cury = maxy;
maxy += curtex->ysize + TEXMARGIN;
if(maxy > ysize) {
if(IncreaseSize()) {
nextSub.clear();
thisSub.clear();
cury = maxy = curx = 0;
recalc = true;
break;
} else {
success = false;
break;
}
}
thisSub.push_back(int2(0, cury));
} else {
thisSub = nextSub;
nextSub.clear();
}
}
if (thisSub.front().x + curtex->xsize > xsize) {
thisSub.clear();
continue;
}
if (thisSub.front().y + curtex->ysize > maxy) {
thisSub.pop_front();
continue;
}
//ok found space for us
curtex->xpos = thisSub.front().x;
curtex->ypos = thisSub.front().y;
done = true;
if (thisSub.front().y + curtex->ysize + TEXMARGIN < maxy) {
nextSub.push_back(int2(thisSub.front().x + TEXMARGIN, thisSub.front().y + curtex->ysize + TEXMARGIN));
}
thisSub.front().x += curtex->xsize + TEXMARGIN;
while (thisSub.size()>1 && thisSub.front().x >= (++thisSub.begin())->x) {
(++thisSub.begin())->x = thisSub.front().x;
thisSub.erase(thisSub.begin());
}
}
if (recalc) {
recalc = false;
a = -1;
continue;
}
}
if (globalRendering->supportNPOTs && !debug) {
maxx = std::max(maxx,curx);
//xsize = maxx;
ysize = maxy;
}
CreateTexture();
for(std::vector<MemTex*>::iterator it = memtextures.begin(); it != memtextures.end(); ++it) {
AtlasedTexture tex;
//adjust texture coordinates by half a pixel (in opengl pixel centers are centeriods)
tex.xstart = ((*it)->xpos + 0.5f) / (float)(xsize);
tex.xend = (((*it)->xpos + (*it)->xsize-1) + 0.5f) / (float)(xsize);
tex.ystart = ((*it)->ypos + 0.5f) / (float)(ysize);
tex.yend = (((*it)->ypos + (*it)->ysize-1) + 0.5f) / (float)(ysize);
tex.ixstart = (*it)->xpos;
tex.iystart = (*it)->ypos;
for (size_t n = 0; n < (*it)->names.size(); ++n) {
textures[(*it)->names[n]] = tex;
}
usedPixels += (*it)->xpos * (*it)->ypos;
delete[] (char*)(*it)->data;
delete (*it);
}
memtextures.clear();
return success;
}
void CTextureAtlas::CreateTexture()
{
PBO pbo;
pbo.Bind();
pbo.Resize(xsize * ysize * 4);
unsigned char* data = (unsigned char*)pbo.MapBuffer(GL_WRITE_ONLY);
std::memset(data, 0, xsize * ysize * 4); //! make spacing between textures black transparent to avoid ugly lines with linear filtering
for (size_t i = 0; i < memtextures.size(); ++i) {
MemTex& tex = *memtextures[i];
for (int y = 0; y < tex.ysize; ++y) {
int* dst = ((int*)data) + tex.xpos + (tex.ypos + y) * xsize;
int* src = ((int*)tex.data) + y * tex.xsize;
memcpy(dst, src, tex.xsize * 4);
}
}
if (debug) {
//! hack to make sure we don't overwrite our own atlases
static int count = 0;
char fname[256];
SNPRINTF(fname, sizeof(fname), "textureatlas%d.png", ++count);
//! even pbo.MapBuffer(GL_WRITE_ONLY) we can readback from it. GL_READ is only needed if we want to readback GPU data!
CBitmap save(data,xsize,ysize);
save.Save(fname);
LOG("Saved finalized texture-atlas to '%s'.", fname);
}
pbo.UnmapBuffer();
glGenTextures(1, &gltex);
glBindTexture(GL_TEXTURE_2D, gltex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST/*GL_NEAREST_MIPMAP_LINEAR*/);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, xsize, ysize, 0, GL_RGBA, GL_UNSIGNED_BYTE, pbo.GetPtr());
pbo.Unbind();
initialized = true;
}
int CTextureAtlas::GetBPP(TextureType texType)
{
switch(texType) {
case RGBA32:
return 32;
default:
return 32;
}
}
int CTextureAtlas::CompareTex(MemTex* tex1, MemTex* tex2)
{
//sort in reverse order
if (tex1->ysize == tex2->ysize) {
return (tex1->xsize > tex2->xsize);
}
return (tex1->ysize > tex2->ysize);
}
bool CTextureAtlas::IncreaseSize()
{
if (ysize < xsize) {
if (ysize < maxysize) {
ysize *= 2;
return true;
} else {
if (xsize < maxxsize) {
xsize *= 2;
return true;
}
}
} else {
if (xsize < maxxsize) {
xsize *= 2;
return true;
} else {
if (ysize < maxysize) {
ysize *= 2;
return true;
}
}
}
return false;
}
void CTextureAtlas::BindTexture()
{
glBindTexture(GL_TEXTURE_2D, gltex);
}
bool CTextureAtlas::TextureExists(const std::string& name)
{
return textures.find(StringToLower(name)) != textures.end();
}
AtlasedTexture CTextureAtlas::GetTexture(const std::string& name)
{
return textures[StringToLower(name)];
}
AtlasedTexture* CTextureAtlas::GetTexturePtr(const std::string& name)
{
return &textures[StringToLower(name)];
}
AtlasedTexture CTextureAtlas::GetTextureWithBackup(const std::string& name, const std::string& backupName)
{
if (textures.find(StringToLower(name)) != textures.end()) {
return textures[StringToLower(name)];
} else {
return textures[StringToLower(backupName)];
}
}
AtlasedTexture* CTextureAtlas::GetTexturePtrWithBackup(const std::string& name, const std::string& backupName)
{
if (textures.find(StringToLower(name)) != textures.end()) {
return &textures[StringToLower(name)];
} else {
return &textures[StringToLower(backupName)];
}
}
|