Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

GLUT Mouse Over Callback? Is there one?

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am looking for a (hopefully) callback function that exists in GLUT used to echo the x,y position of the mouse pointer. you currently have glutMotionFunc(...) glutMouseFunc(....); but these only retrieve the x,y values when there is a click or BUTTON_DOWN is detected. i would like to be able to retrieve the x,y values when the mouse is 'hovering' or over a certain area on the screen, whether it be for an object or in a GUI environment. anyone have any ideas? i have googled and no luck.. thanks all for your help in advance.. the 2nd part of this is once i have the pixel coordinates from x,y i would like to be able to convert these to vertex coordinates which so i can check to see if the mouse is over a certain button or object. thanks..

Share this post


Link to post
Share on other sites
Advertisement
glutPassiveMotionFunc is what you want.

To convert from screen coordinates to world coordinates, use gluUnProject. (Search the forum or google for more info.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Install a handler with glutPassiveMotionFunc(). The man:

...
glutMotionFunc, glutPassiveMotionFunc - set the motion and
passive motion callbacks respectively for the current window.

void glutMotionFunc(void (*func)(int x, int y));
void glutPassiveMotionFunc(void (*func)(int x, int y));

The new motion or passive motion callback function.

glutMotionFunc and glutPassiveMotionFunc set the motion and
passive motion callback respectively for the current window. The motion
callback for a window is called when the mouse moves within the window
while one or more mouse buttons are pressed. The passive motion callback
for a window is called when the mouse moves within the window while
no mouse buttons are pressed.

The x and y callback parameters indicate the mouse location in window
relative coordinates.

Passing NULL to glutMotionFunc or glutPassiveMotionFunc
disables the generation of the mouse or passive motion callback
respectively.

SEE ALSO
glutMouseFunc, glutSpaceballMotionFunc, glutTabletMotionFunc
...

////////////////////////////////////////////////////////////////////////////////
//
// mouseFunc - GLUT mouse "passive motion" callback handler
//
// RETURNS: N/A
//
extern "C" void mouseFunc
(
int x, // mouse location (window-relative x-coordinate)
int y // mouse location (window-relative y-coordinate)
)
{
...
}

...

glutPassiveMotionFunc (mouseFunc);
...

HTH,

Paul

Share this post


Link to post
Share on other sites
Quote:
Original post by TomasH
glutPassiveMotionFunc is what you want.

To convert from screen coordinates to world coordinates, use gluUnProject. (Search the forum or google for more info.)


thanks Tomash, rating++ for your help.. i would rate the other person but he/she chose to be anon. :)

Share this post


Link to post
Share on other sites
this is what i have so far and i am getting garbage back:

 void convert_pixels(int x, int y, GLdouble *wpos)
{
GLdouble model[16], proj[16];
GLint view[4];
GLfloat z;
//glClear(GL_DEPTH_BUFFER_BIT); //clear the depth buffer for reading

glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);

y = view[3] - (GLfloat) y - 1;

glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); //get the pixel(in 2D) w/respect to 3D Coordinates

// if(z == 1.0) return FALSE;

gluUnProject(x, y, z, model, proj, view, wpos, wpos+1, wpos+2);

//return TRUE;

}
void passiveMotion(int x, int y)
{
fprintf(stderr,"Passive Motion: loc (%d %d).\n", x, y);
convert_pixels(x, y, win);
fprintf(stderr,"Object Coordinates: loc (%f %f %f.\n", win[0], win[1], win[2]);
}



sample output:
Passive Motion: loc(642, 540)
Object Coordinates: loc(-14322332, 542323233, 232344553) i made that up but thats the kind of numbers that i am getting.

so what am i doing wrong??? thanks!

[Edited by - OpenGL_Guru on August 11, 2005 11:40:40 AM]

Share this post


Link to post
Share on other sites
Well, you clear the depth buffer before reading from it, meaning you'll always get the same value from it.. Remove the glClear.
Also, %d is for printing integers. You should use %f when printing the elements of "win" (since they are floats)

Topic on gluUnProject

Share this post


Link to post
Share on other sites
thanks tomash.. i did read that thread.. i am using gluperspective so glReadPixels is correct. i am getting "better" output as i changed it to what you suggested.. a sample output would be like this:

Passive Motion: loc(642, 540)
Object Coordinates: loc(-1499.456, -0.5674, 2470.567)

