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;
- const int scrW = 1280, scrH = 960;
- //СНАЧАЛА y, ПОТОМ x
- GLubyte vertexContainer[scrH][scrW][3] = {0};
- // Y снизу вверх
- // X слева направо
- struct vertex {
- GLdouble x, y;
- };
- bool isPaintedOver(const GLubyte *mas) {
- return mas[0] || mas[1] || mas[2];
- }
- vector<vertex> mas;
- int flag = 0;
- int flagCanDraw = 1;
- void drawPixel(int x, int y) {
- vertexContainer[y][x][0] = 0;
- vertexContainer[y][x][1] = 255;
- vertexContainer[y][x][2] = 255;
- }
- void drawline(int x1, int y1, int x2, int y2) {
- const int deltaX = abs(x2 - x1);
- const int deltaY = abs(y2 - y1);
- const int signX = x1 < x2 ? 1 : -1;
- const int signY = y1 < y2 ? 1 : -1;
- int error = deltaX - deltaY;
- drawPixel(x2, y2);
- while(x1 != x2 || y1 != y2) {
- drawPixel(x1, y1);
- const int error2 = error * 2;
- if (error2 > -deltaY) {
- error -= deltaY;
- x1 += signX;
- }
- if (error2 < deltaX) {
- error += deltaX;
- y1 += signY;
- }
- }
- }
- //void draw() {
- //
- // for (int x = 0; x < scrW; x++) {
- // for (int y = 0; y < scrH; y++) {
- // vertexContainer[x][y][0] = 0;
- // vertexContainer[x][y][1] = 255;
- // vertexContainer[x][y][2] = 255;
- // }
- // }
- //
- // glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- // glPixelStorei(GL_UNPACK_ROW_LENGTH, scrW); //длина строки
- // glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); // сколько строк пропустить?
- // glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); // сколько пикселов пропустить в каждой строке?
- //
- // glRasterPos2i(0, 0);
- // glDrawPixels(scrW, scrH, GL_RGB, GL_UNSIGNED_BYTE, vertexContainer);
- //
- //}
- void fill() {
- for (int y = scrH - 1; y >= 0; y--) {
- int flagDraw = 0;
- int countConstDraw = 0;
- int countDraw = 0;
- for (int x = 0; x < scrW; x++) {
- if (isPaintedOver(vertexContainer[y][x])) {
- countConstDraw++;
- }
- }
- for (int x = 0; x < scrW; x++) {
- if (isPaintedOver(vertexContainer[y][x])) {
- if (flagDraw == 0) {
- flagDraw = 1;
- countDraw++;
- } else if (flagDraw == 1) {
- flagDraw = 1;
- countDraw++;
- } else if (flagDraw == 2) {
- flagDraw = 1;
- countDraw++;
- }
- } else {
- if (flagDraw == 0) {
- flagDraw = 0;
- } else if (flagDraw == 1) {
- if (isPaintedOver(vertexContainer[y + 1][x])) {
- flagDraw = 2;
- } else {
- flagDraw = 0;
- }
- } else if (flagDraw == 2) {
- flagDraw = 2;
- }
- }
- if (y < scrH - 1) {
- if (flagDraw == 2 && countConstDraw != countDraw) {
- drawPixel(x, y);
- }
- }
- }
- }
- }
- void drawEdges() {
- if (mas.size() > 1) {
- for (int i = 0; i < mas.size() - 1; i++) {
- drawline(mas[i].x, mas[i].y, mas[i + 1].x, mas[i + 1].y);
- }
- if (flag == 1) {
- drawline(mas[mas.size() - 1].x, mas[mas.size() - 1].y, mas[0].x, mas[0].y);
- }
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glPixelStorei(GL_UNPACK_ROW_LENGTH, scrW);
- glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
- glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
- glRasterPos2i(0, 0);
- glDrawPixels(scrW, scrH, GL_RGB, GL_UNSIGNED_BYTE, vertexContainer);
- if (flag == 2) {
- fill();
- }
- }
- }
- void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
- if (action == GLFW_PRESS) {
- switch (key) {
- case GLFW_KEY_1:
- flag = 1;
- flagCanDraw = 0;
- break;
- case GLFW_KEY_2:
- flag = 2;
- flagCanDraw = 0;
- break;
- case GLFW_KEY_C:
- mas.clear();
- for (int y = 0; y < scrH; y++) {
- for (int x = 0; x < scrW; x++) {
- vertexContainer[y][x][0] = 0;
- vertexContainer[y][x][1] = 0;
- vertexContainer[y][x][2] = 0;
- }
- }
- flag = 0;
- flagCanDraw = 1;
- break;
- case GLFW_KEY_P:
- for (int y = scrH - 1; y >= 0; y--){
- cout << "stroka y: " << y << endl;
- for (int x = 0; x < scrW; x++) {
- cout << int(isPaintedOver(vertexContainer[y][x])) << " ";
- }
- cout << endl;
- }
- }
- }
- }
- void mouseCallback(GLFWwindow *window, int button, int action, int mods) {
- if (flagCanDraw) {
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
- GLdouble x, y;
- glfwGetCursorPos(window, &x, &y);
- vertex vertexLeftBut;
- vertexLeftBut.x = x;
- vertexLeftBut.y = scrH - y;
- mas.push_back(vertexLeftBut);
- }
- }
- }
- int main() {
- GLFWwindow *window;
- 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();
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glOrtho(0.0, scrW, 0.0, scrH, -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