2010-07-10 22:47:29 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-07-13 05:38:10 +00:00
|
|
|
#ifdef USE_OPENGL
|
|
|
|
|
2010-07-10 22:47:29 +00:00
|
|
|
#include "backends/graphics/opengl/gltexture.h"
|
|
|
|
#include "backends/graphics/opengl/glerrorcheck.h"
|
|
|
|
|
|
|
|
#include "common/rect.h"
|
|
|
|
#include "common/array.h"
|
|
|
|
#include "common/util.h"
|
|
|
|
#include "common/tokenizer.h"
|
|
|
|
|
|
|
|
// Supported GL extensions
|
|
|
|
static bool npot_supported = false;
|
|
|
|
|
2010-07-18 05:32:44 +00:00
|
|
|
/*static inline GLint xdiv(int numerator, int denominator) {
|
2010-07-10 22:47:29 +00:00
|
|
|
assert(numerator < (1 << 16));
|
|
|
|
return (numerator << 16) / denominator;
|
2010-07-18 05:32:44 +00:00
|
|
|
}*/
|
2010-07-10 22:47:29 +00:00
|
|
|
|
2010-07-15 04:20:21 +00:00
|
|
|
static GLuint nextHigher2(GLuint v) {
|
|
|
|
if (v == 0)
|
2010-07-10 22:47:29 +00:00
|
|
|
return 1;
|
2010-07-15 04:20:21 +00:00
|
|
|
v--;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
2010-07-16 22:20:21 +00:00
|
|
|
return ++v;
|
2010-07-10 22:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLTexture::initGLExtensions() {
|
2010-07-15 04:20:21 +00:00
|
|
|
static bool inited = false;
|
|
|
|
|
|
|
|
if (inited)
|
|
|
|
return;
|
|
|
|
|
2010-07-10 22:47:29 +00:00
|
|
|
const char* ext_string =
|
|
|
|
reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
|
2010-07-18 05:32:44 +00:00
|
|
|
CHECK_GL_ERROR(0);
|
2010-07-10 22:47:29 +00:00
|
|
|
Common::StringTokenizer tokenizer(ext_string, " ");
|
|
|
|
while (!tokenizer.empty()) {
|
|
|
|
Common::String token = tokenizer.nextToken();
|
|
|
|
if (token == "GL_ARB_texture_non_power_of_two")
|
|
|
|
npot_supported = true;
|
|
|
|
}
|
2010-07-15 04:20:21 +00:00
|
|
|
inited = true;
|
2010-07-10 22:47:29 +00:00
|
|
|
}
|
|
|
|
|
2010-07-12 06:00:19 +00:00
|
|
|
GLTexture::GLTexture(byte bpp, GLenum format, GLenum type)
|
|
|
|
:
|
|
|
|
_bytesPerPixel(bpp),
|
|
|
|
_glFormat(format),
|
|
|
|
_glType(type),
|
2010-07-10 22:47:29 +00:00
|
|
|
_textureWidth(0),
|
2010-07-20 04:32:31 +00:00
|
|
|
_textureHeight(0),
|
|
|
|
_realWidth(0),
|
|
|
|
_realHeight(0),
|
|
|
|
_refresh(false) {
|
2010-07-10 22:47:29 +00:00
|
|
|
|
2010-07-16 04:45:47 +00:00
|
|
|
// Generates the texture ID for GL
|
|
|
|
CHECK_GL_ERROR( glGenTextures(1, &_textureName) );
|
2010-07-10 22:47:29 +00:00
|
|
|
|
|
|
|
// This all gets reset later in allocBuffer:
|
|
|
|
_surface.w = 0;
|
|
|
|
_surface.h = 0;
|
|
|
|
_surface.pitch = 0;
|
|
|
|
_surface.pixels = NULL;
|
|
|
|
_surface.bytesPerPixel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLTexture::~GLTexture() {
|
|
|
|
CHECK_GL_ERROR( glDeleteTextures(1, &_textureName) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLTexture::refresh() {
|
|
|
|
// Generates the texture ID for GL
|
2010-07-20 04:32:31 +00:00
|
|
|
CHECK_GL_ERROR( glGenTextures(1, &_textureName) );
|
|
|
|
_refresh = true;
|
2010-07-10 22:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLTexture::allocBuffer(GLuint w, GLuint h) {
|
2010-07-20 04:32:31 +00:00
|
|
|
_realWidth = w;
|
|
|
|
_realHeight = h;
|
|
|
|
|
|
|
|
if (w <= _textureWidth && h <= _textureHeight && !_refresh)
|
2010-07-10 22:47:29 +00:00
|
|
|
// Already allocated a sufficiently large buffer
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (npot_supported) {
|
2010-07-12 06:00:19 +00:00
|
|
|
_textureWidth = w;
|
|
|
|
_textureHeight = h;
|
2010-07-10 22:47:29 +00:00
|
|
|
} else {
|
2010-07-12 06:00:19 +00:00
|
|
|
_textureWidth = nextHigher2(w);
|
|
|
|
_textureHeight = nextHigher2(h);
|
2010-07-10 22:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate room for the texture now, but pixel data gets uploaded
|
|
|
|
// later (perhaps with multiple TexSubImage2D operations).
|
|
|
|
CHECK_GL_ERROR( glBindTexture(GL_TEXTURE_2D, _textureName) );
|
|
|
|
CHECK_GL_ERROR( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) );
|
|
|
|
CHECK_GL_ERROR( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) );
|
|
|
|
CHECK_GL_ERROR( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
|
|
|
|
CHECK_GL_ERROR( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
|
2010-07-12 06:00:19 +00:00
|
|
|
CHECK_GL_ERROR( glTexImage2D(GL_TEXTURE_2D, 0, _glFormat,
|
2010-07-18 05:32:44 +00:00
|
|
|
_textureWidth, _textureHeight, 0, _glFormat, _glType, NULL) );
|
2010-07-20 04:32:31 +00:00
|
|
|
|
|
|
|
if (_surface.w != _textureWidth || _surface.h != _textureHeight)
|
|
|
|
_surface.create(_textureWidth, _textureHeight, _bytesPerPixel);
|
|
|
|
|
|
|
|
_refresh = false;
|
2010-07-10 22:47:29 +00:00
|
|
|
}
|
|
|
|
|
2010-07-12 06:00:19 +00:00
|
|
|
void GLTexture::updateBuffer(const void *buf, int pitch, GLuint x, GLuint y, GLuint w, GLuint h) {
|
2010-07-10 22:47:29 +00:00
|
|
|
CHECK_GL_ERROR( glBindTexture(GL_TEXTURE_2D, _textureName) );
|
|
|
|
|
2010-07-12 06:00:19 +00:00
|
|
|
if (static_cast<int>(w) * _bytesPerPixel == pitch) {
|
2010-07-10 22:47:29 +00:00
|
|
|
CHECK_GL_ERROR( glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h,
|
2010-07-12 06:00:19 +00:00
|
|
|
_glFormat, _glType, buf) );
|
2010-07-20 04:32:31 +00:00
|
|
|
memcpy(_surface.getBasePtr(x, y), buf, h * pitch);
|
2010-07-10 22:47:29 +00:00
|
|
|
} else {
|
|
|
|
const byte* src = static_cast<const byte*>(buf);
|
|
|
|
do {
|
|
|
|
CHECK_GL_ERROR( glTexSubImage2D(GL_TEXTURE_2D, 0, x, y,
|
2010-07-12 06:00:19 +00:00
|
|
|
w, 1, _glFormat, _glType, src) );
|
2010-07-20 04:32:31 +00:00
|
|
|
memcpy(_surface.getBasePtr(x, y), src, w * _bytesPerPixel);
|
2010-07-10 22:47:29 +00:00
|
|
|
++y;
|
|
|
|
src += pitch;
|
|
|
|
} while (--h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLTexture::fillBuffer(byte x) {
|
2010-07-20 04:32:31 +00:00
|
|
|
memset(_surface.pixels, x, _surface.h * _surface.pitch);
|
2010-07-10 22:47:29 +00:00
|
|
|
CHECK_GL_ERROR( glBindTexture(GL_TEXTURE_2D, _textureName) );
|
|
|
|
CHECK_GL_ERROR( glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _surface.w, _surface.h,
|
2010-07-20 04:32:31 +00:00
|
|
|
_glFormat, _glType, _surface.pixels) );
|
2010-07-10 22:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLTexture::drawTexture(GLshort x, GLshort y, GLshort w, GLshort h) {
|
|
|
|
CHECK_GL_ERROR( glBindTexture(GL_TEXTURE_2D, _textureName) );
|
|
|
|
|
2010-07-20 04:32:31 +00:00
|
|
|
const GLfloat texWidth = (GLfloat)_realWidth / _textureWidth;//xdiv(_surface.w, _textureWidth);
|
|
|
|
const GLfloat texHeight = (GLfloat)_realHeight / _textureHeight;//xdiv(_surface.h, _textureHeight);
|
2010-07-18 05:32:44 +00:00
|
|
|
const GLfloat texcoords[] = {
|
2010-07-10 22:47:29 +00:00
|
|
|
0, 0,
|
2010-07-16 04:45:47 +00:00
|
|
|
texWidth, 0,
|
|
|
|
0, texHeight,
|
|
|
|
texWidth, texHeight,
|
2010-07-10 22:47:29 +00:00
|
|
|
};
|
2010-07-18 05:32:44 +00:00
|
|
|
CHECK_GL_ERROR( glTexCoordPointer(2, GL_FLOAT, 0, texcoords) );
|
2010-07-10 22:47:29 +00:00
|
|
|
|
|
|
|
const GLshort vertices[] = {
|
|
|
|
x, y,
|
|
|
|
x + w, y,
|
|
|
|
x, y + h,
|
|
|
|
x + w, y + h,
|
|
|
|
};
|
|
|
|
CHECK_GL_ERROR( glVertexPointer(2, GL_SHORT, 0, vertices) );
|
|
|
|
|
2010-07-16 04:45:47 +00:00
|
|
|
CHECK_GL_ERROR( glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) );
|
2010-07-10 22:47:29 +00:00
|
|
|
}
|
2010-07-13 05:38:10 +00:00
|
|
|
|
|
|
|
#endif
|