the y value seems to be the only one that makes sense. i must tell you that the values do change drastically when my mouse goes from "hovering" over the GUI and when its not. i dont really understand this..so i have a GUI draw by GL_LINES and GL_QUADS specified by glVertex2f() calls.

if my mouse is not over the GUI and just over the rest of the window i will get something like Object Coordinates: loc(-24499.456, 12987.453, 17470.567) ..it changes to a range a bit different once my mouse is within the bounds of the GUI. so i guess i am a step closer. however only the Y value seems to have any real meaning...

what do you think tomash?

Share this post


Link to post
Share on other sites
Well, I can't see anything obviously wrong.. The thing I can think of is that you should check that the matrices you send to gluUnProject are correct (as I posted in the above-mentioned thread).. Other than that, there's always the standard trick of trying to reduce your program to smallest/simplest possible code that shows the problem and see if you can figure out what's wrong (or if not, you could post some code and see if someone else can tell.)
Good luck :)

Share this post


Link to post
Share on other sites
well basically in my GUI i create 2 QUADS. one QUad will have on texture and the other quad will have a different texture, naturally for a GUI. anyway here is my code in my class that shows what i draw.. anything stick out at you? this is all in gluPerspective..

GDEM_GUI::GDEM_GUI(float x, float y) : xCo(x), yCo(y) 
{
load_tga_texture("Quit.tga", 0, buttonQuit);
load_tga_texture("logo.tga", 0, Logo);
}

GDEM_GUI::drawGui()
{
glLoadIdentity();
glColor4f(1, 0.5, 0, 1);
glTranslatef(-0.75, 0.75, -3.0);

drawBorders();
drawLines();

glPushMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBindTexture(GL_TEXTURE_2D, buttonQuit);
glColor4f(1, 1, 1, 1);
glEnable(GL_TEXTURE_2D);
drawQuit();
glDisable(GL_TEXTURE_2D);
glPopMatrix();

glPushMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBindTexture(GL_TEXTURE_2D, Logo);
glEnable(GL_BLEND);
glColor4f(1, 1, 1, 0.2);
glEnable(GL_TEXTURE_2D);
drawQuad();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glPopMatrix();
}

GDEM_GUI::drawBorders()
{
//the MAIN border
glBegin(GL_QUADS);
glVertex2f(xCo-0.01, -0.01);
glVertex2f(xCo-0.01, -yCo+0.01);
glVertex2f(0.01, -yCo+0.01);
glVertex2f(0.01, -0.01);
glEnd();

//border for Quit button
glBegin(GL_QUADS);
glVertex2f(xCo-0.096, -1.296);
glVertex2f(xCo-0.096, -yCo+0.046);
glVertex2f(1.096, -yCo+0.046);
glVertex2f(1.096, -yCo+0.204);
glEnd();
}


GDEM_GUI::drawLines()
{
glBegin(GL_LINES);
glVertex2f(xCo-0.01, -1.38);
glVertex2f(xCo-0.096, -1.38);
glEnd();
}

GDEM_GUI::drawQuad()
{

glBegin(GL_QUADS);
glTexCoord2f(1, 1);
glVertex2f(xCo-0.01, -0.01);
glTexCoord2f(1, 0);
glVertex2f(xCo-0.01, -yCo+0.01);
glTexCoord2f(0, 0);
glVertex2f(0.01, -yCo+0.01);
glTexCoord2f(0, 1);
glVertex2f(0.01, -0.01);
glEnd();

}

GDEM_GUI::drawQuit()
{
glBegin(GL_QUADS);
glTexCoord2f(1, 1);
glVertex2f(xCo-0.1, -1.3);
glTexCoord2f(1, 0);
glVertex2f(xCo-0.1, -yCo+0.05);
glTexCoord2f(0, 0);
glVertex2f(1.1, -yCo+0.05);
glTexCoord2f(0, 1);
glVertex2f(1.1, -yCo+0.20);
glEnd();

}

Share this post


Link to post
Share on other sites
Ok, so basically that glTranslatef is your viewing transformation. That means that the array model (in convert_pixels) should look like this:
1.0,0,0,0, 0,1.0,0,0, 0,0,1.0,0, -0.75,0.75,-3.0,1.0

so that's one thing you could check..

Another thing you could check (and/or post) is how you setup your projection matrix. Don't know if that has anything to do with the problem, but you might as well take an extra look at it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!