2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
#include "texture.h"
|
2011-05-17 01:21:41 +02:00
|
|
|
#include "graphics.h"
|
2011-12-25 00:14:12 +01:00
|
|
|
#include "framebuffer.h"
|
2012-05-09 22:26:34 +02:00
|
|
|
#include "image.h"
|
2010-11-23 04:13:37 +01:00
|
|
|
|
2012-07-14 03:10:24 +02:00
|
|
|
#include <framework/core/application.h>
|
2012-06-18 10:13:52 +02:00
|
|
|
|
2011-08-15 21:15:49 +02:00
|
|
|
Texture::Texture()
|
|
|
|
{
|
2012-06-08 18:58:08 +02:00
|
|
|
m_id = 0;
|
2011-08-15 21:15:49 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
Texture::Texture(const Size& size)
|
2012-05-09 22:26:34 +02:00
|
|
|
{
|
2012-06-08 18:58:08 +02:00
|
|
|
m_id = 0;
|
2012-05-09 22:26:34 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
if(!setupSize(size))
|
|
|
|
return;
|
2011-05-16 20:30:05 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
createTexture();
|
|
|
|
bind();
|
|
|
|
setupPixels(0, m_glSize, nullptr, 4);
|
|
|
|
setupWrap();
|
|
|
|
setupFilters();
|
2011-08-15 21:15:49 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
Texture::Texture(const ImagePtr& image, bool buildMipmaps)
|
2011-05-16 20:30:05 +02:00
|
|
|
{
|
2012-06-08 18:58:08 +02:00
|
|
|
m_id = 0;
|
2012-04-20 14:07:47 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
if(!setupSize(image->getSize(), buildMipmaps))
|
|
|
|
return;
|
2011-05-17 01:21:41 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
createTexture();
|
2011-08-15 21:15:49 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
ImagePtr glImage = image;
|
|
|
|
if(m_size != m_glSize) {
|
|
|
|
glImage = ImagePtr(new Image(m_glSize, image->getBpp()));
|
|
|
|
glImage->paste(image);
|
|
|
|
} else
|
|
|
|
glImage = image;
|
2010-11-23 04:13:37 +01:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
bind();
|
2012-04-20 12:16:03 +02:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
if(buildMipmaps) {
|
|
|
|
int level = 0;
|
|
|
|
do {
|
|
|
|
setupPixels(level++, glImage->getSize(), glImage->getPixelData(), glImage->getBpp());
|
|
|
|
} while(glImage->nextMipmap());
|
|
|
|
m_hasMipmaps = true;
|
|
|
|
} else
|
|
|
|
setupPixels(0, glImage->getSize(), glImage->getPixelData(), glImage->getBpp());
|
2010-11-23 04:13:37 +01:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
setupWrap();
|
2012-01-28 19:29:03 +01:00
|
|
|
setupFilters();
|
2010-11-23 04:13:37 +01:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
Texture::~Texture()
|
2012-06-01 21:39:09 +02:00
|
|
|
{
|
2012-07-30 14:29:13 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if(g_app.isTerminated())
|
|
|
|
g_logger.warning("a texture reference was not released");
|
|
|
|
#endif
|
2012-06-08 18:58:08 +02:00
|
|
|
// free texture from gl memory
|
2012-06-18 10:13:52 +02:00
|
|
|
if(g_graphics.ok() && m_id != 0)
|
2012-06-08 18:58:08 +02:00
|
|
|
glDeleteTextures(1, &m_id);
|
2012-06-01 21:39:09 +02:00
|
|
|
}
|
|
|
|
|
2012-04-20 12:16:03 +02:00
|
|
|
void Texture::bind()
|
|
|
|
{
|
|
|
|
// must reset painter texture state
|
2012-04-20 14:07:47 +02:00
|
|
|
g_painter->setTexture(this);
|
2012-06-08 18:58:08 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_id);
|
2012-04-20 12:16:03 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
void Texture::copyFromScreen(const Rect& screenRect)
|
2012-01-28 19:29:03 +01:00
|
|
|
{
|
2012-06-08 18:58:08 +02:00
|
|
|
bind();
|
|
|
|
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, screenRect.x(), screenRect.y(), screenRect.width(), screenRect.height());
|
2012-03-21 13:41:43 +01:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
bool Texture::buildHardwareMipmaps()
|
2012-03-21 13:41:43 +01:00
|
|
|
{
|
2012-04-19 01:03:43 +02:00
|
|
|
if(!g_graphics.canUseHardwareMipmaps())
|
|
|
|
return false;
|
|
|
|
|
2012-01-28 19:29:03 +01:00
|
|
|
bind();
|
|
|
|
|
2012-02-20 14:10:54 +01:00
|
|
|
if(!m_hasMipmaps) {
|
|
|
|
m_hasMipmaps = true;
|
2012-01-28 19:29:03 +01:00
|
|
|
setupFilters();
|
|
|
|
}
|
|
|
|
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
2012-04-19 01:03:43 +02:00
|
|
|
return true;
|
2012-01-28 19:29:03 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 23:13:40 +01:00
|
|
|
void Texture::setSmooth(bool smooth)
|
2010-11-23 16:30:43 +01:00
|
|
|
{
|
2012-03-21 13:41:43 +01:00
|
|
|
if(smooth && !g_graphics.canUseBilinearFiltering())
|
|
|
|
return;
|
|
|
|
|
2012-01-10 23:13:40 +01:00
|
|
|
if(smooth == m_smooth)
|
|
|
|
return;
|
|
|
|
|
2012-01-28 19:29:03 +01:00
|
|
|
m_smooth = smooth;
|
|
|
|
bind();
|
|
|
|
setupFilters();
|
2010-11-23 04:13:37 +01:00
|
|
|
}
|
2011-04-10 00:55:58 +02:00
|
|
|
|
2012-06-14 20:26:55 +02:00
|
|
|
void Texture::setRepeat(bool repeat)
|
|
|
|
{
|
|
|
|
if(m_repeat == repeat)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_repeat = repeat;
|
|
|
|
bind();
|
|
|
|
setupWrap();
|
|
|
|
}
|
|
|
|
|
2012-06-02 16:43:27 +02:00
|
|
|
void Texture::setUpsideDown(bool upsideDown)
|
|
|
|
{
|
|
|
|
if(m_upsideDown == upsideDown)
|
|
|
|
return;
|
|
|
|
m_upsideDown = upsideDown;
|
|
|
|
setupTranformMatrix();
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
void Texture::createTexture()
|
2012-01-30 01:00:12 +01:00
|
|
|
{
|
2012-06-08 18:58:08 +02:00
|
|
|
glGenTextures(1, &m_id);
|
|
|
|
assert(m_id != 0);
|
|
|
|
}
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
bool Texture::setupSize(const Size& size, bool forcePowerOfTwo)
|
|
|
|
{
|
|
|
|
Size glSize;
|
|
|
|
if(!g_graphics.canUseNonPowerOfTwoTextures() || forcePowerOfTwo)
|
|
|
|
glSize.resize(stdext::to_power_of_two(size.width()), stdext::to_power_of_two(size.height()));
|
|
|
|
else
|
|
|
|
glSize = size;
|
|
|
|
|
|
|
|
// checks texture max size
|
|
|
|
if(std::max(glSize.width(), glSize.height()) > g_graphics.getMaxTextureSize()) {
|
|
|
|
g_logger.error(stdext::format("loading texture with size %dx%d failed, "
|
|
|
|
"the maximum size allowed by the graphics card is %dx%d,"
|
|
|
|
"to prevent crashes the texture will be displayed as a blank texture",
|
|
|
|
size.width(), size.height(), g_graphics.getMaxTextureSize(), g_graphics.getMaxTextureSize()));
|
|
|
|
return false;
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
m_size = size;
|
|
|
|
m_glSize = glSize;
|
|
|
|
setupTranformMatrix();
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-30 01:00:12 +01:00
|
|
|
|
2012-06-08 18:58:08 +02:00
|
|
|
void Texture::setupWrap()
|
|
|
|
{
|
2012-06-22 05:14:13 +02:00
|
|
|
int texParam;
|
2012-06-14 20:26:55 +02:00
|
|
|
if(!m_repeat && g_graphics.canUseClampToEdge())
|
|
|
|
texParam = GL_CLAMP_TO_EDGE;
|
|
|
|
else
|
|
|
|
texParam = GL_REPEAT;
|
2012-06-08 18:58:08 +02:00
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texParam);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texParam);
|
2012-01-30 01:00:12 +01:00
|
|
|
}
|
|
|
|
|
2012-01-28 19:29:03 +01:00
|
|
|
void Texture::setupFilters()
|
|
|
|
{
|
2012-06-22 05:14:13 +02:00
|
|
|
int minFilter;
|
|
|
|
int magFilter;
|
2012-01-28 19:29:03 +01:00
|
|
|
if(m_smooth) {
|
2012-02-20 14:10:54 +01:00
|
|
|
minFilter = m_hasMipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR;
|
2012-01-28 19:29:03 +01:00
|
|
|
magFilter = GL_LINEAR;
|
|
|
|
} else {
|
2012-02-20 14:10:54 +01:00
|
|
|
minFilter = m_hasMipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST;
|
2012-01-28 19:29:03 +01:00
|
|
|
magFilter = GL_NEAREST;
|
|
|
|
}
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
|
|
|
|
}
|
2012-06-02 16:43:27 +02:00
|
|
|
|
|
|
|
void Texture::setupTranformMatrix()
|
|
|
|
{
|
|
|
|
if(m_upsideDown) {
|
2012-06-02 17:54:35 +02:00
|
|
|
m_transformMatrix = { 1.0f/m_glSize.width(), 0.0f, 0.0f,
|
|
|
|
0.0f, -1.0f/m_glSize.height(), 0.0f,
|
|
|
|
0.0f, m_size.height()/(float)m_glSize.height(), 1.0f };
|
2012-06-02 16:43:27 +02:00
|
|
|
} else {
|
|
|
|
m_transformMatrix = { 1.0f/m_glSize.width(), 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f/m_glSize.height(), 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f };
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 18:58:08 +02:00
|
|
|
|
|
|
|
void Texture::setupPixels(int level, const Size& size, uchar* pixels, int channels)
|
|
|
|
{
|
|
|
|
GLenum format = 0;
|
|
|
|
switch(channels) {
|
|
|
|
case 4:
|
|
|
|
format = GL_RGBA;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
format = GL_RGB;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
format = GL_LUMINANCE_ALPHA;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
format = GL_LUMINANCE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, size.width(), size.height(), 0, format, GL_UNSIGNED_BYTE, pixels);
|
|
|
|
}
|