can you help me because i don’t know when i resize a window a cursor a rubberband is not
on good position
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
float Line[1000][2];
int Lines=0;
float CursorPos[2];
void InitGL();
void Display();
void RubberBand();
void MouseButton(int,int,int,int);
void MouseMovement(int,int);
void Reshape(int,int);
int main(int argc, char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow(“RubberBanding”);
glutDisplayFunc(Display);
glutMouseFunc(MouseButton);
glutPassiveMotionFunc(MouseMovement);
glutReshapeFunc(Reshape);
InitGL();
glutMainLoop();
return EXIT_SUCCESS;
}
void RubberBand()
{
int Vtx;
glBegin(GL_LINE_STRIP);
for(Vtx=0;Vtx<Lines;Vtx++)
{
glVertex2fv(Line[Vtx]);
}
glEnd();
glFlush();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
glFlush();
glutDisplayFunc(RubberBand);
}
void MouseButton(int Button, int State, int x, int y)
{
GLint viewport[4];
GLdouble mvmatrix[16];
GLdouble projmatrix[16];
GLint realy;
GLdouble wx;
GLdouble wy;
GLdouble wz;
if(Button==GLUT_LEFT_BUTTON)
{
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
realy = viewport[3] - (GLint) y - 1;
printf ("Coordinates at cursor are (%4d, %4d)
", x, realy);
gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
printf ("World coords at z=0.0 are (%f, %f, %f)
",
wx, wy, wz);
gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
printf ("World coords at z=1.0 are (%f, %f, %f)
",
wx, wy, wz);
Line[Lines][0]=CursorPos[0];
Line[Lines][1]=CursorPos[1];
Lines++;
}
glutPostRedisplay();
}
void MouseMovement(int x, int y)
{
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_XOR);
if(Lines>0)
{
glBegin(GL_LINES);
glVertex2fv(Line[Lines-1]);
glVertex2fv(CursorPos);
glEnd();
glFlush();
}
CursorPos[0]=x;
CursorPos[1]=500-y;
if(Lines>0)
{
glBegin(GL_LINES);
glVertex2fv(Line[Lines-1]);
glVertex2fv(CursorPos);
glEnd();
glFlush();
}
glDisable(GL_COLOR_LOGIC_OP);
}
void Reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glFlush();
}
void InitGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0,0.0,0.0,1.0);
}