title: OpenGL Raycasting with any object
body: <p>I'm just wondering if there was any way which one can perform mouse picking detection onto any object. Whether it would be generated object or imported object.</p>
  
  <p><strong>[Idea]</strong> -
  The idea I have in mind is that, there would be iterations with every object in the scene. Checking if the mouse ray has intersected with an object. For checking the intersection, it would check the mouse picking ray with the triangles that make up the object.</p>
  
  <p><strong>[Pros]</strong> - 
  I believe the benefit of this approach is that, every object can be detected with mouse picking since they all inherit from the detection method.</p>
  
  <p><strong>[Cons]</strong> -
  I believe this drawbacks are mainly the speed and the method being very expensive. So would need fine tuning of optimization.</p>
  
  <p><strong>[Situation]</strong> -
  In the past I have read about mouse picking and I too have implemented some basic form of mouse picking. But all those were crappy work which I am not proud of. So again today, I have re-read some of the stuff from online. Nowadays I see alot of mouse picking using color ids and shaders. I'm not too keen for this method. I'm more into a mathematical side.</p>
  
  <p>So here is my mouse picking ray thingamajig.</p>
  
  <pre><code>maths::Vector3 Camera::Raycast(s32 mouse_x, s32 mouse_y)
  {
      // Normalized Device Coordinates
      maths::Vector2 window_size = Application::GetApplication().GetWindowSize();
  
      float x = (2.0f * mouse_x) / window_size.x - 1.0f;
      float y = 1.0f;
      float z = 1.0f;
  
      maths::Vector3 normalized_device_coordinates_ray = maths::Vector3(x, y, z);
  
      // Homogeneous Clip Coordinates
      maths::Vector4 homogeneous_clip_coordinates_ray = maths::Vector4(normalized_device_coordinates_ray.x, normalized_device_coordinates_ray.y, -1.0f, 1.0f);
  
      // 4D Eye (Camera) Coordinates
      maths::Vector4 camera_ray = maths::Matrix4x4::Invert(projection_matrix_) * homogeneous_clip_coordinates_ray;
      camera_ray = maths::Vector4(camera_ray.x, camera_ray.y, -1.0f, 0.0f);
  
      // 4D World Coordinates
      maths::Vector3 world_coordinates_ray = maths::Matrix4x4::Invert(view_matrix_) * camera_ray;
      world_coordinates_ray = world_coordinates_ray.Normalize();
  
      return world_coordinates_ray;
  }
  </code></pre>
  
  <p>I have this ray plane intersection function which calculates if a certain ray as intersected with a certain plane. DUH!</p>
  
  <p>Here is the code for that.</p>
  
  <pre><code>bool Camera::RayPlaneIntersection(const maths::Vector3&amp; ray_origin, const maths::Vector3&amp; ray_direction, const maths::Vector3&amp; plane_origin, const maths::Vector3&amp; plane_normal, float&amp; distance)
  {
      float denominator = plane_normal.Dot(ray_direction);
  
      if (denominator &gt;= 1e-6) // 1e-6 = 0.000001
      {
          maths::Vector3 vector_subtraction = plane_origin - ray_origin;
          distance = vector_subtraction.Dot(plane_normal);
  
          return (distance &gt;= 0);
      }
  
      return false;
  }
  </code></pre>
  
  <p>There are many more out there. E.g. Plane Sphere Intersection, Plane Disk Intersection. These things are like very specific. So it feel that is very hard to do mouse picking intersections on a global scale. I feel this way because, for this very RayPlaneIntersection function. What I expect to do with it is, retrieve the objects in the scene and retrieve all the normals for that object (which is a pain in the ass).  So now to re-emphasize my question.</p>
  
  <p>Is there already a method out there which I don't know, that does mouse picking in one way for all objects? Or am I just being stupid and not knowing what to do when I have everything? </p>
  
  <p>Thank you. Thank you.</p>