About Mouse clicks and rectangles

Hi all
I got a question. How do I check if a mouse click was clicked in a specific rectangle? On MFC u had a function PointInRect to check it. How do u check in OpenGL?

With OpenGL, you can’t, cause it doesn’t provide any input functions at all.


As Bob says, OpenGL is just a graphics library and doesn’t have this kind of functions.
One the other hand, you can use gluProject() to get the x,y screen coords of the rectangle and check if the mouse position is inside.

  • nemesis -

There is way to find out if the user has clicked inside a rectangle.
You can use the backbuffer and color different objects in the scene to differen colors and then check the pixel value when u go into selection mode.
For furher information look in the red book under the chapter now that you know, it mentions this technique and u can also look the technique up on the web.

you can use gluUnProject(…) to obtain the object coords and go from there. I did it recently for a circle and it seems to work well

Originally posted by Bob:
With OpenGL, you can’t, cause it doesn’t provide any input functions at all.

well, opengl give you a powerfull way to know which polygon what clicke with the mouse.
However implementing this is requerid severals instrucctions.

The first one is used when you are drawing your scene,

Glloadname(1); //value 1 asigned

Glloadname(2); //value 2 asigned

The glloadname() function will assing a numeric value to all next opengl polys,

now when the user click on the polygon you have to render the scene using the gl_select mode

glRenderMode(GL_SELECT); //select mode
draw_scene; // draw scene.

then you get which numerics values was assigned for the poly that the user did click.

hits:= glRenderMode(GL_RENDER);

now check if “hits” is value 1 or 2.

That was a resumed way to do this, but for implement that you have to do a lot more setups, but i least i am pointing you what instructions you have to check:

glGetIntegerv ();

or if you have acces to the red book (can be dowloaded somewhere) check the “select and feedbakc” section.

good luck