Не могу нарисовать Брезенхем, OpenGL

Я реализую алгоритм Брезенхэма, и я почти уверен, что моя реализация правильная, но экран по-прежнему пустой, я думаю, что неправильно использую 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;
}

person Trần Nam Lộc    schedule 28.07.2020    source источник
comment
Что случилось с этим циклом infile? Это не похоже на то, что вы открываете файл.   -  person genpfault    schedule 28.07.2020
comment
Почему именно вы реализуете алгоритм отрисовки строк попиксельной развертки в GL?   -  person 3Dave    schedule 28.07.2020


Ответы (1)


Я починил это. Проблема была в том, что я не инициализировал 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;
}
person Trần Nam Lộc    schedule 28.07.2020