sdrblurtest

view src/main.cc @ 0:26513fdda566

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Oct 2013 08:19:56 +0300
parents
children e8fc0c9754c9
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
6 #include <GL/glew.h>
7 #ifndef __APPLE__
8 #include <GL/glut.h>
9 #else
10 #include <GLUT/glut.h>
11 #endif
12 #include "rtarg.h"
13 #include "sdr.h"
15 enum {
16 BLUR_REG,
17 BLUR_SEP,
19 NUM_BLUR_TYPES
20 };
22 static bool init();
23 static void cleanup();
24 static void disp();
25 static void print_string(const char *str);
26 static void idle();
27 static void reshape(int x, int y);
28 static void keyb(unsigned char key, int x, int y);
29 static void mouse(int bn, int st, int x, int y);
30 static void motion(int x, int y);
32 static int win_width, win_height;
33 static float cam_theta, cam_phi, cam_dist = 8;
35 static RenderTarget *rtarg, *tmp_rtarg;
36 static unsigned int sdrblur, sdr_hblur, sdr_vblur;
38 static int blur_type = BLUR_REG;
41 int main(int argc, char **argv)
42 {
43 glutInitWindowSize(1280, 800);
44 glutInit(&argc, argv);
45 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
46 glutCreateWindow("convolution");
48 glutDisplayFunc(disp);
49 glutIdleFunc(idle);
50 glutReshapeFunc(reshape);
51 glutKeyboardFunc(keyb);
52 glutMouseFunc(mouse);
53 glutMotionFunc(motion);
55 if(!init()) {
56 return 1;
57 }
58 atexit(cleanup);
60 glutMainLoop();
61 return 0;
62 }
64 static bool init()
65 {
66 glewInit();
68 glClearColor(0.1, 0.1, 0.1, 1);
70 glEnable(GL_DEPTH_TEST);
71 glEnable(GL_CULL_FACE);
72 glEnable(GL_LIGHTING);
73 glEnable(GL_LIGHT0);
75 rtarg = new RenderTarget;
76 tmp_rtarg = new RenderTarget;
78 if(!(sdrblur = create_program_load("sdr/vert.glsl", "sdr/blur.glsl"))) {
79 return false;
80 }
81 if(!(sdr_hblur = create_program_load("sdr/vert.glsl", "sdr/hblur.glsl"))) {
82 return false;
83 }
84 if(!(sdr_vblur = create_program_load("sdr/vert.glsl", "sdr/vblur.glsl"))) {
85 return false;
86 }
88 return true;
89 }
91 static void cleanup()
92 {
93 delete rtarg;
94 delete tmp_rtarg;
95 }
97 static void disp()
98 {
99 glMatrixMode(GL_MODELVIEW);
100 glLoadIdentity();
101 glTranslatef(0, 0, -cam_dist);
102 glRotatef(cam_phi, 1, 0, 0);
103 glRotatef(cam_theta, 0, 1, 0);
105 bind_render_target(rtarg);
107 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109 glFrontFace(GL_CW);
110 glutSolidTeapot(1.0);
111 glFrontFace(GL_CCW);
113 if(blur_type == BLUR_SEP) {
114 // render with horizontal blur into tmp_rtarg
115 bind_render_target(tmp_rtarg);
117 set_uniform_float2(sdr_hblur, "size", rtarg->xsz, rtarg->ysz);
118 set_uniform_float2(sdr_hblur, "texsize", rtarg->tex_xsz, rtarg->tex_ysz);
119 bind_program(sdr_hblur);
121 rtarg->display();
123 // render with vertical blur into the regular framebuffer
124 bind_render_target(0);
126 set_uniform_float2(sdr_vblur, "size", tmp_rtarg->xsz, tmp_rtarg->ysz);
127 set_uniform_float2(sdr_vblur, "texsize", tmp_rtarg->tex_xsz, tmp_rtarg->tex_ysz);
128 bind_program(sdr_vblur);
130 tmp_rtarg->display();
132 } else {
133 // render with the full blur into the regular framebuffer
134 bind_render_target(0);
136 // display the rendered image
137 set_uniform_float2(sdrblur, "size", rtarg->xsz, rtarg->ysz);
138 set_uniform_float2(sdrblur, "texsize", rtarg->tex_xsz, rtarg->tex_ysz);
139 bind_program(sdrblur);
141 rtarg->display();
142 }
143 bind_program(0);
145 print_string(blur_type == BLUR_REG ? "Regular blur" : "Seperable blur");
147 glutSwapBuffers();
148 assert(glGetError() == GL_NO_ERROR);
149 }
151 static void print_string(const char *str)
152 {
153 glPushAttrib(GL_ENABLE_BIT);
155 glMatrixMode(GL_MODELVIEW);
156 glPushMatrix();
157 glLoadIdentity();
158 glMatrixMode(GL_PROJECTION);
159 glPushMatrix();
160 glLoadIdentity();
161 glOrtho(0, win_width, 0, win_height, -1, 1);
163 glDisable(GL_LIGHTING);
164 glDisable(GL_DEPTH_TEST);
166 glRasterPos2f(1, 1);
167 glColor3f(1, 1, 0);
168 while(*str) {
169 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str++);
170 }
172 glPopMatrix();
173 glMatrixMode(GL_MODELVIEW);
174 glPopMatrix();
176 glPopAttrib();
177 }
179 static void idle()
180 {
181 glutPostRedisplay();
182 }
184 static void reshape(int x, int y)
185 {
186 win_width = x;
187 win_height = y;
189 glViewport(0, 0, x, y);
191 glMatrixMode(GL_PROJECTION);
192 glLoadIdentity();
193 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
195 rtarg->reshape(x, y);
196 tmp_rtarg->reshape(x, y);
197 }
199 static void keyb(unsigned char key, int x, int y)
200 {
201 switch(key) {
202 case 27:
203 exit(0);
205 case ' ':
206 blur_type = (blur_type + 1) % NUM_BLUR_TYPES;
207 break;
208 }
209 }
212 static bool bnstate[32];
213 static int prev_x, prev_y;
215 static void mouse(int bn, int st, int x, int y)
216 {
217 prev_x = x;
218 prev_y = y;
219 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
220 }
222 static void motion(int x, int y)
223 {
224 int dx = x - prev_x;
225 int dy = y - prev_y;
226 prev_x = x;
227 prev_y = y;
229 if(bnstate[0]) {
230 cam_theta += dx * 0.5;
231 cam_phi += dy * 0.5;
233 if(cam_phi < -90) cam_phi = -90;
234 if(cam_phi > 90) cam_phi = 90;
235 }
236 if(bnstate[2]) {
237 cam_dist -= dy * 0.1;
239 if(cam_dist < 0) cam_dist = 0;
240 }
241 }