Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. //#include <GL/glu.h>
  2.  
  3. #include <windows.h>
  4. #include <fstream>
  5. #include <vector>
  6.  
  7. #include <GL/glut.h> // gl.h and glu.h are already included in glut.h
  8.  
  9. using namespace std;
  10.  
  11. #define MAX_N 1004
  12.  
  13. ifstream fiA("A.txt");
  14. ifstream fiB("B.txt");
  15.  
  16. float** A;
  17. float** B;
  18. int numberOfVertexes;
  19.  
  20. void init() {
  21. // select clearing (background) color
  22. glClearColor(0.0, 0.0, 0.0, 0.0);
  23. glMatrixMode(GL_PROJECTION);
  24. glLoadIdentity();
  25. gluOrtho2D(0.0, 800.0, 0.0, 600.0);
  26. }
  27.  
  28. void addPoint(float** shape,int n,int k)
  29. {
  30. int interval = n/k;
  31. for (int i=n;i<n+k;i++)
  32. shape[i] = new float[2];
  33. int i=0;
  34. int d=interval;
  35. vector<float> points[2];
  36. while (i<n)
  37. {
  38. points[0].push_back(shape[i][0]);
  39. points[1].push_back(shape[i][1]);
  40. d--;
  41. if (d==0&&k!=0)
  42. {
  43. int nexti=(i+1)%n;
  44. float nx=(shape[i][0]+shape[nexti][0])/(2.0);
  45. float ny=(shape[i][1]+shape[nexti][1])/(2.0);
  46. points[0].push_back(nx);
  47. points[1].push_back(ny);
  48. //points[0].push_back(shape[nexti][0]);
  49. //points[1].push_back(shape[nexti][1]);
  50. k--;
  51. d = interval;
  52. }
  53. }
  54. for (int i=0;i<n+k;i++)
  55. {
  56. for (int j=0;j<2;j++)
  57. shape[i][j] = points[i][j];
  58. }
  59. }
  60.  
  61. void initShape() {
  62. int n,m;
  63. fiA>>n;
  64. fiB>>m;
  65. A = new float*[n];
  66. B = new float*[m];
  67. for (int i=0;i<n;i++)
  68. {
  69. A[i] = new float[2];
  70. fiA>>A[i][0]>>A[i][1];
  71. }
  72. for (int i = 0; i < m;i++)
  73. {
  74. B[i] = new float[2];
  75. fiB>>B[i][0]>>B[i][1];
  76. }
  77. if (n>m)
  78. {
  79. addPoint(B,m,(n-m));
  80. }
  81. else
  82. addPoint(A,n,(m-n));
  83. numberOfVertexes = max(n,m);
  84. }
  85.  
  86. float tweening(float a, float b, float t) {
  87. return (1-t)*a + t*b;
  88. }
  89.  
  90. void drawTweening(float** A, float** B, int n, float t) {
  91. glBegin(GL_LINE_LOOP);
  92. for (int i = 0; i < n; ++i) {
  93. float Px = tweening(A[i][0], B[i][0], t);
  94. float Py = tweening(A[i][1], B[i][1], t);
  95. glVertex2f(Px, Py);
  96. }
  97. glEnd();
  98. glFlush();
  99. }
  100.  
  101. void display(void) {
  102. for (float t = 0.0, deltaT = 0.01; ; t += deltaT) {
  103. glClear(GL_COLOR_BUFFER_BIT);
  104. drawTweening(A, B, numberOfVertexes, t);
  105. glutSwapBuffers();
  106. if (t >= 1.0 || t < 0.0) deltaT = -deltaT;
  107. Sleep(50);
  108. }
  109. }
  110.  
  111. int main(int argc, char** argv) {
  112.  
  113. glutInit(&argc, argv);
  114.  
  115. // single buffer and RGBA
  116. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  117.  
  118. // window size
  119. glutInitWindowSize(800, 600);
  120.  
  121. // window position
  122. glutInitWindowPosition(100, 100);
  123.  
  124. // title bar
  125. glutCreateWindow("Tweening");
  126.  
  127. // call initialization routines
  128. init();
  129.  
  130. initShape();
  131.  
  132. // Register callback function to display graphics
  133. glutDisplayFunc(display);
  134.  
  135. // Enter main loop and process events
  136. glutMainLoop();
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement