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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
// Hyperbolic Rogue -- drawing screens to buffers
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file renderbuffer.cpp
* \brief drawing screens to buffers
*
* This file implements the 'renderbuffer', which is an object
* that can be used to draw a HyperRogue screen into,
* and then either used as a OpenGL texture (e.g. in the Hypersian Rug mode), or saved.
*/
#include "hyper.h"
namespace hr {
#if CAP_GL
#if !CAP_GLEW
#if ISLINUX
extern "C" {
GLAPI void APIENTRY glGenFramebuffers (GLsizei n, GLuint *framebuffers);
GLAPI void APIENTRY glBindFramebuffer (GLenum target, GLuint framebuffer);
GLAPI void APIENTRY glFramebufferTexture (GLenum target, GLenum attachment, GLuint texture, GLint level);
GLAPI GLenum APIENTRY glCheckFramebufferStatus (GLenum target);
GLAPI void APIENTRY glDrawBuffers (GLsizei n, const GLenum *bufs);
GLAPI void APIENTRY glGenRenderbuffers (GLsizei n, GLuint *renderbuffers);
GLAPI void APIENTRY glBindRenderbuffer (GLenum target, GLuint renderbuffer);
GLAPI void APIENTRY glRenderbufferStorage (GLenum target, GLenum internalformat, GLsizei width, GLsizei height);
GLAPI void APIENTRY glFramebufferRenderbuffer (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer);
GLAPI void APIENTRY glDeleteRenderbuffers (GLsizei n, const GLuint *renderbuffers);
GLAPI void APIENTRY glDeleteFramebuffers (GLsizei n, const GLuint *framebuffers);
}
#endif
#if ISMAC
#define glFramebufferTexture glFramebufferTextureEXT
#endif
#endif
#endif
#if HDR
struct renderbuffer {
bool valid;
int x, y;
#if CAP_GL
int tx, ty;
GLuint FramebufferName;
GLuint renderedTexture;
GLuint depth_stencil_rb;
Uint32 *expanded_data;
void use_as_texture();
#endif
#if CAP_SDL
SDL_Surface *srf;
void make_surface();
SDL_Surface *render();
#endif
renderbuffer(int x, int y, bool gl);
~renderbuffer();
void enable();
void clear(color_t col);
};
struct resetbuffer {
GLint drawFboId, readFboId;
#if CAP_SDL
SDL_Surface *sreset;
#endif
resetbuffer();
void reset();
};
#endif
renderbuffer::renderbuffer(int x, int y, bool gl) : x(x), y(y) {
valid = false;
#if CAP_GL
FramebufferName = renderedTexture = depth_stencil_rb = 0; expanded_data = NULL;
#endif
#if CAP_SDL
srf = NULL;
#endif
tx = next_p2(x);
ty = next_p2(y);
# if CAP_GL
if(gl) {
GLERR("renderbuffer init");
resetbuffer rb;
GLERR("after resetbuffer");
FramebufferName = renderedTexture = depth_stencil_rb = 0;
GLERR("even before");
glGenFramebuffers(1, &FramebufferName); //
GLERR("GenFramebuffer");
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
GLERR("BindFramebuffer");
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, tx, ty, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GLERR("GenTextures");
#ifdef TEX
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
#else
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderedTexture, 0);
#endif
GLERR("FramebufferTexture");
// GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
// glDrawBuffers(1, DrawBuffers);
glGenRenderbuffers(1, &depth_stencil_rb);
GLERR("GenRenderbuffer");
glBindRenderbuffer(GL_RENDERBUFFER, depth_stencil_rb);
GLERR("BindRenderbuffer");
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, tx, ty);
bool has_depth = true;
if(glGetError() != GL_NO_ERROR) {
println(hlog, "Could not create: GL_DEPTH24_STENCIL8");
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, tx, ty);
has_depth = false;
}
GLERR("RbS");
if(has_depth)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_stencil_rb);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth_stencil_rb);
GLERR("FrRb");
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
FramebufferName = renderedTexture = 0;
else
valid = true;
DEBB(DF_GRAPH, ("Framebuffer remains = ", int(FramebufferName), " (", int(valid), ")"));
GLERR("initialization");
rb.reset();
}
#endif
#if CAP_SDL
if(!valid)
make_surface();
#endif
}
#if CAP_SDL
void renderbuffer::make_surface() {
if(!srf)
srf = SDL_CreateRGBSurface(SDL_SWSURFACE, x, y, 32,0xff0000,0xff00,0xff,0xff000000);
}
SDL_Surface *renderbuffer::render() {
make_surface() ;
if(FramebufferName) {
glReadPixels(0, 0, x, y, GL_BGRA, GL_UNSIGNED_BYTE, srf->pixels);
GLERR("readPixels");
for(int iy=0; iy<y/2; iy++)
for(int ix=0; ix<x; ix++)
swap(qpixel(srf,ix,iy), qpixel(srf,ix,y-1-iy));
}
return srf;
}
#endif
EX int current_rbuffer = -1;
void renderbuffer::enable() {
#if CAP_GL
if(FramebufferName) {
GLERR("prebind");
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
current_rbuffer = FramebufferName;
GLERR("bind");
vid.usingGL = true;
return;
}
#endif
#if CAP_SDL
make_surface();
s = srf;
vid.usingGL = false;
#endif
}
#if CAP_GL
void renderbuffer::use_as_texture() {
if(!renderedTexture) {
glGenTextures( 1, &renderedTexture);
glBindTexture( GL_TEXTURE_2D, renderedTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
}
if(FramebufferName) {
glBindTexture( GL_TEXTURE_2D, renderedTexture);
}
#if CAP_SDL
else {
if(!expanded_data)
expanded_data = new Uint32[tx * ty];
for(int y=0; y<ty; y++) for(int x=0; x<tx; x++)
expanded_data[y*tx + x] = qpixel(srf, x, ty-1-y) | 0xFF000000;
glBindTexture( GL_TEXTURE_2D, renderedTexture);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tx, ty, 0, GL_BGRA, GL_UNSIGNED_BYTE, expanded_data );
}
#endif
}
#endif
renderbuffer::~renderbuffer() {
#if CAP_GL
if(renderedTexture)
glDeleteTextures(1, &renderedTexture);
if(FramebufferName) {
glDeleteRenderbuffers(1, &depth_stencil_rb);
glDeleteFramebuffers(1, &FramebufferName);
}
if(expanded_data)
delete[] expanded_data;
#endif
#if CAP_SDL
if(srf)
SDL_FreeSurface(srf);
#endif
}
void renderbuffer::clear(color_t col) {
#if CAP_GL
if(FramebufferName) {
setGLProjection(col);
return;
}
#endif
#if CAP_SDL
SDL_FillRect(srf, NULL, col);
#endif
}
resetbuffer::resetbuffer() {
#if CAP_GL
drawFboId = 0, readFboId = 0;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFboId);
GLERR("getInteger a");
#ifndef GLES_ONLY
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFboId);
GLERR("getInteger b");
#endif
#endif
#if CAP_SDL
sreset = s;
#endif
}
void resetbuffer::reset() {
#if CAP_GL
glBindFramebuffer(GL_FRAMEBUFFER, drawFboId);
current_rbuffer = drawFboId;
#endif
#if CAP_SDL
s = sreset;
#endif
}
}
|