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
|
// NamedTextures.cpp: implementation of the CNamedTextures class.
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "mmgr.h"
#include "bitops.h"
#include "NamedTextures.h"
#include "Rendering/GL/myGL.h"
#include "Bitmap.h"
#include "GlobalUnsynced.h"
#include "Vec2.h"
map<string, CNamedTextures::TexInfo> CNamedTextures::texMap;
std::vector<string> CNamedTextures::texWaiting;
/******************************************************************************/
void CNamedTextures::Init()
{
}
void CNamedTextures::Kill()
{
map<string, TexInfo>::iterator it;
for (it = texMap.begin(); it != texMap.end(); ++it) {
const GLuint texID = it->second.id;
glDeleteTextures(1, &texID);
}
}
/******************************************************************************/
bool CNamedTextures::Bind(const string& texName)
{
if (texName.empty()) {
return false;
}
map<string, TexInfo>::iterator it = texMap.find(texName);
if (it != texMap.end()) {
const GLuint texID = it->second.id;
if (texID == 0) {
glBindTexture(GL_TEXTURE_2D, 0);
return false;
} else {
glBindTexture(GL_TEXTURE_2D, texID);
return true;
}
}
GLboolean inListCompile;
glGetBooleanv(GL_LIST_INDEX, &inListCompile);
if (inListCompile) {
GLuint texID = 0;
glGenTextures(1, &texID);
TexInfo texInfo;
texInfo.id = texID;
texMap[texName] = texInfo;
glBindTexture(GL_TEXTURE_2D, texID);
texWaiting.push_back(texName);
return true;
}
GLuint texID = 0;
glGenTextures(1, &texID);
return Load(texName, texID);
}
bool CNamedTextures::Load(const string& texName, GLuint texID)
{
//! strip off the qualifiers
string filename = texName;
bool border = false;
bool clamped = false;
bool nearest = false;
bool linear = false;
bool aniso = false;
bool invert = false;
bool greyed = false;
bool tint = false;
float tintColor[3];
bool resize = false;
int2 resizeDimensions;
if (filename[0] == ':') {
int p;
for (p = 1; p < (int)filename.size(); p++) {
const char ch = filename[p];
if (ch == ':') { break; }
else if (ch == 'n') { nearest = true; }
else if (ch == 'l') { linear = true; }
else if (ch == 'a') { aniso = true; }
else if (ch == 'i') { invert = true; }
else if (ch == 'g') { greyed = true; }
else if (ch == 'c') { clamped = true; }
else if (ch == 'b') { border = true; }
else if (ch == 't') {
const char* cstr = filename.c_str() + p + 1;
const char* start = cstr;
char* endptr;
tintColor[0] = (float)strtod(start, &endptr);
if ((start != endptr) && (*endptr == ',')) {
start = endptr + 1;
tintColor[1] = (float)strtod(start, &endptr);
if ((start != endptr) && (*endptr == ',')) {
start = endptr + 1;
tintColor[2] = (float)strtod(start, &endptr);
if (start != endptr) {
tint = true;
p += (endptr - cstr);
}
}
}
}
else if (ch == 'r') {
const char* cstr = filename.c_str() + p + 1;
const char* start = cstr;
char* endptr;
resizeDimensions.x = (int)strtoul(start, &endptr, 10);
if ((start != endptr) && (*endptr == ',')) {
start = endptr + 1;
resizeDimensions.y = (int)strtoul(start, &endptr, 10);
if (start != endptr) {
resize = true;
p += (endptr - cstr);
}
}
}
}
if (p < (int)filename.size()) {
filename = filename.substr(p + 1);
} else {
filename.clear();
}
}
//! get the image
CBitmap bitmap;
TexInfo texInfo;
if (!bitmap.Load(filename)) {
texMap[texName] = texInfo;
glBindTexture(GL_TEXTURE_2D, 0);
return false;
}
if (bitmap.type == CBitmap::BitmapTypeDDS) {
texID = bitmap.CreateDDSTexture(texID);
} else {
if (resize) {
bitmap = bitmap.CreateRescaled(resizeDimensions.x,resizeDimensions.y);
}
if (invert) {
bitmap.InvertColors();
}
if (greyed) {
bitmap.GrayScale();
}
if (tint) {
bitmap.Tint(tintColor);
}
//! make the texture
glBindTexture(GL_TEXTURE_2D, texID);
if (clamped) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
if (nearest || linear) {
if (border) {
GLfloat white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, white);
}
if (nearest) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
//! Note: NPOTs + nearest filtering seems broken on ATIs
if ( !(count_bits_set(bitmap.xsize)==1 && count_bits_set(bitmap.ysize)==1) &&
(!GLEW_ARB_texture_non_power_of_two || (gu->atiHacks && nearest)) )
{
bitmap = bitmap.CreateRescaled(next_power_of_2(bitmap.xsize),next_power_of_2(bitmap.ysize));
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
bitmap.xsize, bitmap.ysize, border ? 1 : 0,
GL_RGBA, GL_UNSIGNED_BYTE, bitmap.mem);
} else {
//! MIPMAPPING (default)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
if ((count_bits_set(bitmap.xsize)==1 && count_bits_set(bitmap.ysize)==1) ||
GLEW_ARB_texture_non_power_of_two)
{
glBuildMipmaps(GL_TEXTURE_2D, GL_RGBA8, bitmap.xsize, bitmap.ysize,
GL_RGBA, GL_UNSIGNED_BYTE, bitmap.mem);
} else {
//! glu auto resizes to next POT
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, bitmap.xsize, bitmap.ysize,
GL_RGBA, GL_UNSIGNED_BYTE, bitmap.mem);
}
}
if (aniso && GLEW_EXT_texture_filter_anisotropic) {
static GLfloat maxAniso = -1.0f;
if (maxAniso == -1.0f) {
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
}
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
}
}
texInfo.id = texID;
texInfo.type = bitmap.type;
texInfo.xsize = bitmap.xsize;
texInfo.ysize = bitmap.ysize;
texMap[texName] = texInfo;
return true;
}
void CNamedTextures::Update()
{
if (texWaiting.empty()) {
return;
}
glPushAttrib(GL_TEXTURE_BIT);
for (std::vector<string>::iterator it = texWaiting.begin(); it != texWaiting.end(); it++) {
map<string, TexInfo>::iterator mit = texMap.find(*it);
if (mit != texMap.end()) {
Load(*it,mit->second.id);
}
}
glPopAttrib();
texWaiting.clear();
}
bool CNamedTextures::Free(const string& texName)
{
if (texName.empty()) {
return false;
}
map<string, TexInfo>::iterator it = texMap.find(texName);
if (it != texMap.end()) {
const GLuint texID = it->second.id;
glDeleteTextures(1, &texID);
texMap.erase(it);
return true;
}
return false;
}
const CNamedTextures::TexInfo* CNamedTextures::GetInfo(const string& texName)
{
if (texName.empty()) {
return false;
}
map<string, TexInfo>::const_iterator it = texMap.find(texName);
if (it != texMap.end()) {
return &it->second;
}
return NULL;
}
/******************************************************************************/
|