openGL 實現繪製、修改多邊形

Polyline Editor

Using a mouse and keyboard to do polyline editing.

  • A house in the process of being drawn. The user has just clicked at the position drawn, and a line has been drawn from the previous point to the one by the mouse.
  • Moving a point. The user positions the cursor near the vertex of some polyline, presses down the mouse button, and “drags” the chosen point to some other location before releasing the button. Upon release of the button, the previous lines connected to this point are erased, and new lines are drawn to it.
  • A point is deleted from a polyline. The user clicks near the vertex of some polyline, and the two line segments connected to that vertex are erased. Then the two other endpoints of the segments just erased are connected with a line segment.

The functionality of the program should include the following “actions”:

  • Begin (‘b’) (create a new polyline)
  • Delete (‘d’) (delete the next point pointed to)
  • Move (‘m’) (drag the point pointed to to a new location)
  • Refresh (‘r’) (erase the screen and redraw all the polylines)
  • Quit (‘q’) (exit from the program)
// ConsoleApplication1-openGL-lab1.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"

struct point {
    int x;
    int y;
    // 一個多邊形的起始點
    bool polygonStart;
};
std::vector<point> points;
bool newPolygon = true;
bool deleteMode = false;
bool moveMode = false;
// 用於標記正在拖動
bool dragging = false;
// 被拖動的點的編號
int draggedNum = -1;

void RenderSence() {
    // 將窗口顏色的設置顯示出來
    glClear(GL_COLOR_BUFFER_BIT);
    // 內容顯示顏色
    glColor3f(1, 1, 1);
    for (int i = 0; i < points.size(); i++) {
        if (points[i].polygonStart) {
            if (i != 0) {
                glEnd();
            }
            glBegin(GL_LINE_STRIP);
        }
        // 注意y值的改變
        glVertex2f(points[i].x, 600 - points[i].y);
    }
    glEnd();
    glutSwapBuffers();
}

// -1 when no points
int findNearPointNum(int x, int y) {
    if (points.size() == 0) {
        return -1;
    }
    int near = 0;
    double mix = -1;
    for (int i = 0; i < points.size(); i++) {
        double distence = (points[i].x - x)*(points[i].x - x) + (points[i].y - y)*(points[i].y - y);
        if (mix == -1) {
            mix = distence;
        }
        else if (mix > distence) {
            mix = distence;
            near = i;
        }
    }
    return near;
}

void processMouse(int button, int state, int x, int y) {
    if (button == GLUT_LEFT_BUTTON&&state == GLUT_DOWN) {
        if (deleteMode) {
            int near = findNearPointNum(x, y);
            if (near == -1) {
                return;
            }
            if (points[near].polygonStart) {
                if (points.size() > near + 1) {
                    points[near + 1].polygonStart = true;
                }
            }
            points.erase(points.begin() + near, points.begin() + near + 1);
            return;
        }
        if (moveMode) {
            dragging = true;
            draggedNum = findNearPointNum(x, y);
            return;
        }
        point t{ x,y,false };
        if (newPolygon) {
            newPolygon = false;
            t.polygonStart = true;
        }
        points.push_back(t);
    }
    else if (button == GLUT_LEFT_BUTTON&&state == GLUT_UP) {
        if (!dragging) {
            return;
        }
        dragging = false;
        if (draggedNum != -1) {
            points[draggedNum].x = x;
            points[draggedNum].y = y;
        }
    }
}

void processKey(unsigned char key, int x, int y) {
    if (key == 'b') {
        newPolygon = true;
    }
    if (key == 'd') {
        deleteMode = true;
    }
    if (key == 'm') {
        moveMode = true;
    }
    if (key == 'r') {
        points.clear();
    }
    if (key == 'q') {
        exit(0);
    }
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

    glutInitWindowSize(1000, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello GLUT");

    glutDisplayFunc(RenderSence);
    glutIdleFunc(RenderSence);

    gluOrtho2D(0, 1000, 0, 600);

    glutMouseFunc(processMouse);
    glutKeyboardFunc(processKey);

    glutMainLoop();

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章