سؤال

I have a couple of 3D objects in OpenGL in a processing sketch and I need to find out if the mouse is hovering over those objects. Since there is constant transformation I can't compare the original coordinates and size to the mouse position. I already found the screenX() and screenY() methods which return the translated screen coordinates after transformation and translation but I would still need to get the displayed size after rotation.

هل كانت مفيدة؟

المحلول

Determining which object the mouse is over is called picking and there are 2 main approaches:

  1. Color picking. Draw each object using a different color into the back buffer (this is only done when picking, the colored objects are never displayed on screen). Then use glReadPixels to read the pixel under the cursor and check its color to determine which object it is. If the mouse isn't over an object you'll get the background color. More details here: Lighthouse 3D Picking Tutorial, Color Coding
  2. Ray casting. You cast a ray through the cursor location into the scene and check if it intersects any objects. More details here: Mouse picking with ray casting

From reading your description option 1 would probably be simpler and do what you need.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top