Sign in to follow this  
sobeit

help with glutPassiveMotionFunc

Recommended Posts

sobeit    463
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():

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

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!

Share this post


Link to post
Share on other sites
Brother Bob    10344
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.

Share this post


Link to post
Share on other sites
sobeit    463
[quote name='Brother Bob' timestamp='1310225907' post='4833103']
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.
[/quote]

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.
[code]#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();
}
[/code]
Thank you for your reply!

Share this post


Link to post
Share on other sites
Brother Bob    10344
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.

Share this post


Link to post
Share on other sites
sobeit    463
[quote name='Brother Bob' timestamp='1310267032' post='4833232']
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.
[/quote]

Thank you for your help! I have fixed it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this