How to drag two objects in opengl?

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();
}


[QUOTE=Cgeek_Lucky;1282267]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.
[/QUOTE]
Whenever the mouse position changes, update any necessary state then call glutPostRedisplay() so that the display function will redraw the entire “scene”.

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.
All i want to drag 2 objects.

glClear should only be used once in the display function.
I think you’re problem was due to not using glClear and glutPostDisplay quite right.
A simplified version of your code is shown below. It works for me, by which I mean
that the two rectangles follow the cursor around. I did not need inter to accomplish
this. Also, I combined your two rect routines into one called Rect.
Good luck. Have fun!

//--+----4----+----3----+----2----+----1----+----|----+----1----+----2----+----3----+----4----+----5
//---------------------------------   Carmine April 24, 2016   -------------------------------------

#include <stdio.h>
#include <GLUT/glut.h>

int wh, ww, xmouse = 130, ymouse = 50, size = 10;

float red[3] = {1.0, 0.3, 0.3},
      blu[3] = {0.3, 0.3, 1.0};

//-------------------------------------------   Rect   ---------------------------------------------

void Rect (float kolor[3], int x, int y)
{
    y = wh - y;

    glColor3fv (kolor);
    
    glBegin (GL_POLYGON);
       glVertex2f (x+size, y+size);
       glVertex2f (x-size, y+size);
       glVertex2f (x-size, y-size);
       glVertex2f (x+size, y-size);
    glEnd();
}

//------------------------------------------   Motion   ---------------------------------------------

void Motion (int x, int y)
{
    xmouse = x;
    ymouse = y;

    glutPostRedisplay ();
}

//-----------------------------------------   display   --------------------------------------------

void display (void)
{
    glClear (GL_COLOR_BUFFER_BIT);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();

    Rect (red, (xmouse - 20), ymouse);
    Rect (blu, (xmouse + 20), ymouse);

    glFlush ();
}

//-----------------------------------------   Reshape   -------------------------------------------

void Reshape (int w,int h)
{
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();

    gluOrtho2D (0,w,0,h);
    glViewport (0,0,w,h);

    ww = w;
    wh = h;
}

//----------------------------------------   Init_Glut   -------------------------------------------

int Init_Grafix (void)
{
    glutInitWindowPosition (300, 100);
    glutInitDisplayMode    (GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize     (1000, 500);
    glutCreateWindow       ("Carmine Apr 24, 2016");

    glutDisplayFunc (display);
    glutReshapeFunc (Reshape);
    glutMotionFunc  (Motion );

    glClearColor (0.0, 0.2, 0.2, 1.0);
}

//------------------------------------------   main   ----------------------------------------------

int main (int argc,char **argv)
{
    glutInit (&argc, argv);
    Init_Grafix  ();
    glutMainLoop ();
}

// -------------------------------------------------------------------------------------------------