I am trying to drag two objects in opengl.
Since I have used glClear in both functions, rect_b is not drawn.
If I don’t use glClear, It will drag like I am painting something.
Please help to resolve this problem.
All i want to drag 2 objects…
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
int wh,ww,size=10;
float t_x,t_y;
int unique=0;
struct area_bound
{
int xmin,xmax,ymin,ymax,midx,midy,id;
}rect1,rect2;
void rect_b(int x,int y)
{
glClear(GL_COLOR_BUFFER_BIT);
y=wh-y;
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex2f(x+size,y+size);
glVertex2f(x-size,y+size);
glVertex2f(x-size,y-size);
glVertex2f(x+size,y-size);
glEnd();
glFlush();
}
void rect_r(int x,int y)
{
glClear(GL_COLOR_BUFFER_BIT);
y=wh-y;
glBegin(GL_POLYGON);
glColor3f(1,0,0);
glVertex2f(x+size,y+size);
glVertex2f(x-size,y+size);
glVertex2f(x-size,y-size);
glVertex2f(x+size,y-size);
glEnd();
glFlush();
}
void myreshape(int w,int h)
{
glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,0,h);
glViewport(0,0,w,h);
ww=w;
wh=h;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
rect_b(110,50);
rect_r(150,50);
glFlush();
}
void mouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
printf("%d %d
",x,y);
if(x>rect1.xmin && x<rect1.xmax && y>rect1.ymin && y<rect1.ymax)
unique=1;
if(x>rect2.xmin && x<rect2.xmax && y>rect2.ymin && y<rect2.ymax)
unique=2;
if(btn==GLUT_LEFT_BUTTON && state==GLUT_UP)
unique=0;
}
void inter(int x,int y)
{
if(unique==1)
{
printf("inter -- %d
",unique);
rect_b(x,y);
rect1.xmin=x-10;
rect1.xmax=x+10;
rect1.ymin=y-10;
rect1.ymax=y+10;
}
if(unique==2)
{
rect_r(x,y);
rect2.xmin=x-10;
rect2.xmax=x+10;
rect2.ymin=y-10;
rect2.ymax=y+10;
}
//glutPostRedisplay();
}
int main(int argc,char **argv)
{
rect1.xmin=100,rect1.xmax=120,rect1.ymin=40,rect1.ymax=60;//xmin=midx-10;xmax=midx+10;ymin=midy-10;ymax=midy+10;
rect2.xmin=140,rect2.xmax=160,rect2.ymin=40,rect2.ymax=60;
glutInit(&argc,argv);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1000,500);
glutCreateWindow("HELLO");
glutDisplayFunc(display);
glutReshapeFunc(myreshape);
glutMouseFunc(mouse);
glutMotionFunc(inter);
// glutKeyboardFunc(keys);
// glutIdleFunc(display);
glutMainLoop();
}