Advertisement
maxim_shlyahtin

tmp_main

Mar 27th, 2024 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.59 KB | None | 0 0
  1. #ifndef MAIN
  2. #define MAIN
  3. #ifdef _WIN32
  4. #include <Windows.h>
  5. #include <GL/GL.h>
  6. #include <GL/GLU.h>
  7. #endif
  8. #ifndef _WIN32
  9. #include <GL/gl.h>
  10. #include <GL/glu.h>
  11. #endif
  12.  
  13. #include <chrono>
  14. #include "ode.cpp"
  15. #include "thread_pool.cpp"
  16. #define _USE_MATH_DEFINES
  17.  
  18. //Choose whether to use SDL1 or SDL2
  19.  
  20.  
  21. #include <SDL.h>
  22. #include <SDL_opengl.h>
  23. #pragma comment(lib,"SDL2.lib")
  24. #pragma comment(lib,"SDL2main.lib")
  25. static SDL_Window* window;
  26. static SDL_Renderer* renderer;
  27. #pragma comment(lib,"opengl32.lib")
  28. #pragma comment(lib,"glu32.lib")
  29.  
  30. static int const screen_size[2] = { 1000, 800 };
  31.  
  32. using glf = GLfloat;
  33. using point3d = std::array<glf, N>;
  34.  
  35. double cameraX = 0.0;
  36. double cameraY = 0.0;
  37. const double cameraZ = 20.0;
  38. const double R = 10.0;
  39. double t;
  40. const int number_of_points = 100000;
  41. double dt = 20.0 / 100.0;
  42.  
  43. // переменные для создания потоков
  44. const int number_of_cores = std::thread::hardware_concurrency();
  45. std::vector<RungeKutta> attractors;
  46.  
  47. std::array<point3d, number_of_points> points;
  48. ThreadPool thread_pool(number_of_cores);
  49.  
  50. int b_val_index = 0;
  51. // красивые формы аттрактора получаются при данных коэффициентах
  52. const int len = 5;
  53. std::array<double, len> b_values = { 0.11, 0.15, 0.215, 0.265, 0.32899};
  54.  
  55. double red = 1.0f;
  56. double blue = 1.0f;
  57. double green = 1.0f;
  58.  
  59. double step_red = 0.05;
  60. double step_blue = 0.025;
  61. double step_green = 0.016;
  62.  
  63. void process(RungeKutta& attr, int start, int end, double dt) {
  64.     for (int i = start; i < end; ++i) {
  65.         attr.SetInit({ points[i].at(0), points[i].at(1), points[i].at(2) });
  66.         attr.NextStep(dt);
  67.         points[i] = { static_cast<glf>(attr.getX(0)), static_cast<glf>(attr.getX(1)), static_cast<glf>(attr.getX(2)) };
  68.     }
  69. }
  70.  
  71. void inittializeAttractors() {
  72.     for (size_t i = 0; i < number_of_cores; ++i)
  73.         attractors.push_back(RungeKutta());
  74. }
  75.  
  76.  
  77. Uint32 callback(Uint32 interval, void* name) {
  78.     //Задаем с период вращения камеры
  79.     std::chrono::time_point<std::chrono::high_resolution_clock> tp = std::chrono::high_resolution_clock::now();
  80.     std::chrono::duration<double, std::micro> dur = tp.time_since_epoch();
  81.     t = dur.count();
  82.  
  83.     cameraX = R * cos((2 * M_PI) / (60.0 * 1000.0) * (t / 1000.0));
  84.     cameraY = R * sin((2 * M_PI) / (60.0 * 1000.0) * (t / 1000.0));
  85.     for (size_t i = 0; i < number_of_cores; ++i) {
  86.         if (i + 1 < number_of_cores) {
  87.             thread_pool.enqueue([i] {
  88.                 process(std::ref(attractors[i]), i * number_of_points / number_of_cores,
  89.                 (i + 1) * number_of_points / number_of_cores, dt);
  90.                 });
  91.         }
  92.         else {
  93.             thread_pool.enqueue([i] {process(std::ref(attractors[i]), (i - 1) * number_of_points / number_of_cores,
  94.                 i * number_of_points / number_of_cores, dt);});
  95.         }
  96.         std::cout << i * number_of_points / number_of_cores << " " << (i + 1) * number_of_points / number_of_cores << '\n';
  97.     }
  98.     return interval;
  99. }
  100.  
  101. Uint32 colorChange(Uint32 interval, void* name) {
  102.     red += step_red;
  103.     blue += step_blue;
  104.     green += step_green;
  105.     step_red = (red >= 1 || red <= 0) ? -step_red : step_red;
  106.     step_blue = (blue >= 1 || blue <= 0) ? -step_blue : step_blue;
  107.     step_green = (green >= 1 || green <= 0) ? -step_green : step_green;
  108.     return interval;
  109. }
  110.  
  111. static bool get_input(void) {
  112.     SDL_Event event;
  113.     while (SDL_PollEvent(&event)) {
  114.         switch (event.type) {
  115.         case SDL_QUIT: return false; //The little X in the window got pressed
  116.         case SDL_KEYDOWN:
  117.             if (event.key.keysym.sym == SDLK_ESCAPE) {
  118.                 return false;
  119.             }
  120.             if (event.key.keysym.sym == SDLK_w) {
  121.                 b_val_index = (b_val_index + 1) % len;
  122.                
  123.             }
  124.             if (event.key.keysym.sym == SDLK_s) {
  125.                 b_val_index = (b_val_index - 1) % len >= 0 ? (b_val_index - 1) % len : len + (b_val_index - 1) % len;
  126.  
  127.             }
  128.             std::cout << b_values[b_val_index] << '\n';
  129.             break;
  130.         }
  131.     }
  132.     return true;
  133. }
  134. static void draw(void) {
  135.     //Clear the screen's color and depth (default color is black, but can change with glClearColor(...))
  136.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  137.  
  138.     //Drawing to an area starting at the bottom left, screen_size[0] wide, and screen_size[1] high.
  139.     glViewport(0, 0, screen_size[0], screen_size[1]);
  140.     //OpenGL is a state machine.  Tell it that future commands changing the matrix are to change OpenGL's projection matrix
  141.     glMatrixMode(GL_PROJECTION);
  142.     //Reset the projection matrix
  143.     glLoadIdentity();
  144.     //Multiply a perspective projection matrix into OpenGL's projection matrix
  145.     gluPerspective(45.0, (double)(screen_size[0]) / (double)(screen_size[1]), 0.1, 100.0);
  146.  
  147.     //Tell OpenGL that future commands changing the matrix are to change the modelview matrix
  148.     glMatrixMode(GL_MODELVIEW);
  149.     //Reset the modelview matrix
  150.     glLoadIdentity();
  151.     //Multiply OpenGL's modelview matrix with a transform matrix that simulates a camera at (2,3,4) looking towards the location (0,0,0) with up defined to be (0,1,0)
  152.  
  153.     gluLookAt(cameraX, cameraY, cameraZ, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  154.     //gluLookAt(-20.0f, -15.0f, 20.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  155.  
  156.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  157.  
  158.     glColor3f(red, blue, green); // Set the color to white
  159.     //int ind = 6;
  160.     //glColor3f(colors[ind].at(0), colors[ind].at(1), colors[ind].at(2)); // Set the color to white
  161.     glBegin(GL_POINTS);
  162.  
  163.     for (size_t i = 0; i < number_of_points; i++) {
  164.         glVertex3f(points[i].at(0), points[i].at(1), points[i].at(2));
  165.     }
  166.  
  167.     glEnd();
  168.     //OpenGL works best double-buffered.  SDL automatically sets that up for us.  This will draw what we have
  169.     //  just drawn to the screen so that we can see it.
  170.     SDL_GL_SwapWindow(window);
  171. }
  172.  
  173. int main(int argc, char* argv[]) {
  174.     inittializeAttractors();
  175.     //считываем заготовленные точки из файла
  176.     std::ifstream in("prep.txt");
  177.     if (in.is_open()) {
  178.         glf x, y, z;
  179.         int check = 0;
  180.         while (in >> x >> y >> z && check < number_of_points) {
  181.             points[check] = { x, y, z };
  182.             check++;
  183.         }
  184.     }
  185.     in.close();
  186.     //Initialize everything, but don't catch fatal signals; give them to the OS.
  187.     SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE);
  188.  
  189.  
  190.     //Creates the window
  191.     window = SDL_CreateWindow("Thomas' cyclically symmetric attractor", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_size[0], screen_size[1], SDL_WINDOW_OPENGL);
  192.     //Create an OpenGL context.  In SDL 1, this was done automatically.
  193.     SDL_GLContext context = SDL_GL_CreateContext(window);
  194.  
  195.     //We now have an OpenGL context, and can call OpenGL functions.
  196.  
  197.     //Objects need to test each other to see which one is in front.  If you don't do this, you'll "see through" things!
  198.     glEnable(GL_DEPTH_TEST);
  199.  
  200.     SDL_TimerID timerID = SDL_AddTimer(20, callback, const_cast<char*>("SDL"));
  201.     SDL_TimerID timerID_color = SDL_AddTimer(1000, colorChange, const_cast<char*>("SDL"));
  202.  
  203.  
  204.     //Main application loop
  205.     while (true) {
  206.         if (!get_input()) break;
  207.         draw();
  208.     }
  209.  
  210.     SDL_RemoveTimer(timerID);
  211.     SDL_RemoveTimer(timerID_color);
  212.  
  213.     //Clean up
  214.     SDL_GL_DeleteContext(context);
  215.     SDL_DestroyWindow(window);
  216.     SDL_Quit();
  217.  
  218.     //Return success; program exits
  219.     return 0;
  220. }
  221.  
  222. #endif // !MAIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement