OpenGL program not working correctly

Hello, I am facing a problem while trying to run an OpenGL program (written in C++) to draw 4 quadrants and a simple line, dotted line, a dashed line and a boat on the OpenGL display window. The Digital Differential Analyzer(DDA) Algorithm has been used to draw the lines. According to me, the control of the program while printing the output is not going ahead of the setquadrants() function in the myDisplay() function. The 2 lines representing the quadrants are drawn, after which the window gets stuck and does not respond. Hence, even though the program runs, the graphical output does not get displayed
My operating system is Windows 10 and the IDE I am using is Eclipse Helios. This is my code :


#include<GL/glut.h>
#include<windows.h>
#include<iostream>
#include<math.h>
#define w 500
#define h 500

using namespace std;

void setpixel(int, int);
void plot(int , int );
void simple();
void dotted();
void dashed();
void boat();
void setxy();

void myInit(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);

    glColor3f(0.0f, 0.0f, 0.0f);

    glPointSize(2.5);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluOrtho2D(-w, w, -h, h);
}

int len(float x1, float y1, float x2, float y2)
{
    int length;

    if(abs(x2 - x1) >= abs(y2- y1))
    {
        length = x2 - x1;
    }

    else
    {
        length = abs(y2 - y1);
    }

    return length;
}

void setpixel(int x, int y)
{
    glBegin(GL_POINTS);
    glVertex2f(x, y);
    glEnd();
    glFlush();
}

void plot(int a, int b)
{
    glBegin(GL_POINTS);
    glVertex2f(a, b);
    glEnd();
    glFlush();
}

void simple(float x1, float y1, float x2, float y2)
{
    int i, l=0;
    float x, y, dx, dy;

    l = len(x1, y1, x2, y2);

    dx = (x2 - x1)/l;
    dy = (y2 - y1)/l;
    x = x1 + 0.5;
    y = y1 + 0.5;

    plot(round(x), round(y));
    i = 1;
    while(i < l)
    {
        plot(round(x), round(y));
        x = x + dx;
        y = y + dy;
        i++;
    }

}

void dotted(float x1, float y1, float x2, float y2)
{
    int i, l=0;
    float x, y, dx, dy;

    l = len(x1, y1, x2, y2);

    dx = (x2 - x1)/l;
    dy = (y2 - y1)/l;
    x = x1 + 0.5;
    y = y1 + 0.5;

    plot(round(x), round(y));
    i = 1;
    while(i < l)
    {
        if(i%10 == 0)
            plot(round(x), round(y));

        x = x + dx;
        y = y + dy;
        cout<<x<<" "<<y<<endl;
        i++;
    }

}

void dashed(float x1, float y1, float x2, float y2)
{
    int i, l=0;
    float x, y, dx, dy;

    l = len(x1, y1, x2, y2);

    dx = (x2 - x1)/l;
    dy = (y2 - y1)/l;
    x = x1 + 0.5;
    y = y1 + 0.5;

    glBegin(GL_LINES);
    glVertex2i(round(x), round(y));
    glVertex2i(round(x+10), round(y+20));
    glEnd();

    i = 1;
    while(i < l)
    {
        if(i%30 == 0)
        {
            glBegin(GL_LINES);
            glVertex2i(round(x), round(y));
            glVertex2i(round(x+10), round(y+20));
            glEnd();
            glFlush();
        }

        x = x + dx;
        y = y + dy;

        i++;
    }
}

void boat()
{
    simple(70, 100, 170, 100);
    simple(170, 100, 200, 170);
    simple(200, 170, 40, 170);
    simple(40, 170, 170, 250);
    simple(170, 250, 200, 170);

}
void DDA()
{
    float x1, y1, x2, y2;
    int choice;

    do{
        cout<<" Menu: 
 1)Simple line 
 2)Dotted line 
 3)Dashed line 
 4)Boat 
 5)Exit 
";
        cout<<"Enter your choice: ";
        cin>>choice;

        switch(choice)
        {
            case 1 :
                cout<<"Enter value of: 
 1) x1: ";
                cin>>x1;
                cout<<"
 2) y1: ";
                cin>>y1;
                cout<<"
 3) x2: ";
                cin>>x2;
                cout<<"
 4) y2: ";
                cin>>y2;
                simple(x1, y1, x2, y2);
                break;

        case 2 :
            cout<<"Enter value of: 
 1) x1: ";
            cin>>x1;
            cout<<"
 2) y1: ";
            cin>>y1;
            cout<<"
 3) x2: ";
            cin>>x2;
            cout<<"
 4) y2: ";
            cin>>y2;
            dotted(x1, y1, x2, y2);
            break;

        case 3 :
            cout<<"Enter value of: 
 1) x1: ";
            cin>>x1;
            cout<<"
 2) y1: ";
            cin>>y1;
            cout<<"
 3) x2: ";
            cin>>x2;
            cout<<"
 4) y2: ";
            cin>>y2;
            dashed(x1, y1, x2, y2);
            break;

        case 4:
            boat();
            break;

        case 5 :
            cout<<"Exiting..";
            break;

        default:
        {
            cout<<"Wrong choice entered. Enter again or press 3 to exit. 
";
        }
    }

    }while(choice!=5);
}

void setquadrants()
{
    int i;
    glClear(GL_COLOR_BUFFER_BIT);

    for(i = -w; i<= w; i+=2)
    {
        setpixel(i, 0);
        setpixel(0, i);
    }

    glEnd();
    glFlush();
}

void myDisplay(void)
{
    setquadrants();
    DDA();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    glutInitWindowSize(800, 800);

    glutInitWindowPosition(300, 300);

    glutCreateWindow("DDA");

    myInit();

    glutDisplayFunc(myDisplay);

    glutMainLoop();
}

Please help me out with this problem. Thank you so much.
-Mugdha

You’re reading from standard input (cin) inside your display function. This is of course going to hang. Specifically, see DDA().

Do your input someplace else, like in a glutKeyboardFunc()-registered function, in a non-blocking way.

Also once you do that, you can get rid of all of your glFlush()s and just add a single glFlush() at the end of your display function (since you’re using single-buffering: GLUT_SINGLE). Alternatively, switch to double-buffering (GLUT_DOUBLE), again get rid of all of your glFlush()s, and just add a single glutSwapBuffers() at the end of your display function.

When you want your display function to be called again (e.g. in response to a key in your keyboard function), just call glutPostRedisplay(). If you want your display function to be called continuously, call it at the end of your display function.

Hi, Dark Photon. Thank you so much for your quick response. :slight_smile:
I am really new to using OpenGL so I’m not getting how to use the glutKeyboardFunc().
I want to take an input of 2 co-ordinates and plot a line joining the 2 points. The type of line (simple/dotted/dashed) has to be selected by the user. How do I implement this using the glutKeyboardFunc() function (or any other function if required)?

This is the part of the code which has to be inputted by the user:

cout<<" Menu:
1)Simple line
2)Dotted line
3)Dashed line
4)Boat
";
cout<<"Enter your choice: ";
cin>>choice;

cout<<"Enter value of:

  1. x1: “;
    cin>>x1;
    cout<<”
  2. y1: “;
    cin>>y1;
    cout<<”
  3. x2: “;
    cin>>x2;
    cout<<”
  4. y2: ";
    cin>>y2;

I would really appreciate any help from you. Thanks a lot. :slight_smile:
-Mugdha

[ul]
[li]GLUT Keyboard (Lighthouse3D) [/li][/ul]
Also, see the other GLUT tutorials on this site:

[ul]
[li]GLUT (Lighthouse3D) [/li][/ul]

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.