-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblur.c
137 lines (119 loc) · 3.96 KB
/
blur.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
#include <math.h>
int maxFPS = 61.0f; // refresh interval in milliseconds
int blur = 0; // 0: no motion blur, 1: eye tracking, 2: shutter simulation
int speedFactor = 2; // speed: 1x to 8x
int refresh = 0;
char title[] = "A-buffer motion blur. Type = 0, Speed = 2x";
int iterations = 10; // Motion blur quality
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner y
GLfloat xPos = 0.0f; // Object position
GLfloat yPos = 0.0f;
GLfloat xVel = 0.0025f; // Objects speeds
GLfloat yVel = 0.02f;
GLfloat angle = 0.0f; // rotational angle per frame
GLfloat dx = 0.0f; // rotational angle of the shapes
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque Background
}
/* Called back when the timer expired */
void Timer(int value) {
glutPostRedisplay();
if (!blur) glutTimerFunc(1/(maxFPS), Timer, 0); // subsequent timer call at milliseconds
else glutTimerFunc(1/(maxFPS*iterations), Timer, 0); // subsequent timer call at milliseconds
}
void display() {
if (blur == 0){ //Calculating step sizes
xPos+=xVel*speedFactor;
yPos+=yVel*speedFactor;
angle -= dx*speedFactor;
}
else if (blur == 1){
if (refresh == 0){
xPos+=xVel*speedFactor;
yPos+=yVel*speedFactor;
}
angle -= dx/iterations*speedFactor;
}
else {
xPos+=xVel/iterations*speedFactor;
yPos+=yVel/iterations*speedFactor;
angle -= dx/iterations*speedFactor;
}
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix(); // Creating figure
glTranslatef(xPos, yPos, 0.0f); // Translate
glRotatef(angle, 0.0f, 0.0f, 1.0f); // rotate
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-0.3f, -0.2f);
glVertex2f( 0.3f, -0.2f);
glVertex2f( 0.0f, 0.3f);
glEnd();
glPopMatrix();
glPushMatrix(); // Inner triangle
glTranslatef(xPos, yPos, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-0.3f*0.8, -0.2f*0.8);
glVertex2f( 0.3f*0.8, -0.2f*0.8);
glVertex2f( 0.0f*0.8, 0.3f*0.8);
glEnd();
glPopMatrix();
if (blur) { //Motion blur algorithm
refresh++;
if(refresh == 0) glAccum(GL_LOAD, 1.0 / iterations);
else glAccum(GL_ACCUM, 1.0 / iterations);
if(refresh >= iterations ) {
refresh = 0;
glAccum(GL_RETURN, 1.0);
glutSwapBuffers();
glClear(GL_ACCUM_BUFFER_BIT);
}
}
else glutSwapBuffers();
if (yPos > 0.7) yVel=-fabs(yVel); //Bouncing
else if (yPos < -0.7) { yVel=fabs(yVel); dx = 10*xPos*yPos;}
if (xPos > 0.7) xVel=-fabs(xVel);
else if (xPos < -0.7) xVel=fabs(xVel);
}
void mouse(int button, int state, int x, int y) { //Control
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
blur++;
if (blur == 3) blur = 0;
glClear(GL_ACCUM_BUFFER_BIT);
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
speedFactor*=2;
if (speedFactor == 16) speedFactor =1;
glClear(GL_ACCUM_BUFFER_BIT);
}
title[29]=(char)(((int)'0')+blur);;
title[40]=(char)(((int)'0')+speedFactor);
glutSetWindowTitle(title);
}
void reshape(GLsizei width, GLsizei height) { }
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(windowPosX, windowPosY);
glutCreateWindow(title);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutTimerFunc(0, Timer, 0);
initGL();
glutMainLoop();
return 0;
}