help with glutPassiveMotionFunc

Started by
3 comments, last by sobeit 12 years, 9 months ago
I use the function "glutPassiveMotionFunc" to track my mouse.But it seems to have an initial position in (0,1).
Here is my function registered in glutPassiveMotionFunc():

void myMouseMove( int x, int y)
{ vec2 localVertex( double( x), double( viewport.getHeight() - y));
tempVertex = localVertex;
glutPostRedisplay();
}


I intend to draw a line whose start-point is at the center of my program window and the end-point follows mouse pointer.
But when I run my program with mouse pointer outside window, there is already a line with end-point being at the left-bottom corner.
And the variable has already been assigned to position(0.0, 0.0).
Why does this situation happen? How does the glutPassiveMostionFunc work? and how can I solve this problems?

Thanks in advance!
Advertisement
The glutPassiveMotionFunc callback is called with the coordinates of the pointer whenever it is moved within the window. You will not get any callbacks if moved outside the window. But I'm not sure what your question really is.

The glutPassiveMotionFunc callback is called with the coordinates of the pointer whenever it is moved within the window. You will not get any callbacks if moved outside the window. But I'm not sure what your question really is.


I'm sorry that I can explain my problem well. Here is a short program that can demonstrate what I'm talking about.
You may run this program with the pointer outside the program window. And before you move you mouse into the program window, you can already see a black line which is the problem that confuses me.
#include<GL/glut.h>

GLint viewWidth = 400, viewHeight = 400;
int centerX = 200, centerY = 200;
int tempX, tempY;

void drawLine( int x, int y)
{
glBegin( GL_LINES);
glVertex2i( centerX, centerY);
glVertex2i( x, y);
glEnd();
}

void init()
{
glClearColor( 0.0, 0.0, 1.0, 1.0);
glMatrixMode( GL_PROJECTION);
gluOrtho2D( 0.0, 400.0, 0.0, 400.0);
}

void myDisplay()
{
glClear( GL_COLOR_BUFFER_BIT);
glColor3f( 0.0, 0.0, 0.0);

drawLine( tempX, tempY);

glutSwapBuffers();
}

void myMouseMove( int x, int y)
{
tempX = x;
tempY = viewHeight - y;

glutPostRedisplay();
}

void main( int argc, char ** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE| GLUT_RGB);
glutInitWindowPosition( 100, 100);
glutInitWindowSize( viewWidth, viewHeight);
glutCreateWindow( "problem demonstration");

init();
glutDisplayFunc( myDisplay);
glutPassiveMotionFunc( myMouseMove);

glutMainLoop();
}

Thank you for your reply!
You get a black line because you draw a black line, even if you haven't moved the pointer anywhere. Do you want to draw the line only after you have actually moved the pointer somewhere? In that case you need to conditionally draw it. For example, make a flag that you initialize to false, set it to true once you have registered a movement, and draw the line only if the flag is true.

You get a black line because you draw a black line, even if you haven't moved the pointer anywhere. Do you want to draw the line only after you have actually moved the pointer somewhere? In that case you need to conditionally draw it. For example, make a flag that you initialize to false, set it to true once you have registered a movement, and draw the line only if the flag is true.


Thank you for your help! I have fixed it.

This topic is closed to new replies.

Advertisement