Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLFW/glfw3.h>
- #include <windows.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <math.h>
- #include <stack>
- #include <vector>
- using namespace std;
- static void error_callback(int error, const char* description) {
- fputs(description, stderr);
- }
- struct Point {
- double x;
- double y;
- };
- int width = 640;
- int height = 480;
- vector<Point> points;
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods );
- void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
- void draw_Line();
- int main(void) {
- GLFWwindow* window;
- glfwSetErrorCallback(error_callback);
- if (!glfwInit())
- exit(EXIT_FAILURE);
- window = glfwCreateWindow(width, height, "Simple example", NULL, NULL);
- if (!window)
- {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- glfwSetMouseButtonCallback(window, mouse_button_callback);
- while (!glfwWindowShouldClose(window)) {
- float windowWidth;
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- windowWidth = width / (float) height;
- glViewport(0, 0, width, height);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-windowWidth, windowWidth, -1.f, 1.f, 1.f, -1.f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- draw_Line();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- exit(EXIT_SUCCESS);
- }
- void mouse_button_callback (GLFWwindow *window, int button, int action, int mods) {
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
- double x;
- double y;
- glfwGetCursorPos(window, &x, &y);
- points.push_back({x, y});
- }
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods ) {
- if (action == GLFW_PRESS || action == GLFW_REPEAT) {
- switch (key) {
- case GLFW_KEY_ESCAPE:
- glfwSetWindowShouldClose(window, GL_TRUE);
- break;
- }
- }
- }
- void draw_Line() {
- if (points.size() > 1) {
- for (int i = 0; i < points.size() - 1; i++) {
- Point p1 = points[i];
- Point p2 = points[i + 1];
- p1.x = 2 * (p1.x - width / 2) / width;
- p1.y = 2 * (height / 2 - p1.y) / height;
- p2.x = 2 * (p2.x - width / 2) / width;
- p2.y = 2 * (height / 2 - p2.y) / height;
- cout << p2.x << " " << p2.y << endl;
- cout << p1.x << " " << p1.y << endl;
- glBegin(GL_LINES);
- glColor3b(1, 1, 0);
- glVertex2d(p1.x, p1.y);
- glVertex2d(p2.x, p2.y);
- glEnd();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement