-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_IK_CCD.cpp
More file actions
733 lines (733 loc) · 26.3 KB
/
main_IK_CCD.cpp
File metadata and controls
733 lines (733 loc) · 26.3 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
//#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 "model_animation.h"
//#include <iostream>
//#include "gui.h"
//#define _USE_MATH_DEFINES
//#include <math.h>
//using namespace std;
//using namespace glm;
//// ========================================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);
//unsigned int LoadTextureImageToGPU(const char* filename, GLint internalFormat, GLenum format, int textureSlot);
//unsigned int loadTexture(const char* path);
//float limitValue(float value, float min_value, float max_value);
//void updatePosition();
//glm::vec3 lerp(const glm::vec3& start, const glm::vec3& end, float t);
//float easeInOut(float t);
//void ccd(glm::vec3 PosTgt);
//// window settings
//const unsigned int SCR_WIDTH = 800;
//const unsigned int SCR_HEIGHT = 600;
//bool MouseButton2(false);
//
//// 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;
//
//// timing
//float deltaTime = 0.0f;
//float lastFrame = 0.0f;
//
//int screenWidth = 800;
//int screenHeight = 600;
//int cursorPosX = screenWidth / 2;
//int cursorPosY = screenHeight / 2;
//
//float posx(0.0f);
//float posy(0.0f);
//float posz(0.0f);
//
//float theta_1; // bone 1 rotation
//float theta_2; // bone 2 rotation
//
//float posX(5.0);
//float posY(5.0);
//float posZ(5.0);
//
//bool is2D(true);
//bool is3D(false);
//bool is3DSpline(false);
//float endDistance(0.2);
//
//float L1 = 2.5; // bone 1 length
//float L2 = 2.5; // bone 2 length
//float L3 = 2.5; // bone 3 length
//
//float currentTimeCCD = 0.0f; // Current time in the animation
//
//
//
//glm::mat4 trans;
//glm::mat4 modelMat;
//glm::mat4 viewMat;
//glm::mat4 projMat;
////projMat = glm::ortho(-20.0f, 20.0f, -20.0f, 20.0f, -100.0f, 100.0f); // same size regardless of distacne from camera
//
//glm::vec3 rotAxis;
//glm::quat rot;
//
//glm::vec3 PosL1 = glm::vec3(0.0f); // bone 1 position
//glm::vec3 PosL2 = glm::vec3(0.0f); // bone 2 position
//glm::vec3 PosL3 = glm::vec3(0.0f); // bone 3 position
//glm::vec3 PosEnd = glm::vec3(0.0f); // end-effector position
//
//glm::quat QuatL1 = glm::angleAxis(glm::radians(0.01f), glm::vec3(1.0f, 0.0f, 0.0f));
//glm::quat QuatL2 = glm::angleAxis(glm::radians(0.01f), glm::vec3(1.0f, 0.0f, 0.0f));
//glm::quat QuatL3 = glm::angleAxis(glm::radians(0.01f), glm::vec3(1.0f, 0.0f, 0.0f));
//
//glm::mat4 rotMat1(1.0f);
//glm::mat4 rotMat2(1.0f);
//glm::mat4 rotMat3(1.0f);
//
//// Define a set of control points for the spline
//std::vector<glm::vec3> controlPoints = {
// glm::vec3(0, 0, 0), // Starting position
// glm::vec3(2, 5, -1), // First control point
// glm::vec3(5, 10, 0), // Second control point
// glm::vec3(10, 5, 1), // Third control point
// glm::vec3(15, 0, 0) // Ending position
//};
//
//float totalDuration = 5.0f; // Total duration of the Hermite curve animation
//
//
//
//float limitValue(float value, float min_value, float max_value) {
// return std::max(min_value, std::min(value, max_value));
//}
//
//#pragma endregion
//// ========================================initialize (end) ===========================================================
//
//int main()
//{
// // ========================================init (start)==========================================================
//#pragma region
// // glfw: initialize and configure
// // ------------------------------
// glfwInit();
// 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
//
// // glfw window creation
// // --------------------
// GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL", NULL, NULL);
// if (window == NULL)
// {
// std::cout << "Failed to create GLFW window" << std::endl;
// glfwTerminate();
// return -1;
// }
// glfwMakeContextCurrent(window);
// GLFWcursor* crosshairCursor = glfwCreateStandardCursor(GLFW_CROSSHAIR_CURSOR);
// glfwSetCursor(window, crosshairCursor);
//
// 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);
//
// // configure global opengl state
// // -----------------------------
// glEnable(GL_DEPTH_TEST);
//
//
//#pragma endregion
// // ========================================init (end) ===========================================================
//
// // ========================================init shaders & models (start)==========================================================
//#pragma region
//
// Shader shader("assets/shaders/Animation/kinematic/normal_map.vs", "assets/shaders/Animation/kinematic/normal_map.fs");
// Shader shaderBall("assets/shaders/Animation/kinematic/ball.vs", "assets/shaders/Animation/kinematic/ball.fs");
//
// Model boneModel("assets/models/bone/bone4/bone.obj");
// Model boneModel2("assets/models/bone/bone4/bone.obj");
// Model boneModel3("assets/models/bone/bone4/bone.obj");
// Model targetBall("assets/models/ball/ball.obj");
//
//
// stbi_set_flip_vertically_on_load(true); // other models flip
//
//#pragma endregion
//
//// ========================================init shaders & models (end) ===========================================================
//#pragma region Prepare MVP matrices
//
//#pragma endregion
//
// // initialize IMG UI
// myGUI myGui(window);
//// ========================================init render loop (start)==========================================================
//#pragma region
//
// // render loop
// while (!glfwWindowShouldClose(window))
// {
// //float currentTime = glfwGetTime();
// // 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);
//
// //glViewport(0, 0, 1400, 1000);
//
//#pragma endregion
//// ========================================IK Bone Setting (end) ===========================================================
//
//
//
//// ========================================draw bone (start)==========================================================
//#pragma region
//
// if (is2D)
// {
//
// // cursor position
// posX = (cursorPosX - (screenWidth / 2)) / float(screenWidth / 2) * 3.5f;
// posY = -(cursorPosY - (screenHeight / 2)) / float(screenHeight / 2) * 3.5f; // minus symbol check
//
// float L1 = 2.5; // bone 1 length
// float L2 = 1.5; // bone 2 length
// float L3 = 1.5; // bone 3 length
//
// // IK analytical solution
// float d = std::sqrt(posX * posX + posY * posY); // distance of target to origin
// float theta_T = std::acos(posX / d); // angle for target point with respect to axis-x
// float tmpCos_1T = limitValue((L1 * L1 + d * d - L2 * L2) / (2 * L1 * d), -1.0f, 1.0f); // cos(theta1 - thetaT), clamp range into [-1, 1]
// float tmpCos_2 = limitValue(-(L1 * L1 + L2 * L2 - d * d) / (2 * L1 * L2), -1.0f, 1.0f); // cos(theta2), clamp range into [-1, 1]
// float tempTheta_1 = std::acos(tmpCos_1T); // convert to radian
// float tempTheta_2 = std::acos(tmpCos_2); // convert to radian
//
// // target point is over distance of sum length of two bones
// if (d > (L1 + L2))
// {
// theta_1 = std::atan2(posY, posX);
// theta_2 = 0;
// }
// // normal case
// else if (posY > 0)
// {
// theta_1 = tempTheta_1 + theta_T;
// theta_2 = -tempTheta_2;
// }
// else
// {
// theta_1 = -(theta_T - tempTheta_1);
// theta_2 = -tempTheta_2;
// }
//
// projMat = glm::ortho(-20.0f, 20.0f, -20.0f, 20.0f, -100.0f, 100.0f); // same size regardless of distacne from camera
//
// // bone 1
// glm::mat4 rotMat = glm::scale(glm::mat4(1.0f), glm::vec3(5.0f));
// rotMat = glm::rotate(rotMat, theta_1, glm::vec3(0.0f, 0.0f, 1.0f));
// glm::mat4 modelBone1 = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
// modelBone1 = modelBone1 * rotMat; // rotate first, then translate
//
// shader.use();
// shader.setMat4("projection", projMat);
// shader.setMat4("view", camera.GetViewMatrix());
// shader.setVec3("viewPos", camera.Position);
// shader.setMat4("model", modelBone1);
// boneModel.Draw(shader);
//
// // bone 2 (hierarchical structure: rotate at pivot point)
// glm::mat4 transBone2 = glm::translate(rotMat, glm::vec3(L1, 0.0f, 0.0f));
// transBone2 = glm::rotate(transBone2, theta_2, glm::vec3(0.0f, 0.0f, 1.0f));
// glm::mat4 modelBone2 = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
// modelBone2 = modelBone2 * transBone2;
// shader.setMat4("model", modelBone2);
// boneModel2.Draw(shader);
//
// // target ball
// shaderBall.use();
// shaderBall.setMat4("projection", projMat);
// shaderBall.setMat4("view", camera.GetViewMatrix());
// shaderBall.setVec3("viewPos", camera.Position);
// modelMat = translate(mat4(1.0f), vec3(lastX, lastY, 0.0f));
// shaderBall.setMat4("model", modelMat);
// targetBall.Draw(shaderBall);
// }
// else if(is3D)
// {
// glm::vec3 PosTgt = glm::vec3(posX, posY, posZ);
// projMat = glm::perspective(glm::radians(45.0f), 1400.0f / 1000.0f, 0.1f, 10000.0f);
//
// ccd(PosTgt); // move end-effector to target point
//
// // draw bone1
// trans = rotMat1;
// modelMat = translate(mat4(1.0f), PosL1);
//
// shader.use();
// shader.setMat4("projection", projMat);
// shader.setMat4("view", camera.GetViewMatrix());
// shader.setVec3("viewPos", camera.Position);
// shader.setMat4("model", modelMat * trans);
// boneModel.Draw(shader);
//
// // draw bone2
// trans *= rotMat2;
// modelMat = translate(mat4(1.0f), PosL2);
// shader.setMat4("model", modelMat * trans);
// boneModel2.Draw(shader);
//
// // draw bone3
// trans *= rotMat3;
// modelMat = translate(mat4(1.0f), PosL3);
// shader.setMat4("model", modelMat * trans);
// boneModel3.Draw(shader);
//
// // draw target ball
// shaderBall.use();
// shaderBall.setMat4("projection", projMat);
// shaderBall.setMat4("view", camera.GetViewMatrix());
// shaderBall.setVec3("viewPos", camera.Position);
// modelMat = translate(mat4(1.0f), PosTgt);
// shaderBall.setMat4("model", modelMat);
// targetBall.Draw(shaderBall);
// }
// else if (is3DSpline)
// {
// glm::vec3 PosTgt; // target point
//
// currentTimeCCD += deltaTime;
// if (currentTimeCCD > totalDuration)
// {
// ccd(PosEnd);
// currentTimeCCD = 0.0f;
// }
//
// float t = currentTimeCCD / totalDuration;
// float easedT = easeInOut(t);
// glm::vec3 startPos = glm::vec3(5.0f, 3.0f, 4.0f); // Starting position of the Hermite curve
// glm::vec3 endPos = glm::vec3(-3.3f, -3.6f, -3.0f); // Ending position of the Hermite curve
// PosTgt = lerp(startPos, endPos, easedT); // interpolate new points
// ccd(PosTgt); // ccd to target point
//
// // draw bone1
// projMat = glm::perspective(glm::radians(45.0f), 1400.0f / 1000.0f, 0.1f, 10000.0f);
//
// trans = rotMat1;
// modelMat = translate(mat4(1.0f), PosL1);
//
// shader.use();
// shader.setMat4("projection", projMat);
// shader.setMat4("view", camera.GetViewMatrix());
// shader.setVec3("viewPos", camera.Position);
// shader.setMat4("model", modelMat * trans);
// boneModel.Draw(shader);
//
// // draw bone2
// trans *= rotMat2;
// modelMat = translate(mat4(1.0f), PosL2);
// shader.setMat4("model", modelMat * trans);
// boneModel2.Draw(shader);
//
// // draw bone3
// trans *= rotMat3;
// modelMat = translate(mat4(1.0f), PosL3);
// shader.setMat4("model", modelMat * trans);
// boneModel3.Draw(shader);
//
// // draw target ball
// shaderBall.use();
// shaderBall.setMat4("projection", projMat);
// shaderBall.setMat4("view", camera.GetViewMatrix());
// shaderBall.setVec3("viewPos", camera.Position);
// modelMat = translate(mat4(1.0f), PosTgt);
// shaderBall.setMat4("model", modelMat);
// targetBall.Draw(shaderBall);
// }
//
//
//
//#pragma endregion
//// ========================================draw bone (end) ===========================================================
//
//
//// ========================================IMGUI (start)==========================================================
//#pragma region
// // Start the Dear ImGui frame
// ImGui_ImplOpenGL3_NewFrame();
// ImGui_ImplGlfw_NewFrame();
// ImGui::NewFrame();
//
// ImGui::Begin("IMGUI Editor");
//
// // ====== UI content start ======
// if (ImGui::RadioButton("2D", is2D))
// {
// is3D = false; // Ensure only one is selected
// is2D = true;
// is3DSpline = false;
// }
// ImGui::SameLine();
// if (ImGui::RadioButton("3D", is3D))
// {
// is2D = false; // Ensure only one is selected
// is3D = true;
// is3DSpline = false;
// }
// ImGui::SameLine();
// if (ImGui::RadioButton("3D Spline", is3DSpline))
// {
// is2D = false; // Ensure only one is selected
// is3D = false;
// is3DSpline = true;
// }
//
// if (is3D)
// {
// ImGui::SliderFloat("Ending Distance", &endDistance, 0.001f, 10.0f);
// //ImGui::SliderInt("Iteration Times", &iterNum, 1000, 6000);
// ImGui::SliderFloat("PosX", &posX, -50.0f, 50.0f);
// ImGui::SliderFloat("PosY", &posY, -50.0f, 50.0f);
// ImGui::SliderFloat("PosZ", &posZ, -50.0f, 50.0f);
// }
// if (is3DSpline)
// {
// ImGui::SliderFloat("Ending Distance", &endDistance, 0.0001f, .4f);
// ImGui::SliderFloat("Total Duration", &totalDuration, 0.5f, 10.0f);
//
// }
// // ====== UI content end ======
//
// 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)
//{
// 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;
//}
//
//// 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;
//
// cursorPosX = xpos;
// cursorPosY = ypos;
//
// 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) ===========================================================
//
//
//unsigned int loadCubemap(vector<std::string> faces)
//{
// unsigned int textureID;
// glGenTextures(1, &textureID);
// glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
//
// int width, height, nrChannels;
// for (unsigned int i = 0; i < faces.size(); i++)
// {
// unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 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);
// std::cout << "Cubemap tex succeeded to load at path: " << faces[i] << std::endl;
// }
// else
// {
// std::cout << "Cubemap tex 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;
//}
//
//unsigned int LoadTextureImageToGPU(const char* filename, GLint internalFormat, GLenum format, int textureSlot)
//{
// unsigned int TexBuffer;
// glGenTextures(1, &TexBuffer);
//
// glActiveTexture(GL_TEXTURE0 + textureSlot);
// glBindTexture(GL_TEXTURE_2D, TexBuffer);
//
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// // set texture filtering parameters
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//
// int width, height, nrChannel;
// unsigned char* data = stbi_load(filename, &width, &height, &nrChannel, 0);
// if (data)
// {
// glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
// glGenerateMipmap(GL_TEXTURE_2D);
// }
// else
// {
// cout << "Texture image " << filename << " load failed." << endl;
// }
// stbi_image_free(data);
//
// //cout << filename << "Slot: " << TexBuffer << endl;
//
// return TexBuffer;
//}
//
//unsigned int loadTexture(char const* path)
//{
// unsigned int textureID;
// glGenTextures(1, &textureID);
//
// int width, height, nrComponents;
// unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
// if (data)
// {
// GLenum format;
// if (nrComponents == 1)
// format = GL_RED;
// else if (nrComponents == 3)
// format = GL_RGB;
// else if (nrComponents == 4)
// format = GL_RGBA;
//
// glBindTexture(GL_TEXTURE_2D, textureID);
// glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
// glGenerateMipmap(GL_TEXTURE_2D);
//
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT); // for this tutorial: use GL_CLAMP_TO_EDGE to prevent semi-transparent borders. Due to interpolation it takes texels from next repeat
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, format == GL_RGBA ? GL_CLAMP_TO_EDGE : GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//
// stbi_image_free(data);
// }
// else
// {
// std::cout << "Texture failed to load at path: " << path << std::endl;
// stbi_image_free(data);
// }
//
// return textureID;
//}
//
//// bones hierarchical structure
//void updatePosition()
//{
// PosL2 = glm::mat3(rotMat1) * glm::vec3(L1, 0.0f, 0.0f) + PosL1; // bone 2 start position
// PosL3 = glm::mat3(rotMat1 * rotMat2) * glm::vec3(L2, 0.0f, 0.0f) + PosL2; // bone 3 start position
// PosEnd = mat3(rotMat1 * rotMat2 * rotMat3) * vec3(L3, 0.0f, 0.0f) + PosL3; // end-effector start position
//}
//
//// bones hierarchical structure
//void updateInitPosition()
//{
// PosL2 = glm::mat3(rotMat1) * glm::vec3(L1, 0.0f, 0.0f) + PosL1; // bone 2 start position
// PosL3 = glm::mat3(rotMat1 * rotMat2) * glm::vec3(L2, 0.0f, 0.0f) + PosL2; // bone 3 start position
// PosEnd = mat3(rotMat1 * rotMat2 * rotMat3) * vec3(L3, 0.0f, 0.0f) + PosL3; // end-effector start position
//}
//
//
//glm::vec3 lerp(const glm::vec3& start, const glm::vec3& end, float t) {
// return start + t * (end - start);
//}
//
//// Ease-In-Ease-Out function
//float easeInOut(float t) {
// return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
//}
//
//void ccd(glm::vec3 PosTgt)
//{
// updatePosition(); // update bone positions after rotated
// int iterNum = 3000; // max iteration times
// while (--iterNum)
// {
// glm::vec3 vecP2E; // vector point to end-effector
// glm::vec3 vecP2T; // vector point to target point
//
// if (length(PosTgt) >= L1 + L2 + L3)
// {
// vecP2E = normalize(PosEnd - PosL1); // bone3 -> end-effector
// vecP2T = normalize(PosTgt - PosL1); // bone3 -> target
// float theta = acos(limitValue(dot(vecP2E, vecP2T), -1, 1)); // rotation theta
// rotAxis = normalize(cross(vecP2T, vecP2E)); // rotation axis
// rotMat3 = glm::rotate(rotMat3, theta, rotAxis);
//
// updatePosition();
//
// // 2. rotate bone2 to target point
// //vecP2E = normalize(PosEnd - PosL2); // bone2 -> end-effector
// //vecP2T = normalize(PosTgt - PosL2); // bone2 -> target
// //theta = acos(limitValue(dot(vecP2E, vecP2T), -1, 1)); // rotation theta
// //rotAxis = normalize(cross(vecP2T, vecP2E)); // rotation axis
// rotMat2 = glm::rotate(rotMat2, theta, rotAxis);
//
// updatePosition();
//
// // 3. rotate bone1 to target point
// //vecP2E = normalize(PosEnd - PosL1); // bone1 -> end-effector
// //vecP2T = normalize(PosTgt - PosL1); // bone1 -> target
// //theta = acos(limitValue(dot(vecP2E, vecP2T), -1, 1)); // rotation theta
// //rotAxis = normalize(cross(vecP2T, vecP2E)); // rotation axis
// rotMat1 = glm::rotate(rotMat1, theta, rotAxis);
//
// updatePosition();
// break;
// }
//
//
// if (distance(PosTgt, PosEnd) < endDistance) {
// break;
// }
//
// // 1. rotate bone3 to target point
// vecP2E = normalize(PosEnd - PosL3); // bone3 -> end-effector
// vecP2T = normalize(PosTgt - PosL3); // bone3 -> target
// float theta = acos(limitValue(dot(vecP2E, vecP2T), -1, 1)); // rotation theta
// rotAxis = normalize(cross(vecP2T, vecP2E)); // rotation axis
// rotMat3 = glm::rotate(rotMat3, theta, rotAxis);
//
// updatePosition(); // update bone positions after rotated
// //if (distance(PosTgt, PosEnd) < endDistance) break;
//
// // 2. rotate bone2 to target point
// vecP2E = normalize(PosEnd - PosL2); // bone2 -> end-effector
// vecP2T = normalize(PosTgt - PosL2); // bone2 -> target
// theta = acos(limitValue(dot(vecP2E, vecP2T), -1, 1)); // rotation theta
// rotAxis = normalize(cross(vecP2T, vecP2E)); // rotation axis
// rotMat2 = glm::rotate(rotMat2, theta, rotAxis);
//
// updatePosition(); // update bone positions after rotated
// //if (distance(PosTgt, PosEnd) < endDistance) break;
//
// // 3. rotate bone1 to target point
// vecP2E = normalize(PosEnd - PosL1); // bone1 -> end-effector
// vecP2T = normalize(PosTgt - PosL1); // bone1 -> target
// theta = acos(limitValue(dot(vecP2E, vecP2T), -1, 1)); // rotation theta
// rotAxis = normalize(cross(vecP2T, vecP2E)); // rotation axis
// rotMat1 = glm::rotate(rotMat1, theta, rotAxis);
//
// updatePosition();
// //if (distance(PosTgt, PosEnd) < endDistance) break;
//
// }
//}