91109b2d97770cb0a8438a95cd0355d73b4a9166
[demo_prior] / src / post.c
1 #include "opengl.h"
2 #include "texture.h"
3 #include "post.h"
4 #include "demo.h"
5 #include "opt.h"
6 #include "sdr.h"
7
8 static unsigned int post_fbtex_gltexid[2];
9 static unsigned int rbuf_depth[2];
10
11 unsigned int post_fbo[2];
12 struct texture post_fbtex[2];
13 int post_fbtex_cur;
14
15 unsigned int post_sdr[MAX_POST_SDR];
16
17 int post_init(void)
18 {
19         int i;
20         static const char *psdr_fname[] = {"sdr/oldfig.p.glsl"};
21
22         for(i=0; i<MAX_POST_SDR; i++) {
23                 if(!(post_sdr[i] = create_program_load("sdr/post.v.glsl", psdr_fname[i]))) {
24                         return -1;
25                 }
26         }
27
28         glGenTextures(2, post_fbtex_gltexid);
29         glGenRenderbuffers(2, rbuf_depth);
30
31         for(i=0; i<2; i++) {
32                 post_fbtex[i].id = post_fbtex_gltexid[i];
33                 post_fbtex[i].width = 0;
34                 post_fbtex[i].height = 0;
35                 post_fbtex[i].pixels = 0;
36
37                 glBindTexture(GL_TEXTURE_2D, post_fbtex[i].id);
38                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
39                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
40         }
41         return 0;
42 }
43
44 void post_cleanup(void)
45 {
46         if(post_fbtex_gltexid[0]) {
47                 glDeleteTextures(2, post_fbtex_gltexid);
48         }
49         if(rbuf_depth[0]) {
50                 glDeleteRenderbuffers(2, rbuf_depth);
51         }
52         if(post_fbo[0]) {
53                 glDeleteFramebuffers(2, post_fbo);
54         }
55 }
56
57 void post_reshape(int x, int y)
58 {
59         int i, ifmt;
60
61         if(x != post_fbtex[0].width || y != post_fbtex[0].height) {
62                 ifmt = opt.srgb ? GL_SRGB_ALPHA : GL_RGBA;
63
64                 for(i=0; i<2; i++) {
65                         glBindTexture(GL_TEXTURE_2D, post_fbtex[i].id);
66                         glTexImage2D(GL_TEXTURE_2D, 0, ifmt, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
67                         post_fbtex[i].width = x;
68                         post_fbtex[i].height = y;
69
70                         glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth[i]);
71                         glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, x, y);
72                 }
73
74                 if(!post_fbo[0]) {
75                         glGenFramebuffers(2, post_fbo);
76                         for(i=0; i<2; i++) {
77                                 glBindFramebuffer(GL_FRAMEBUFFER, post_fbo[i]);
78                                 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, post_fbtex_gltexid[i], 0);
79                                 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth[i]);
80                         }
81                         glBindFramebuffer(GL_FRAMEBUFFER, 0);
82                 }
83         }
84 }
85
86 void overlay(unsigned int tex, float aspect, float alpha)
87 {
88         float xscale = aspect / win_aspect;
89
90         glMatrixMode(GL_MODELVIEW);
91         glPushMatrix();
92         glLoadIdentity();
93         glMatrixMode(GL_PROJECTION);
94         glPushMatrix();
95         glLoadIdentity();
96         glScalef(xscale, 1.0, 1.0);
97
98         glPushAttrib(GL_ENABLE_BIT);
99         glDisable(GL_DEPTH_TEST);
100         glDisable(GL_LIGHTING);
101         glEnable(GL_BLEND);
102         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
103         glEnable(GL_TEXTURE_2D);
104
105         glBindTexture(GL_TEXTURE_2D, tex);
106
107         glBegin(GL_QUADS);
108         glColor4f(1, 1, 1, alpha);
109         glTexCoord2f(0, 1);
110         glVertex2f(-1, -1);
111         glTexCoord2f(1, 1);
112         glVertex2f(1, -1);
113         glTexCoord2f(1, 0);
114         glVertex2f(1, 1);
115         glTexCoord2f(0, 0);
116         glVertex2f(-1, 1);
117         glEnd();
118
119         glPopAttrib();
120
121         glPopMatrix();
122         glMatrixMode(GL_MODELVIEW);
123         glPopMatrix();
124 }
125
126 void overlay_tex(struct texture *tex, float alpha)
127 {
128         overlay(tex->id, (float)tex->width / tex->height, alpha);
129 }