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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

Posting Permissions

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