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