sdrblurtest

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.cc	Thu Oct 17 08:19:56 2013 +0300
     1.3 @@ -0,0 +1,241 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <math.h>
     1.7 +#include <assert.h>
     1.8 +
     1.9 +#include <GL/glew.h>
    1.10 +#ifndef __APPLE__
    1.11 +#include <GL/glut.h>
    1.12 +#else
    1.13 +#include <GLUT/glut.h>
    1.14 +#endif
    1.15 +#include "rtarg.h"
    1.16 +#include "sdr.h"
    1.17 +
    1.18 +enum {
    1.19 +	BLUR_REG,
    1.20 +	BLUR_SEP,
    1.21 +
    1.22 +	NUM_BLUR_TYPES
    1.23 +};
    1.24 +
    1.25 +static bool init();
    1.26 +static void cleanup();
    1.27 +static void disp();
    1.28 +static void print_string(const char *str);
    1.29 +static void idle();
    1.30 +static void reshape(int x, int y);
    1.31 +static void keyb(unsigned char key, int x, int y);
    1.32 +static void mouse(int bn, int st, int x, int y);
    1.33 +static void motion(int x, int y);
    1.34 +
    1.35 +static int win_width, win_height;
    1.36 +static float cam_theta, cam_phi, cam_dist = 8;
    1.37 +
    1.38 +static RenderTarget *rtarg, *tmp_rtarg;
    1.39 +static unsigned int sdrblur, sdr_hblur, sdr_vblur;
    1.40 +
    1.41 +static int blur_type = BLUR_REG;
    1.42 +
    1.43 +
    1.44 +int main(int argc, char **argv)
    1.45 +{
    1.46 +	glutInitWindowSize(1280, 800);
    1.47 +	glutInit(&argc, argv);
    1.48 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.49 +	glutCreateWindow("convolution");
    1.50 +
    1.51 +	glutDisplayFunc(disp);
    1.52 +	glutIdleFunc(idle);
    1.53 +	glutReshapeFunc(reshape);
    1.54 +	glutKeyboardFunc(keyb);
    1.55 +	glutMouseFunc(mouse);
    1.56 +	glutMotionFunc(motion);
    1.57 +
    1.58 +	if(!init()) {
    1.59 +		return 1;
    1.60 +	}
    1.61 +	atexit(cleanup);
    1.62 +
    1.63 +	glutMainLoop();
    1.64 +	return 0;
    1.65 +}
    1.66 +
    1.67 +static bool init()
    1.68 +{
    1.69 +	glewInit();
    1.70 +
    1.71 +	glClearColor(0.1, 0.1, 0.1, 1);
    1.72 +
    1.73 +	glEnable(GL_DEPTH_TEST);
    1.74 +	glEnable(GL_CULL_FACE);
    1.75 +	glEnable(GL_LIGHTING);
    1.76 +	glEnable(GL_LIGHT0);
    1.77 +
    1.78 +	rtarg = new RenderTarget;
    1.79 +	tmp_rtarg = new RenderTarget;
    1.80 +
    1.81 +	if(!(sdrblur = create_program_load("sdr/vert.glsl", "sdr/blur.glsl"))) {
    1.82 +		return false;
    1.83 +	}
    1.84 +	if(!(sdr_hblur = create_program_load("sdr/vert.glsl", "sdr/hblur.glsl"))) {
    1.85 +		return false;
    1.86 +	}
    1.87 +	if(!(sdr_vblur = create_program_load("sdr/vert.glsl", "sdr/vblur.glsl"))) {
    1.88 +		return false;
    1.89 +	}
    1.90 +
    1.91 +	return true;
    1.92 +}
    1.93 +
    1.94 +static void cleanup()
    1.95 +{
    1.96 +	delete rtarg;
    1.97 +	delete tmp_rtarg;
    1.98 +}
    1.99 +
   1.100 +static void disp()
   1.101 +{
   1.102 +	glMatrixMode(GL_MODELVIEW);
   1.103 +	glLoadIdentity();
   1.104 +	glTranslatef(0, 0, -cam_dist);
   1.105 +	glRotatef(cam_phi, 1, 0, 0);
   1.106 +	glRotatef(cam_theta, 0, 1, 0);
   1.107 +
   1.108 +	bind_render_target(rtarg);
   1.109 +
   1.110 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.111 +
   1.112 +	glFrontFace(GL_CW);
   1.113 +	glutSolidTeapot(1.0);
   1.114 +	glFrontFace(GL_CCW);
   1.115 +
   1.116 +	if(blur_type == BLUR_SEP) {
   1.117 +		// render with horizontal blur into tmp_rtarg
   1.118 +		bind_render_target(tmp_rtarg);
   1.119 +
   1.120 +		set_uniform_float2(sdr_hblur, "size", rtarg->xsz, rtarg->ysz);
   1.121 +		set_uniform_float2(sdr_hblur, "texsize", rtarg->tex_xsz, rtarg->tex_ysz);
   1.122 +		bind_program(sdr_hblur);
   1.123 +
   1.124 +		rtarg->display();
   1.125 +
   1.126 +		// render with vertical blur into the regular framebuffer
   1.127 +		bind_render_target(0);
   1.128 +
   1.129 +		set_uniform_float2(sdr_vblur, "size", tmp_rtarg->xsz, tmp_rtarg->ysz);
   1.130 +		set_uniform_float2(sdr_vblur, "texsize", tmp_rtarg->tex_xsz, tmp_rtarg->tex_ysz);
   1.131 +		bind_program(sdr_vblur);
   1.132 +
   1.133 +		tmp_rtarg->display();
   1.134 +
   1.135 +	} else {
   1.136 +		// render with the full blur into the regular framebuffer
   1.137 +		bind_render_target(0);
   1.138 +
   1.139 +		// display the rendered image
   1.140 +		set_uniform_float2(sdrblur, "size", rtarg->xsz, rtarg->ysz);
   1.141 +		set_uniform_float2(sdrblur, "texsize", rtarg->tex_xsz, rtarg->tex_ysz);
   1.142 +		bind_program(sdrblur);
   1.143 +
   1.144 +		rtarg->display();
   1.145 +	}
   1.146 +	bind_program(0);
   1.147 +
   1.148 +	print_string(blur_type == BLUR_REG ? "Regular blur" : "Seperable blur");
   1.149 +
   1.150 +	glutSwapBuffers();
   1.151 +	assert(glGetError() == GL_NO_ERROR);
   1.152 +}
   1.153 +
   1.154 +static void print_string(const char *str)
   1.155 +{
   1.156 +	glPushAttrib(GL_ENABLE_BIT);
   1.157 +
   1.158 +	glMatrixMode(GL_MODELVIEW);
   1.159 +	glPushMatrix();
   1.160 +	glLoadIdentity();
   1.161 +	glMatrixMode(GL_PROJECTION);
   1.162 +	glPushMatrix();
   1.163 +	glLoadIdentity();
   1.164 +	glOrtho(0, win_width, 0, win_height, -1, 1);
   1.165 +
   1.166 +	glDisable(GL_LIGHTING);
   1.167 +	glDisable(GL_DEPTH_TEST);
   1.168 +
   1.169 +	glRasterPos2f(1, 1);
   1.170 +	glColor3f(1, 1, 0);
   1.171 +	while(*str) {
   1.172 +		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str++);
   1.173 +	}
   1.174 +
   1.175 +	glPopMatrix();
   1.176 +	glMatrixMode(GL_MODELVIEW);
   1.177 +	glPopMatrix();
   1.178 +
   1.179 +	glPopAttrib();
   1.180 +}
   1.181 +
   1.182 +static void idle()
   1.183 +{
   1.184 +	glutPostRedisplay();
   1.185 +}
   1.186 +
   1.187 +static void reshape(int x, int y)
   1.188 +{
   1.189 +	win_width = x;
   1.190 +	win_height = y;
   1.191 +
   1.192 +	glViewport(0, 0, x, y);
   1.193 +
   1.194 +	glMatrixMode(GL_PROJECTION);
   1.195 +	glLoadIdentity();
   1.196 +	gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
   1.197 +
   1.198 +	rtarg->reshape(x, y);
   1.199 +	tmp_rtarg->reshape(x, y);
   1.200 +}
   1.201 +
   1.202 +static void keyb(unsigned char key, int x, int y)
   1.203 +{
   1.204 +	switch(key) {
   1.205 +	case 27:
   1.206 +		exit(0);
   1.207 +
   1.208 +	case ' ':
   1.209 +		blur_type = (blur_type + 1) % NUM_BLUR_TYPES;
   1.210 +		break;
   1.211 +	}
   1.212 +}
   1.213 +
   1.214 +
   1.215 +static bool bnstate[32];
   1.216 +static int prev_x, prev_y;
   1.217 +
   1.218 +static void mouse(int bn, int st, int x, int y)
   1.219 +{
   1.220 +	prev_x = x;
   1.221 +	prev_y = y;
   1.222 +	bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
   1.223 +}
   1.224 +
   1.225 +static void motion(int x, int y)
   1.226 +{
   1.227 +	int dx = x - prev_x;
   1.228 +	int dy = y - prev_y;
   1.229 +	prev_x = x;
   1.230 +	prev_y = y;
   1.231 +
   1.232 +	if(bnstate[0]) {
   1.233 +		cam_theta += dx * 0.5;
   1.234 +		cam_phi += dy * 0.5;
   1.235 +
   1.236 +		if(cam_phi < -90) cam_phi = -90;
   1.237 +		if(cam_phi > 90) cam_phi = 90;
   1.238 +	}
   1.239 +	if(bnstate[2]) {
   1.240 +		cam_dist -= dy * 0.1;
   1.241 +
   1.242 +		if(cam_dist < 0) cam_dist = 0;
   1.243 +	}
   1.244 +}