Advertisement
maxim_shlyahtin

tmp

Mar 16th, 2024 (edited)
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.01 KB | None | 0 0
  1. #include <Windows.h> //Don't worry about this; it's needed to make GL.h work properly on Windows only (and then, only sometimes).
  2. //#include <GL/glew.h>
  3. #include <GL/GL.h>
  4. #include <GL/GLU.h>
  5. #include "ode.cpp"
  6. //#include <glm.hpp>
  7. #include <chrono>
  8. #include <iomanip>
  9. #include <thread>
  10. #include <mutex>
  11.  
  12. #define _USE_MATH_DEFINES
  13.  
  14. //Choose whether to use SDL1 or SDL2
  15.  
  16. #include <cstdio>
  17. #include <SDL.h>
  18. #include <SDL_opengl.h>
  19. #pragma comment(lib,"SDL2.lib")
  20. #pragma comment(lib,"SDL2main.lib")
  21. static SDL_Window* window;
  22. static SDL_Renderer* renderer;
  23. #pragma comment(lib,"opengl32.lib")
  24. #pragma comment(lib,"glu32.lib")
  25.  
  26.  
  27. static int const screen_size[2] = { 800, 600 };
  28.  
  29. using glf = GLfloat;
  30. using point3d = std::vector<glf>;
  31.  
  32. double camX = 0.0;
  33. double camY = 0.0;
  34. double R = 10.0;
  35. double t;
  36. double h = 0.002;
  37. size_t dist = 100;
  38.  
  39. TA attractor(3);
  40. std::vector<double> init_state = { 0, 1, 0 };
  41. std::vector<point3d> points;
  42. std::vector<point3d> buffer;
  43.  
  44. void set_attr(TA& attr, std::vector<double> init) {
  45.     attr.SetInit(0, init);
  46. }
  47.  
  48.  
  49.  
  50. Uint32 callback(Uint32 interval, void* name) {
  51.     double dt = interval / 100.0;
  52.     // Get the current time point
  53.     std::chrono::time_point<std::chrono::high_resolution_clock> tp = std::chrono::high_resolution_clock::now();
  54.  
  55.     // Convert the time point to a duration in microseconds (or any other unit you prefer)
  56.     std::chrono::duration<double, std::micro> dur = tp.time_since_epoch();
  57.  
  58.     // Get the count of the duration as a double
  59.     t = dur.count();
  60.  
  61.     camX = R * cos((2 * M_PI) / (60.0 * 1000.0) * (t / 1000.0));
  62.     camY = R * sin((2 * M_PI) / (60.0 * 1000.0) * (t / 1000.0));
  63.     attractor.NextStep(dt);
  64.     return interval;
  65. }
  66.  
  67. TA attractor2(3) ;
  68. size_t count = 0;
  69.  
  70. Uint32 CalcNextBfunc(Uint32 interval, void* name) {
  71.     double dt = interval / 100.0;
  72.     attractor2.NextStep(dt);
  73.     return interval;
  74. }
  75.  
  76. static bool get_input(void) {
  77.     SDL_Event event;
  78.     while (SDL_PollEvent(&event)) {
  79.         switch (event.type) {
  80.         case SDL_QUIT: return false; //The little X in the window got pressed
  81.         case SDL_KEYDOWN:
  82.             if (event.key.keysym.sym == SDLK_ESCAPE) {
  83.                 return false;
  84.             }
  85.             break;
  86.         }
  87.     }
  88.     return true;
  89. }
  90. static void draw(void) {
  91.     //Clear the screen's color and depth (default color is black, but can change with glClearColor(...))
  92.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93.  
  94.     //Drawing to an area starting at the bottom left, screen_size[0] wide, and screen_size[1] high.
  95.     glViewport(0, 0, screen_size[0], screen_size[1]);
  96.     //OpenGL is a state machine.  Tell it that future commands changing the matrix are to change OpenGL's projection matrix
  97.     glMatrixMode(GL_PROJECTION);
  98.     //Reset the projection matrix
  99.     glLoadIdentity();
  100.     //Multiply a perspective projection matrix into OpenGL's projection matrix
  101.     gluPerspective(45.0, (double)(screen_size[0]) / (double)(screen_size[1]), 0.1, 100.0);
  102.  
  103.     //Tell OpenGL that future commands changing the matrix are to change the modelview matrix
  104.     glMatrixMode(GL_MODELVIEW);
  105.     //Reset the modelview matrix
  106.     glLoadIdentity();
  107.     //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)
  108.     glf cameraX = static_cast<glf>(camX);
  109.     glf cameraY = static_cast<glf>(camY);
  110.     gluLookAt(cameraX, cameraY, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  111.     //gluLookAt(-20.0f, -15.0f, 20.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  112.  
  113.     //Begin drawing triangles.  Every subsequent triplet of vertices will be interpreted as a single triangle.
  114.     //  OpenGL's default color is white (1.0,1.0,1.0), so that's what color the triangle will be.
  115.     //glColor3f(1.0f, 1.0f, 1.0f);
  116.  
  117.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  118.  
  119.     glf x1 = static_cast<glf>(attractor.getX(0));
  120.     glf y1 = static_cast<glf>(attractor.getX(1));
  121.     glf z1 = static_cast<glf>(attractor.getX(2));
  122.     points.push_back({ x1, y1, z1 });
  123.     glf x2 = static_cast<glf>(attractor2.getX(0));
  124.     glf y2 = static_cast<glf>(attractor2.getX(1));
  125.     glf z2 = static_cast<glf>(attractor2.getX(2));
  126.     buffer.push_back({ x2, y2, z2 });
  127.     /*std::ifstream in("prep.h");
  128.     if (in.is_open()) {
  129.         glf x, y, z;
  130.         int check = 0;
  131.         while (in >> x >> y >> z && check < 100000) {
  132.             points.push_back({ x, y, z });
  133.             check++;
  134.         }
  135.     }
  136.     in.close();*/
  137.     //points_reverse.push_back({ x, y, z });
  138.     //points_reverse.insert(points_reverse.begin(), {x, y, z});
  139.     //std::reverse(points_reverse.begin(), points_reverse.end());
  140.     glColor3f(1.0f, 1.0f, 1.0f); // Set the color to white
  141.     glBegin(GL_POINTS);
  142.     size_t number_of_points = points.size() - 1 > 0 ? points.size() - 1 : 1;
  143.     //std::cout << number_of_points << " time taken (in seconds): " << std::fixed << t / 1000000.0  << '\n';
  144.     for (size_t i = 0; i < number_of_points; i++) {
  145.         if ((number_of_points - i) % dist == 0) {
  146.             //tmp.push_back(number_of_points - i);
  147.             glVertex3f(points[i].at(0), points[i].at(1), points[i].at(2));
  148.         }
  149.         //glVertex3f(points[i].at(0), points[i].at(1), points[i].at(2));
  150.     }
  151.  
  152.     glEnd();
  153.     SDL_GL_SwapWindow(window);
  154.  
  155.     //OpenGL works best double-buffered.  SDL automatically sets that up for us.  This will draw what we have
  156.     //  just drawn to the screen so that we can see it.
  157.     SDL_GL_SwapWindow(window);
  158.     //OpenGL works best double-buffered.  SDL automatically sets that up for us.  This will draw what we have
  159.     //  just drawn to the screen so that we can see it.
  160.     SDL_GL_SwapWindow(window);
  161. }
  162.  
  163. int main(int argc, char* argv[]) {
  164.     attractor2.b += 0.01;
  165.     set_attr(attractor, init_state);
  166.     set_attr(attractor2, init_state);
  167.     //Initialize everything, but don't catch fatal signals; give them to the OS.
  168.     SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE);
  169.  
  170.  
  171.     //Creates the window
  172.     window = SDL_CreateWindow("SDL and OpenGL example - Ian Mallett", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_size[0], screen_size[1], SDL_WINDOW_OPENGL);
  173.     //Create an OpenGL context.  In SDL 1, this was done automatically.
  174.     SDL_GLContext context = SDL_GL_CreateContext(window);
  175.  
  176.     //We now have an OpenGL context, and can call OpenGL functions.
  177.  
  178.     //Objects need to test each other to see which one is in front.  If you don't do this, you'll "see through" things!
  179.     glEnable(GL_DEPTH_TEST);
  180.  
  181.     SDL_TimerID timerID = SDL_AddTimer(20, callback, const_cast<char*>("SDL"));
  182.     //std::thread CalculateNextB(&CalcNextBfunc, attractor2);
  183.     //SDL_TimerID timerID1 = SDL_AddTimer(20, CalcNextBfunc, const_cast<char*>("SDL"));
  184.  
  185.     //Main application loop
  186.     int c = 0;
  187.     auto start = std::chrono::steady_clock::now();
  188.     while (true) {
  189.         if (!get_input()) break;
  190.         draw();
  191.         c++;
  192.         //std::cout << c << '\n';
  193.         //if (c % 500 == 0) {
  194.         //    std::cout << c << '\n';
  195.         //    /*for (const auto& i : buffer) {
  196.         //        for (const auto& j : i) {
  197.         //            std::cout << j << " ";
  198.         //        }
  199.         //        std::cout << '\n';
  200.         //    }*/
  201.         //    attractor = attractor2;
  202.         //    points = buffer;
  203.         //    /*buffer.clear();
  204.         //    TA attr(3);
  205.         //    attractor2 = attr;*/
  206.         //    attractor2.b = attractor.b + 0.01;
  207.         //}
  208.         if (c == 20000) {
  209.             auto end = std::chrono::steady_clock::now();
  210.             std::cout << "Elapsed time in seconds: "
  211.                 << std::chrono::duration_cast<std::chrono::seconds>(end - start).count()
  212.                 << " sec";
  213.         }
  214.     }
  215.  
  216.     //TA::Test();
  217.  
  218.     SDL_RemoveTimer(timerID);
  219.     //SDL_RemoveTimer(timerID1);
  220.  
  221.     //Clean up
  222.     //CalculateNextB.join();
  223.     SDL_GL_DeleteContext(context);
  224.     SDL_DestroyWindow(window);
  225.     SDL_Quit();
  226.  
  227.     //Return success; program exits
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement