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