无法绘制 Bresenham,OpenGL

Can't draw Bresenham, OpenGL

我正在实现 Bresenham 算法,我很确定我的实现是正确的,但屏幕仍然是空白,我想我使用的 OpenGL 有误。

这是我的完整程序。如果有帮助,我正在使用 nupengl.core.0.1.0.1

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
#include <fstream>
#include <math.h>
#define Round(a) (int)(a+0.5) 
#define max(a,b) (a>b)?a:b
using namespace std;


/*truct vectors = {
    vector
}*/

void init(void) {
    glClearColor(1.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}

class Shape {
public:
    virtual void Draw() = 0;
};
class lineBressenham : public Shape {
    int x1, y1, x2, y2;
public:
    lineBressenham(int a1, int b1, int a2, int b2) {
        x1 = a1;
        y1 = b1;
        x2 = a2;
        y2 = b2;
    }
    void Draw() {
        int dx = x2 - x1;
        int dy = y2 - y1;
        int x = x1;
        int y = y1;
        int p = 2 * dy - dx;
        glBegin(GL_POINTS);
        while (x < x2) {
            if (p >= 0) {
                glVertex2i(x, y);               
                y = y + 1;
                p = p + 2 * dy - 2 * dx;
            }
            else {
                glVertex2i(x, y);
                p = p + 2 * dy;
            }
            x = x + 1;
        }
        glEnd();
    }
};

void displayWindow(void) {
    ifstream infile;
    glClear(GL_COLOR_BUFFER_BIT);    // settings for buffer.
    glColor3f(0.0, 0.0, 0.0);
    glPointSize(3.0);
    while (!infile.eof()) {
        int a;
        int x1 = 3, y1 = 4, x2 = 7, y2 = 9;
        Shape* shape;
        shape = new lineBressenham(x1, y1, x2, y2);
        shape->Draw();
        glutSwapBuffers();
        //glFlush();  // draw everything in input, buffer in one time.
    }
}
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);                    // window size
    glutInitWindowPosition(0, 0);  // distance from the top-left screen
    glutCreateWindow("Show the window");    // message displayed on top bar window
    glewInit();
    if (glewIsSupported("GL_VERSION_3_3")) {
        std::cout << " GLEW Version is 3.3\n ";
    }
    else {
        std::cout << "GLEW 3.3 not supported\n ";
    }
    
        glutDisplayFunc(displayWindow);
    init();
    glutMainLoop();
    return 0;
}

我修好了。问题是我没有初始化 gluOrtho2D 和 glMatrixMode。完整代码为

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
#include <fstream>
#include <math.h>
GLsizei windows_witdh = 800;
GLsizei windows_height = 600;
class lineBressenham {
    int x1, y1, x2, y2;
public:
    lineBressenham(int a1, int b1, int a2, int b2) {
        x1 = a1;
        y1 = b1;
        x2 = a2;
        y2 = b2;
    }
    void Draw() {
        int dx = x2 - x1;
        int dy = y2 - y1;
        int x = x1;
        int y = y1;
        int p = 2 * dy - dx;
        glBegin(GL_POINTS);
        while (x < x2) {
            if (p >= 0) {
                glVertex2i(x, y);
                y = y + 1;
                p = p + 2 * dy - 2 * dx;
            }
            else {
                glVertex2i(x, y);
                p = p + 2 * dy;
            }
            x = x + 1;
        }
        glEnd();
    }
};
void displayWindow(void) {
    glClear(GL_COLOR_BUFFER_BIT);    // settings for buffer.
    glColor3f(1.0, 0.3, 0.7);
    glPointSize(1.0);
    int x1 = 30, y1 = 40, x2 = 700, y2 = 300;
    lineBressenham *shape = new lineBressenham(x1, y1, x2, y2);;
    shape->Draw();
    glutSwapBuffers();
    glFlush();
}
void init() {

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D(0.0, windows_witdh, windows_height, 0.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);                    // window size
    glutInitWindowPosition(0, 0);  // distance from the top-left screen
    glutCreateWindow("Bressenham");    // message displayed on top bar window
    glLoadIdentity();
    init();
    if (glewIsSupported("GL_VERSION_3_3")) {
        std::cout << " GLEW Version is 3.3\n ";
    }
    else {
        std::cout << "GLEW 3.3 not supported\n ";
    }

    glutDisplayFunc(displayWindow);
    glutMainLoop();
    return 0;
}