Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to detect mouse-location/clicks on object in OpenGL?

Tags:

opengl

I am creating a simple 2D OpenGL game, and I need to know when the player clicks or mouses over an OpenGL primitive. (For example, on a GL_QUADS that serves as one of the tiles...) There doesn't seems to be a simple way to do this beyond brute force or opengl.org's suggestion of using a unique color for every one of my primitives, which seems a little hacky. Am I missing something? Thanks...

like image 932
Soren Johnson Avatar asked Dec 29 '22 06:12

Soren Johnson


1 Answers

My advice, don't use OpenGL's selection mode or OpenGL rendering (brute force method you are talking about), use a CPU-based ray picking algorithm if 3D. For 2D, like in your case, it should be straightforward, it's just a test to know if a 2D point is in a 2D rectangle.

like image 89
Stringer Avatar answered Feb 03 '23 13:02

Stringer