sdrblurtest

view 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 source
1 #include <GL/glew.h>
2 #include "rtarg.h"
4 RenderTarget::RenderTarget()
5 {
6 fbo = tex = zbuf = 0;
7 }
9 RenderTarget::~RenderTarget()
10 {
11 destroy();
12 }
14 bool RenderTarget::create(int xsz, int ysz)
15 {
16 destroy();
18 glGenFramebuffers(1, &fbo);
19 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
21 this->xsz = xsz;
22 this->ysz = ysz;
23 tex_xsz = next_pow2(xsz);
24 tex_ysz = next_pow2(ysz);
26 glGenTextures(1, &tex);
27 glBindTexture(GL_TEXTURE_2D, tex);
28 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
29 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
30 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
31 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
32 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
34 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
36 glGenRenderbuffers(1, &zbuf);
37 glBindRenderbuffer(GL_RENDERBUFFER, zbuf);
38 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
40 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf);
42 glBindFramebuffer(GL_FRAMEBUFFER, 0);
43 return true;
44 }
46 void RenderTarget::destroy()
47 {
48 if(fbo) {
49 glDeleteFramebuffers(1, &fbo);
50 }
51 if(tex) {
52 glDeleteTextures(1, &tex);
53 }
54 if(zbuf) {
55 glDeleteRenderbuffers(1, &zbuf);
56 }
57 }
59 void RenderTarget::reshape(int new_xsz, int new_ysz)
60 {
61 if(!fbo) {
62 create(new_xsz, new_ysz);
63 return;
64 }
66 xsz = new_xsz;
67 ysz = new_ysz;
69 int new_txsz = next_pow2(new_xsz);
70 int new_tysz = next_pow2(new_ysz);
72 if(tex_xsz >= new_txsz && tex_ysz >= new_tysz) {
73 return;
74 }
76 tex_xsz = new_txsz;
77 tex_ysz = new_tysz;
79 glBindTexture(GL_TEXTURE_2D, tex);
80 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
82 glBindRenderbuffer(GL_RENDERBUFFER, zbuf);
83 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
84 }
86 void RenderTarget::display() const
87 {
88 glMatrixMode(GL_MODELVIEW);
89 glPushMatrix();
90 glLoadIdentity();
91 glMatrixMode(GL_PROJECTION);
92 glPushMatrix();
93 glLoadIdentity();
95 glPushAttrib(GL_ENABLE_BIT);
96 glDisable(GL_DEPTH_TEST);
97 glDisable(GL_LIGHTING);
98 glEnable(GL_TEXTURE_2D);
99 glBindTexture(GL_TEXTURE_2D, tex);
101 float max_u = (float)xsz / (float)tex_xsz;
102 float max_v = (float)ysz / (float)tex_ysz;
104 glBegin(GL_QUADS);
105 glColor3f(1, 1, 1);
106 glTexCoord2f(0, 0);
107 glVertex2f(-1, -1);
108 glTexCoord2f(max_u, 0);
109 glVertex2f(1, -1);
110 glTexCoord2f(max_u, max_v);
111 glVertex2f(1, 1);
112 glTexCoord2f(0, max_v);
113 glVertex2f(-1, 1);
114 glEnd();
116 glPopAttrib();
117 glPopMatrix();
118 glMatrixMode(GL_MODELVIEW);
119 glPopMatrix();
120 }
122 void bind_render_target(const RenderTarget *rtarg)
123 {
124 glBindFramebuffer(GL_FRAMEBUFFER, rtarg ? rtarg->fbo : 0);
125 }
127 int next_pow2(int x)
128 {
129 x--;
130 x = (x >> 1) | x;
131 x = (x >> 2) | x;
132 x = (x >> 4) | x;
133 x = (x >> 8) | x;
134 x = (x >> 16) | x;
135 return x + 1;
136 }