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, let’s say if i hold the left button of my mouse on the polygon, the polygon will be drag around based on my cursor movement. Now what i want is when i click on outside the polygon instead of on the polygon itself, the polygon wont move, even i hold my mouse button across the polygon, it will stay where it is only when i initially click on the polygon itself, it will be drag around. Is there any solution to do tat?
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <glut.h>
GLint referenceX = 0; //Declar reference points used to drag
GLint referenceY = 0; // the rectangle
GLfloat xPosition = 300.0;
GLfloat yPosition = 220.0;
GLsizei winWidth = 400, winHeight = 400, shapeSize = 50;
GLfloat red = 1.0f, blue = 0.0f, green = 0.0f;
#define FALSE 0
#define TRUE 1
int up = FALSE;
int down = FALSE;
int left = FALSE;
int right = FALSE;
void moveObject()
{
if(left)
{
xPosition -= 4.0;
if(xPosition - shapeSize < 0.0)
xPosition = 0.0 + shapeSize;
}
if(right)
{
xPosition += 4.0;
if(xPosition + shapeSize > winWidth)
xPosition = winWidth - shapeSize;
}
}
void displayFcn (void)
{
glClearColor (1.0, 1.0, 1.0, 1.0);
glLoadIdentity();
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, winWidth, 0.0, winHeight);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (red, green, blue);
glRectf(xPosition - shapeSize, yPosition - shapeSize, xPosition + shapeSize, yPosition + shapeSize);
glFlush ();
moveObject();
glutSwapBuffers();
}
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:
switch(state){
case 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);
break;
case GLUT_UP:
break;
}
break;
case GLUT_LEFT_BUTTON:
switch(state){
case GLUT_DOWN:
referenceX = x;
referenceY = y;
break;
case GLUT_UP:
break;
}
break;
glEnd ();
glFlush ( );
}
}
void motion (int x, int y)
{
int dx = x - referenceX;
int dy = - (y - referenceY); // the y-axis is down
xPosition += dx;
yPosition += dy;
if (yPosition + glRectf > winHeight)
yPosition = winHeight - GL_POLYGON;
if (yPosition - GL_POLYGON < 0)
yPosition = GL_POLYGON ;
if(xPosition + GL_POLYGON > winWidth)
xPosition = winWidth - GL_POLYGON;
if(xPosition - GL_POLYGON < 0.0)
xPosition = GL_POLYGON ;
referenceX = x;
referenceY = y;
// Print the mouse drag position
printf ("Mouse Drag Position: %d, %d.
", x, y);
glutPostRedisplay();
}
void reshape (int width, int height)
{
winWidth = width;
winHeight = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLfloat) width / (GLfloat) height, 1.0, 20.0);
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(motion);
glutDisplayFunc (displayFcn);
glutTimerFunc(25,update,0);
glutMainLoop ( );
}