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