| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
#include <cassert> |
| 24 |
#include <iostream> |
| 25 |
// 3rd party library includes |
| 26 |
|
| 27 |
// FIFE includes |
| 28 |
// These includes are split up in two parts, separated by one empty line |
| 29 |
// First block: files included from the FIFE root src directory |
| 30 |
// Second block: files included from the same folder |
| 31 |
#include "util/structures/rect.h" |
| 32 |
#include "video/imagemanager.h" |
| 33 |
#include "video/sdl/sdlimage.h" |
| 34 |
#include "video/renderbackend.h" |
| 35 |
#include "video/opengl/renderbackendopengl.h" |
| 36 |
|
| 37 |
#include "glimage.h" |
| 38 |
|
| 39 |
namespace FIFE { |
| 40 |
GLImage::GLImage(IResourceLoader* loader): |
| 41 |
Image(loader), |
| 42 |
m_compressed(false), |
| 43 |
m_texId(0) { |
| 44 |
|
| 45 |
resetGlimage(); |
| 46 |
} |
| 47 |
|
| 48 |
GLImage::GLImage(const std::string& name, IResourceLoader* loader): |
| 49 |
Image(name, loader), |
| 50 |
m_compressed(false), |
| 51 |
m_texId(0) { |
| 52 |
|
| 53 |
resetGlimage(); |
| 54 |
} |
| 55 |
|
| 56 |
GLImage::GLImage(SDL_Surface* surface): |
| 57 |
Image(surface), |
| 58 |
m_compressed(false), |
| 59 |
m_texId(0) { |
| 60 |
|
| 61 |
resetGlimage(); |
| 62 |
} |
| 63 |
|
| 64 |
GLImage::GLImage(const std::string& name, SDL_Surface* surface): |
| 65 |
Image(name, surface), |
| 66 |
m_compressed(false), |
| 67 |
m_texId(0) { |
| 68 |
|
| 69 |
resetGlimage(); |
| 70 |
} |
| 71 |
|
| 72 |
GLImage::GLImage(const uint8_t* data, uint32_t width, uint32_t height): |
| 73 |
Image(data, width, height), |
| 74 |
m_compressed(false), |
| 75 |
m_texId(0) { |
| 76 |
|
| 77 |
assert(m_surface); |
| 78 |
resetGlimage(); |
| 79 |
} |
| 80 |
|
| 81 |
GLImage::GLImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height): |
| 82 |
Image(name, data, width, height), |
| 83 |
m_compressed(false), |
| 84 |
m_texId(0) { |
| 85 |
|
| 86 |
assert(m_surface); |
| 87 |
resetGlimage(); |
| 88 |
} |
| 89 |
|
| 90 |
GLImage::~GLImage() { |
| 91 |
cleanup(); |
| 92 |
} |
| 93 |
|
| 94 |
void GLImage::invalidate() { |
| 95 |
resetGlimage(); |
| 96 |
} |
| 97 |
|
| 98 |
void GLImage::setSurface(SDL_Surface* surface) { |
| 99 |
reset(surface); |
| 100 |
resetGlimage(); |
| 101 |
} |
| 102 |
|
| 103 |
void GLImage::resetGlimage() { |
| 104 |
cleanup(); |
| 105 |
|
| 106 |
m_chunk_size_w = 0; |
| 107 |
m_chunk_size_h = 0; |
| 108 |
|
| 109 |
m_colorkey = RenderBackend::instance()->getColorKey(); |
| 110 |
} |
| 111 |
|
| 112 |
void GLImage::cleanup() { |
| 113 |
if (m_texId) { |
| 114 |
if(!m_shared) { |
| 115 |
glDeleteTextures(1, &m_texId); |
| 116 |
} |
| 117 |
m_texId = 0; |
| 118 |
m_compressed = false; |
| 119 |
} |
| 120 |
|
| 121 |
m_tex_coords[0] = m_tex_coords[1] = |
| 122 |
m_tex_coords[2] = m_tex_coords[3] = 0.0f; |
| 123 |
} |
| 124 |
|
| 125 |
void GLImage::render(const Rect& rect, uint8_t alpha, uint8_t const* rgb) { |
| 126 |
// completely transparent so dont bother rendering |
| 127 |
if (0 == alpha) { |
| 128 |
return; |
| 129 |
} |
| 130 |
RenderBackend* rb = RenderBackend::instance(); |
| 131 |
SDL_Surface* target = rb->getRenderTargetSurface(); |
| 132 |
assert(target != m_surface); // can't draw on the source surface |
| 133 |
|
| 134 |
// not on the screen. dont render |
| 135 |
if (rect.right() < 0 || rect.x > static_cast<int32_t>(target->w) || |
| 136 |
rect.bottom() < 0 || rect.y > static_cast<int32_t>(target->h)) { |
| 137 |
return; |
| 138 |
} |
| 139 |
if (!m_texId) { |
| 140 |
generateGLTexture(); |
| 141 |
} else if (m_shared) { |
| 142 |
validateShared(); |
| 143 |
} |
| 144 |
|
| 145 |
rb->addImageToArray(m_texId, rect, m_tex_coords, alpha, rgb); |
| 146 |
} |
| 147 |
|
| 148 |
void GLImage::render(const Rect& rect, const ImagePtr& overlay, uint8_t alpha, uint8_t const* rgb) { |
| 149 |
// completely transparent so dont bother rendering |
| 150 |
if (0 == alpha) { |
| 151 |
return; |
| 152 |
} |
| 153 |
RenderBackend* rb = RenderBackend::instance(); |
| 154 |
SDL_Surface* target = rb->getRenderTargetSurface(); |
| 155 |
assert(target != m_surface); // can't draw on the source surface |
| 156 |
|
| 157 |
// not on the screen. dont render |
| 158 |
if (rect.right() < 0 || rect.x > static_cast<int32_t>(target->w) || |
| 159 |
rect.bottom() < 0 || rect.y > static_cast<int32_t>(target->h)) { |
| 160 |
return; |
| 161 |
} |
| 162 |
if (!m_texId) { |
| 163 |
generateGLTexture(); |
| 164 |
} else if (m_shared) { |
| 165 |
validateShared(); |
| 166 |
} |
| 167 |
|
| 168 |
GLImage* img = static_cast<GLImage*>(overlay.get()); |
| 169 |
img->forceLoadInternal(); |
| 170 |
|
| 171 |
static_cast<RenderBackendOpenGL*>(rb)->addImageToArray(rect, m_texId, m_tex_coords, img->getTexId(), img->getTexCoords(), alpha, rgb); |
| 172 |
//rb->addImageToArray(rect, m_texId, m_tex_coords, img->getTexId(), img->getTexCoords(), alpha, rgb); |
| 173 |
} |
| 174 |
|
| 175 |
void GLImage::renderZ(const Rect& rect, float vertexZ, uint8_t alpha, uint8_t const* rgb) { |
| 176 |
// completely transparent so dont bother rendering |
| 177 |
if (0 == alpha) { |
| 178 |
return; |
| 179 |
} |
| 180 |
RenderBackend* rb = RenderBackend::instance(); |
| 181 |
SDL_Surface* target = rb->getRenderTargetSurface(); |
| 182 |
assert(target != m_surface); // can't draw on the source surface |
| 183 |
|
| 184 |
// not on the screen. dont render |
| 185 |
if (rect.right() < 0 || rect.x > static_cast<int32_t>(target->w) || |
| 186 |
rect.bottom() < 0 || rect.y > static_cast<int32_t>(target->h)) { |
| 187 |
return; |
| 188 |
} |
| 189 |
if (!m_texId) { |
| 190 |
generateGLTexture(); |
| 191 |
} else if (m_shared) { |
| 192 |
validateShared(); |
| 193 |
} |
| 194 |
static_cast<RenderBackendOpenGL*>(rb)->addImageToArrayZ(m_texId, rect, vertexZ, m_tex_coords, alpha, rgb); |
| 195 |
//rb->addImageToArray(m_texId, rect, m_tex_coords, alpha, rgb); |
| 196 |
} |
| 197 |
|
| 198 |
void GLImage::renderZ(const Rect& rect, float vertexZ, const ImagePtr& overlay, uint8_t alpha, uint8_t const* rgb) { |
| 199 |
// completely transparent so dont bother rendering |
| 200 |
if (0 == alpha) { |
| 201 |
return; |
| 202 |
} |
| 203 |
RenderBackend* rb = RenderBackend::instance(); |
| 204 |
SDL_Surface* target = rb->getRenderTargetSurface(); |
| 205 |
assert(target != m_surface); // can't draw on the source surface |
| 206 |
|
| 207 |
// not on the screen. dont render |
| 208 |
if (rect.right() < 0 || rect.x > static_cast<int32_t>(target->w) || |
| 209 |
rect.bottom() < 0 || rect.y > static_cast<int32_t>(target->h)) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
if (!m_texId) { |
| 214 |
generateGLTexture(); |
| 215 |
} else if (m_shared) { |
| 216 |
validateShared(); |
| 217 |
} |
| 218 |
|
| 219 |
GLImage* img = static_cast<GLImage*>(overlay.get()); |
| 220 |
img->forceLoadInternal(); |
| 221 |
|
| 222 |
static_cast<RenderBackendOpenGL*>(rb)->addImageToArrayZ(rect, vertexZ, m_texId, m_tex_coords, img->getTexId(), img->getTexCoords(), alpha, rgb); |
| 223 |
//rb->addImageToArray(rect, m_texId, m_tex_coords, img->getTexId(), img->getTexCoords(), alpha, rgb); |
| 224 |
} |
| 225 |
|
| 226 |
void GLImage::generateGLTexture() { |
| 227 |
if (m_shared) { |
| 228 |
// First make sure we loaded big image to opengl |
| 229 |
validateShared(); |
| 230 |
return; |
| 231 |
} |
| 232 |
// ultimate possibility to load the image |
| 233 |
// is used e.g. in case a cursor or gui image is freed even if there is a reference |
| 234 |
if (!m_surface) { |
| 235 |
if (m_state == IResource::RES_NOT_LOADED) { |
| 236 |
load(); |
| 237 |
} |
| 238 |
} |
| 239 |
const uint32_t width = m_surface->w; |
| 240 |
const uint32_t height = m_surface->h; |
| 241 |
|
| 242 |
// With OpenGL 2.0 or GL_ARB_texture_non_power_of_two we don't really need to care |
| 243 |
// about non power of 2 textures |
| 244 |
if(GLEW_ARB_texture_non_power_of_two && RenderBackend::instance()->isNPOTEnabled()) { |
| 245 |
m_chunk_size_w = width; |
| 246 |
m_chunk_size_h = height; |
| 247 |
} |
| 248 |
else { |
| 249 |
//calculate the nearest larger power of 2 |
| 250 |
m_chunk_size_w = nextPow2(width); |
| 251 |
m_chunk_size_h = nextPow2(height); |
| 252 |
} |
| 253 |
|
| 254 |
// used to calculate the fill ratio for given chunk |
| 255 |
m_tex_coords[0] = m_tex_coords[1] = 0.0f; |
| 256 |
m_tex_coords[2] = static_cast<float>(m_surface->w%m_chunk_size_w) / static_cast<float>(m_chunk_size_w); |
| 257 |
m_tex_coords[3] = static_cast<float>(m_surface->h%m_chunk_size_h) / static_cast<float>(m_chunk_size_h); |
| 258 |
|
| 259 |
if (m_tex_coords[2] == 0.0f){ |
| 260 |
m_tex_coords[2] = 1.0f; |
| 261 |
} |
| 262 |
|
| 263 |
if (m_tex_coords[3] == 0.0f){ |
| 264 |
m_tex_coords[3] = 1.0f; |
| 265 |
} |
| 266 |
|
| 267 |
uint8_t* data = static_cast<uint8_t*>(m_surface->pixels); |
| 268 |
int32_t pitch = m_surface->pitch; |
| 269 |
|
| 270 |
assert(!m_texId); |
| 271 |
|
| 272 |
// get texture id from opengl |
| 273 |
glGenTextures(1, &m_texId); |
| 274 |
// set focus on that texture |
| 275 |
static_cast<RenderBackendOpenGL*>(RenderBackend::instance())->bindTexture(m_texId); |
| 276 |
// set filters for texture |
| 277 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 278 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 279 |
bool mipmapping = RenderBackend::instance()->isMipmappingEnabled(); |
| 280 |
if (mipmapping) { |
| 281 |
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
| 282 |
} |
| 283 |
// without mipmapping, trilinear and bilinear filters are the same |
| 284 |
switch (RenderBackend::instance()->getTextureFiltering()) { |
| 285 |
case TEXTURE_FILTER_NONE: |
| 286 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 287 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmapping ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); |
| 288 |
break; |
| 289 |
case TEXTURE_FILTER_BILINEAR: |
| 290 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 291 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmapping ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR); |
| 292 |
break; |
| 293 |
case TEXTURE_FILTER_TRILINEAR: |
| 294 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 295 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmapping ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); |
| 296 |
break; |
| 297 |
case TEXTURE_FILTER_ANISOTROPIC: |
| 298 |
// currently trilinear anisotropic |
| 299 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, static_cast<GLint>(RenderBackend::instance()->getMaxAnisotropy())); |
| 300 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 301 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmapping ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR); |
| 302 |
break; |
| 303 |
default: |
| 304 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 305 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmapping ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
GLint internalFormat = GL_RGBA8; |
| 310 |
if(GLEW_ARB_texture_compression && RenderBackend::instance()->isImageCompressingEnabled()) { |
| 311 |
internalFormat = GL_COMPRESSED_RGBA; |
| 312 |
m_compressed = true; |
| 313 |
} else { |
| 314 |
m_compressed = false; |
| 315 |
} |
| 316 |
|
| 317 |
bool monochrome = RenderBackend::instance()->isMonochromeEnabled(); |
| 318 |
SDL_Surface* target = RenderBackend::instance()->getRenderTargetSurface(); |
| 319 |
int32_t bpp_target = RenderBackend::instance()->getPixelFormat().BitsPerPixel; |
| 320 |
int32_t bpp_source = m_surface->format->BitsPerPixel; |
| 321 |
// create 16 bit texture, RGBA_4444 |
| 322 |
if (bpp_target == 16 && bpp_source == 32) { |
| 323 |
uint16_t* oglbuffer = new uint16_t[m_chunk_size_w * m_chunk_size_h]; |
| 324 |
memset(oglbuffer, 0x00, m_chunk_size_w*m_chunk_size_h*sizeof(uint16_t)); |
| 325 |
|
| 326 |
for (uint32_t y = 0; y < height; ++y) { |
| 327 |
for (uint32_t x = 0; x < width; ++x) { |
| 328 |
uint32_t pos = (y * pitch) + (x * 4); |
| 329 |
|
| 330 |
uint8_t r = data[pos + 0]; |
| 331 |
uint8_t g = data[pos + 1]; |
| 332 |
uint8_t b = data[pos + 2]; |
| 333 |
uint8_t a = data[pos + 3]; |
| 334 |
|
| 335 |
if (RenderBackend::instance()->isColorKeyEnabled()) { |
| 336 |
// only set alpha to zero if colorkey feature is enabled |
| 337 |
if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) { |
| 338 |
a = 0; |
| 339 |
} |
| 340 |
} |
| 341 |
// if monochrome rendering is enabled, then the colors are converted to grayscale |
| 342 |
if (monochrome) { |
| 343 |
uint8_t lum = static_cast<uint8_t>(r*0.3 + g*0.59 + b*0.11); |
| 344 |
r = lum; |
| 345 |
g = lum; |
| 346 |
b = lum; |
| 347 |
} |
| 348 |
oglbuffer[(y*m_chunk_size_w) + x] = ((r >> 4) << 12) | |
| 349 |
((g >> 4) << 8) | |
| 350 |
((b >> 4) << 4) | |
| 351 |
((a >> 4) << 0); |
| 352 |
} |
| 353 |
} |
| 354 |
// in case of compression we let OpenGL handle it |
| 355 |
if (!m_compressed) { |
| 356 |
internalFormat = GL_RGBA4; |
| 357 |
} |
| 358 |
|
| 359 |
// transfer data from sdl buffer |
| 360 |
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_chunk_size_w, m_chunk_size_h, |
| 361 |
0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, oglbuffer); |
| 362 |
|
| 363 |
delete[] oglbuffer; |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
if(GLEW_ARB_texture_non_power_of_two && RenderBackend::instance()->isNPOTEnabled()) { |
| 368 |
if(RenderBackend::instance()->isColorKeyEnabled()) { |
| 369 |
uint8_t* oglbuffer = new uint8_t[width * height * 4]; |
| 370 |
memcpy(oglbuffer, data, width * height * 4 * sizeof(uint8_t)); |
| 371 |
|
| 372 |
for (uint32_t y = 0; y < height; ++y) { |
| 373 |
for (uint32_t x = 0; x < width * 4; x += 4) { |
| 374 |
uint32_t gid = x + y * pitch; |
| 375 |
|
| 376 |
uint8_t r = oglbuffer[gid + 0]; |
| 377 |
uint8_t g = oglbuffer[gid + 1]; |
| 378 |
uint8_t b = oglbuffer[gid + 2]; |
| 379 |
|
| 380 |
// set alpha to zero |
| 381 |
if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) { |
| 382 |
oglbuffer[gid + 3] = 0; |
| 383 |
} |
| 384 |
// if monochrome rendering is enabled, then the colors are converted to grayscale |
| 385 |
if (monochrome) { |
| 386 |
uint8_t lum = static_cast<uint8_t>(r*0.3 + g*0.59 + b*0.11); |
| 387 |
oglbuffer[gid + 0] = lum; |
| 388 |
oglbuffer[gid + 1] = lum; |
| 389 |
oglbuffer[gid + 2] = lum; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
// transfer data from sdl buffer |
| 395 |
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_chunk_size_w, m_chunk_size_h, |
| 396 |
0, GL_RGBA, GL_UNSIGNED_BYTE, oglbuffer); |
| 397 |
|
| 398 |
delete [] oglbuffer; |
| 399 |
} else if (monochrome) { |
| 400 |
uint8_t* oglbuffer = new uint8_t[width * height * 4]; |
| 401 |
memcpy(oglbuffer, data, width * height * 4 * sizeof(uint8_t)); |
| 402 |
|
| 403 |
for (uint32_t y = 0; y < height; ++y) { |
| 404 |
for (uint32_t x = 0; x < width * 4; x += 4) { |
| 405 |
uint32_t gid = x + y * pitch; |
| 406 |
// if monochrome rendering is enabled, then the colors are converted to grayscale |
| 407 |
uint8_t lum = static_cast<uint8_t>(oglbuffer[gid + 0]*0.3 + oglbuffer[gid + 1]*0.59 + oglbuffer[gid + 2]*0.11); |
| 408 |
oglbuffer[gid + 0] = lum; |
| 409 |
oglbuffer[gid + 1] = lum; |
| 410 |
oglbuffer[gid + 2] = lum; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
// transfer data from sdl buffer |
| 415 |
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_chunk_size_w, m_chunk_size_h, |
| 416 |
0, GL_RGBA, GL_UNSIGNED_BYTE, oglbuffer); |
| 417 |
|
| 418 |
delete [] oglbuffer; |
| 419 |
} else { |
| 420 |
// transfer data directly from sdl buffer |
| 421 |
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_chunk_size_w, m_chunk_size_h, |
| 422 |
0, GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 423 |
} |
| 424 |
// Non power of 2 textures are not supported, we need to pad the size of texture to nearest power of 2 |
| 425 |
} else { |
| 426 |
uint32_t* oglbuffer = new uint32_t[m_chunk_size_w * m_chunk_size_h]; |
| 427 |
memset(oglbuffer, 0x00, m_chunk_size_w*m_chunk_size_h*sizeof(uint32_t)); |
| 428 |
|
| 429 |
for (uint32_t y = 0; y < height; ++y) { |
| 430 |
for (uint32_t x = 0; x < width; ++x) { |
| 431 |
uint32_t pos = (y * pitch) + (x * 4); |
| 432 |
|
| 433 |
uint8_t a = data[pos + 3]; |
| 434 |
uint8_t b = data[pos + 2]; |
| 435 |
uint8_t g = data[pos + 1]; |
| 436 |
uint8_t r = data[pos + 0]; |
| 437 |
|
| 438 |
if (RenderBackend::instance()->isColorKeyEnabled()) { |
| 439 |
// only set alpha to zero if colorkey feature is enabled |
| 440 |
if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) { |
| 441 |
a = 0; |
| 442 |
} |
| 443 |
} |
| 444 |
// if monochrome rendering is enabled, then the colors are converted to grayscale |
| 445 |
if (monochrome) { |
| 446 |
uint8_t lum = static_cast<uint8_t>(r*0.3 + g*0.59 + b*0.11); |
| 447 |
r = lum; |
| 448 |
g = lum; |
| 449 |
b = lum; |
| 450 |
} |
| 451 |
|
| 452 |
oglbuffer[(y*m_chunk_size_w) + x] = r | (g << 8) | (b << 16) | (a<<24); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
// transfer data from sdl buffer |
| 457 |
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_chunk_size_w, m_chunk_size_h, |
| 458 |
0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(oglbuffer)); |
| 459 |
|
| 460 |
delete[] oglbuffer; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
void GLImage::generateGLSharedTexture(const GLImage* shared, const Rect& region) { |
| 465 |
uint32_t width = shared->getWidth(); |
| 466 |
uint32_t height = shared->getHeight(); |
| 467 |
|
| 468 |
if(!GLEW_ARB_texture_non_power_of_two || !RenderBackend::instance()->isNPOTEnabled()) { |
| 469 |
width = nextPow2(width); |
| 470 |
height = nextPow2(height); |
| 471 |
} |
| 472 |
|
| 473 |
if (RenderBackend::instance()->getTextureFiltering() != TEXTURE_FILTER_NONE || RenderBackend::instance()->isMipmappingEnabled()) { |
| 474 |
// half pixel correction |
| 475 |
m_tex_coords[0] = (static_cast<GLfloat>(region.x)+0.5) / static_cast<GLfloat>(width); |
| 476 |
m_tex_coords[1] = (static_cast<GLfloat>(region.y)+0.5) / static_cast<GLfloat>(height); |
| 477 |
m_tex_coords[2] = (static_cast<GLfloat>(region.x + region.w)-0.5) / static_cast<GLfloat>(width); |
| 478 |
m_tex_coords[3] = (static_cast<GLfloat>(region.y + region.h)-0.5) / static_cast<GLfloat>(height); |
| 479 |
} else { |
| 480 |
m_tex_coords[0] = static_cast<GLfloat>(region.x) / static_cast<GLfloat>(width); |
| 481 |
m_tex_coords[1] = static_cast<GLfloat>(region.y) / static_cast<GLfloat>(height); |
| 482 |
m_tex_coords[2] = static_cast<GLfloat>(region.x + region.w) / static_cast<GLfloat>(width); |
| 483 |
m_tex_coords[3] = static_cast<GLfloat>(region.y + region.h) / static_cast<GLfloat>(height); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
void GLImage::useSharedImage(const ImagePtr& shared, const Rect& region){ |
| 488 |
GLImage* img = static_cast<GLImage*>(shared.get()); |
| 489 |
|
| 490 |
m_shared_img = img; |
| 491 |
m_texId = img->m_texId; |
| 492 |
m_shared = true; |
| 493 |
m_subimagerect = region; |
| 494 |
m_atlas_img = shared; |
| 495 |
m_surface = m_shared_img->m_surface; |
| 496 |
m_compressed = m_shared_img->m_compressed; |
| 497 |
m_atlas_name = m_shared_img->getName(); |
| 498 |
|
| 499 |
if(m_texId) { |
| 500 |
generateGLSharedTexture(img, region); |
| 501 |
} |
| 502 |
|
| 503 |
setState(m_shared_img->getState()); |
| 504 |
} |
| 505 |
|
| 506 |
void GLImage::forceLoadInternal() { |
| 507 |
if (m_texId == 0) { |
| 508 |
generateGLTexture(); |
| 509 |
} else if (m_shared) { |
| 510 |
validateShared(); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
void GLImage::validateShared() { |
| 515 |
// if image is valid we can return |
| 516 |
if (m_shared_img->m_texId && m_shared_img->m_texId == m_texId) { |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
if (m_shared_img->getState() == IResource::RES_NOT_LOADED) { |
| 521 |
m_shared_img->load(); |
| 522 |
m_shared_img->generateGLTexture(); |
| 523 |
} else if (!m_shared_img->m_texId) { |
| 524 |
m_shared_img->generateGLTexture(); |
| 525 |
} |
| 526 |
|
| 527 |
m_texId = m_shared_img->m_texId; |
| 528 |
m_surface = m_shared_img->m_surface; |
| 529 |
m_compressed = m_shared_img->m_compressed; |
| 530 |
generateGLSharedTexture(m_shared_img, m_subimagerect); |
| 531 |
} |
| 532 |
|
| 533 |
void GLImage::copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr& img) { |
| 534 |
Image::copySubimage(xoffset, yoffset, img); |
| 535 |
|
| 536 |
if(m_texId) { |
| 537 |
static_cast<RenderBackendOpenGL*>(RenderBackend::instance())->bindTexture(m_texId); |
| 538 |
glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, img->getWidth(), img->getHeight(), |
| 539 |
GL_RGBA, GL_UNSIGNED_BYTE, img->getSurface()->pixels); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
void GLImage::load() { |
| 544 |
if (m_shared) { |
| 545 |
// check atlas image |
| 546 |
// if it does not exist, it is generated. |
| 547 |
if (!ImageManager::instance()->exists(m_atlas_name)) { |
| 548 |
ImagePtr newAtlas = ImageManager::instance()->create(m_atlas_name); |
| 549 |
GLImage* img = static_cast<GLImage*>(newAtlas.get()); |
| 550 |
m_atlas_img = newAtlas; |
| 551 |
m_shared_img = img; |
| 552 |
} |
| 553 |
validateShared(); |
| 554 |
// check if texture ids and surfaces are identical |
| 555 |
if (m_shared_img->m_surface != m_surface || m_texId != m_shared_img->m_texId) { |
| 556 |
m_texId = m_shared_img->m_texId; |
| 557 |
m_surface = m_shared_img->m_surface; |
| 558 |
m_compressed = m_shared_img->m_compressed; |
| 559 |
if (m_texId) { |
| 560 |
generateGLSharedTexture(m_shared_img, m_subimagerect); |
| 561 |
} |
| 562 |
} |
| 563 |
m_state = IResource::RES_LOADED; |
| 564 |
} else { |
| 565 |
Image::load(); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
void GLImage::free() { |
| 570 |
// save the image offsets |
| 571 |
int32_t xshift = m_xshift; |
| 572 |
int32_t yshift = m_yshift; |
| 573 |
setSurface(NULL); |
| 574 |
m_xshift = xshift; |
| 575 |
m_yshift = yshift; |
| 576 |
m_state = IResource::RES_NOT_LOADED; |
| 577 |
} |
| 578 |
|
| 579 |
GLuint GLImage::getTexId() const { |
| 580 |
return m_texId; |
| 581 |
} |
| 582 |
|
| 583 |
const GLfloat* GLImage::getTexCoords() const { |
| 584 |
return m_tex_coords; |
| 585 |
} |
| 586 |
} |