1

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;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Dhruv Chadha
  • 1,161
  • 2
  • 11
  • 33

1 Answers1

2

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();
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I have edited my code as u suggested, still doesnt work... :( @Rabbid76 – Dhruv Chadha Jan 20 '18 at 16:23
  • Input I've globally declared, X1 should range from 10 to 100, so atleast 90 points should be displayed, as glVertex2f should be called 90 times. – Dhruv Chadha Jan 20 '18 at 16:41
  • How is this all working at the backend ? I am clearing all points on the frame, but am I not also printing all points on every frame ? As one time glClear() is called, and 90 times glVertex2f() is called. – Dhruv Chadha Jan 20 '18 at 16:47
  • 1
    @DhruvChadha In the 2nd frame `X1` starts with `X2`, becaus you modeified it in the 1st frame. Use a local varibale an do not modify `X1` or `Y1`. See the 2nd part of my answer. – Rabbid76 Jan 20 '18 at 16:51