vrseasons
diff src/rtarg.cc @ 0:393ef1143c9c
VR seasons
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 07 Apr 2015 11:16:56 +0300 |
parents | |
children | eea1b91dc3d4 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/rtarg.cc Tue Apr 07 11:16:56 2015 +0300 1.3 @@ -0,0 +1,135 @@ 1.4 +#include "opengl.h" 1.5 +#include "rtarg.h" 1.6 + 1.7 +static int next_pow2(int x); 1.8 + 1.9 +RenderTarget::RenderTarget() 1.10 +{ 1.11 + xsz = ysz = 0; 1.12 + fbo = color = depth = 0; 1.13 + 1.14 + tex_fmt = 0; 1.15 + tex_xsz = tex_ysz = 0; 1.16 +} 1.17 + 1.18 +RenderTarget::~RenderTarget() 1.19 +{ 1.20 + destroy(); 1.21 +} 1.22 + 1.23 +bool RenderTarget::create(int xsz, int ysz) 1.24 +{ 1.25 + destroy(); 1.26 + 1.27 + this->xsz = xsz; 1.28 + this->ysz = ysz; 1.29 + 1.30 + tex_xsz = next_pow2(xsz); 1.31 + tex_ysz = next_pow2(ysz); 1.32 + tex_fmt = check_extension("GL_ARB_texture_float") ? GL_RGB16F : GL_RGB; 1.33 + 1.34 + glGenFramebuffers(1, &fbo); 1.35 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 1.36 + 1.37 + glGenTextures(1, &color); 1.38 + glBindTexture(GL_TEXTURE_2D, color); 1.39 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.40 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.41 + glTexImage2D(GL_TEXTURE_2D, 0, tex_fmt, tex_xsz, tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 1.42 + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0); 1.43 + 1.44 + glGenRenderbuffers(1, &depth); 1.45 + glBindRenderbuffer(GL_RENDERBUFFER, depth); 1.46 + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz); 1.47 + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth); 1.48 + 1.49 + return true; 1.50 +} 1.51 + 1.52 +bool RenderTarget::resize(int xsz, int ysz) 1.53 +{ 1.54 + if(!fbo) { 1.55 + return create(xsz, ysz); 1.56 + } 1.57 + 1.58 + this->xsz = xsz; 1.59 + this->ysz = ysz; 1.60 + int new_tex_xsz = next_pow2(xsz); 1.61 + int new_tex_ysz = next_pow2(ysz); 1.62 + 1.63 + if(tex_xsz != new_tex_xsz || tex_ysz != new_tex_ysz) { 1.64 + glBindTexture(GL_TEXTURE_2D, color); 1.65 + glTexImage2D(GL_TEXTURE_2D, 0, tex_fmt, tex_xsz, tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 1.66 + 1.67 + glBindRenderbuffer(GL_RENDERBUFFER, depth); 1.68 + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz); 1.69 + 1.70 + tex_xsz = new_tex_xsz; 1.71 + tex_ysz = new_tex_ysz; 1.72 + } 1.73 + return true; 1.74 +} 1.75 + 1.76 +void RenderTarget::destroy() 1.77 +{ 1.78 + if(fbo) { 1.79 + glDeleteFramebuffers(1, &fbo); 1.80 + } 1.81 + if(color) { 1.82 + glDeleteTextures(1, &color); 1.83 + } 1.84 + if(depth) { 1.85 + glDeleteRenderbuffers(1, &depth); 1.86 + } 1.87 + fbo = color = depth = 0; 1.88 +} 1.89 + 1.90 +bool RenderTarget::is_valid() const 1.91 +{ 1.92 + return fbo != 0; 1.93 +} 1.94 + 1.95 +unsigned int RenderTarget::get_texture() const 1.96 +{ 1.97 + return color; 1.98 +} 1.99 + 1.100 +int RenderTarget::get_width() const 1.101 +{ 1.102 + return xsz; 1.103 +} 1.104 + 1.105 +int RenderTarget::get_height() const 1.106 +{ 1.107 + return ysz; 1.108 +} 1.109 + 1.110 +int RenderTarget::get_tex_width() const 1.111 +{ 1.112 + return tex_xsz; 1.113 +} 1.114 + 1.115 +int RenderTarget::get_tex_height() const 1.116 +{ 1.117 + return tex_ysz; 1.118 +} 1.119 + 1.120 +void set_render_target(const RenderTarget *rtarg) 1.121 +{ 1.122 + if(rtarg) { 1.123 + glBindFramebuffer(GL_FRAMEBUFFER, rtarg->color); 1.124 + } else { 1.125 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 1.126 + } 1.127 +} 1.128 + 1.129 +static int next_pow2(int x) 1.130 +{ 1.131 + x--; 1.132 + x = (x >> 1) | x; 1.133 + x = (x >> 2) | x; 1.134 + x = (x >> 4) | x; 1.135 + x = (x >> 8) | x; 1.136 + x = (x >> 16) | x; 1.137 + return x + 1; 1.138 +}