| 1 |
/** |
| 2 |
* Texture handler for OpenGL (ES) 2 |
| 3 |
|
| 4 |
* Copyright (C) 2015 Sylvain Beucler |
| 5 |
|
| 6 |
* This file is part of GNU FreeDink |
| 7 |
|
| 8 |
* GNU FreeDink is free software: you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Affero General Public License |
| 10 |
* as published by the Free Software Foundation, either version 3 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
|
| 13 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Affero General Public License for more details. |
| 17 |
|
| 18 |
* You should have received a copy of the GNU Affero General Public |
| 19 |
* License along with GNU FreeDink. If not, see |
| 20 |
* <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "IOGfxSurfaceGL2.h" |
| 28 |
|
| 29 |
#include "SDL.h" |
| 30 |
#include <glm/glm.hpp> |
| 31 |
#include <glm/gtc/matrix_transform.hpp> |
| 32 |
#include <glm/gtc/type_ptr.hpp> |
| 33 |
|
| 34 |
#include "log.h" |
| 35 |
#include "IOGfxDisplayGL2.h" |
| 36 |
#include "IOGfxGLFuncs.h" |
| 37 |
#include "ImageLoader.h" /* GFX_ref_pal */ // TODO: break dep |
| 38 |
|
| 39 |
IOGfxSurfaceGL2::IOGfxSurfaceGL2(IOGfxDisplayGL2* display, GLuint texture, int w, int h, SDL_Color colorkey) |
| 40 |
: IOGfxSurface(w, h), display(display), texture(texture), colorkey(colorkey) { |
| 41 |
fbo = 0; |
| 42 |
} |
| 43 |
|
| 44 |
IOGfxSurfaceGL2::~IOGfxSurfaceGL2() { |
| 45 |
IOGfxGLFuncs* gl = display->gl; |
| 46 |
gl->DeleteTextures(1, &texture); |
| 47 |
gl->DeleteFramebuffers(1, &fbo); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Lazy GL framebuffer creation |
| 52 |
* Only necessary for the few target textures (backbuffer, background...) |
| 53 |
*/ |
| 54 |
bool IOGfxSurfaceGL2::bindAsFramebuffer() { |
| 55 |
IOGfxGLFuncs* gl = display->gl; |
| 56 |
if (fbo == 0) { |
| 57 |
// bind this texture as framebuffer |
| 58 |
gl->GenFramebuffers(1, &fbo); |
| 59 |
gl->BindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 60 |
gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->texture, 0); |
| 61 |
GLenum status; |
| 62 |
if ((status = gl->CheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) { |
| 63 |
log_error("glCheckFramebufferStatus: error 0x%x", status); |
| 64 |
switch (status) { |
| 65 |
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: |
| 66 |
log_error("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"); |
| 67 |
break; |
| 68 |
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: |
| 69 |
log_error("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS"); |
| 70 |
break; |
| 71 |
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: |
| 72 |
log_error("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"); |
| 73 |
break; |
| 74 |
case GL_FRAMEBUFFER_UNSUPPORTED: |
| 75 |
log_error("GL_FRAMEBUFFER_UNSUPPORTED"); |
| 76 |
break; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
} else { |
| 81 |
gl->BindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/* Function specifically made for Dink'C fill_screen() */ |
| 88 |
void IOGfxSurfaceGL2::fill_screen(int num, SDL_Color* palette) { |
| 89 |
fillRect(NULL, palette[num].r, palette[num].g, palette[num].b); |
| 90 |
// TODO: check indexed mode + palette change |
| 91 |
} |
| 92 |
|
| 93 |
int IOGfxSurfaceGL2::fillRect(const SDL_Rect *dstrect, Uint8 r, Uint8 g, Uint8 b) { |
| 94 |
SDL_Rect dstrect_if_not_null; |
| 95 |
if (dstrect == NULL) { |
| 96 |
dstrect_if_not_null.x = 0; |
| 97 |
dstrect_if_not_null.y = 0; |
| 98 |
dstrect_if_not_null.w = w; |
| 99 |
dstrect_if_not_null.h = h; |
| 100 |
dstrect = &dstrect_if_not_null; |
| 101 |
} |
| 102 |
|
| 103 |
if (!display->truecolor) { |
| 104 |
SDL_PixelFormat fmt; |
| 105 |
fmt.palette = SDL_AllocPalette(256); |
| 106 |
memcpy(fmt.palette->colors, GFX_ref_pal, sizeof(GFX_ref_pal)); |
| 107 |
Uint8 i = SDL_MapRGB(&fmt, r,g,b); |
| 108 |
r = g = b = i; |
| 109 |
} |
| 110 |
|
| 111 |
IOGfxGLFuncs* gl = display->gl; |
| 112 |
|
| 113 |
display->gl->UseProgram(display->fillRect->program); |
| 114 |
display->setVertexAttrib(display->fillRect, display->fillRect->attributes["v_coord"], display->vboSpriteVertices, 4); |
| 115 |
|
| 116 |
glm::mat4 projection = glm::ortho(0.0f, 1.0f*display->w, 0.0f, 1.0f*display->h); |
| 117 |
glm::mat4 m_transform; |
| 118 |
m_transform = glm::translate(glm::mat4(1.0f), glm::vec3(dstrect->x,dstrect->y, 0.0)) |
| 119 |
* glm::scale(glm::mat4(1.0f), glm::vec3(dstrect->w, dstrect->h, 0.0)); |
| 120 |
glm::mat4 mvp = projection * m_transform; // * view * model * anim; |
| 121 |
gl->UniformMatrix4fv(display->fillRect->uniforms["mvp"], 1, GL_FALSE, glm::value_ptr(mvp)); |
| 122 |
gl->Uniform4f(display->fillRect->uniforms["color"], r/255.0,g/255.0,b/255.0,1.0); |
| 123 |
|
| 124 |
/* Push each element in buffer_vertices to the vertex shader */ |
| 125 |
bindAsFramebuffer(); |
| 126 |
gl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 127 |
|
| 128 |
// gl->DisableVertexAttribArray(display->fillRect->attributes["v_coord"]); |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
int IOGfxSurfaceGL2::internalBlit(IOGfxSurface* src, const SDL_Rect* srcrect, SDL_Rect* dstrect, bool useColorKey) { |
| 134 |
if (src == NULL) |
| 135 |
return SDL_SetError("IOGfxSurfaceGL2::blitStretch: passed a NULL surface"); |
| 136 |
if (dstrect == NULL) |
| 137 |
return SDL_SetError("IOGfxSurfaceGL2::blitStretch: passed a NULL dstrect"); |
| 138 |
IOGfxSurfaceGL2* src_surf = dynamic_cast<IOGfxSurfaceGL2*>(src); |
| 139 |
GLuint src_tex = src_surf->texture; |
| 140 |
|
| 141 |
IOGfxGLFuncs* gl = display->gl; |
| 142 |
|
| 143 |
GLuint vbo; |
| 144 |
if (srcrect == NULL || (srcrect->x == 0 && srcrect->y == 0 && srcrect->w == src_surf->w && srcrect->h == src_surf->h)) { |
| 145 |
vbo = display->vboSpriteTexcoords; |
| 146 |
} else { |
| 147 |
float x1 = 1.0 * srcrect->x / src_surf->w; |
| 148 |
float y1 = 1.0 * srcrect->y / src_surf->h; |
| 149 |
float x2 = 1.0 * (srcrect->x+srcrect->w) / src_surf->w; |
| 150 |
float y2 = 1.0 * (srcrect->y+srcrect->h) / src_surf->h; |
| 151 |
GLfloat croppedSpriteTexcoords[] = { |
| 152 |
x1, y1, |
| 153 |
x2, y1, |
| 154 |
x1, y2, |
| 155 |
x2, y2, |
| 156 |
}; |
| 157 |
gl->BindBuffer(GL_ARRAY_BUFFER, display->vboCroppedSpriteTexcoords); |
| 158 |
gl->BufferData(GL_ARRAY_BUFFER, sizeof(croppedSpriteTexcoords), croppedSpriteTexcoords, GL_STATIC_DRAW); |
| 159 |
vbo = display->vboCroppedSpriteTexcoords; |
| 160 |
} |
| 161 |
|
| 162 |
gl->UseProgram(display->blit->program); |
| 163 |
display->setVertexAttrib(display->blit, display->blit->attributes["v_coord"], display->vboSpriteVertices, 4); |
| 164 |
display->setVertexAttrib(display->blit, display->blit->attributes["v_texcoord"], vbo, 2); |
| 165 |
|
| 166 |
gl->ActiveTexture(GL_TEXTURE0); |
| 167 |
gl->Uniform1i(display->blit->uniforms["texture"], /*GL_TEXTURE*/0); |
| 168 |
gl->BindTexture(GL_TEXTURE_2D, src_tex); |
| 169 |
|
| 170 |
glm::mat4 projection = glm::ortho(0.0f, 1.0f*display->w, 0.0f, 1.0f*display->h); |
| 171 |
glm::mat4 m_transform; |
| 172 |
m_transform = glm::translate(glm::mat4(1.0f), glm::vec3(dstrect->x,dstrect->y, 0.0)) |
| 173 |
* glm::scale(glm::mat4(1.0f), glm::vec3(dstrect->w, dstrect->h, 0.0)); |
| 174 |
glm::mat4 mvp = projection * m_transform; // * view * model * anim; |
| 175 |
gl->UniformMatrix4fv(display->blit->uniforms["mvp"], 1, GL_FALSE, glm::value_ptr(mvp)); |
| 176 |
|
| 177 |
if (useColorKey && src_surf->colorkey.a == SDL_ALPHA_TRANSPARENT) |
| 178 |
gl->Uniform3f(display->blit->uniforms["colorkey"], |
| 179 |
src_surf->colorkey.r/255.0, src_surf->colorkey.g/255.0, src_surf->colorkey.b/255.0); |
| 180 |
else |
| 181 |
gl->Uniform3f(display->blit->uniforms["colorkey"], -1,-1,-1); |
| 182 |
|
| 183 |
/* Push each element in buffer_vertices to the vertex shader */ |
| 184 |
bindAsFramebuffer(); |
| 185 |
gl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 186 |
|
| 187 |
// gl->DisableVertexAttribArray(display->blit->attributes["v_coord"]); |
| 188 |
// gl->DisableVertexAttribArray(display->blit->attributes["v_texcoord"]); |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
int IOGfxSurfaceGL2::blit(IOGfxSurface* src, const SDL_Rect* srcrect, SDL_Rect* dstrect) { |
| 195 |
if (src == NULL) |
| 196 |
return SDL_SetError("IOGfxSurfaceGL2::blit: passed a NULL surface"); |
| 197 |
IOGfxSurfaceGL2* src_surf = dynamic_cast<IOGfxSurfaceGL2*>(src); |
| 198 |
|
| 199 |
SDL_Rect dstrect_if_not_null; |
| 200 |
if (dstrect == NULL) { |
| 201 |
dstrect = &dstrect_if_not_null; |
| 202 |
dstrect->x = 0; |
| 203 |
dstrect->y = 0; |
| 204 |
} |
| 205 |
// Force no-stretch blit |
| 206 |
if (srcrect == NULL) { |
| 207 |
dstrect->w = src_surf->w; |
| 208 |
dstrect->h = src_surf->h; |
| 209 |
} else { |
| 210 |
dstrect->w = srcrect->w; |
| 211 |
dstrect->h = srcrect->h; |
| 212 |
} |
| 213 |
|
| 214 |
return internalBlit(src, srcrect, dstrect, true); |
| 215 |
} |
| 216 |
|
| 217 |
int IOGfxSurfaceGL2::blitStretch(IOGfxSurface* src, const SDL_Rect* srcrect, SDL_Rect* dstrect) { |
| 218 |
return internalBlit(src, srcrect, dstrect, true); |
| 219 |
} |
| 220 |
|
| 221 |
int IOGfxSurfaceGL2::blitNoColorKey(IOGfxSurface* src, const SDL_Rect* srcrect, SDL_Rect* dstrect) { |
| 222 |
if (src == NULL) |
| 223 |
return SDL_SetError("IOGfxSurfaceGL2::blitStretch: passed a NULL surface"); |
| 224 |
IOGfxSurfaceGL2* src_surf = dynamic_cast<IOGfxSurfaceGL2*>(src); |
| 225 |
|
| 226 |
SDL_Rect dstrect_if_not_null; |
| 227 |
if (dstrect == NULL) { |
| 228 |
dstrect = &dstrect_if_not_null; |
| 229 |
dstrect->x = 0; |
| 230 |
dstrect->y = 0; |
| 231 |
} |
| 232 |
// Force no-stretch blit |
| 233 |
if (srcrect == NULL) { |
| 234 |
dstrect->w = src_surf->w; |
| 235 |
dstrect->h = src_surf->h; |
| 236 |
} else { |
| 237 |
dstrect->w = srcrect->w; |
| 238 |
dstrect->h = srcrect->h; |
| 239 |
} |
| 240 |
|
| 241 |
return internalBlit(src, srcrect, dstrect, false); |
| 242 |
} |
| 243 |
|
| 244 |
SDL_Surface* IOGfxSurfaceGL2::screenshot() { |
| 245 |
IOGfxGLFuncs* gl = display->gl; |
| 246 |
|
| 247 |
bindAsFramebuffer(); |
| 248 |
|
| 249 |
// assume 4-bytes alignment |
| 250 |
SDL_Surface* image = SDL_CreateRGBSurface(0, |
| 251 |
w, h, 32, |
| 252 |
#if SDL_BYTEORDER == SDL_BIG_ENDIAN |
| 253 |
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff |
| 254 |
#else |
| 255 |
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 |
| 256 |
#endif |
| 257 |
); |
| 258 |
unsigned char* pixels = (unsigned char*)image->pixels; |
| 259 |
gl->ReadPixels(0, 0, w, h, |
| 260 |
GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 261 |
|
| 262 |
// Don't flip vertically - textures were uploaded OpenGL-reversed already |
| 263 |
|
| 264 |
return image; |
| 265 |
} |
| 266 |
|
| 267 |
unsigned int IOGfxSurfaceGL2::getMemUsage() { |
| 268 |
// TODO |
| 269 |
return 0; |
| 270 |
} |