-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimator.cpp
More file actions
217 lines (162 loc) · 5.75 KB
/
Copy pathanimator.cpp
File metadata and controls
217 lines (162 loc) · 5.75 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "animator.hpp"
void Animator::renderFrame() {
if(mode == NONE) {
draw();
}
else if(mode == PLAY) {
play();
}
}
void Animator::draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
// Draw world scene
WORLD.drawScene();
// Swap front and back buffers
glfwSwapBuffers(glfwGetCurrentContext());
// Poll for and process events
glfwPollEvents();
}
void Animator::play() {
ifstream keyfile;
keyfile.open(recordFile.c_str(),ios::in);
frameNo = 0;
Frame prev = read(keyfile);
Frame next,current;
do {
//Walk_Spot_light
next = read(keyfile);
if(!next.valid) break;
for( int i = 0; i <= nFrames; i += 1) {
current = interpolate(next,prev,i);
WORLD.robot.keys.key_frame = current; // update frame
frameNo++;
if(capture) {
capture_frame(frameNo);
}
glfwSetTime(0);
draw();
while(glfwGetTime() <= 0.05);
}
prev = next;
}while( !keyfile.eof() );
keyfile.close();
mode = NONE;
}
void Animator::record() {
Frame frame = WORLD.robot.keys.key_frame;
ofstream keyfile;
keyfile.open(recordFile.c_str(),ios::out | ios::app);
if(keyfile.is_open()) {
keyfile << frame.getCurrentVector() << endl;
keyfile << WORLD.getLightVector() << endl;
keyfile << WORLD.robot.getHeadLightVector() << endl;
keyfile << defaultNFrames <<" "<< endl;
keyfile.close();
} else {
cout<<" Error opening keyframe file" << endl;
}
}
Frame Animator::read(ifstream &keyfile) {
Frame frame;
string vector,light,headlight,frameLen;
if(keyfile.is_open()) {
getline(keyfile,vector);
getline(keyfile,light);
getline(keyfile,headlight);
getline(keyfile,frameLen);
nFrames = atoi(frameLen.c_str());
} else {
cout<<" Error opening keyframe file" << endl;
frame.valid = false;
}
frame.updateFrameFromVector(vector);
if(!light.empty())
WORLD.setLightVector(light);
if(!headlight.empty())
WORLD.robot.setHeadLightVector(headlight);
if(vector.empty())
frame.valid = false;
return frame;
}
void Animator::resetFile() {
ofstream keyfile;
keyfile.open(recordFile.c_str(),ios::out | ios::trunc);
keyfile.close();
}
Frame Animator::interpolate(Frame &next,Frame &prev,int frameNo) {
Frame delta = prev;
double prog = double( frameNo) / double(nFrames);
delta.bust_X += prog * ( next.bust_X - prev.bust_X );
delta.bust_Y += prog * ( next.bust_Y - prev.bust_Y );
delta.bust_Z += prog * ( next.bust_Z - prev.bust_Z );
delta.head_X += prog * ( next.head_X - prev.head_X );
delta.head_Y += prog * ( next.head_Y - prev.head_Y );
delta.head_Z += prog * ( next.head_Z - prev.head_Z );
delta.leftUpperArm_X += prog * ( next.leftUpperArm_X - prev.leftUpperArm_X );
delta.leftUpperArm_Y += prog * ( next.leftUpperArm_Y - prev.leftUpperArm_Y );
delta.leftUpperArm_Z += prog * ( next.leftUpperArm_Z - prev.leftUpperArm_Z );
delta.leftLowerArm_X += prog * ( next.leftLowerArm_X - prev.leftLowerArm_X );
delta.rightUpperArm_X += prog * ( next.rightUpperArm_X - prev.rightUpperArm_X );
delta.rightUpperArm_Y += prog * ( next.rightUpperArm_Y - prev.rightUpperArm_Y );
delta.rightUpperArm_Z += prog * ( next.rightUpperArm_Z - prev.rightUpperArm_Z );
delta.rightLowerArm_X += prog * ( next.rightLowerArm_X - prev.rightLowerArm_X );
delta.leftHand_X += prog * ( next.leftHand_X - prev.leftHand_X );
delta.rightHand_X += prog * ( next.rightHand_X - prev.rightHand_X );
delta.leftUpperLeg_X += prog * ( next.leftUpperLeg_X - prev.leftUpperLeg_X );
delta.leftUpperLeg_Y += prog * ( next.leftUpperLeg_Y - prev.leftUpperLeg_Y );
delta.leftUpperLeg_Z += prog * ( next.leftUpperLeg_Z - prev.leftUpperLeg_Z );
delta.leftLowerLeg_X += prog * ( next.leftLowerLeg_X - prev.leftLowerLeg_X );
delta.rightUpperLeg_X += prog * ( next.rightUpperLeg_X - prev.rightUpperLeg_X );
delta.rightUpperLeg_Y += prog * ( next.rightUpperLeg_Y - prev.rightUpperLeg_Y );
delta.rightUpperLeg_Z += prog * ( next.rightUpperLeg_Z - prev.rightUpperLeg_Z );
delta.rightLowerLeg_X += prog * ( next.rightLowerLeg_X - prev.rightLowerLeg_X );
delta.leftFoot_X += prog * ( next.leftFoot_X - prev.leftFoot_X );
delta.rightFoot_X += prog * ( next.rightFoot_X - prev.rightFoot_X );
delta.hip_TX += prog * ( next.hip_TX -prev.hip_TX );
delta.hip_TY += prog * ( next.hip_TY -prev.hip_TY );
delta.hip_TZ += prog * ( next.hip_TZ -prev.hip_TZ );
delta.hip_X += prog * ( next.hip_X - prev.hip_X );
delta.hip_Y += prog * ( next.hip_Y - prev.hip_Y );
delta.hip_Z += prog * ( next.hip_Z - prev.hip_Z );
//cout<<prog << " : " <<delta.getCurrentVector() <<endl;
return delta;
}
void Animator::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
/* Toggle start capturing frames when playing*/
if(key == GLFW_KEY_F7 && action == GLFW_PRESS) {
capture = !capture;
}
/* Empty keyframe file */
if(key == GLFW_KEY_F8 && action == GLFW_PRESS) {
resetFile();
}
/* Record a frame to file*/
if(key == GLFW_KEY_F9 && action == GLFW_PRESS) {
record();
}
/* Start playback*/
if(key == GLFW_KEY_F10 && action == GLFW_PRESS) {
mode = PLAY;
}
}
void Animator::capture_frame(unsigned int framenum)
{
//global pointer float *pRGB
pRGB = new unsigned char [3 * (screenWidth+1) * (screenHeight + 1) ];
// set the framebuffer to read
//default for double buffered
glReadBuffer(GL_BACK);
glPixelStoref(GL_PACK_ALIGNMENT,1);//for word allignment
glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, pRGB);
char filename[200];
sprintf(filename,"frames/frame_%04d.ppm",framenum);
std::ofstream out(filename, std::ios::out);
out<<"P6"<<std::endl;
out<<screenWidth<<" "<<screenHeight<<" 255"<<std::endl;
out.write(reinterpret_cast<char const *>(pRGB), (3 * (screenWidth+1) * (screenHeight + 1)) * sizeof(int));
out.close();
//function to store pRGB in a file named count
delete pRGB;
}