sdrblurtest

view src/main.cc @ 1:e8fc0c9754c9

added fps counter
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Oct 2015 17:46:02 +0300
parents 26513fdda566
children
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(int x, int y, 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 static int nframes;
100 static char fpsstr[64] = "fps: ???";
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();
104 glTranslatef(0, 0, -cam_dist);
105 glRotatef(cam_phi, 1, 0, 0);
106 glRotatef(cam_theta, 0, 1, 0);
108 bind_render_target(rtarg);
110 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
112 glFrontFace(GL_CW);
113 glutSolidTeapot(1.0);
114 glFrontFace(GL_CCW);
116 if(blur_type == BLUR_SEP) {
117 // render with horizontal blur into tmp_rtarg
118 bind_render_target(tmp_rtarg);
120 set_uniform_float2(sdr_hblur, "size", rtarg->xsz, rtarg->ysz);
121 set_uniform_float2(sdr_hblur, "texsize", rtarg->tex_xsz, rtarg->tex_ysz);
122 bind_program(sdr_hblur);
124 rtarg->display();
126 // render with vertical blur into the regular framebuffer
127 bind_render_target(0);
129 set_uniform_float2(sdr_vblur, "size", tmp_rtarg->xsz, tmp_rtarg->ysz);
130 set_uniform_float2(sdr_vblur, "texsize", tmp_rtarg->tex_xsz, tmp_rtarg->tex_ysz);
131 bind_program(sdr_vblur);
133 tmp_rtarg->display();
135 } else {
136 // render with the full blur into the regular framebuffer
137 bind_render_target(0);
139 // display the rendered image
140 set_uniform_float2(sdrblur, "size", rtarg->xsz, rtarg->ysz);
141 set_uniform_float2(sdrblur, "texsize", rtarg->tex_xsz, rtarg->tex_ysz);
142 bind_program(sdrblur);
144 rtarg->display();
145 }
146 bind_program(0);
148 ++nframes;
149 static unsigned int prev_fps_upd;
150 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
151 unsigned int dt = msec - prev_fps_upd;
152 if(dt > 1500) {
153 float fps = 1000.0 * (float)nframes / (float)dt;
154 prev_fps_upd = msec;
155 nframes = 0;
156 sprintf(fpsstr, "fps: %.1f", fps);
157 }
159 print_string(1, 30, blur_type == BLUR_REG ? "Regular blur" : "Seperable blur");
160 print_string(1, 5, fpsstr);
162 glutSwapBuffers();
163 assert(glGetError() == GL_NO_ERROR);
164 }
166 static void print_string(int x, int y, const char *str)
167 {
168 glPushAttrib(GL_ENABLE_BIT);
170 glMatrixMode(GL_MODELVIEW);
171 glPushMatrix();
172 glLoadIdentity();
173 glMatrixMode(GL_PROJECTION);
174 glPushMatrix();
175 glLoadIdentity();
176 glOrtho(0, win_width, 0, win_height, -1, 1);
178 glDisable(GL_LIGHTING);
179 glDisable(GL_DEPTH_TEST);
181 glRasterPos2f(x, y);
182 glColor3f(1, 1, 0);
183 while(*str) {
184 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str++);
185 }
187 glPopMatrix();
188 glMatrixMode(GL_MODELVIEW);
189 glPopMatrix();
191 glPopAttrib();
192 }
194 static void idle()
195 {
196 glutPostRedisplay();
197 }
199 static void reshape(int x, int y)
200 {
201 win_width = x;
202 win_height = y;
204 glViewport(0, 0, x, y);
206 glMatrixMode(GL_PROJECTION);
207 glLoadIdentity();
208 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
210 rtarg->reshape(x, y);
211 tmp_rtarg->reshape(x, y);
212 }
214 static void keyb(unsigned char key, int x, int y)
215 {
216 switch(key) {
217 case 27:
218 exit(0);
220 case ' ':
221 blur_type = (blur_type + 1) % NUM_BLUR_TYPES;
222 break;
223 }
224 }
227 static bool bnstate[32];
228 static int prev_x, prev_y;
230 static void mouse(int bn, int st, int x, int y)
231 {
232 prev_x = x;
233 prev_y = y;
234 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
235 }
237 static void motion(int x, int y)
238 {
239 int dx = x - prev_x;
240 int dy = y - prev_y;
241 prev_x = x;
242 prev_y = y;
244 if(bnstate[0]) {
245 cam_theta += dx * 0.5;
246 cam_phi += dy * 0.5;
248 if(cam_phi < -90) cam_phi = -90;
249 if(cam_phi > 90) cam_phi = 90;
250 }
251 if(bnstate[2]) {
252 cam_dist -= dy * 0.1;
254 if(cam_dist < 0) cam_dist = 0;
255 }
256 }