Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <GLFW/glfw3.h>
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <math.h>
  7. #include <stack>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. static void error_callback(int error, const char* description) {
  13.     fputs(description, stderr);
  14. }
  15.  
  16. struct Point {
  17.     double x;
  18.     double y;
  19. };
  20.  
  21. int width = 640;
  22. int height = 480;
  23.  
  24. vector<Point> points;
  25.  
  26. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods );
  27. void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
  28. void draw_Line();
  29. int main(void) {
  30.     GLFWwindow* window;
  31.     glfwSetErrorCallback(error_callback);
  32.     if (!glfwInit())
  33.         exit(EXIT_FAILURE);
  34.     window = glfwCreateWindow(width, height, "Simple example", NULL, NULL);
  35.     if (!window)
  36.     {
  37.         glfwTerminate();
  38.         exit(EXIT_FAILURE);
  39.     }
  40.  
  41.     glfwMakeContextCurrent(window);
  42.     glfwSetKeyCallback(window, key_callback);
  43.     glfwSetMouseButtonCallback(window, mouse_button_callback);
  44.  
  45.     while (!glfwWindowShouldClose(window)) {
  46.         float windowWidth;
  47.         int width, height;
  48.         glfwGetFramebufferSize(window, &width, &height);
  49.         windowWidth = width / (float) height;
  50.         glViewport(0, 0, width, height);
  51.         glClear(GL_COLOR_BUFFER_BIT);
  52.  
  53.         glMatrixMode(GL_PROJECTION);
  54.         glLoadIdentity();
  55.         glOrtho(-windowWidth, windowWidth, -1.f, 1.f, 1.f, -1.f);
  56.         glMatrixMode(GL_MODELVIEW);
  57.         glLoadIdentity();
  58.         draw_Line();
  59.         glfwSwapBuffers(window);
  60.         glfwPollEvents();
  61.     }
  62.     glfwDestroyWindow(window);
  63.     glfwTerminate();
  64.     exit(EXIT_SUCCESS);
  65. }
  66.  
  67.  
  68. void mouse_button_callback (GLFWwindow *window, int button, int action, int mods) {
  69.     if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  70.         double x;
  71.         double y;
  72.         glfwGetCursorPos(window, &x, &y);
  73.         points.push_back({x, y});
  74.     }
  75. }
  76.  
  77.  
  78. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods ) {
  79.     if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  80.         switch (key) {
  81.             case GLFW_KEY_ESCAPE:
  82.                 glfwSetWindowShouldClose(window, GL_TRUE);
  83.                 break;
  84.         }
  85.     }
  86. }
  87.  
  88. void draw_Line() {
  89.     if (points.size() > 1) {
  90.         for (int i = 0; i < points.size() - 1; i++) {
  91.             Point p1 = points[i];
  92.             Point p2 = points[i + 1];
  93.             p1.x = 2 * (p1.x - width / 2) / width;
  94.             p1.y = 2 * (height / 2 - p1.y) / height;
  95.             p2.x = 2 * (p2.x - width / 2) / width;
  96.             p2.y = 2 * (height / 2 - p2.y) / height;
  97.  
  98.             cout << p2.x << " " << p2.y << endl;
  99.            cout << p1.x << " " << p1.y << endl;
  100.  
  101.             glBegin(GL_LINES);
  102.             glColor3b(1, 1, 0);
  103.             glVertex2d(p1.x, p1.y);
  104.             glVertex2d(p2.x, p2.y);
  105.             glEnd();
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement