Advertisement
maxim_shlyahtin

new_main_opengl

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