I’m still new to openGL programming, and I’m now doing the mouse motion event handler function, so far i know how to set the position value of shape but seems not working anyway, here’s my code:
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <glut.h>
GLfloat xPosition = 0.0;
GLfloat yPosition = 0.0;
GLsizei winWidth = 400, winHeight = 400, shapeSize = 25;
GLfloat red = 1.0f, blue = 0.0f, green = 0.0f;
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 1.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, winWidth, 0.0, winHeight);
}
void displayFcn (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glColor3f (red, green, blue);
glBegin (GL_POLYGON);
glVertex2i (50,100);
glVertex2i (50,50);
glVertex2i (200,50);
glVertex2i (200,100);
glEnd ();
glFlush ( );
}
void update(int value)
{
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27)
exit(0);
}
void processSpecialKeys(int key,int x,int y)
{
switch (key)
{
case GLUT_KEY_F1:
red = 1.0;
green = 0.0;
blue = 0.0;
break;
case GLUT_KEY_F2:
red = 0.0;
green = 1.0;
blue = 0.0;
break;
case GLUT_KEY_F3:
red = 0.0;
green = 0.0;
blue = 1.0;
break;
default:
break;
}
}
void mouseClickHandler(int button, int state, int x, int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glBegin (GL_POLYGON);
glVertex2i (45, 75);
glVertex2i (75, 45);
glVertex2i (105, 75);
glVertex2i (75, 105);
glEnd ();
glFlush ( );
break;
default:
break;
}
}
void drag(int x, int y)
{
xPosition = (GLfloat) x/winWidth;
yPosition = 1.0 - (GLfloat) y/winHeight;
glutPostRedisplay();
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (350, 100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow (“Rectangle Program”);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
glutMouseFunc(mouseClickHandler);
glutMotionFunc(drag);
glutPassiveMotionFunc(drag);
init();
glutDisplayFunc (displayFcn);
glutTimerFunc(25,update,0);
glutMainLoop ( );
}