使用dda / bresenham算法绘制具有开放gl鼠标功能的线

时间:2015-03-31 06:51:28

标签: opengl mouseevent bresenham dda

所以我必须使用鼠标功能绘制一条线。当鼠标点击两个点时,程序需要画一条连接它们的线。

我需要制作一个虚构的10x10阵列并在其上映射点然后连接点。窗口大小无关紧要。

我不知道如何处理虚数阵列以及如何保存鼠标坐标以便我可以使用它们。帮助

#include <glut.h>
#include<stdio.h>

void RenderScene(void) //점
{
    glClear(GL_COLOR_BUFFER_BIT);
    // Flush drawing commands
    glFlush();
}


void SetupRC(void) //바탕
{
    // setup clear color
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}


void ChangeSize(GLsizei w, GLsizei h) {
        GLfloat  aspectRatio;
        // Prevent a divide by zero
        if (h == 0)
            h = 1;
        // Set Viewport to window dimensions
        glViewport(0, 0, w, h);
        // Reset coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        // Establish clipping volume (left, right, bottom, top, near, far)
        aspectRatio = (GLfloat)w / (GLfloat)h;
        glOrtho(0, w, h, 0, 1.0, -1.0); 
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}


void myRectf(GLfloat    x1, GLfloat     y1, GLfloat     x2, GLfloat     y2){
    glPointSize(10);

    ////ddaLine

    int dx, dy;
    int m = (y2 - y1) / (x2 - x1);
    for (int i = x1; i <= x2; i++)
    {
        if (m <= 1)
        {
            dx = 1;
            dy = m * dx;
        }
        else
        {
            dy = 1;
            dx = dy / m;
        }
        x1 += dx;
        y1 += dy;

        glBegin(GL_POINTS);
        glVertex3f(x1, y1, 0.f);
        glEnd();
    }

    /////breline
    while (x1 < x2)
    {
        int dx = x2 - x1;
        int dy = y2 - y1;
        int di = 2 * dy - dx;
        int ds = 2 * dy;
        int dt = 2 * (dy - dx);

        x1++;
        if (di < 0)
            di += ds;
        else
        {
            y1++;
            di += dt;
        }
        glBegin(GL_POINTS);
        glVertex3f(x1, y1, 0.f);
        glEnd();
    }
}

GLfloat g_x = -1000.f, g_y = -1000.f;

void MouseHandler(int button, int state, int x, int y){
    if (button == GLUT_LEFT_BUTTON
        && state == GLUT_DOWN){
        g_x = x; g_y = y;
        printf("%d,%d \n", x, y);
    }
    glutPostRedisplay();
}


void main(int argc, char* argv[]){
    int array[10][10];

    glutInit(&argc, argv); // initialize GL context
    // single or double buffering | RGBA color mode
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutCreateWindow("Lec01");
    // callback function
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);

    SetupRC();          // initialize render context(RC)
    glutMouseFunc(MouseHandler);

    glutMainLoop();     // run GLUT framework
}

我不允许使用GL_LINES

0 个答案:

没有答案