-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_FacialAnimation_Sample.cpp
More file actions
966 lines (803 loc) · 30.7 KB
/
main_FacialAnimation_Sample.cpp
File metadata and controls
966 lines (803 loc) · 30.7 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
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader_m.h"
#include "camera.h"
#include "animator.h"
#include "model_animation.h"
#include <iostream>
#include "gui.h"
#include<Eigen/Dense>
#define NOMINMAX
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <vector>
#include "mesh_Facial_Animation.h"
#include "shader_Facial_Animation.h"
#include "vbo_Facial_Animation.h"
using namespace glm;
using namespace std;
using namespace Eigen;
// ========================================initialize (start)==========================================================
#pragma region
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow* window);
unsigned int loadCubemap(vector<std::string> faces);
// facial animation functions
void getDeltaM(Model modelNeutral, Model modelBlend);
int pickNearestVertex(int mouseX, int mouseY, mat4 VM, mat4 P);
// window settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 100.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
bool MouseButton2(false);
bool MouseButton1(false);
// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
glm::mat4 trans;
glm::mat4 modelMat;
glm::mat4 viewMat;
glm::mat4 projMat = glm::perspective(glm::radians(45.0f), 1400.0f / 1000.0f, 0.1f, 300.0f);
float startTime = glfwGetTime();
//bool isEuler(true);
//bool isQuater(false);
#pragma endregion
// ========================================initialize (end) ===========================================================
// ========================================facial init (start)==========================================================
#pragma region
vector <vector<vec3>> vecDeltaMs;
vector<float> vecWeights;
std::vector<std::string> meshFileNames{
"Mery_jaw_open.obj",
"Mery_kiss.obj",
"Mery_l_brow_lower.obj",
"Mery_l_brow_narrow.obj",
"Mery_l_brow_raise.obj",
"Mery_l_eye_closed.obj",
"Mery_l_eye_lower_open.obj",
"Mery_l_eye_upper_open.obj",
"Mery_l_nose_wrinkle.obj",
"Mery_l_puff.obj",
"Mery_l_sad.obj",
"Mery_l_smile.obj",
"Mery_l_suck.obj",
"Mery_r_brow_lower.obj",
"Mery_r_brow_narrow.obj",
"Mery_r_brow_raise.obj",
"Mery_r_eye_closed.obj",
"Mery_r_eye_lower_open.obj",
"Mery_r_eye_upper_open.obj",
"Mery_r_nose_wrinkle.obj",
"Mery_r_puff.obj",
"Mery_r_sad.obj",
"Mery_r_smile.obj",
};
bool playAnim = false;
vector<vector<float>> animationWeights;
std::vector<int> constraintsIndex;
Eigen::VectorXf m0;
Eigen::VectorXf m;
Eigen::MatrixXf B;
Eigen::MatrixXf I;
Eigen::VectorXf w_prev;
vec3 NearestVertex = vec3(0, 0, 0);
vector<vec3> vecNeutralFaceVertice;
float lightX;
float lightY;
float lightZ;
float offsetX(0);
float offsetY(0);
float offsetZ(0);
int minIndex;
bool isOffsetApplied = false;
bool isWireframe = false;
bool isPartA = true;
bool isPartB = false;
#pragma endregion
// ========================================facial init (end) ===========================================================
int main()
{
// ========================================init (start)==========================================================
#pragma region
glfwInit(); //glfw: initializeand configure
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "FacialAnimation", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// tell GLFW to capture our mouse
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// tell stb_image.h to flip loaded texture's on the y-axis (before loading model).
stbi_set_flip_vertically_on_load(true);
glEnable(GL_DEPTH_TEST); // configure global opengl state
#pragma endregion
// ========================================init (end) ===========================================================
// ========================================init shaders & models (start)==========================================================
#pragma region
Shader sShader("assets/shaders/phong.vs", "assets/shaders/phong.fs");
Shader shaderBall("assets/shaders/Animation/kinematic/ball.vs", "assets/shaders/Animation/kinematic/ball.fs");
Model mNeutralFace = Model("assets/blendshapes/high_res/neutral.obj");
Model mBall = Model("assets/models/ball/ball.obj");
stbi_set_flip_vertically_on_load(true); // other models flip
#pragma endregion
// ========================================init shaders & models (end) ===========================================================
// ========================================PartB Prepare: Facial Animation Replay (start)==========================================================
#pragma region
vecNeutralFaceVertice = mNeutralFace.vecVertice;
vector<Model> vecBlendFacialExps; // store all blendshape facial expressions
for (const auto& meshFileName : meshFileNames)
{
string tmpPath = "assets/blendshapes/high_res/" + meshFileName;
vecBlendFacialExps.emplace_back(tmpPath); // emplace_back: call Model constructor to create model object
getDeltaM(mNeutralFace, vecBlendFacialExps.back()); // update vecDeltaMs: get all delta between neutral face and blendshapes
}
unsigned int num_rows = static_cast<unsigned int>(mNeutralFace.vecModelVertex.size()) * 3;
unsigned int num_cols = static_cast<unsigned int>(vecBlendFacialExps.size());
// Equation: (BT.B + (a+u).I).W = BT.(m-m0) + a.Wt-1
float a(0.1); // alpha
float u(0.001); // mu
B(num_rows, num_cols); // Matrix B: containing deltas to solve weights
I = MatrixXf::Identity(num_cols, num_cols); // Identity Matrix I
m(num_rows); // Vector New Facial Vertice Matrix
m0(num_rows); // Vector Previous Facial Vertice Matrix
w_prev(num_cols); // The weights from the previous time step
// populate matrix B
for (unsigned int col = 0; col < num_cols; ++col) {
for (unsigned int row = 0; row < mNeutralFace.vecModelVertex.size(); ++row) { // iterate over vertices
B(row * 3 + 0, col) = vecDeltaMs[col][row].x; // X component
B(row * 3 + 1, col) = vecDeltaMs[col][row].y; // Y component
B(row * 3 + 2, col) = vecDeltaMs[col][row].z; // Z component
}
}
// populate m and m0 with the current and original positions of the vertices
for (unsigned int i = 0; i < mNeutralFace.vecModelVertex.size(); ++i) {
m(i * 3 + 0) = mNeutralFace.vecModelVertex[i].Position.x; // Current X position
m(i * 3 + 1) = mNeutralFace.vecModelVertex[i].Position.y; // Current Y position
m(i * 3 + 2) = mNeutralFace.vecModelVertex[i].Position.z; // Current Z position
m0(i * 3 + 0) = vecNeutralFaceVertice[i].x; // Original X position
m0(i * 3 + 1) = vecNeutralFaceVertice[i].y; // Original Y position
m0(i * 3 + 2) = vecNeutralFaceVertice[i].z; // Original Z position
}
// populate weights from the previous time step
for (unsigned int i = 0; i < num_cols; ++i) {
w_prev(i) = vecWeights[i];
}
// set up the equation
MatrixXf A = B.transpose() * B + (a + u) * I;
VectorXf b = B.transpose() * (m - m0) + a * w_prev;
VectorXf w = A.ldlt().solve(b);
for (unsigned int i = 0; i < num_cols; ++i) {
vecWeights[i] = w(i);
}
#pragma endregion
// ========================================PartB Prepare: Facial Animation Replay (end) ===========================================================
// ========================================facial animation (start)==========================================================
#pragma region
#pragma endregion
// ========================================facial animation (end) ===========================================================
// ========================================init render loop (start)==========================================================
#pragma region
glm::mat4 rotateMat;
myGUI myGui(window); // initialize IMG UI
vec3 initialPositions;
//std::vector<glm::vec3> initialPositions;
// render loop
while (!glfwWindowShouldClose(window))
{
if (isWireframe)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // draw in wireframe
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
// per-frame time logic
// --------------------
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#pragma endregion
// ========================================init render loop (end) ===========================================================
// ========================================draw face model (start)==========================================================
#pragma region
glm::mat4 projMat = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 10000.0f);
glm::mat4 viewMat = camera.GetViewMatrix();
glm::mat4 modelMat = glm::mat4(1.0f);
vec3 lightPos(10.0f, 50.0f, 50.0f);
if (isPartA)
{
// pick nearest vertex by clicking right mouse
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_PRESS)
{
offsetX = 0.0f; // reset offset for next selected point
offsetY = 0.0f;
offsetZ = 0.0f;
minIndex = pickNearestVertex(lastX, lastY, viewMat, projMat);
initialPositions = mNeutralFace.vecModelVertex[minIndex].Position;
}
if (offsetX != 0.0 || offsetY != 0.0 || offsetZ != 0.0)
{
mNeutralFace.vecModelVertex[minIndex].Position = initialPositions + vec3(offsetX, offsetY, offsetZ);
}
// draw face
sShader.use();
sShader.setMat4("projection", projMat);
sShader.setMat4("view", viewMat);
sShader.setVec3("lightPos", lightPos);
modelMat = glm::translate(modelMat, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene
sShader.setMat4("model", modelMat);
mNeutralFace.updateMeshVertices(mNeutralFace.vecModelVertex); // update new position of selected vertex
mNeutralFace.Draw(sShader);
// draw target ball
glm::mat4 modelMat2 = glm::translate(glm::mat4(1.0f), mNeutralFace.vecModelVertex[minIndex].Position); // translate it to nearest vertex
shaderBall.use();
shaderBall.setMat4("projection", projMat);
shaderBall.setMat4("view", viewMat);
shaderBall.setMat4("model", modelMat2);
mBall.Draw(shaderBall); // draw target ball
}
#pragma endregion
// ========================================draw face model (end) ===========================================================
// ========================================IMGUI (start)==========================================================
#pragma region
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("IMGUI Editor");
//ImGui::Text();
if (ImGui::RadioButton("PartA", isPartA)) {
isPartA = true;
isPartB = false;
}
else if((ImGui::RadioButton("PartB", isPartB))) {
isPartA = false;
isPartB = true;
}
//ImGui::Checkbox("isOffestApplied", &isOffsetApplied);
ImGui::Checkbox("isWireframe", &isWireframe);
//ImGui::SliderInt("slider int", &i1, -1, 3);
//ImGui::SliderFloat("lightX", &lightX, -1000.0f, 10000.0f);
//ImGui::SliderFloat("lightY", &lightY, -1000.0f, 10000.0f);
//ImGui::SliderFloat("lightZ", &lightZ, -1000.0f, 10000.0f);
ImGui::SliderFloat("offsetX", &offsetX, -10.0f, 10.0f);
ImGui::SliderFloat("offsetY", &offsetY, -10.0f, 10.0f);
ImGui::SliderFloat("offsetZ", &offsetZ, -10.0f, 10.0f);
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
#pragma endregion
// ========================================IMGUI (end) ===========================================================
// ========================================end render loop (start)==========================================================
#pragma region
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
#pragma endregion
// ========================================end render loop (end) ===========================================================
}
// ========================================process mouse and keyboard (start)==========================================================
#pragma region
void processInput(GLFWwindow* window)
{
// Basic
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
// UI
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_PRESS)
MouseButton2 = true;
else
MouseButton2 = false;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS)
MouseButton1 = true;
else
MouseButton1 = false;
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
//cout << "mouse position: " << "x=" << lastX << ", y=" << lastY << endl;
if (MouseButton2)
camera.ProcessMouseMovement(xoffset, yoffset);
}
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
#pragma endregion
// ========================================process mouse and keyboard (end) ===========================================================
// ========================================load cube (start)==========================================================
#pragma region
unsigned int loadCubemap(vector<std::string> faces)
{
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrComponents;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrComponents, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
cout << "successfully loaded image: " << faces[i].c_str() << endl;
}
else
{
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
stbi_image_free(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return textureID;
}
#pragma endregion
// ========================================load cube (end) ===========================================================
void getDeltaM(Model modelNeutral, Model modelBlend)
{
vector<vec3> deltaM;
for (int i = 0; i < modelNeutral.vecModelVertex.size(); i++)
{
vec3 tmpVertex;
tmpVertex.x = modelNeutral.vecModelVertex[i].Position.x - modelBlend.vecModelVertex[i].Position.x;
tmpVertex.y = modelNeutral.vecModelVertex[i].Position.y - modelBlend.vecModelVertex[i].Position.y;
tmpVertex.z = modelNeutral.vecModelVertex[i].Position.z - modelBlend.vecModelVertex[i].Position.z;
deltaM.push_back(tmpVertex);
}
vecDeltaMs.push_back(deltaM);
vecWeights.push_back(0.0f);
}
int pickNearestVertex(int mouseX, int mouseY, mat4 VM, mat4 PM)
{
// find nearest vertex from mouse click
// convert mouse coordinates to normalized device coordinate NDC
float x = (2.0f * lastX) / SCR_WIDTH - 1.0f;
float y = 1.0f - (2.0f * lastY) / SCR_HEIGHT;
float z = 1.0f; // For a ray, we can use z = 1.0f to shoot the ray forward.
vec3 ray_ndc = vec3(static_cast<float>(x), static_cast<float>(y), z);
vec4 ray_clip = vec4(ray_ndc, -1.0f);
vec4 ray_eye = inverse(PM) * ray_clip;
ray_eye = vec4(ray_eye.x, ray_eye.y, -1.0f, 0.0f);
vec4 ray_eye_world = inverse(VM) * ray_eye;
vec3 ray_wor = vec3(ray_eye_world.x, ray_eye_world.y, ray_eye_world.z);
ray_wor = normalize(ray_wor); // It's a direction so should be normalized.
float minDistance = std::numeric_limits<float>::max();
int closestVertexIndex = -1;
for (size_t i = 0; i < vecNeutralFaceVertice.size(); ++i) {
glm::vec3 vecToVertex = vecNeutralFaceVertice[i] - camera.Position;
float projectionLength = glm::dot(vecToVertex, ray_wor);
glm::vec3 projectionPoint = camera.Position + projectionLength * ray_wor;
float distance = glm::length(vecNeutralFaceVertice[i] - projectionPoint);
if (distance < minDistance) {
minDistance = distance;
closestVertexIndex = i;
}
}
//vec3& vertex = vecNeutralFaceVertice[closestVertexIndex];
return closestVertexIndex;
}
//vec3 pickVertex(int x, int y, mat4 VM, mat4 P, Model& NeutralFace)
//{
// vec3 window;
// window.x = x;
// window.y = height - y - 1;
// glReadPixels(x, height - y - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &window.z);
//
// vec3 mouseVerticePosition = unProject(window, VM, P, vec4(0.0f, 0.0f, SCR_WIDTH, SCR_HEIGHT));
//
// // find nearest vertex
// double minDist = numeric_limits<double>::max(); // minimum distance of mouse position and vertex of neutral face
// double tmpDist;
// GLuint closestVerIndex = 0; // index of closest vertex
//
// // brute force through all vertices to find nearest one
// for (int j = 0; j < NeutralFace.vecModelVertex.size(); j++)
// {
// tmpDist = distance(mouseVerticePosition, NeutralFace.vecModelVertex[j].Position);
//
// if (tmpDist <= minDist)
// {
// minDist = tmpDist;
// closestVerIndex = j;
// }
// }
// vec3 vertex = NeutralFace.vecModelVertex[closestVerIndex].Position;
//
// constraintsIndex.push_back(closestVerIndex); //add index of constrained vertex to list of constraints
// m0.conservativeResize(constraintsIndex.size() * 3);
// m0(3 * constraintsIndex.size() - 3) = vertex.x;
// m0(3 * constraintsIndex.size() - 2) = vertex.y;
// m0(3 * constraintsIndex.size() - 1) = vertex.z;
//
// return vertex;
//}
float calcDistance(const vec3& p1, const vec3& p2) {
return std::sqrt((p2.x - p1.x) * (p2.x - p1.x) +
(p2.y - p1.y) * (p2.y - p1.y) +
(p2.z - p1.z) * (p2.z - p1.z));
}
void read_anim_text_file() {
fstream newfile;
std::string delimiter = " ";
std::string weight;
newfile.open("assets/blendshapes/blendshape_animation.txt", ios::in);
if (newfile.is_open())
{
string line;
while (getline(newfile, line)) {
size_t pos = 0;
vector<float> lineWeights;
while ((pos = line.find(delimiter)) != std::string::npos) {
std::string weight = line.substr(0, pos);
lineWeights.push_back(std::stof(weight));
line.erase(0, pos + delimiter.length());
}
animationWeights.push_back(lineWeights);
}
newfile.close(); //close the file object.
}
}
void removeWordFromLine(std::string& line, const std::string& word)
{
auto n = line.find(word);
if (n != std::string::npos)
{
line.erase(n, word.length());
}
}
//glm::mat4 view;
//glm::mat4 persp_proj;
//void init_facial()
//{
// glUseProgram(shaderProgramID);
//
// // load mesh into a vertex buffer array
// int matrix_location = glGetUniformLocation(shaderProgramID, "model");
// int view_mat_location = glGetUniformLocation(shaderProgramID, "view");
// int proj_mat_location = glGetUniformLocation(shaderProgramID, "proj");
//
// // Root of the Hierarchy
// view = glm::mat4(1.0f);
// view = glm::translate(view, glm::vec3(0.0f, -15.0f, -50.0f));
// persp_proj = glm::perspective(45.0f, (float)width / (float)height, 0.1f, 1000.0f);
//
// // update uniforms & draw
// glUniformMatrix4fv(proj_mat_location, 1, GL_FALSE, glm::value_ptr(persp_proj));
// glUniformMatrix4fv(view_mat_location, 1, GL_FALSE, glm::value_ptr(view));
//
// glm::mat4 modelNeutralFace;
// loadNeutral(modelNeutralFace, matrix_location);
//
// glm::mat4 modelVertexPicker;
// loadVertexPicker(modelVertexPicker, matrix_location);
//
//
//
//
//
//
// string mesh_neutral = "assets/blendshapes/high-res-blendshapes/neutral.obj";
// string mesh_vertex = "assets/blendshapes/high-res-blendshapes/picker.dae";
//
// read_anim_text_file();
//
// for (std::string name : mesh_file_names) {
// std::string label = name;
// removeWordFromLine(label, ".obj");
// removeWordFromLine(label, "Mery_");
// labels.push_back(label);
// }
//
// // Set up the shaders
// shaderProgramID = CompileShaders();
//
//
// //mesh_data_vertex_picker = load_mesh(MESH_VERTEX);
//
// std::cout << "Calculating deltaM vertices..." << std::endl;
// for (std::string name : mesh_file_names) {
// std::string filepath = "models/high-res2/" + name;
//
// ModelData mesh_data = load_mesh(filepath.c_str());
//
//
// calcDeltaM(mesh_data);
//
// }
// std::cout << "Finished loading deltaM vertices." << std::endl;
//
//}
//bool activate = false;
//glm::vec3 rotate_face = glm::vec3(0, -10.0f, 0);
//void loadNeutral(glm::mat4& modelNeutral, int matrix_location)
//{
// modelNeutral = glm::mat4(1.0f);
// generateObjectBufferMesh(mesh_data_neutral, shaderProgramID);
// glUniformMatrix4fv(matrix_location, 1, GL_FALSE, glm::value_ptr(modelNeutral));
// glDrawArrays(GL_TRIANGLES, 0, mesh_data_neutral.mPointCount);
//}
//
//glm::vec3 pickerLocation = glm::vec3(0, 0, 0);
//void loadVertexPicker(glm::mat4& modelVertexPicker, int matrix_location)
//{
//
// modelVertexPicker = glm::mat4(1.0f);
// modelVertexPicker = glm::translate(modelVertexPicker, pickerLocation);
//
// //generateObjectBufferMesh(mesh_data_vertex_picker, shaderProgramID);
//
// glUniformMatrix4fv(matrix_location, 1, GL_FALSE, glm::value_ptr(modelVertexPicker));
// glDrawArrays(GL_TRIANGLES, 0, mesh_data_vertex_picker.mPointCount);
//}
//
//void applyDeltaM(ModelData& mesh_data_neutral, std::vector<glm::vec3> deltaM, float weight)
//{
//
// for (unsigned int i = 0; i < mesh_data_neutral.mPointCount; i++) {
// mesh_data_neutral.mVertices[i].x -= deltaM[i].x * weight;
// mesh_data_neutral.mVertices[i].y -= deltaM[i].y * weight;
// mesh_data_neutral.mVertices[i].z -= deltaM[i].z * weight;
// }
//
//}
//
//glm::vec3 vertexPicker(int x, int y, glm::mat4 VM, glm::mat4 P, ModelData currentFaceMesh)
//{
// glm::vec3 window;
// window.x = x;
// window.y = height - y - 1;
// glReadPixels(x, height - y - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &window.z);
//
// glm::vec3 mouseVerticePosition = glm::unProject(window, VM, P, glm::vec4(0.0f, 0.0f, width, height));
//
// // find nearest vertex
// GLfloat dist = 10;
// GLfloat temp = 0.0f;
// GLuint v_index = 0; // index of closest vertex
//
// // brute force through all vertices to find nearest one
// for (int j = 0; j < currentFaceMesh.mVertices.size(); j++)
// {
// temp = calcDistance(mouseVerticePosition, currentFaceMesh.mVertices[j]);
// if (temp <= dist) {
// dist = temp;
//
// v_index = j;
// }
//
// }
//
// glm::vec3 vertex = currentFaceMesh.mVertices[v_index];
// constraints_index.push_back(v_index); //add index of constrained vertex to list of constraints
// m0.conservativeResize(constraints_index.size() * 3);
// m0(3 * constraints_index.size() - 3) = vertex.x;
// m0(3 * constraints_index.size() - 2) = vertex.y;
// m0(3 * constraints_index.size() - 1) = vertex.z;
// return vertex;
//}
//
//void getMouseLocation(int x, int y, glm::mat4 VM, glm::mat4 P, int chosenVertexIndex) {
// glm::vec3 window;
// window.x = x;
// window.y = height - y - 1;
// glReadPixels(x, height - y - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &window.z);
//
// glm::vec3 targetVerticePos = glm::unProject(window, VM, P, glm::vec4(0.0f, 0.0f, width, height));
//
// pickerLocation = targetVerticePos;
//
// if (targetVerticePos.z < -100.0f) {
// cout << "OUT OF BOUNDS" << endl;
// }
// //works better if you set the target vertice z the same
// targetVerticePos.z = mesh_data_neutral.mVertices[constraints_index.back()].z;
// //std::cout << "object coords are " << glm::to_string(targetVerticePos) << endl;
//
//
//
// //constraints.push_back(v_index); //add index of constrained vertex to list of constraints
// m.conservativeResize(constraints_index.size() * 3);
// m(3 * constraints_index.size() - 3) = targetVerticePos.x;
// m(3 * constraints_index.size() - 2) = targetVerticePos.y;
// m(3 * constraints_index.size() - 1) = targetVerticePos.z;
//
// cout << "m0" << m0 << endl;
// cout << "m" << m << endl;
//
//}
//
//float alpha = 1;
//float mu = 0.001;
//Eigen::VectorXf blendshapeSolver(std::vector<ModelData> expressionMeshes) {
// Eigen::MatrixXf Bbar(constraints_index.size() * 3, mesh_file_names.size());
//
// for (int i = 0; i < constraints_index.size(); i++) {
// for (int j = 0; j < mesh_file_names.size(); j++) {
// Bbar(3 * i, j) = expressionMeshes[j].mVertices[constraints_index[i]].x;
// Bbar(3 * i + 1, j) = expressionMeshes[j].mVertices[constraints_index[i]].y;
// Bbar(3 * i + 2, j) = expressionMeshes[j].mVertices[constraints_index[i]].z;
// }
// }
//
// //Left side of Equation
// Eigen::MatrixXf LHS =
// Bbar.transpose() * Bbar +
// (alpha + mu) * Eigen::MatrixXf::Identity(mesh_file_names.size(), mesh_file_names.size());
//
// //Right Side Of Equation
// Eigen::VectorXf RHS(mesh_file_names.size());
//
// //put mWeights into Eigen
// Eigen::VectorXf mWeightsCurrent(mesh_file_names.size());
// for (int i = 0; i < mWeights.size(); i++) {
// cout << "glm mWeights: " << mWeights[i];
// mWeightsCurrent(i) = mWeights[i];
// }
// cout << endl;
// cout << "eigen mWeightsCurrent: " << mWeightsCurrent << endl;
//
// RHS = Bbar.transpose() * (m - m0) + alpha * mWeightsCurrent;
//
// //solve Equation
// Eigen::LDLT<Eigen::MatrixXf> solver(LHS);
// Eigen::VectorXf mWeightsNew = solver.solve(RHS);
// cout << "eigen mWeightsNEW: " << mWeightsNew << endl;
//
// return mWeightsNew;
//
//}
//
//int frame_num = 0;
//int mouse_x;
//bool mouseClickedDown = false;
//bool mouseClickedUp = false;
//glm::vec2 mousePosDown;
//glm::vec2 mousePosUp;
//int chosen_vertex_index = -1;
//void updateScene() {
//
// static DWORD last_time = 0;
// DWORD curr_time = glfwGetTime();
// if (last_time == 0)
// last_time = curr_time;
// float delta = (curr_time - last_time) * 0.001f;
// last_time = curr_time;
//
// mesh_data_neutral = mesh_data_neutral_original;
//
// if (playAnim) {
//
// for (int i = 0; i < mesh_file_names.size(); i++) {
// applyDeltaM(mesh_data_neutral, deltaMs[i], animationWeights[frame_num][i]);
// }
// frame_num++;
// if (frame_num == animationWeights.size()) {
// playAnim = false;
// }
// }
// else {
// for (int i = 0; i < mesh_file_names.size(); i++) {
// applyDeltaM(mesh_data_neutral, deltaMs[i], mWeights[i]);
// frame_num = 0;
// }
// }
//
// if (mouseClickedDown) {
// //std::cout << "MOUSE DOWN X: " << mousePosDown.x << std::endl;
// //std::cout << "MOUSE DOWN Y: " << mousePosDown.y << std::endl;
//
// int mesh_index = -1;
//
// //std::cout << "View Martrix " << glm::to_string(view) << std::endl;
//
// glm::vec3 mouseVertex = vertexPicker((int)mousePosDown.x, (int)mousePosDown.y, view, persp_proj, mesh_data_neutral);
// //std::cout << "mesh index " << mesh_index << std::endl;
// //std::cout << "chosen vertix is: " << glm::to_string(mouseVertex) << std::endl;
// pickerLocation = mouseVertex;
// mouseClickedDown = false;
// }
// if (mouseClickedUp) {
// std::cout << "MOUSE UP X: " << mousePosUp.x << std::endl;
// std::cout << "MOUSE UP Y: " << mousePosUp.y << std::endl;
// //std::vector<ModelData> fa;
// //neutral.push_back(mesh_data_neutral);
//
//
// getMouseLocation((int)mousePosUp.x, (int)mousePosUp.y, view, persp_proj, chosen_vertex_index);
// //mWeights[mesh_index]=1.0f;
// //std::cout << "mesh index " << mesh_index << std::endl;
//
// Eigen::VectorXf mWeightsNew = blendshapeSolver(facialExpressions);
//
// //convert from eigen back to glm
// std::vector<float> mWeightsNewGlm;
// for (int i = 0; i < mWeightsNew.size(); i++) {
// mWeightsNewGlm.push_back(mWeightsNew[i]);
// }
//
// mWeights = mWeightsNewGlm;
//
// mouseClickedUp = false;
// }
//}