From 941d8f96d8decdfbce13418c5dde26816f0ca6d5 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Mon, 7 Sep 2020 11:19:24 -0400 Subject: [PATCH] Initial Commit. --- .gitignore | 4 ++ Makefile | 2 + main.cpp | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ca38d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*~ +*.gpx +.o +a.out diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..407efad --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +a.out: main.cpp + clang++ -std=c++17 -g -o a.out main.cpp -lGL -lGLU -lglut -lstdc++fs diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..22e5c49 --- /dev/null +++ b/main.cpp @@ -0,0 +1,164 @@ +#include + +#include + +#include +#include +#include +#include // fmod +#include +#include + +GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */ +GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */ + +struct Point { + float lat; + float lon; +}; + +static std::vector > tracks; + +// TODO(dev): +// As per https://www.opengl.org/resources/libraries/glut/spec3/node11.html +// "Therefore, GLUT programs should not assume the window was created at the specified size or position. +// A GLUT program should use the window's reshape callback to determine the true size of the window." + +void draw(const std::vector &points) +{ + static float rot=0.0; + rot += 0.1f; + + glLineWidth(0.5f); + glColor3d(1.0,1.0,1.0); + + // Draw Sun: + glBegin(GL_LINE_STRIP); + for(auto p : points) + glVertex3f(p.lat, p.lon, 0.0); + glEnd(); + + // usleep(100000); + + glutPostRedisplay(); +} + +void display() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + for(auto track : tracks) + draw(track); + glutSwapBuffers(); +} + +void init() +{ + // dvereb: + srand(time(NULL)); + + /* Enable a single OpenGL light. */ + // glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + // glLightfv(GL_LIGHT0, GL_POSITION, light_position); + // glEnable(GL_LIGHT0); + // glEnable(GL_LIGHTING); + + /* Use depth buffering for hidden surface elimination. */ + glEnable(GL_DEPTH_TEST); + + /* Setup the view of the cube. */ + glMatrixMode(GL_PROJECTION); + gluPerspective( /* field of view in degree */ 70.0, + /* aspect ratio */ 1.0, + /* Z near */ 1.0, /* Z far */ 1000.0); + glMatrixMode(GL_MODELVIEW); + gluLookAt(0.0, 0.0, 20.0, /* eye is at */ + 0.0, 0.0, 0.0, /* center is at */ + 0.0, 1.0, 0.); /* up is in positive Y direction */ +} + +int main(int argc, char *argv[]) +{ + if(argc != 2) + { + std::cerr << "You pust pass the folder which contains gpx files." << std::endl; + return -1; + } + + std::vector files; + for(const auto& entry : std::experimental::filesystem::directory_iterator(argv[1])) + { + auto extension = entry.path().extension(); + if(extension == ".gpx") + files.push_back(entry.path().filename()); + } + + for(auto file : files) + { + std::ifstream ifs(file); + if(!ifs.is_open()) + { + std::cerr << "Couldn't open " << file << "." << std::endl; + return -2; + } + + std::string line = ""; + bool first = true; + float diff_lat, diff_lon; + std::vector track; + while(std::getline(ifs, line)) + { + // get starting position of field NAMES + auto lat = line.find("lat=\""); + if(lat == std::string::npos) + continue; + auto lon = line.find("lon=\""); + if(lon == std::string::npos) + continue; + + // get ending position of field DATA + auto lat_to = line.find("\"", lat); + if(lat_to == std::string::npos) + continue; + auto lon_to = line.find("\"", lon); + if(lon_to == std::string::npos) + continue; + + Point p; + try { + p.lat = std::atof(line.substr(lat + 5, lat_to).c_str()) * 500; + p.lon = std::atof(line.substr(lon + 5, lon_to).c_str()) * 500; + } catch (const std::exception &e) { + std::cerr << "Error in file " << file << ", line \"" << line << "\"." << std::endl; + continue; + } + + if(first) + { + first = false; + diff_lat = p.lat; + diff_lon = p.lon; + } + + p.lat -= diff_lat; + p.lon -= diff_lon; + track.push_back(p); + } + tracks.push_back(track); + + ifs.close(); + } + + glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA | GLUT_MULTISAMPLE); + glutInit(&argc, argv); + glutCreateWindow("test opengl program"); + glutDisplayFunc(display); + + // NOTE(dev): lmfao: + // glutSetCursor(GLUT_CURSOR_SPRAY); + glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR); + + init(); + glutMainLoop(); + + return 0; +}