/
MotionBlur.cpp
105 lines (81 loc) · 2.3 KB
/
MotionBlur.cpp
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
/*
* MotionBlur.cpp
* CS248-Final-Project
*
* Created by Jeremy Keeshin on 3/8/11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#include "MotionBlur.h"
#include <cstdio>
#include <vector>
using namespace std;
const int SEPARATION = 2;
MotionBlur::MotionBlur(int numFrames, int width, int height){
buffers = new Framebuffer*[numFrames];
this->numFrames = numFrames;
for(int i = 0; i < numFrames; i++){
buffers[i] = new Framebuffer(width, height);
}
counter = 0;
cur = 0;
curIndex = 0;
this->width = width;
this->height = height;
}
MotionBlur::~MotionBlur(){
for(int i = 0; i < numFrames; i++){
delete buffers[i];
}
delete buffers;
}
bool MotionBlur::shouldRenderFrame(){
return counter % SEPARATION == 0;
}
Framebuffer* MotionBlur::getFrame(int frame){
return buffers[frame];
}
void MotionBlur::update(){
counter++;
}
void MotionBlur::bind(){
getFrame(cur)->bind();
}
void MotionBlur::unbind(){
getFrame(cur)->unbind();
cur++;
cur = cur % numFrames;
}
// This method renders a full screen quad. It takes the four last textures which are
// averaged to compute the current frame.
void MotionBlur::render(Shader* shader){
glUseProgram(shader->programID());
GLint frame1 = glGetUniformLocation(shader->programID(), "frame1");
glUniform1i(frame1, 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, getFrame(0)->colorTextureId());
GLint frame2 = glGetUniformLocation(shader->programID(), "frame2");
glUniform1i(frame2, 2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, getFrame(1)->colorTextureId());
GLint frame3 = glGetUniformLocation(shader->programID(), "frame3");
glUniform1i(frame3, 3);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, getFrame(2)->colorTextureId());
GLint frame4 = glGetUniformLocation(shader->programID(), "frame4");
glUniform1i(frame4, 4);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, getFrame(3)->colorTextureId());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLint pos = glGetAttribLocation(shader->programID(), "positionIn");
glBegin(GL_QUADS);
glVertexAttrib2f(pos, -1.0, -1.0);
glVertexAttrib2f(pos, 1.0, -1.0);
glVertexAttrib2f(pos, 1.0, 1.0);
glVertexAttrib2f(pos, -1.0, 1.0);
glEnd();
}