-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
722 lines (592 loc) · 17.1 KB
/
driver.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// A driver to perform inverse kinametics with skinning.
// CSCI 520 Computer Animation and Simulation
// Jernej Barbic and Yijing Li
#include "sceneObjectDeformable.h"
#include "lighting.h"
#include "cameraLighting.h"
#include "openGL-headers.h"
#include "camera.h"
#include "objMesh.h"
#include "performanceCounter.h"
#include "averagingBuffer.h"
#include "inputDevice.h"
#include "openGLHelper.h"
#include "valueIndex.h"
#include "configFile.h"
#include "skinning.h"
#include "FK.h"
#include "IK.h"
#include "handleControl.h"
#include "skeletonRenderer.h"
#include "pic.h"
#ifdef WIN32
#include <windows.h>
#endif
#include <vector>
#include <set>
#include <stdlib.h>
#include <stdio.h>
#include <climits>
#include <iostream>
#include <math.h>
#include <time.h>
#include <cmath>
#include <adolc/adolc.h>
using namespace std;
static string meshFilename;
static string configFilename;
static string screenshotBaseName;
static string jointHierarchyFilename;
static string jointWeightsFilename;
static string jointRestTransformsFilename;
static bool fullScreen = 0;
static bool showAxes = false;
static bool showWireframe = true;
static bool showObject = true;
static bool useLighting = true;
static double allLightsIntensity = 1.0;
static Vec3d modelCenter(0.0);
static double modelRadius = 1.0;
static ObjMesh* mesh = nullptr;
static SceneObjectDeformable* meshDeformable = nullptr;
static FK* fk = nullptr;
static IK* ik = nullptr;
static Skinning* skinning = nullptr;
static SkeletonRenderer* skeletonRenderer = nullptr;
static bool renderSkeleton = true;
static int curJointID = -1;
static SphericalCamera* camera = nullptr;
static int windowWidth = 800, windowHeight = 600;
static double zNear = 0.001, zFar = 1000;
static int selectedVertex = -1;
static int windowID = 0;
static int graphicsFrameID = 0;
static Lighting* lighting = nullptr;
static CameraLighting* cameraLighting = nullptr;
static InputDevice id;
static bool reverseHandle = false;
static HandleControl handleControl;
static PerformanceCounter counter, titleBarCounter;
static int titleBarFrameCounter = 0;
static AveragingBuffer fpsBuffer(5);
static vector<int> IKJointIDs;
static vector<Vec3d> IKJointPos;
// ====================== Functions to save ppm files, copied from hw1 ===========
int saveScreenToFile = 0;
int sprite = 0;
/* Write a screenshot, in the PPM format, to the specified filename, in PPM format */
void saveScreenshot(int wWidth, int wHeight, char* filename)
{
if (filename == NULL)
return;
// Allocate a picture buffer
Pic* in = pic_alloc(wWidth, wHeight, 3, NULL);
printf("File to save to: %s\n", filename);
for (int i = wHeight - 1; i >= 0; i--)
{
glReadPixels(0, wHeight - i - 1, wWidth, 1, GL_RGB, GL_UNSIGNED_BYTE,
&in->pix[i * in->nx * in->bpp]);
}
if (ppm_write(filename, in))
printf("File saved Successfully\n");
else
printf("Error in Saving\n");
pic_free(in);
}
void captureScreenShots()
{
char s[50] = "picxxxx.ppm";
// save screen to file
/*s[3] = 48 + (sprite / 1000);
s[4] = 48 + (sprite % 1000) / 100;
s[5] = 48 + (sprite % 100) / 10;
s[6] = 48 + sprite % 10;*/
s[3] = 48 + (sprite / 1000);
s[4] = 48 + (sprite % 1000) / 100;
s[5] = 48 + (sprite % 100) / 10;
s[6] = 48 + sprite % 10;
if (saveScreenToFile == 1)
{
saveScreenshot(windowWidth, windowHeight, s);
//saveScreenToFile = 0; // save only once, change this if you want continous image generation (i.e. animation)
sprite++;
if (sprite >= 300) // allow only 300 snapshots
{
printf("Already captured 300 frames. Stopped.\n");
saveScreenToFile = 0;
sprite = 0;
}
}
}
//======================= Functions =============================
static void updateSkinnedMesh()
{
vector<Vec3d> newPos(meshDeformable->GetNumVertices());
double* newPosv = (double*)newPos.data();
fk->computeJointTransforms();
skinning->applySkinning(fk->getJointSkinTransforms(), fk->getNumJoints(), newPosv);
for (size_t i = 0; i < mesh->getNumVertices(); i++)
mesh->setPosition(i, newPos[i]);
meshDeformable->BuildNormals();
}
static void resetSkinningToRest()
{
fk->resetToRestPose();
updateSkinnedMesh();
for (size_t i = 0; i < IKJointIDs.size(); i++)
{
IKJointPos[i] = fk->getJointGlobalPosition(IKJointIDs[i]);
}
handleControl.clearHandleSelection();
curJointID = -1;
cout << "reset mesh to rest" << endl;
}
static void idleFunction()
{
glutSetWindow(windowID);
counter.StopCounter();
// double dt = counter.GetElapsedTime();
counter.StartCounter();
captureScreenShots();
// Take appropriate action in case the user is dragging a vertex.
auto processDrag = [&](int vertex, Vec3d posDiff)
{
if (len2(posDiff) > 0 && handleControl.isHandleSelected())
{
IKJointPos[handleControl.getSelectedHandle()] += posDiff;
}
};
handleControl.processHandleMovement(id.getMousePosX(), id.getMousePosY(), id.shiftPressed(), processDrag);
const int maxIKIters = 10;
const double maxOneStepDistance = modelRadius / 1000;
ik->doIK(IKJointPos.data(), fk->getJointEulerAngles(), maxIKIters, maxOneStepDistance);
updateSkinnedMesh();
titleBarFrameCounter++;
// update title bar at 4 Hz
titleBarCounter.StopCounter();
double elapsedTime = titleBarCounter.GetElapsedTime();
if (elapsedTime >= 1.0 / 4)
{
titleBarCounter.StartCounter();
double fps = titleBarFrameCounter / elapsedTime;
fpsBuffer.addValue(fps);
// update menu bar
char windowTitle[4096];
sprintf(windowTitle, "Vertices: %d | %.1f FPS | graphicsFrame %d ", meshDeformable->Getn(), fpsBuffer.getAverage(), graphicsFrameID);
glutSetWindowTitle(windowTitle);
titleBarFrameCounter = 0;
}
graphicsFrameID++;
glutPostRedisplay();
}
static void reshape(int x, int y)
{
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
windowWidth = x;
windowHeight = y;
// Calculate the aspect ratio of the window
gluPerspective(45.0f, 1.0 * windowWidth / windowHeight, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
camera->Look(); // calls gluLookAt
glDisable(GL_LIGHTING);
glLineWidth(1.0);
if (showAxes)
RenderAxes(1);
if (useLighting)
{
glEnable(GL_LIGHTING);
if (cameraLighting)
cameraLighting->LightScene(camera);
else if (lighting)
lighting->LightScene();
}
else
glDisable(GL_LIGHTING);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); //only when stencil pass and z-buffer pass, set stencil value to stencil reference
glStencilFunc(GL_ALWAYS, 1, ~(0u)); //always pass stencil test, stencil renference value is 1
if (true)
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
// glEnable(GL_POLYGON_OFFSET_FILL);
// glPolygonOffset(1.0, 1.0);
// glDrawBuffer(GL_NONE);
/***********************************
* render transparent object
***********************************/
// glDisable(GL_POLYGON_OFFSET_FILL);
// glDrawBuffer(GL_BACK);
glDisable(GL_BLEND);
}
glColor3f(0.9, 0.9, 0.9);
if (showObject)
{
meshDeformable->Render();
}
glColor3f(0, 0, 0);
if (showWireframe)
meshDeformable->RenderEdges();
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glPointSize(10.0);
// ---------------------------------------------------------
// now rendering non-material/non-texture stuff here
if (selectedVertex >= 0)
{
glColor3f(1, 0, 0);
glBegin(GL_POINTS);
Draw(mesh->getPosition(selectedVertex));
glEnd();
}
glDisable(GL_DEPTH_TEST);
if (renderSkeleton)
{
skeletonRenderer->renderSkeleton();
}
if (curJointID >= 0 && curJointID < fk->getNumJoints())
{
skeletonRenderer->renderJoint(curJointID);
}
for (int jointID : IKJointIDs)
{
skeletonRenderer->renderJointCoordAxes(jointID);
}
glEnable(GL_DEPTH_TEST);
glStencilFunc(GL_ALWAYS, 0, ~(0u)); // always pass stencil test, stencil renference value is set to 0
// render the vertex currently being manipulated via IK
if (handleControl.isHandleSelected())
{
int handleID = handleControl.getSelectedHandle();
Vec3d handlePos = fk->getJointGlobalPosition(IKJointIDs[handleID]);
glColor3f(1, 0, 0);
glPointSize(8.0);
Draw(handlePos);
// render the moving handle at location IKJointPos[handleID]
handleControl.renderHandle(camera, IKJointPos[handleID], reverseHandle);
}
glutSwapBuffers();
}
static void keyboardFunc(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
case '0':
resetSkinningToRest();
break;
case 9:
fullScreen = 1 - fullScreen;
if (fullScreen == 1)
glutFullScreen();
else {
glutReshapeWindow(800, 600);
glutPositionWindow(5, 150);
}
break;
case '\\':
camera->Reset();
break;
case '=':
curJointID++;
if (curJointID >= fk->getNumJoints())
curJointID = -1;
break;
case 'a':
showAxes = !showAxes;
break;
case 'w':
showWireframe = !showWireframe;
break;
case 'e':
showObject = !showObject;
break;
case 's':
renderSkeleton = !renderSkeleton;
break;
// added 3 keys so that we can switch skinning/IK/substep mode at runtime
case 'z':
skinning->switchSkinningMode();
break;
case 'x':
ik->switchIKMethod();
break;
case 'c':
ik->switchSubStepIKOnOff();
break;
case ' ':
saveScreenToFile = 1 - saveScreenToFile;
if (saveScreenToFile == 0) { sprite = 0; }
break;
default:
break;
}
}
static void specialKeysFunc(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
camera->MoveFocusRight(0.1 * fabs(camera->GetRadius()));
break;
case GLUT_KEY_RIGHT:
camera->MoveFocusRight(-0.1 * fabs(camera->GetRadius()));
break;
case GLUT_KEY_DOWN:
camera->MoveFocusUp(0.1 * fabs(camera->GetRadius()));
break;
case GLUT_KEY_UP:
camera->MoveFocusUp(-0.1 * fabs(camera->GetRadius()));
break;
}
}
static void mouseNoDrag(int x, int y)
{
id.setMousePos(x, y);
if (handleControl.isHandleSelected())
{
Vec3d worldPos(0.0);
GLubyte stencilValue;
float zValue;
unprojectPointFromScreen(x, y, &worldPos[0], &stencilValue, &zValue);
if (stencilValue == 1)
{
handleControl.setMousePosition(worldPos);
}
}
}
static void mouseDrag(int x, int y)
{
int mouseDeltaX = x - id.getMousePosX();
int mouseDeltaY = y - id.getMousePosY();
id.setMousePos(x, y);
// we moved the camera...
if (id.rightMouseButtonDown())
{
// right mouse button handles camera rotations
double scale = 0.2;
if (id.shiftPressed()) scale *= 0.1;
camera->MoveRight(scale * mouseDeltaX);
camera->MoveUp(scale * mouseDeltaY);
}
if (id.middleMouseButtonDown() || (id.altPressed() && id.leftMouseButtonDown()))
{
// middle mouse button (or ALT + left mouse button) handles camera translations
double scale = 0.2 * modelRadius;
if (id.shiftPressed()) scale *= 0.1;
camera->ZoomIn(scale * mouseDeltaY);
}
}
static void mouseButtonActivity(int button, int state, int x, int y)
{
id.setButton(button, state);
switch (button)
{
case GLUT_LEFT_BUTTON:
{
Vec3d clickedPosition(0.0);
GLubyte stencilValue;
float zValue = 0.0f;
unprojectPointFromScreen(x, y, &clickedPosition[0], &stencilValue, &zValue);
if (id.leftMouseButtonDown())
{
if (stencilValue == 0)
{
cout << "Clicked on empty space." << endl;
selectedVertex = -1;
return;
}
MinValueIndex vi;
for (size_t i = 0; i < mesh->getNumVertices(); i++)
{
vi.update(len2(clickedPosition - mesh->getPosition(i)), i);
}
selectedVertex = vi.index;
cout << "Clicked on vertex " << vi.index << endl;
if (fk->getNumJoints() > 0)
{
MinValueIndex vi;
for (int i = 0; i < fk->getNumJoints(); i++)
{
vi.update(len2(fk->getJointGlobalPosition(i) - clickedPosition), i);
}
assert(vi.index >= 0);
if (vi.index != curJointID)
{
curJointID = vi.index;
cout << "select joint ID " << curJointID << ", #joints " << fk->getNumJoints() << endl;
}
}
}
auto getClosestHandle = [&]() -> int
{
MinValueIndex vi;
for (size_t handleID = 0; handleID < IKJointIDs.size(); handleID++)
{
vi.update(len2(clickedPosition - fk->getJointGlobalPosition(IKJointIDs[handleID])), handleID);
}
return vi.index;
};
auto addOrRemoveHandle = [&]()
{
return make_pair(-1, false);
};
handleControl.setMouseButtonActivity(id.leftMouseButtonDown(), stencilValue == 1, false,
clickedPosition, zValue, getClosestHandle, addOrRemoveHandle);
break;
}
case GLUT_MIDDLE_BUTTON:
break;
case GLUT_RIGHT_BUTTON:
break;
}
}
static void initialize()
{
// initialize random number generator
srand(time(nullptr));
// detect the OpenGL version being used
printf("GL_VENDOR: %s\n", glGetString(GL_VENDOR));
printf("GL_RENDERER: %s\n", glGetString(GL_RENDERER));
printf("GL_VERSION: %s\n", glGetString(GL_VERSION));
mesh = new ObjMesh(meshFilename);
meshDeformable = new SceneObjectDeformable(mesh, false);
if (meshDeformable->HasTextures())
{
meshDeformable->EnableTextures();
meshDeformable->SetUpTextures(SceneObject::MODULATE, SceneObject::NOMIPMAP);
}
meshDeformable->BuildNeighboringStructure();
meshDeformable->BuildNormals();
// meshDeformable->BuildDisplayList();
// ---------------------------------------------------
// joint initialization
// ---------------------------------------------------
if (IKJointIDs.size() == 0)
{
cout << "No IK handles specified in the config file" << endl;
exit(0);
}
assert(jointRestTransformsFilename.size() > 0 && jointWeightsFilename.size() > 0);
skinning = new Skinning(meshDeformable->Getn(), meshDeformable->GetVertexRestPositions(), jointWeightsFilename);
fk = new FK(jointHierarchyFilename, jointRestTransformsFilename);
// ---------------------------------------------------
// Setting up Adol-c
// ---------------------------------------------------
ik = new IK(IKJointIDs.size(), IKJointIDs.data(), fk);
IKJointPos.resize(IKJointIDs.size());
for (size_t i = 0; i < IKJointIDs.size(); i++)
{
IKJointPos[i] = fk->getJointGlobalPosition(IKJointIDs[i]);
}
// ---------------------------------------------------
// rendering setup
// ---------------------------------------------------
double cameraUp[3] = { 0,1,0 };
Vec3d cameraFocus;
Vec3d bmin, bmax;
mesh->computeBoundingBox();
mesh->getCubicBoundingBox(1.0, &bmin, &bmax);
modelCenter = (bmin + bmax) / 2.0;
modelRadius = mesh->getDiameter() / 2;
// compute the size of the shape for getting a proper renderedLocalAxisLength
double localAxisLength = modelRadius / 5.0;
skeletonRenderer = new SkeletonRenderer(fk, localAxisLength);
cout << "Finished joint initialization" << endl;
double cameraRadius = 0;
cameraFocus = modelCenter;
cameraRadius = modelRadius * 2.5;
zNear = cameraRadius * 0.01;
zFar = cameraRadius * 100.0;
double cameraPhi = 270.0;
double cameraTheta = 0;
camera = new SphericalCamera(cameraRadius,
1.0 * cameraPhi / 360 * (2.0 * PI),
1.0 * cameraTheta / 360 * (2.0 * PI),
&cameraFocus[0], cameraUp, 0.05);
// lighting = new Lighting;
// lighting->SetLightBox(&bmin[0], &bmax[0]);
// lighting->SetAllLightsEnabled(false);
// lighting->SetLightEnabled(2, true);
// lighting->SetLightEnabled(3, true);
// lighting->SetLightEnabled(6, true);
// lighting->SetLightEnabled(7, true);
// lighting->SetAllLightsIntensity(allLightsIntensity);
cameraLighting = new CameraLighting;
cameraLighting->SetLightIntensity(allLightsIntensity);
// clear to white
glClearColor(256.0 / 256, 256.0 / 256, 256.0 / 256, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_LINE_SMOOTH);
printf("Initialization complete.\n");
return;
}
#define ADD_CONFIG(v) configFile.addOptionOptional(#v, &v, v)
static void initConfigurations()
{
ConfigFile configFile;
ADD_CONFIG(allLightsIntensity);
ADD_CONFIG(screenshotBaseName);
ADD_CONFIG(meshFilename);
// Maya data needs jointHierarchyFilename, jointRestTransformsFilename and jointWeightsFilename
ADD_CONFIG(jointHierarchyFilename);
ADD_CONFIG(jointRestTransformsFilename);
ADD_CONFIG(jointWeightsFilename);
ADD_CONFIG(IKJointIDs);
// parse the configuration file
if (configFile.parseOptions(configFilename.c_str()) != 0)
{
printf("Error parsing options.\n");
exit(1);
}
// The config variables have now been loaded with their specified values.
// Informatively print the variables (with assigned values) that were just parsed.
configFile.printOptions();
}
int main(int argc, char** argv)
{
int numFixedArgs = 2;
if (argc < numFixedArgs)
{
cout << "Renders an obj mesh on the screen." << endl;
cout << "Usage: " << argv[0] << " configFilename" << endl;
return 0;
}
configFilename = argv[1];
initConfigurations();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE);
windowWidth = 800;
windowHeight = 800;
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(0, 0);
windowID = glutCreateWindow("IK viewer");
if (fullScreen == 1)
glutFullScreen();
initialize();
// callbacks
glutDisplayFunc(displayFunction);
glutMotionFunc(mouseDrag);
glutPassiveMotionFunc(mouseNoDrag);
glutIdleFunc(idleFunction);
glutKeyboardFunc(keyboardFunc);
glutSpecialFunc(specialKeysFunc);
glutReshapeFunc(reshape);
glutMouseFunc(mouseButtonActivity);
reshape(windowWidth, windowHeight);
glutMainLoop();
return(0);
}