new render target class while working on the exhibit UI
[laserbrain_demo] / src / post.cc
1 #include "opengl.h"
2 #include "app.h"
3 #include "texture.h"    // next_pow2
4
5 static void update_fbtex();
6
7 static unsigned int fb_tex;
8 static int fb_tex_width, fb_tex_height;
9
10 void slow_post(unsigned int sdr)
11 {
12         update_fbtex();
13
14         glBindTexture(GL_TEXTURE_2D, fb_tex);
15         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, win_width, win_height);
16
17         glPushAttrib(GL_ENABLE_BIT);
18         glDisable(GL_DEPTH_TEST);
19         glDisable(GL_LIGHTING);
20
21         glUseProgram(sdr);
22
23         float umax = (float)win_width / (float)fb_tex_width;
24         float vmax = (float)win_height / (float)fb_tex_height;
25
26         glBegin(GL_QUADS);
27         glTexCoord2f(0, 0);
28         glVertex2f(-1, -1);
29         glTexCoord2f(umax, 0);
30         glVertex2f(1, -1);
31         glTexCoord2f(umax, vmax);
32         glVertex2f(1, 1);
33         glTexCoord2f(0, vmax);
34         glVertex2f(-1, 1);
35         glEnd();
36
37         glPopAttrib();
38 }
39
40 static void update_fbtex()
41 {
42         if(win_width <= fb_tex_width && win_height <= fb_tex_height) {
43                 return;         // nothing to do
44         }
45         if(!fb_tex) {
46                 glGenTextures(1, &fb_tex);
47                 glBindTexture(GL_TEXTURE_2D, fb_tex);
48                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
49                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
50         } else {
51                 glBindTexture(GL_TEXTURE_2D, fb_tex);
52         }
53
54         fb_tex_width = next_pow2(win_width);
55         fb_tex_height = next_pow2(win_height);
56         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
57 }