Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include "vector.h"
  2. #include <cstdlib>
  3. #include "GL/glut.h"
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. const double PI = acos(-1.0);
  10.  
  11. // baru 03-09-2014
  12. double a = 10.0, b = 10.0, c = 10.0;
  13. double speed = 0.1;
  14. vector4d oby[210][105];
  15. int point;
  16. vector<vector4d> points;
  17. bool mouseState = false;
  18.  
  19. void ellipsoid() {
  20. int i = 0;
  21. for (double u = -PI; i < 20; u += PI / 10, i++) {
  22. int j = 0;
  23. for (double v = -PI / 2; j < 10; v += PI / 10, j++) {
  24. double x = a * cos(v)*cos(u); oby[i][j].setX(x);
  25. double y = b * cos(v)*sin(u); oby[i][j].setY(y);
  26. double z = c * sin(v); oby[i][j].setZ(z);
  27. }
  28. }
  29. }
  30.  
  31. void keyboard(unsigned char key, int x, int y) {
  32. if (key == ' ' && !points.empty())
  33. points.pop_back();
  34. if (key == 'a')
  35. speed -= 0.05;
  36. if (key == 'd')
  37. speed += 0.05;
  38. if (key == 'j')
  39. a -= 5;
  40. if (key == 'u')
  41. a += 5;
  42. if (key == 'k')
  43. b -= 5;
  44. if (key == 'i')
  45. b += 5;
  46. if (key == 'l')
  47. c -= 5;
  48. if (key == 'o')
  49. c += 5;
  50. ellipsoid();
  51. glutPostRedisplay();
  52. }
  53.  
  54. void mouse(int button, int state, int x, int y) {
  55. float fx = x/600.f, fy = y/600.0f;
  56. if (button == 0 && state == 0) {
  57. for (int i = 0; i < points.size(); i++) {
  58. if (hypot(fx-points[i].getX(), fy-points[i].getY()) > 0.03) //out circle
  59. continue;
  60. point = i;
  61. points[point].setX(points[i].getX());
  62. points[point].setY(points[i].getY());
  63. mouseState = true;
  64. }
  65. if (!mouseState)
  66. points.push_back(vector4d(fx, fy));
  67. }
  68. if (button == 0 && state == 1) {
  69. mouseState = false;
  70. }
  71. glutPostRedisplay();
  72. }
  73.  
  74. void motion(int x, int y) {
  75. if (mouseState) {
  76. points[point].setX(x/600.0f);
  77. points[point].setY(y/600.0f);
  78. }
  79. glutPostRedisplay();
  80. }
  81.  
  82. void display() {
  83. glClear(GL_COLOR_BUFFER_BIT);
  84. // baru 03-09-2014
  85. glColor3f(0.5, 0.8, 0.1);
  86. for (int i = 0; i < 20; i++)
  87. for (int j = 0; j < 9; j++) {
  88. glBegin(GL_LINES);
  89. glVertex3f(oby[i][j].getX(), oby[i][j].getY(), oby[i][j].getZ());
  90. glVertex3f(oby[i][(j+1)%10].getX(), oby[i][(j+1)%10].getY(), oby[i][(j+1)%10].getZ());
  91. glEnd();
  92. }
  93. glColor3f(1.0, 1.0, 0.0);
  94. for (int j = 0; j < 10; j++)
  95. for (int i = 0; i < 20; i++) {
  96. glBegin(GL_LINES);
  97. glVertex3f(oby[i][j].getX(), oby[i][j].getY(), oby[i][j].getZ());
  98. glVertex3f(oby[(i+1)%20][j].getX(), oby[(i+1)%20][j].getY(), oby[(i+1)%20][j].getZ());
  99. glEnd();
  100. }
  101. glutSwapBuffers();
  102. }
  103.  
  104. void idle() {
  105. //speed += 0.5;
  106. glRotatef(speed, 0, 1, 1);
  107. glutPostRedisplay();
  108. }
  109.  
  110. void myInit() {
  111. glMatrixMode (GL_PROJECTION);
  112. //gluOrtho2D (-1.0, 1.0, 1.0, -1.0);
  113. glOrtho(-100.0, 100.0, 100.0, -100.0, -1000.0, 1000.0);
  114. glClearColor(0.0, 0.0, 0.0, 0.1);
  115. // baru 03-09-2014
  116. a = 80.0, b = 50.0, c = 50.0;
  117. ellipsoid();
  118.  
  119. }
  120.  
  121. int main(int argc, char**argv) {
  122. glutInit(&argc, argv);
  123. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
  124. glutInitWindowSize(600,600);
  125. glutInitWindowPosition(100,100);
  126. glutCreateWindow("GeLUT");
  127. glutDisplayFunc(display);
  128. glutKeyboardFunc(keyboard);
  129. glutMouseFunc(mouse);
  130. glutMotionFunc(motion);
  131. glutIdleFunc(idle);
  132. myInit();
  133. glutMainLoop();
  134. return 0;
  135. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement