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
|
/* 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$
*
*/
#ifdef USE_OPENGL
#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;
/*static inline GLint xdiv(int numerator, int denominator) {
assert(numerator < (1 << 16));
return (numerator << 16) / denominator;
}*/
static GLuint nextHigher2(GLuint v) {
if (v == 0)
return 1;
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return ++v;
}
void GLTexture::initGLExtensions() {
static bool inited = false;
if (inited)
return;
const char* ext_string =
reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
CHECK_GL_ERROR();
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;
}
inited = true;
}
GLTexture::GLTexture(byte bpp, GLenum format, GLenum type)
:
_bytesPerPixel(bpp),
_glFormat(format),
_glType(type),
_textureWidth(0),
_textureHeight(0),
_realWidth(0),
_realHeight(0),
_refresh(false),
_filter(GL_NEAREST) {
// Generates the texture ID for GL
glGenTextures(1, &_textureName); CHECK_GL_ERROR();
// This all gets reset later in allocBuffer:
_surface.w = 0;
_surface.h = 0;
_surface.pitch = 0;
_surface.pixels = NULL;
_surface.bytesPerPixel = 0;
}
GLTexture::~GLTexture() {
glDeleteTextures(1, &_textureName); CHECK_GL_ERROR();
}
void GLTexture::refresh() {
// Generates the texture ID for GL
glGenTextures(1, &_textureName); CHECK_GL_ERROR();
_refresh = true;
}
void GLTexture::refreshBuffer() {
updateBuffer(_surface.pixels, _surface.pitch, 0, 0, _surface.w, _surface.h);
}
void GLTexture::allocBuffer(GLuint w, GLuint h) {
_realWidth = w;
_realHeight = h;
if (w <= _textureWidth && h <= _textureHeight && !_refresh)
// Already allocated a sufficiently large buffer
return;
if (npot_supported) {
_textureWidth = w;
_textureHeight = h;
} else {
_textureWidth = nextHigher2(w);
_textureHeight = nextHigher2(h);
}
// Allocate room for the texture now, but pixel data gets uploaded
// later (perhaps with multiple TexSubImage2D operations).
glBindTexture(GL_TEXTURE_2D, _textureName); CHECK_GL_ERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _filter); CHECK_GL_ERROR();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _filter); 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); CHECK_GL_ERROR();
glTexImage2D(GL_TEXTURE_2D, 0, _glFormat,
_textureWidth, _textureHeight, 0, _glFormat, _glType, NULL); CHECK_GL_ERROR();
if (_surface.w != _textureWidth || _surface.h != _textureHeight)
_surface.create(_textureWidth, _textureHeight, _bytesPerPixel);
else if (_refresh)
refreshBuffer();
_refresh = false;
}
void GLTexture::updateBuffer(const void *buf, int pitch, GLuint x, GLuint y, GLuint w, GLuint h) {
glBindTexture(GL_TEXTURE_2D, _textureName); CHECK_GL_ERROR();
if (static_cast<int>(w) * _bytesPerPixel == pitch) {
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h,
_glFormat, _glType, buf); CHECK_GL_ERROR();
if (buf != _surface.pixels)
memcpy(_surface.getBasePtr(x, y), buf, h * pitch);
} else {
const byte *src = static_cast<const byte *>(buf);
do {
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y,
w, 1, _glFormat, _glType, src); CHECK_GL_ERROR();
if (buf != _surface.pixels)
memcpy(_surface.getBasePtr(x, y), src, w * _bytesPerPixel);
++y;
src += pitch;
} while (--h);
}
}
void GLTexture::fillBuffer(uint32 x) {
if (_bytesPerPixel == 1)
memset(_surface.pixels, x, _surface.w * _surface.h);
else {
for (int i = 0; i < _surface.w * _surface.h; i++) {
memcpy(_surface.pixels, &x, _bytesPerPixel);
}
}
glBindTexture(GL_TEXTURE_2D, _textureName); CHECK_GL_ERROR();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _surface.w, _surface.h,
_glFormat, _glType, _surface.pixels); CHECK_GL_ERROR();
}
void GLTexture::drawTexture(GLshort x, GLshort y, GLshort w, GLshort h) {
glBindTexture(GL_TEXTURE_2D, _textureName); CHECK_GL_ERROR();
const GLfloat texWidth = (GLfloat)_realWidth / _textureWidth;//xdiv(_surface.w, _textureWidth);
const GLfloat texHeight = (GLfloat)_realHeight / _textureHeight;//xdiv(_surface.h, _textureHeight);
const GLfloat texcoords[] = {
0, 0,
texWidth, 0,
0, texHeight,
texWidth, texHeight,
};
glTexCoordPointer(2, GL_FLOAT, 0, texcoords); CHECK_GL_ERROR();
const GLshort vertices[] = {
x, y,
x + w, y,
x, y + h,
x + w, y + h,
};
glVertexPointer(2, GL_SHORT, 0, vertices); CHECK_GL_ERROR();
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); CHECK_GL_ERROR();
}
#endif
|