making a square move your mouse pointer

Hi,
This is my first post.I wanted to draw a square the follows my mouse pointer .I wrote the following piece of code but it doesn’t work as expected.Please help!!
#include<stdio.h>
#include<GL/glut.h>
int moving=0;
int startx=0,starty=0;
float xmin=0.0,ymin=0.0;
float xmax=0.5,ymax=0.5;
int length=0.5;

void myinit(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0,0.0,0.0,0.0);
}
void display(void)
{
myinit();
glColor3f(0.0,1.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
glFlush();
}

void mouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
moving=1;
startx=x;
starty=y;
}
if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP)
moving=0;
}
void motion(int x,int y)
{
if(moving==1)
{
xmax=startx;
ymax=starty;
xmin=startx-length;
ymin=starty-length;
startx=x;
starty=y;
glutPostRedisplay();
}
}

void myReshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(480,480);
glutInitWindowPosition(0,0);
glutCreateWindow(“MYPROJECT”);

glutReshapeFunc(myReshape);
glutDisplayFunc(display);

glutMouseFunc(mouse);
glutMotionFunc(motion);

glutMainLoop();
return 0;

}

When posting on these forums, it’s helpful to say what isn’t working as expected. Is the square completely motionless? Does it disappear? Fly off screen?

Don’t over complicate the code. Have a position of where the square is being drawn. When you click, make that a point of the initial mouse position “p1” and when you move the mouse to the new position “p2”, add the difference p2-p1 to your square position, and set p1 = p2.

Hi ,
I did as you mentioned but this time the square disappears .My initial point is xi and yi both set to 0.0 and the square is drawn as v0=(xi,yi) v1=(xi+a,yi) v2=(xi+a,yi+a) v3=(xi,yi+a) for all 0<=i<=3 .Then i wrote my motion function as follows.
void mouse(int btn,int state,int x,int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{

                           moving=1;
                           startx=x;
                           starty=y;
                          }
                           
 if(btn==GLUT_LEFT_BUTTON && state == GLUT_UP)
                          moving=0;

}
void motion(int x,int y)
{

 if(moving==1)
 {
              xi=xi+(x-startx);
              yi=yi+(y-starty);
              startx=x;
              starty=y;
              glutPostRedisplay();
              }

}
This time on a single mouse click the square disappears .Please Help.

What values are you getting for xi and yi after you move them?

Hi,
suppose I click my mouse on (2,2) then
startx=2
starty=2
then if i drag my mouse to 3,3 i.e. x=3,y=3
then xi=0+(3-2)=1
and yi too is 1

So it sounds like the mouse code is working correctly and it’s not drawing correctly now.

Hey since the value of x and y should get updated instantly as and when your motion function is called why don’t you use XOR?

Hi,
I suppose there is a problem in the reshape function which reshapes the window .Any suggestions