RubberBand and Reshape with openGL

Hi all

I am beginner with openGL…

i trying to draw rubberband with my exist code …following problem i am getting

a)I clicking on left side of screen and that rubberband drawing right side

and that is not in proper shape also

i think it conflict with my reshape function…

-------------------------------------------xxxxxxxxxxxxxxxx---------------------------------------
here are my two file code

Main.cpp:

#include"Headers.h"
void design(void);
void mouse(int , int , int , int);
void move(int,int);
void keyboard(unsigned char , int , int );
int screen_width = 600;
int screen_height = 400;
GLfloat xpos = 0.0;
GLfloat ypos = 0.0;

float W,H;

void reshape (int width, int height)
{
W=(float)width;
H=(float)height;
printf("%d-----%d
“,width,height);
GLfloat w, h;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width > height)
{
//printf(”%f----%f
“,w,h);
w = (1.5 * width) / height;
h = 1.5;
printf(”%f----%f
",w,h);
}
else
{
printf("else part
“);
w = 5.0;
h = (5.0 * height) / width;
printf(”%f----%f
",w,h);
}
glOrtho(-w, w, -h, h, -5.0, 5.0);
//glutPostRedisplay();
}

int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(screen_width, screen_height);
glutCreateWindow(“Demonstration of reshaping”);
glutDisplayFunc(design);

glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(move);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
------------------------------------------xxxxxxxxxxxxxxxx-----------------------------------------------------
Rubberband.cpp

#include"Headers.h"
void DrawTrackRect(float , float , float , float );

float xm, ym, xmm, ymm;
int first = 0;
void mouse(int button, int state, int x, int y)
{
glEnable(GL_COLOR_LOGIC_OP);
glDrawBuffer(GL_FRONT);

if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{

	printf("1st IF

“);
printf(”%d----%d
“,x,y);
printf(”%f----%f
“,W,H);
xm = (float)x/W;
ym = (H-(float)y)/H;
printf(”%f----%f
",xm,ym);
glColor3f(0.0, 0.0, 1.0);
glLogicOp(GL_XOR);
first = 0;
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
printf("2st IF
“);
glBegin(GL_POLYGON);
DrawTrackRect(xm,ym,xmm,ymm);
glFlush();
xmm = x/W;
ymm = (H-y)/H;
printf(”%f----%f
",xm,ym);
glLogicOp(GL_COPY);
glutSwapBuffers();
glFlush();
}

}

void move(int x, int y)
{

if (first == 1)
{
	printf("Move--3st IF

");
DrawTrackRect(xm,ym,xmm,ymm);
glFlush();
}
printf("Move not IF
“);
xmm = x/W;
ymm = (H-y)/H;
printf(”%f----%f
",xmm,ymm);
DrawTrackRect(xm,ym,xmm,ymm);
glutSwapBuffers();
glFlush();
first = 1;

}

void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27: // ESC key
exit(0);
break;

}
}
void DrawTrackRect(float x1, float y1, float x2, float y2)
{
printf("In DrawTrack–%f–%f–%f----%f
",x1,y1,x2,y2);

//glLogicOp(GL_XOR);
// drawing different rubber-banding rectangle
// depending on the mouse movement x-direction
if(x1 < x2)
{
glColor4f(0.0, 0.0, 1.0, 0.5);
}
else
{
glColor4f(1.0, 0.0, 0.0, 0.5);
}
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// OpenGL window coordinates are different from GDI’s
glRectf(x1, y1, x2,y2);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glFlush(); // must flush here

}