Advertisement
Ladies_Man

cg lab1 (solar system)

Mar 3rd, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.44 KB | None | 0 0
  1. // glfw_30.cpp : Defines the entry point for the console application.
  2. //  http://www.glfw.org/docs/latest/quick.html
  3.  
  4. #include "stdafx.h"
  5. #include <time.h>
  6. #define _USE_MATH_DEFINES // for C++
  7. #include <cmath>
  8.  
  9. #define SCREEN_WIDTH 1800
  10. #define SCREEN_HEIGHT 1000
  11.  
  12. #define CENTER_X SCREEN_WIDTH/2
  13. #define CENTER_Y SCREEN_HEIGHT/2
  14. //------------IDS--------------
  15. #define SUN_ID 0
  16. #define MERCURY_ID 1
  17. #define VENUS_ID 2
  18. #define EARTH_ID 3
  19. #define MARS_ID 4
  20. #define JUPITER_ID 5
  21. #define SATURN_ID 6
  22. #define URANUS_ID 7
  23. #define PLANEPTUNE_ID 8
  24. #define PLUTO_ID 9
  25. //----------SIZES--------------
  26. #define SUN_SIZE 70
  27. #define MERCURY_SIZE 8
  28. #define VENUS_SIZE 11
  29. #define EARTH_SIZE 11
  30. #define LUNA_SIZE 3
  31. #define MARS_SIZE 8
  32. #define JUPITER_SIZE 58
  33. #define JUPITER_MOON_SIZE_MIN 2
  34. #define JUPITER_MOON_SIZE_MAX 4
  35. #define SATURN_SIZE 37
  36. #define SATURN_MOON_SIZE_MIN 2
  37. #define SATURN_MOON_SIZE_MAX 4
  38. #define URANUS_SIZE 25
  39. #define PLANEPTUNE_SIZE 23
  40. #define PLUTO_SIZE 3
  41. //----------DISTS--------------
  42. #define SUN_DIST 0
  43. #define MERCURY_DIST 170
  44. #define VENUS_DIST 210
  45. #define EARTH_DIST 245
  46. #define LUNA_DIST (EARTH_SIZE + 8)  //relatively to earth
  47. #define MARS_DIST 290
  48. #define AST_BELT_START 340
  49. #define AST_BELT_STOP 355
  50. #define JUPITER_DIST 460
  51. #define JUPITER_MOON_DIST (JUPITER_SIZE + 8)    //relatively to saturn
  52. #define SATURN_DIST 600
  53. #define SATURN_MOON_DIST (SATURN_SIZE + 10) //relatively to saturn
  54. #define URANUS_DIST 685
  55. #define PLANEPTUNE_DIST 750
  56. #define PLUTO_DIST 795
  57. //---------OFFSETS------------
  58. #define SUN_OFFSET 0
  59. #define MERCURY_OFFSET 0
  60. #define VENUS_OFFSET 0
  61. #define EARTH_OFFSET 0
  62. #define MARS_OFFSET 0
  63. #define JUPITER_OFFSET 0
  64. #define SATURN_OFFSET 0
  65. #define URANUS_OFFSET 15
  66. #define PLANEPTUNE_OFFSET -20
  67. #define PLUTO_OFFSET 40
  68. //--------VELOCITIES----------
  69. #define MERCURY_SPEED 1.5
  70. #define VENUS_SPEED 3
  71. #define EARTH_SPEED 6.3
  72. #define MARS_SPEED 9
  73. #define JUPITER_SPEED 25
  74. #define SATURN_SPEED 34
  75. #define URANUS_SPEED 50
  76. #define PLANEPTUNE_SPEED 60
  77. #define PLUTO_SPEED 70
  78. //-----------MISC------------
  79. #define JUPITER_MOON_NUM 1
  80. #define SATURN_MOON_NUM 10
  81.  
  82. GLdouble A, B, C, D;
  83.  
  84. static void cursor_callback(GLFWwindow* window, double x, double y)
  85. {
  86.    
  87. }
  88.  
  89. static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
  90. {
  91.     if(button == GLFW_MOUSE_BUTTON_RIGHT)
  92.     {
  93.         if(action == GLFW_PRESS) glfwSetInputMode( window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  94.         if(action == GLFW_RELEASE) glfwSetInputMode( window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  95.     }
  96.  
  97.     if(button == GLFW_MOUSE_BUTTON_LEFT)
  98.     {
  99.        
  100.     }
  101. }
  102.  
  103. static void resize_callback(GLFWwindow* window, int width, int height)
  104. {
  105. //  windowWidth = width;
  106. //  windowHeight = height;
  107.  
  108.     glViewport(0, 0, width, height);
  109.     glMatrixMode(GL_PROJECTION);
  110.     glLoadIdentity();
  111.     glOrtho( 0.0, (GLdouble)width, 0.0, (GLdouble)height, -1, 1);  
  112.  
  113.     glMatrixMode(GL_MODELVIEW);
  114.     glLoadIdentity();
  115.        
  116.     A = width / 4.0;
  117.     B = 0.0;
  118.     C = D = height / 2.0;
  119.  
  120.     printf("Reshape occured\n");
  121. }
  122.  
  123. static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  124. {
  125.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  126.         glfwSetWindowShouldClose(window, GL_TRUE);
  127. }
  128.  
  129. static void error_callback(int error, const char* description)
  130. {
  131.     fputs(description, stderr);
  132. }
  133.  
  134. void draw_stars(int num)
  135. {
  136.     int i, rand_x, rand_y;
  137.  
  138.     glPointSize(1.0);
  139.     srand(time(NULL));
  140.     for (i = 0; i < num; i++) {
  141.         glBegin(GL_POINTS);
  142.         glColor3f(1, 1, 1);
  143.             rand_x = rand() % SCREEN_WIDTH;
  144.             rand_y = rand() % SCREEN_HEIGHT;
  145.             glVertex2i(rand_x, rand_y);
  146.         glEnd();
  147.     }
  148. }
  149.  
  150. void draw_space_object(int id, int dist, int offset, int size, GLfloat angle)
  151. {
  152.     GLfloat phi = 0, theta = 0;
  153.     int i = 0, moon_size = 5, moon_dist = 5, moon_speed = 10, start_pos;
  154.  
  155.     if (id == SUN_ID) {
  156.  
  157.     }
  158.     if (id == EARTH_ID) {
  159.         glPointSize(2.0);
  160.         glBegin(GL_POLYGON);    //luna
  161.         for (theta = 0; theta < 2 * M_PI; theta += 0.1) {
  162.             glColor3f(0.8, 0.8, 0.9);
  163.             glVertex2f(CENTER_X + offset + dist*cos(angle) - LUNA_DIST*sin(angle*EARTH_SPEED) + LUNA_SIZE*cos(theta), CENTER_Y + offset + dist*sin(angle) + LUNA_DIST*cos(angle*EARTH_SPEED) + LUNA_SIZE*sin(theta));
  164.         }
  165.         glEnd();
  166.     }
  167.  
  168.     srand(time(NULL));
  169.     if (id == JUPITER_ID) {
  170.         for (i = 0; i < JUPITER_MOON_NUM; i++) {
  171.             glPointSize(1.0);
  172.             glBegin(GL_POLYGON);    //jupiter rings
  173.             for (theta = 0; theta < 2 * M_PI; theta += 0.1) {
  174.                 glColor3f(1, 1, 1);
  175.                 moon_size = rand() % JUPITER_MOON_SIZE_MAX + JUPITER_MOON_SIZE_MIN;
  176.                 moon_dist = rand() % JUPITER_MOON_NUM * 10 + JUPITER_MOON_DIST + i * 3;
  177.                 moon_speed = 10;
  178.                 glVertex2f(CENTER_X + offset + dist*cos(angle) + (JUPITER_MOON_DIST + i * 3)*cos(angle*(moon_speed + i) + i) + moon_size*cos(theta), CENTER_Y + offset + dist*sin(angle) + (JUPITER_MOON_DIST + i * 3)*sin(angle*(moon_speed + i) + i) + moon_size*sin(theta));
  179.             }
  180.             glEnd();
  181.         }
  182.     }
  183.     if (id == SATURN_ID) {
  184.         glPointSize(2.0);
  185.         glBegin(GL_POLYGON);
  186.             glColor3f(1, 0, 0);
  187.             glVertex2f(CENTER_X + offset + dist*1.5*cos(angle) + size*cos(phi), CENTER_Y + offset + dist*1.5*sin(angle) + size*sin(phi));
  188.         glEnd();
  189.         for (i = 0; i < SATURN_MOON_NUM; i++) {
  190.             glPointSize(1.0);
  191.             glBegin(GL_POLYGON);    //saturn rings
  192.             for (theta = 0; theta < 2 * M_PI; theta += 0.1) {
  193.                 glColor3f(1, 1, 1);
  194.                 moon_size = rand() % SATURN_MOON_SIZE_MAX + SATURN_MOON_SIZE_MIN;
  195.                 moon_dist = rand() % SATURN_MOON_NUM * 10 + SATURN_MOON_DIST + i * 3;
  196.                 moon_speed = 15;
  197.                 glVertex2f(CENTER_X + offset + dist*cos(angle) + (SATURN_MOON_DIST + i * 3)*cos(angle*(moon_speed + i) + i) + moon_size*cos(theta), CENTER_Y + offset + dist*sin(angle) + (SATURN_MOON_DIST + i * 3)*sin(angle*(moon_speed + i) + i) + moon_size*sin(theta));
  198.             }
  199.             glEnd();
  200.         }
  201.     }
  202.     glPointSize(2.0);
  203.     glBegin(GL_POLYGON);
  204.     for (phi = 0; phi < 2*M_PI; phi += 0.1) {
  205.         //glColor3f(1, 1, 1);
  206.         if (id == SUN_ID) glColor3f(253, 160, 0.2);
  207.         if (id == MERCURY_ID) glColor3f(0.5, 0.5, 0.5);
  208.         if (id == VENUS_ID) glColor3f(1, 0.7, 0);
  209.         if (id == EARTH_ID) glColor3f(0, 0.4, 1);
  210.         if (id == MARS_ID) glColor3f(0.5, 0, 0);
  211.         if (id == JUPITER_ID) glColor3f(1, 0.6, 0.3);
  212.         if (id == SATURN_ID) glColor3f(1, 0.5, 0.2);
  213.         if (id == URANUS_ID) glColor3f(0, 0.7, 0.7);
  214.         if (id == PLANEPTUNE_ID) glColor3f(0, 0, 0.6);
  215.         if (id == PLUTO_ID) glColor3f(1, 0.7, 0);
  216.         glVertex2f(CENTER_X + offset + dist*cos(angle) + size*cos(phi), CENTER_Y + offset + dist*sin(angle) + size*sin(phi));
  217.     }
  218.     glEnd();
  219. }
  220.  
  221.  
  222. void drawPoint(int x, int y)
  223. {
  224.     glPointSize(1.0);
  225.     glBegin(GL_POINTS);
  226.         glColor3f(0.1f, 0.4f, 0.4f);
  227.         glVertex2i(x,y);
  228.     glEnd();
  229. }
  230.  
  231. typedef struct GLintPoint
  232. {
  233.     GLint   x;
  234.     GLint   y;
  235. } GLintPoint;
  236.  
  237. void serpinsky_drawing(void)
  238. {
  239.     GLintPoint T[3]= {{10,10},{210,310},{410,10}};
  240.  
  241.     int index = (int)floor((3.0 * rand()) / RAND_MAX);
  242.     // 0, 1 or 2 equally likely
  243.     // 0, 1 или 2 равновероятны
  244.     GLintPoint point = T[index];
  245.     // initial point
  246.     // начальная точка
  247.     drawPoint(point.x, point.y);
  248.     // draw initial point
  249.     // рисуем начальную точку
  250.     for(int i = 0; i < 100000; i++)
  251.         // draw 1000 dots
  252.         // рисуем 1000 точек
  253.     {
  254.         index = (int)floor((3.0 * rand()) / RAND_MAX);
  255.         point.x = (point.x + T[index].x) / 2;
  256.         point.y = (point.y + T[index].y) / 2;
  257.         drawPoint(point.x,point.y);
  258.     }
  259. }
  260.  
  261. void function_drawing(void)
  262. {
  263.     glClear(GL_COLOR_BUFFER_BIT);
  264.    
  265.     glEnable(GL_LINE_STIPPLE);
  266.     glColor3f(1.0f,0.3f,0.3f);
  267.     //glBegin(GL_LINE_STRIP);
  268.     //glBegin(GL_LINE_LOOP);
  269.     glBegin(GL_POLYGON);
  270.         for(GLdouble x = 0; x < 1.0; x += 0.05)
  271.         {
  272.             GLdouble func = exp(-x) * cos(2 * 3.14159265 * x);
  273.             glVertex2d(A * x + B, C * func + D);
  274.         }
  275.     glEnd();
  276.     glDisable(GL_LINE_STIPPLE);
  277.     //glBegin(GL_LINE_STRIP);
  278.     glBegin(GL_LINE_LOOP);
  279.         for(GLdouble x = 1.0; x < 4.0; x += 0.05)
  280.         {
  281.             GLdouble func = exp(-x) * cos(2 * 3.14159265 * x);
  282.             glVertex2d(A * x + B, C * func + D);
  283.         }
  284.     glEnd();
  285.  
  286.  
  287.     glPointSize(4.0);
  288.     glColor3b((GLbyte)20,(GLbyte)26,(GLbyte)250);
  289.     glBegin(GL_POINTS);
  290.         for(GLdouble x = 0; x < 4.0; x += 0.05)
  291.         {
  292.             GLdouble func = exp(-x) * cos(2 * 3.14159265 * x);
  293.             glVertex2d(A * x + B, C * func + D);
  294.         }
  295.     glEnd();
  296. }
  297.  
  298. void test_drawing_A(void)
  299. {
  300.     glClearColor(1.0, 1.0, 1.0, 0.0);
  301.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  302.     glColor3f(1.0,0.0,0.0);
  303.  
  304.     glPointSize(10);
  305.  
  306.     glBegin(GL_POINTS);
  307.         glVertex2i(200,200);
  308.         glVertex2i(300,300);
  309.     glEnd();
  310.  
  311.     glBegin(GL_QUADS);
  312.     {
  313.         glVertex2i(200, 200);
  314.         glVertex2i(100, 200);
  315.         glVertex2i(100, 100);
  316.         glVertex2i(200, 100);
  317.     }
  318.     glEnd();
  319. }
  320.  
  321. void draw(void)
  322. {
  323.     //test_drawing();
  324.     //test_drawing_A();
  325.     //serpinsky_drawing();
  326.     //function_drawing();  
  327. }
  328.  
  329. int main(int argc, _TCHAR* argv[])
  330. {
  331.     // initialise GLFW
  332.     if(!glfwInit())
  333.     {
  334.         printf("glfwInit failed\n");
  335.         return -1;
  336.     }
  337.  
  338.     glfwSetErrorCallback(error_callback);
  339.  
  340.     GLFWwindow* window;
  341.    
  342. //  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  343. //  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  344.     //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
  345.     window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test app", NULL, NULL);
  346.     if (window == NULL)
  347.     {
  348.         printf("glfwOpenWindow failed.\n");
  349.         glfwTerminate();
  350.         return -2;
  351.     }
  352.  
  353.     int attrib;
  354.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  355.     attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  356.     attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
  357.  
  358.     glfwMakeContextCurrent(window);
  359.  
  360.     glfwSetKeyCallback(window, keyboard_callback);
  361.     glfwSetFramebufferSizeCallback(window, resize_callback);
  362.     glfwSetMouseButtonCallback(window, mouse_callback);
  363.     glfwSetCursorPosCallback(window, cursor_callback);
  364.    
  365.     resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
  366.     GLfloat phi = 0, center = 0;
  367.     int i = 1;
  368.     while (!glfwWindowShouldClose(window))
  369.     {
  370.         //glClearColor(0.6, 0.37, 0.6, 0.0);
  371.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  372.        
  373.  
  374.         //use arrow button for actions
  375.  
  376.         if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
  377.             i += 5;
  378.         }
  379.         if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
  380.             i -= 5;
  381.         }
  382.         if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
  383.             phi += 0.3;
  384.             draw_stars(1000);
  385.         }
  386.         if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
  387.             phi -= 0.3;
  388.             draw_stars(1000);
  389.         }
  390.         draw_space_object(SUN_ID, SUN_DIST, SUN_OFFSET, SUN_SIZE + i, phi + M_PI);
  391.         draw_space_object(MERCURY_ID, MERCURY_DIST + i, MERCURY_OFFSET, MERCURY_SIZE, phi / MERCURY_SPEED + M_PI);
  392.         draw_space_object(VENUS_ID, VENUS_DIST + i, VENUS_OFFSET, VENUS_SIZE, phi / VENUS_SPEED + M_PI);
  393.         draw_space_object(EARTH_ID, EARTH_DIST + i, EARTH_OFFSET, EARTH_SIZE, phi / EARTH_SPEED + M_PI);
  394.         draw_space_object(MARS_ID, MARS_DIST + i, MARS_OFFSET, MARS_SIZE, phi / MARS_SPEED + M_PI);
  395.         //draw_asteroid_belt
  396.         draw_space_object(JUPITER_ID, JUPITER_DIST + i, JUPITER_OFFSET, JUPITER_SIZE, phi / JUPITER_SPEED);
  397.         draw_space_object(SATURN_ID, SATURN_DIST + i, SATURN_OFFSET, SATURN_SIZE, phi / SATURN_SPEED + M_PI / 10);
  398.         draw_space_object(URANUS_ID, URANUS_DIST + i, URANUS_OFFSET, URANUS_SIZE, phi / URANUS_SPEED);
  399.         draw_space_object(PLANEPTUNE_ID, PLANEPTUNE_DIST + i, PLANEPTUNE_OFFSET, PLANEPTUNE_SIZE, phi / PLANEPTUNE_SPEED - M_PI / 10);
  400.         draw_space_object(PLUTO_ID, PLUTO_DIST + i, PLUTO_OFFSET, PLUTO_SIZE, phi / PLUTO_SPEED);
  401.  
  402.         glfwSwapBuffers(window);
  403.         //glfwPollEvents();
  404.         glfwWaitEvents();
  405.     }
  406.  
  407.     glfwDestroyWindow(window);
  408.  
  409.     // clean up and exit
  410.     glfwTerminate();
  411.  
  412.     return 0;
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement