Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- // Y сверху вниз
- // X слева направо
- struct vertex {
- GLfloat x, y;
- };
- vector<vertex> mas;
- int flag = 0;
- GLfloat centerScreenW, centerScreenH;
- //void paintOver() {
- // sort(mas, )
- //
- // vertex array[vertexCount] = {};
- //}
- void drawEdges() {
- if (flag) {
- glBegin(GL_LINE_LOOP);
- } else {
- glBegin(GL_LINE_STRIP);
- }
- glColor3f(1, 1, 1);
- for (int i = 0; i < mas.size(); i++) {
- glVertex2f(mas[i].x, mas[i].y);
- }
- }
- void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
- if (action == GLFW_PRESS) {
- switch (key) {
- case GLFW_KEY_1:
- flag = 1;
- break;
- case GLFW_KEY_C:
- mas.clear();
- flag = 0;
- break;
- }
- }
- }
- void mouseCallback(GLFWwindow* window, int button, int action, int mods) {
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
- double x, y;
- glfwGetCursorPos(window, &x, &y);
- vertex vertexLeftBut;
- vertexLeftBut.x = x;
- vertexLeftBut.y = y;
- mas.push_back(vertexLeftBut);
- }
- }
- int main() {
- GLFWwindow *window;
- GLfloat scrW = 1280, scrH = 960;
- if (!glfwInit()) {
- return -1;
- }
- window = glfwCreateWindow(scrW, scrH, "Lab4", NULL, NULL);
- glfwSetKeyCallback(window, keyCallback);
- glfwSetMouseButtonCallback(window, mouseCallback);
- glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
- int screenW, screenH;
- glfwGetFramebufferSize(window, &screenW, &screenH);
- if (!window) {
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- centerScreenW = scrW / 2;
- centerScreenH = scrH / 2;
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glOrtho(0.0, scrW, scrH, 0.0, -1.0, 1.0);
- glViewport(0, 0, screenW, screenH);
- drawEdges();
- glEnd();
- glPopMatrix();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement