My aim is to draw a line using mouse click. When you click the first click it reads the coordinates then when for the nest click it will draw the line using GL_LINES with first and second points.
int first, x1, yi, x2, yj, ww = 600, wh = 400;
void drawl()
{
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(5.00);
glColor3f(0,1,0);
glBegin(GL_LINES);
glVertex2i(x1,yi);
glVertex2i(x2,yj);
glEnd();
glFlush();
}
void Display()
{
glClearColor(0.5, 0.5, 0.5, 1.0);
glColor3f(0.7, 0.4, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if(first == 0)
{
first++;
x1 = x;
yi = wh - y;
}
else
{
x2 = x;
yj = wh - y;
drawl();
printf("%d,%d,%d,%d\n",x1,yi,x2,yj);
first--;
}
}
}
void main(int argc, char **argv)
{
first = 0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Mouse");
gluOrtho2D(0,800,0,500);
glutDisplayFunc(Display);
glutMouseFunc(mouse);
glutMainLoop();
}
Output I got is given below. It is not drawing the line. Should I include myinit() function and why?