C++ Can';t draw Bresenham,OpenGL

C++ Can';t draw Bresenham,OpenGL,c++,opengl,graphics,bresenham,C++,Opengl,Graphics,Bresenham,我正在实现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

我正在实现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;
}
#包括
#包括
#包括
#包括
#包括
#定义圆(a)(整数)(a+0.5)
#定义最大值(a,b)(a>b)?a:b
使用名称空间std;
/*truct向量={
矢量
}*/
void init(void){
glClearColor(1.0,0.0,0.0,1.0);
glMatrixMode(GL_投影);
glLoadIdentity();
gluOrtho2D(0.0,200.0,0.0,150.0);
}
阶级形态{
公众:
虚空绘制()=0;
};
lineBressenham类:公共形状{
int-x1,y1,x2,y2;
公众:
lineBressenham(内部a1、内部b1、内部a2、内部b2){
x1=a1;
y1=b1;
x2=a2;
y2=b2;
}
作废提款(){
int dx=x2-x1;
int dy=y2-y1;
int x=x1;
int y=y1;
int p=2*dy-dx;
glBegin(总分);
而(x=0){
glVertex2i(x,y);
y=y+1;
p=p+2*dy-2*dx;
}
否则{
glVertex2i(x,y);
p=p+2*dy;
}
x=x+1;
}
格伦德();
}
};
void显示窗口(void){
河流充填;
glClear(GL_COLOR_BUFFER_BIT);//缓冲区的设置。
GL3F(0.0,0.0,0.0);
glPointSize(3.0);
而(!infle.eof()){
INTA;
int x1=3,y1=4,x2=7,y2=9;
形状*形状;
形状=新的线条Bressenham(x1,y1,x2,y2);
形状->绘图();
glutSwapBuffers();
//glFlush();//一次性绘制输入、缓冲区中的所有内容。
}
}
int main(int argc,字符**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
GLUTINITWindowsSize(500500);//窗口大小
glutInitWindowPosition(0,0);//距左上屏幕的距离
glutCreateWindow(“显示窗口”);//顶部栏窗口上显示的消息
glewInit();
如果(受支持(“GL_版本_3_3”)){

我修复了它。问题是我没有初始化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;
}
#包括
#包括
#包括
#包括
#包括
GLsizei windows_witdh=800;
玻璃窗高度=600;
lineBressenham类{
int-x1,y1,x2,y2;
公众:
lineBressenham(内部a1、内部b1、内部a2、内部b2){
x1=a1;
y1=b1;
x2=a2;
y2=b2;
}
作废提款(){
int dx=x2-x1;
int dy=y2-y1;
int x=x1;
int y=y1;
int p=2*dy-dx;
glBegin(总分);
而(x=0){
glVertex2i(x,y);
y=y+1;
p=p+2*dy-2*dx;
}
否则{
glVertex2i(x,y);
p=p+2*dy;
}
x=x+1;
}
格伦德();
}
};
void显示窗口(void){
glClear(GL_COLOR_BUFFER_BIT);//缓冲区的设置。
GL3F(1.0,0.3,0.7);
glPointSize(1.0);
int x1=30,y1=40,x2=700,y2=300;
lineBressenham*shape=新的lineBressenham(x1,y1,x2,y2);;
形状->绘图();
glutSwapBuffers();
glFlush();
}
void init(){
//重置坐标系
glMatrixMode(GL_投影);
glLoadIdentity();
gluOrtho2D(0.0,窗宽,窗高,0.0);
glMatrixMode(GLU模型视图);
glLoadIdentity();
}
int main(int argc,字符**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
GLUTINITWindowsSize(500500);//窗口大小
glutInitWindowPosition(0,0);//距左上屏幕的距离
glutCreateWindow(“Bressenham”);//顶部栏窗口上显示的消息
glLoadIdentity();
init();
如果(受支持(“GL_版本_3_3”)){

std::cout那
infle
循环是怎么回事?这不像是在打开文件。确切地说,为什么要在GL中实现逐像素扫描线绘制算法?