kilolilo

Untitled

Apr 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include<cmath>
  4. #include <algorithm>
  5.  
  6. #include <GL/glut.h>
  7. #include <GL/gl.h>
  8. #include <GL/glu.h>
  9.  
  10. using namespace std;
  11.  
  12. double scale = 1.0;
  13. double vx=0,vy=0;
  14. double p=3.14,r_1=0;
  15. double kx=5,ky=5;
  16.  
  17. int msec = 20;
  18.  
  19. void render_square(double x, double y) {
  20. glPushMatrix();
  21.  
  22. double dx = 0.5;
  23. double dy = 0.5;
  24. glBegin(GL_POLYGON);
  25. glVertex2f(x, y);
  26. glVertex2f(x + dx, y);
  27. glVertex2f(x + dx, y - dy);
  28. glVertex2f(x, y - dy);
  29. glEnd();
  30. glPopMatrix();
  31. }
  32.  
  33. void draw_line(double x, double y, double x1, double y1) {
  34. glBegin(GL_LINES);
  35. glVertex2d(x, y);
  36. glVertex2d(x1, y1);
  37. glEnd();
  38. }
  39. class circle{
  40. public:
  41. double x,y,r,vx,vy;
  42. circle(double x_, double y_, double r_, double vx_, double vy_):r(r_), x(x_), y(y_){
  43. vx = vx_ / 1000.0 * msec;
  44. vy = vy_ / 1000.0 * msec;
  45. }
  46. void print_circle(){
  47. int n=41;
  48. double cx=x + r;
  49. double cy=y;
  50. double step = p/n;
  51. for (float f=step; f<=p*2;f+=step){
  52. draw_line(cx ,cy ,cos(f)*r+x ,sin(f)*r+y);
  53. cx=x + cos(f)*r;
  54. cy=y + sin(f)*r;
  55. }
  56. }
  57. void update(){
  58. if(x+r>kx || x-r<-kx){
  59. vx*=-1;
  60. }
  61. if(y+r>ky || y-r<-ky){
  62. vx*=-1;
  63. }
  64. x+=vx;
  65. y+=vy;
  66. }
  67. };
  68. class Vector{
  69. public:
  70. float x,y;
  71. Vector(float a,float b):x(a),y(b){
  72. }
  73. Vector operator +(Vector other){
  74. return Vector(x+other.x, y+other.y);
  75. }
  76. Vector operator -(Vector other){
  77. return Vector(x-other.x, y-other.y);
  78. }
  79. Vector operator *(float a){
  80. return Vector(x*a,y*a);
  81. }
  82. Vector operator /(float a){
  83. return Vector(x/a,y/a);
  84. }
  85. float dot (Vector other){
  86. return x*other.x+y*other.y;
  87. }
  88. void print(){
  89. cout<<'('<<x<<','<<y<<')'<<endl;
  90. }
  91. void draw(){
  92. draw_line(0,0,x,y);
  93. draw_line(x,y,Vector)
  94. }
  95. };
  96. Vector b(2,2);
  97. circle a(0, 0, 0.4, 1, 0.5);
  98.  
  99. void Render() {
  100. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  101. glLoadIdentity();
  102. glTranslatef(0, 0, -10);
  103. glColor3d(1, 1, 1);
  104. glScalef(scale, scale, scale);
  105. a.print_circle();
  106. draw_line(-kx,-ky,kx,-ky);
  107. draw_line(kx,-ky,kx,ky);
  108. draw_line(kx,ky,-kx,ky);
  109. draw_line(-kx,ky,-kx,-ky);
  110. b.draw();
  111. a.update();
  112. glFlush();
  113. }
  114.  
  115. void update(int t) {
  116. Render();
  117. glutTimerFunc(msec, update, 0);
  118. }
  119.  
  120. void keyb(unsigned char key, int x, int y) {
  121. if(key == '+') {
  122. scale *= 1.1;
  123. }
  124. if(key == '-') {
  125. scale *= 0.9;
  126. }
  127. Render();
  128. }
  129.  
  130. void reshape(int w, int h) {
  131.  
  132. // предупредим деление на ноль
  133. // если окно сильно перетянуто будет
  134. if(h == 0)
  135. h = 1;
  136. float ratio = 1.0* w / h;
  137.  
  138. // используем матрицу проекции
  139. glMatrixMode(GL_PROJECTION);
  140.  
  141. // Reset матрицы
  142. glLoadIdentity();
  143.  
  144. // определяем окно просмотра
  145. glViewport(0, 0, w, h);
  146.  
  147. // установить корректную перспективу.
  148. gluPerspective(45,ratio,1,1000);
  149.  
  150. // вернуться к модели
  151. glMatrixMode(GL_MODELVIEW);
  152. }
  153.  
  154. int main(int argc, char **argv)
  155. {
  156. glutInit(&argc, argv);
  157. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  158. glutInitWindowSize(800, 600);
  159. glutCreateWindow("Physics Engine");
  160. glEnable(GL_DEPTH_TEST);
  161. glMatrixMode(GL_PROJECTION);
  162. glLoadIdentity();
  163. glMatrixMode(GL_MODELVIEW);
  164. glLoadIdentity();
  165. glClearColor(0, 0, 1.0, 0.0);
  166.  
  167. glutTimerFunc(50, update, 0);
  168. glutReshapeFunc(reshape);
  169. glutDisplayFunc(Render);
  170. glutKeyboardFunc(keyb);
  171. glutMainLoop();
  172. }
Add Comment
Please, Sign In to add comment