-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
287 lines (245 loc) · 6.43 KB
/
main.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <chrono>
#include "GlHelper/DrawHelper.h"
#include "BVHViewer.h"
#define PI 3.14159265
vector<BVHViewer*> bvhs;
int frame = 0;
int frame_size = 0;
double accel = 10.0;
unsigned timeStep = 30;
bool play = false;
GLdouble rotMatrix[16] =
{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
/* set global variables */
int width, height;
bool leftButton = false;
GLfloat mousePosX, mousePosY;
/* vectors that makes the rotation and translation of the cube */
Eigen::Vector3f eye(-300.0f, 200.0f, -300.0f);
Eigen::Vector3f ori(1.0f, 0.0f, 1.0f);
Eigen::Vector2f oriAngle(3.0 * PI / 4.0, PI / 2.0);
Eigen::Vector3f rightVec(0.0f, 0.0f, 1.0f);
Eigen::Vector3f up(0.0f, 1.0f, 0.0f);
void DrawStringOnScreen(float _x, float _y, const std::string& _s,bool _bigFont,const Eigen::Vector3d& color)
{
glColor3f(color[0],color[1],color[2]);
// draws text on the screen
GLint oldMode;
glGetIntegerv(GL_MATRIX_MODE, &oldMode);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRasterPos2f(_x, _y);
unsigned int length = _s.length();
for (unsigned int c = 0; c < length; c++) {
if (_bigFont)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, _s.at(c) );
else
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, _s.at(c) );
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(oldMode);
}
void loadGlobalCoord()
{
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], eye[0]+ori[0], eye[1]+ori[1], eye[2]+ori[2], up[0], up[1], up[2]);
glMultMatrixd(rotMatrix);
}
//------------------------------------------------------------------------
// Moves the screen based on mouse pressed button
//------------------------------------------------------------------------
void glutMotion(int x, int y)
{
if (leftButton) {
float dx = x - mousePosX;
float dy = y - mousePosY;
mousePosX = x;
mousePosY = y;
oriAngle[0] += dx / width;
oriAngle[1] -= dy / width;
ori[0] = sin(oriAngle[0])*sin(oriAngle[1]);
ori[1] = -cos(oriAngle[1]);
ori[2] = -cos(oriAngle[0])*sin(oriAngle[1]);
ori.normalize();
rightVec = ori.cross(up).normalized();
loadGlobalCoord();
}
return;
}
//------------------------------------------------------------------------
// Function that handles mouse input
//------------------------------------------------------------------------
void glutMouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
mousePosX = x;
mousePosY = y;
leftButton = true;
}
else if (state == GLUT_UP)
{
leftButton = false;
}
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN){
play = !play;
}
break;
case 3:break;
default:break;
}
return;
}
void DrawPlane() {
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 1.0, 0.5);
glVertex3f(-1000, 0, -1000);
glVertex3f(1000, 0, -1000);
glVertex3f(1000, 0, 1000);
glVertex3f(-1000, 0, 1000);
glEnd();
}
void DrawGridPlane() {
glBegin(GL_LINES);
glColor3f(0.5, 0.5, 0.5);
for (int i = -1000; i < 1001; i+=100){
glVertex3f(i, 0, -1000);
glVertex3f(i, 0, 1000);
glVertex3f(1000, 0, i);
glVertex3f(-1000, 0, i);
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
loadGlobalCoord();
//glRotatef(45, -1, 0, 0);
glPushMatrix();
//DrawPlane();
DrawGridPlane();
for (auto bvh: bvhs)
bvh->draw();
glPopMatrix();
DrawStringOnScreen(0.8, 0.9, std::to_string(frame) + "/" + std::to_string(frame_size), true, Eigen::Vector3d::Zero());
glutSwapBuffers();
}
void resize(int w, int h) {
width = w;
height = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, .1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'w':
eye += ori * accel;
break;
case 's':
eye -= ori * accel;
break;
case 'a':
eye -= rightVec * accel;
break;
case 'd':
eye += rightVec * accel;
break;
case 'q':
if(!play){
frame -= 1;
if(frame < 0) frame = frame_size - 1;
for (auto bvh: bvhs)
bvh->loadFrame(frame);
}
break;
case 'e':
if(!play){
frame += 1;
if(frame >= frame_size) frame = 0;
for (auto bvh: bvhs)
bvh->loadFrame(frame);
}
break;
case 27:
for (auto bvh: bvhs)
free(bvh);
exit(0);
break;
default:
break;
}
}
void Timer(int unused)
{
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
if (play){
frame += 1;
if(frame >= frame_size) frame = 0;
for (auto bvh: bvhs)
bvh->loadFrame(frame);
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() / 1000.;
glutTimerFunc(timeStep - elapsed, Timer, 0);
/* call the display callback and forces the current window to be displayed */
glutPostRedisplay();
}
int main(int argc, char** argv) {
if (argc < 2){
printf("Usage: %s Filename", argv[0]);
return 0;
}
for (int i = 1; i < argc; i++){
BVHReader reader = BVHReader(argv[i]);
if (!reader.loadFile()){
std::cout << "Failed to load .bvh file" << std::endl;
return 1;
}
timeStep = reader.getFrameTime() * 1000.0;
std::cout << "Loaded " << argv[i] << " successfully" << std::endl;
auto bvh = new BVHViewer(move(reader.getRoots()), reader.getMotion(), reader.getChannels());
bvhs.push_back(bvh);
if (bvh->frameSize() > frame_size) frame_size = bvh->frameSize();
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(50, 0);
glutCreateWindow("BVH Viewer");
glClearColor(135.0/255.0, 206.0/255.0, 235.0/255.0, 1.0f);
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutTimerFunc(timeStep, Timer, 0);
glutKeyboardFunc(keyboard);
glutMouseFunc(glutMouse);
glutMotionFunc(glutMotion);
glutMainLoop();
return 0;
}