Search code examples
c++openglorthographicbresenhamdda

No output for simple openGL line drawing program?


I am unable to understand why I am not getting any output for the given program. This problem has been occurring to me for my last 2 openGL programs, the previous one being DDA algorithm, again in which I didnt get any output. Is there a problem with the algorithm, or in my understanding how openGL works internally ?

#include <iostream>
#include <GL/glut.h>
using namespace std;
float X1 = 0, X2 = 100, Y1 = 0, Y2 = 400, X11, Y11, X22, Y22, slope, dely, delx, c, intercept, pi;
float absl(float x) {
    if (x >= 0) return x;
    return -1*x;
}
void drawScene() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
    glClearColor(1.0, 1.0, 1.0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);
    //start bresenham algo
    glBegin(GL_POINTS);
        glVertex2f(X1, Y1);
        while(X1 <= X2) {
            X1++;
            if(pi < 0) {
                glVertex2f(X1, Y1);
            }
            else {
                Y1++;
                glVertex2f(X1, Y1);
            }
            float flag = (pi >= 0);
            pi = pi + 2*dely - 2*delx*flag;
        }
    glEnd();
    //end DDA algo
    glFlush();
}

int main(int argc, char **argv) {
    //cin >> X1 >> Y1 >> X2 >> Y2;
    // dely = Y2 - Y1;
    // delx = X2 - X1;
    // pi = 2*dely - delx;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0,0);
    glutCreateWindow("DDA");
    glutDisplayFunc(drawScene);
    glutMainLoop();
    return 0;
}

Solution

  • gluOrtho2D defines a 2D orthographic projection matrix. But it also multiplies the current matrix on the matrix stack with the orthographic projection matrix and replaces the current matrix with the product.

    Note, in OpenGL fixed function pipeline all matrix operations work like this (except glPushMatrix, glPopMatrix, glLoadMatrix and glLoadIdentity).

    This means you have to replace the current matrix with the identity matrix (glLoadIdentity), befor you apply the orthographic projection matrix:

    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
    

    Or you do gluOrtho2D only one time before you start the main loop in the main function:

    gluOrtho2D(0, 500, 0, 500);
    glutMainLoop();
    


    Since the display function is called (drawScene) continuously, you should use local control varibales and not modify X1 and Y1:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
    glClearColor(0.0, 0.0, 0.0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);
    //start bresenham algo
    int x = X1, y = Y1;
    glBegin(GL_LINES);
    glVertex2f(100, 400);
    glVertex2f(400, 100);
    glEnd();
    glBegin(GL_POINTS);
        glVertex2f(x, Y1);
        while(x <= X2) {
            x ++;
            if(pi < 0) {
                glVertex2f(x, y);
            }
            else {
                y ++
                glVertex2f(x, y);
            }
        }
        int flag = (pi >= 0);
        pi = pi + 2*dely - 2*delx*flag;
    glEnd();