0

In opengl, how would I be able to check if the mouse pointer is inside an object. So far, I can only check if its inside the screen, using glutPassiveMotionFunc.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
anaksky2k19
  • 51
  • 2
  • 8

1 Answers1

1

I guess that you want to retrieve which object is under your mouse or under the clicked pixel. There is a method called 3d picking. It consist of drawing your scene on a texture created specifically for this purpose. It must have same dimension as your window. You will draw your scene with a special shader that will draw the id of your objects on the texture. After what you just need to read the pixel where the mouse is to know which object is selected.

There is my fragment shader, it takes the id of the current object:

#version 330 core

uniform uint id;

layout(location = 0) out uvec4 out_color;

void main()
{
    uvec4 color;
    color.x = (id & 0xff0000u) >> 16;
    color.y = (id & 0x00ff00u) >>  8;
    color.z = (id & 0x0000ffu) >>  0;
    color.w = uint(255);

    out_color = color;
    return;
}

You can check out this tutorial also : http://ogldev.atspace.co.uk/www/tutorial29/tutorial29.html

Erwan Daniel
  • 1,319
  • 11
  • 26