Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: OpenGL program not working correctly

  1. #1
    Newbie Newbie
    Join Date
    Mar 2018
    Posts
    2

    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 :

    Code cpp:
    #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: \n 1)Simple line \n 2)Dotted line \n 3)Dashed line \n 4)Boat \n 5)Exit \n";
            cout<<"Enter your choice: ";
            cin>>choice;
     
            switch(choice)
            {
                case 1 :
                    cout<<"Enter value of: \n 1) x1: ";
                    cin>>x1;
                    cout<<"\n 2) y1: ";
                    cin>>y1;
                    cout<<"\n 3) x2: ";
                    cin>>x2;
                    cout<<"\n 4) y2: ";
                    cin>>y2;
                    simple(x1, y1, x2, y2);
                    break;
     
            case 2 :
                cout<<"Enter value of: \n 1) x1: ";
                cin>>x1;
                cout<<"\n 2) y1: ";
                cin>>y1;
                cout<<"\n 3) x2: ";
                cin>>x2;
                cout<<"\n 4) y2: ";
                cin>>y2;
                dotted(x1, y1, x2, y2);
                break;
     
            case 3 :
                cout<<"Enter value of: \n 1) x1: ";
                cin>>x1;
                cout<<"\n 2) y1: ";
                cin>>y1;
                cout<<"\n 3) x2: ";
                cin>>x2;
                cout<<"\n 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. \n";
            }
        }
     
        }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
    Last edited by Dark Photon; 03-01-2018 at 06:00 PM.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,478
    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.
    Last edited by Dark Photon; 03-01-2018 at 06:11 PM.

  3. #3
    Newbie Newbie
    Join Date
    Mar 2018
    Posts
    2
    Hi, Dark Photon. Thank you so much for your quick response.
    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: \n 1)Simple line \n 2)Dotted line \n 3)Dashed line \n 4)Boat \n";
    cout<<"Enter your choice: ";
    cin>>choice;

    cout<<"Enter value of: \n 1) x1: ";
    cin>>x1;
    cout<<"\n 2) y1: ";
    cin>>y1;
    cout<<"\n 3) x2: ";
    cin>>x2;
    cout<<"\n 4) y2: ";
    cin>>y2;

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

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,478

    Also, see the other GLUT tutorials on this site:

    • GLUT (Lighthouse3D)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •