#include #include #include #include struct Point { int x, y; }; int WIDTH = 640, HEIGHT = 480; std::vector polygon; void error_callback(int error, const char* description) { fputs(description, stderr); } void put_angles(GLFWwindow* window, int button, int action, int mods){ if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { double x, y; glfwGetCursorPos(window, &x, &y); y = HEIGHT - (int)y; polygon.push_back({ (int)x, (int)y }); } } void project(double width, double height) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, width, 0, height); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void window_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); project(width, height); } void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { glfwSetWindowShouldClose(window, GL_TRUE); } else if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { polygon.push_back(polygon[0]); } } void init(GLFWwindow* window, int width, int height) { glfwMakeContextCurrent(window); glfwSetKeyCallback(window, key_callback); glfwSetWindowSizeCallback(window, window_size_callback); glfwSetMouseButtonCallback(window, put_angles); glClearColor(0, 0, 0, 0); project(width, height); } void display() { glColor3d(0, 100, 0); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINE_STRIP); for (Point p: polygon) { glVertex2d(p.x, p.y); } glEnd(); } int main() { GLFWwindow* window; int width = WIDTH, height = HEIGHT; glfwSetErrorCallback(error_callback); if (!glfwInit()) { exit(EXIT_FAILURE); } window = glfwCreateWindow(width, height, "lab4", nullptr, nullptr); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } init(window, width, height); while (!glfwWindowShouldClose(window)) { display(); glfwSwapBuffers(window); glfwWaitEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }