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
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/textconsole.h"
#if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
#include "backends/graphics3d/opengl/texture.h"
#include "graphics/opengl/context.h"
namespace OpenGL {
template<class T>
static T nextHigher2(T k) {
if (k == 0)
return 1;
--k;
for (uint i = 1; i < sizeof(T) * 8; i <<= 1)
k = k | k >> i;
return k + 1;
}
const Graphics::PixelFormat TextureGL::getRGBAPixelFormat() {
#ifdef SCUMM_BIG_ENDIAN
return Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
#else
return Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
#endif
}
const Graphics::PixelFormat TextureGL::get565PixelFormat() {
return Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0);
}
TextureGL::TextureGL(const Graphics::Surface &srf) :
_managedTexture(true), _width(srf.w), _height(srf.h) {
if (OpenGLContext.NPOTSupported) {
_texWidth = _width;
_texHeight = _height;
} else {
_texWidth = nextHigher2(_width);
_texHeight = nextHigher2(_height);
}
const Graphics::Surface *surfaceToUpload = &srf;
Graphics::Surface *convertedSurface = nullptr;
Graphics::Surface *copiedSurface = nullptr;
GLenum type;
GLenum format;
if (srf.format == getRGBAPixelFormat()) {
type = GL_UNSIGNED_BYTE;
format = GL_RGBA;
} else if (srf.format == get565PixelFormat()) {
type = GL_UNSIGNED_SHORT_5_6_5;
format = GL_RGB;
} else {
type = GL_UNSIGNED_BYTE;
format = GL_RGBA;
convertedSurface = srf.convertTo(getRGBAPixelFormat());
surfaceToUpload = convertedSurface;
}
if (surfaceToUpload->pitch != surfaceToUpload->w * surfaceToUpload->format.bytesPerPixel) {
if (OpenGLContext.unpackSubImageSupported) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, surfaceToUpload->pitch / surfaceToUpload->format.bytesPerPixel);
} else {
// When unpack sub-image is not supported we can't specify the pitch
// and have to copy the subimage
copiedSurface = new Graphics::Surface();
copiedSurface->copyFrom(*surfaceToUpload);
surfaceToUpload = copiedSurface;
}
}
glGenTextures(1, &_texture);
glBindTexture(GL_TEXTURE_2D, _texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, format, _texWidth, _texHeight, 0, format, type, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _width, _height, format, type, const_cast<void *>(surfaceToUpload->getPixels()));
if (OpenGLContext.unpackSubImageSupported) {
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
if (convertedSurface) {
convertedSurface->free();
delete convertedSurface;
}
if (copiedSurface) {
copiedSurface->free();
delete copiedSurface;
}
}
TextureGL::TextureGL(uint width, uint height) :
_managedTexture(true), _width(width), _height(height) {
if (OpenGLContext.NPOTSupported) {
_texWidth = _width;
_texHeight = _height;
} else {
_texWidth = nextHigher2(_width);
_texHeight = nextHigher2(_height);
}
glGenTextures(1, &_texture);
glBindTexture(GL_TEXTURE_2D, _texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _texWidth, _texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}
TextureGL::TextureGL(GLuint texture_name, uint width, uint height, uint texture_width, uint texture_height) :
_managedTexture(false), _texture(texture_name), _width(width), _height(height),
_texWidth(texture_width), _texHeight(texture_height) {
}
TextureGL::~TextureGL() {
if (_managedTexture) {
glDeleteTextures(1, &_texture);
}
}
} // End of namespace OpenGL
#endif
|