首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法绘制Bresenham,OpenGL

无法绘制Bresenham,OpenGL
EN

Stack Overflow用户
提问于 2020-07-29 00:39:58
回答 1查看 52关注 0票数 0

我正在实现Bresenham算法,我非常确定我的实现是正确的,但屏幕仍然是空白的,我认为我使用OpenGL是错误的。

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

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-29 00:54:08

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

代码语言:javascript
复制
#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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63138885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档