sdrblurtest
diff src/rtarg.cc @ 0:26513fdda566
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Oct 2013 08:19:56 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/rtarg.cc Thu Oct 17 08:19:56 2013 +0300 1.3 @@ -0,0 +1,136 @@ 1.4 +#include <GL/glew.h> 1.5 +#include "rtarg.h" 1.6 + 1.7 +RenderTarget::RenderTarget() 1.8 +{ 1.9 + fbo = tex = zbuf = 0; 1.10 +} 1.11 + 1.12 +RenderTarget::~RenderTarget() 1.13 +{ 1.14 + destroy(); 1.15 +} 1.16 + 1.17 +bool RenderTarget::create(int xsz, int ysz) 1.18 +{ 1.19 + destroy(); 1.20 + 1.21 + glGenFramebuffers(1, &fbo); 1.22 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 1.23 + 1.24 + this->xsz = xsz; 1.25 + this->ysz = ysz; 1.26 + tex_xsz = next_pow2(xsz); 1.27 + tex_ysz = next_pow2(ysz); 1.28 + 1.29 + glGenTextures(1, &tex); 1.30 + glBindTexture(GL_TEXTURE_2D, tex); 1.31 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.32 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.33 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 1.34 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 1.35 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); 1.36 + 1.37 + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0); 1.38 + 1.39 + glGenRenderbuffers(1, &zbuf); 1.40 + glBindRenderbuffer(GL_RENDERBUFFER, zbuf); 1.41 + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz); 1.42 + 1.43 + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf); 1.44 + 1.45 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 1.46 + return true; 1.47 +} 1.48 + 1.49 +void RenderTarget::destroy() 1.50 +{ 1.51 + if(fbo) { 1.52 + glDeleteFramebuffers(1, &fbo); 1.53 + } 1.54 + if(tex) { 1.55 + glDeleteTextures(1, &tex); 1.56 + } 1.57 + if(zbuf) { 1.58 + glDeleteRenderbuffers(1, &zbuf); 1.59 + } 1.60 +} 1.61 + 1.62 +void RenderTarget::reshape(int new_xsz, int new_ysz) 1.63 +{ 1.64 + if(!fbo) { 1.65 + create(new_xsz, new_ysz); 1.66 + return; 1.67 + } 1.68 + 1.69 + xsz = new_xsz; 1.70 + ysz = new_ysz; 1.71 + 1.72 + int new_txsz = next_pow2(new_xsz); 1.73 + int new_tysz = next_pow2(new_ysz); 1.74 + 1.75 + if(tex_xsz >= new_txsz && tex_ysz >= new_tysz) { 1.76 + return; 1.77 + } 1.78 + 1.79 + tex_xsz = new_txsz; 1.80 + tex_ysz = new_tysz; 1.81 + 1.82 + glBindTexture(GL_TEXTURE_2D, tex); 1.83 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); 1.84 + 1.85 + glBindRenderbuffer(GL_RENDERBUFFER, zbuf); 1.86 + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz); 1.87 +} 1.88 + 1.89 +void RenderTarget::display() const 1.90 +{ 1.91 + glMatrixMode(GL_MODELVIEW); 1.92 + glPushMatrix(); 1.93 + glLoadIdentity(); 1.94 + glMatrixMode(GL_PROJECTION); 1.95 + glPushMatrix(); 1.96 + glLoadIdentity(); 1.97 + 1.98 + glPushAttrib(GL_ENABLE_BIT); 1.99 + glDisable(GL_DEPTH_TEST); 1.100 + glDisable(GL_LIGHTING); 1.101 + glEnable(GL_TEXTURE_2D); 1.102 + glBindTexture(GL_TEXTURE_2D, tex); 1.103 + 1.104 + float max_u = (float)xsz / (float)tex_xsz; 1.105 + float max_v = (float)ysz / (float)tex_ysz; 1.106 + 1.107 + glBegin(GL_QUADS); 1.108 + glColor3f(1, 1, 1); 1.109 + glTexCoord2f(0, 0); 1.110 + glVertex2f(-1, -1); 1.111 + glTexCoord2f(max_u, 0); 1.112 + glVertex2f(1, -1); 1.113 + glTexCoord2f(max_u, max_v); 1.114 + glVertex2f(1, 1); 1.115 + glTexCoord2f(0, max_v); 1.116 + glVertex2f(-1, 1); 1.117 + glEnd(); 1.118 + 1.119 + glPopAttrib(); 1.120 + glPopMatrix(); 1.121 + glMatrixMode(GL_MODELVIEW); 1.122 + glPopMatrix(); 1.123 +} 1.124 + 1.125 +void bind_render_target(const RenderTarget *rtarg) 1.126 +{ 1.127 + glBindFramebuffer(GL_FRAMEBUFFER, rtarg ? rtarg->fbo : 0); 1.128 +} 1.129 + 1.130 +int next_pow2(int x) 1.131 +{ 1.132 + x--; 1.133 + x = (x >> 1) | x; 1.134 + x = (x >> 2) | x; 1.135 + x = (x >> 4) | x; 1.136 + x = (x >> 8) | x; 1.137 + x = (x >> 16) | x; 1.138 + return x + 1; 1.139 +}