Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h> //Don't worry about this; it's needed to make GL.h work properly on Windows only (and then, only sometimes).
- //#include <GL/glew.h>
- #include <GL/GL.h>
- #include <GL/GLU.h>
- #include "ode.cpp"
- //#include <glm.hpp>
- #include <chrono>
- #include <iomanip>
- #define _USE_MATH_DEFINES
- //Choose whether to use SDL1 or SDL2
- #include <cstdio>
- #include <SDL.h>
- #include <SDL_opengl.h>
- #pragma comment(lib,"SDL2.lib")
- #pragma comment(lib,"SDL2main.lib")
- static SDL_Window* window;
- static SDL_Renderer* renderer;
- #pragma comment(lib,"opengl32.lib")
- #pragma comment(lib,"glu32.lib")
- static int const screen_size[2] = { 800, 600 };
- using glf = GLfloat;
- using point3d = std::vector<glf>;
- double camX = 0.0;
- double camY = 0.0;
- double R = 10.0;
- double t;
- size_t dist = 100;
- TA attractor(3);
- std::vector<double> init_state = { 0, 1, 0 };
- std::vector<point3d> points;
- std::vector<point3d> points_reverse;
- void set_attr(TA& attr, std::vector<double> init) {
- attr.SetInit(0, init);
- }
- Uint32 callback(Uint32 interval, void* name) {
- double dt = interval / 100.0;
- // Get the current time point
- std::chrono::time_point<std::chrono::high_resolution_clock> tp = std::chrono::high_resolution_clock::now();
- // Convert the time point to a duration in microseconds (or any other unit you prefer)
- std::chrono::duration<double, std::micro> dur = tp.time_since_epoch();
- // Get the count of the duration as a double
- t = dur.count();
- camX = R * cos((2 * M_PI) / (60.0 * 1000.0) * (t / 1000.0));
- camY = R * sin((2 * M_PI) / (60.0 * 1000.0) * (t / 1000.0));
- glf x = static_cast<glf>(attractor.getX(0));
- glf y = static_cast<glf>(attractor.getX(1));
- glf z = static_cast<glf>(attractor.getX(2));
- attractor.NextStep(dt);
- return interval;
- }
- static bool get_input(void) {
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_QUIT: return false; //The little X in the window got pressed
- case SDL_KEYDOWN:
- if (event.key.keysym.sym == SDLK_ESCAPE) {
- return false;
- }
- break;
- }
- }
- return true;
- }
- static void draw(void) {
- //Clear the screen's color and depth (default color is black, but can change with glClearColor(...))
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //Drawing to an area starting at the bottom left, screen_size[0] wide, and screen_size[1] high.
- glViewport(0, 0, screen_size[0], screen_size[1]);
- //OpenGL is a state machine. Tell it that future commands changing the matrix are to change OpenGL's projection matrix
- glMatrixMode(GL_PROJECTION);
- //Reset the projection matrix
- glLoadIdentity();
- //Multiply a perspective projection matrix into OpenGL's projection matrix
- gluPerspective(45.0, (double)(screen_size[0]) / (double)(screen_size[1]), 0.1, 100.0);
- //Tell OpenGL that future commands changing the matrix are to change the modelview matrix
- glMatrixMode(GL_MODELVIEW);
- //Reset the modelview matrix
- glLoadIdentity();
- //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)
- glf cameraX = static_cast<glf>(camX);
- glf cameraY = static_cast<glf>(camY);
- gluLookAt(cameraX, cameraY, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
- //gluLookAt(-20.0f, -15.0f, 20.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
- //Begin drawing triangles. Every subsequent triplet of vertices will be interpreted as a single triangle.
- // OpenGL's default color is white (1.0,1.0,1.0), so that's what color the triangle will be.
- //glColor3f(1.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glf x = static_cast<glf>(attractor.getX(0));
- glf y = static_cast<glf>(attractor.getX(1));
- glf z = static_cast<glf>(attractor.getX(2));
- points.push_back({ x, y, z });
- points_reverse.push_back({ x, y, z });
- //points_reverse.insert(points_reverse.begin(), {x, y, z});
- std::reverse(points_reverse.begin(), points_reverse.end());
- glColor3f(1.0f, 1.0f, 1.0f); // Set the color to white
- glBegin(GL_POINTS);
- size_t number_of_points = points.size() - 1 > 0 ? points.size() - 1 : 1;
- //std::cout << number_of_points << " time taken (in seconds): " << std::fixed << t / 1000000.0 << '\n';
- for (size_t i = 0; i < number_of_points; i++) {
- if ((number_of_points - i) % dist == 0) {
- //tmp.push_back(number_of_points - i);
- glVertex3f(points[i].at(0), points[i].at(1), points[i].at(2));
- }
- }
- glEnd();
- SDL_GL_SwapWindow(window);
- //OpenGL works best double-buffered. SDL automatically sets that up for us. This will draw what we have
- // just drawn to the screen so that we can see it.
- SDL_GL_SwapWindow(window);
- //OpenGL works best double-buffered. SDL automatically sets that up for us. This will draw what we have
- // just drawn to the screen so that we can see it.
- SDL_GL_SwapWindow(window);
- }
- int main(int argc, char* argv[]) {
- set_attr(attractor, init_state);
- //Initialize everything, but don't catch fatal signals; give them to the OS.
- SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE);
- //Creates the window
- window = SDL_CreateWindow("SDL and OpenGL example - Ian Mallett", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_size[0], screen_size[1], SDL_WINDOW_OPENGL);
- //Create an OpenGL context. In SDL 1, this was done automatically.
- SDL_GLContext context = SDL_GL_CreateContext(window);
- //We now have an OpenGL context, and can call OpenGL functions.
- //Objects need to test each other to see which one is in front. If you don't do this, you'll "see through" things!
- glEnable(GL_DEPTH_TEST);
- SDL_TimerID timerID = SDL_AddTimer(20, callback, const_cast<char*>("SDL"));
- //Main application loop
- int c = 0;
- auto start = std::chrono::steady_clock::now();
- while (true) {
- if (!get_input()) break;
- draw();
- c++;
- if (c == 20000) {
- auto end = std::chrono::steady_clock::now();
- std::cout << "Elapsed time in seconds: "
- << std::chrono::duration_cast<std::chrono::seconds>(end - start).count()
- << " sec";
- }
- }
- //TA::Test();
- SDL_RemoveTimer(timerID);
- //Clean up
- SDL_GL_DeleteContext(context);
- SDL_DestroyWindow(window);
- SDL_Quit();
- //Return success; program exits
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement