0

I want to select an object in opengl glfw renderer and move the object in the scene. Currently I am able to create ray and its direction. Now I want to check if this ray intersects with my object in the renderer. How can I check if the ray hits my object and then I can say that my object is selected?

glm::vec3 CreateRay()
{
    float mouseX = xPos/ (width  * 0.5f) - 1.0f;
    float mouseY = yPos / (height * 0.5f) - 1.0f;

    glm::mat4 invVP = glm::inverse(proj * view);
    glm::vec4 screenPos = glm::vec4(mouseX, -mouseY, 1.0f, 1.0f);
    glm::vec4 worldPos = invVP * screenPos;

    glm::vec3 dir = glm::normalize(glm::vec3(worldPos));

    return dir;
}

glm::vec3 rayDirection = CreateRay();
glm::vec3 rayStartPositon = pCamera->GetCameraPosition();
glm::vec3 rayEndPosition = rayStartPositon + rayDirection * 2.0f;
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 3
    OpenGL doesn't handle such operations. You have to write your own ray-whatever intersection code. – BDL Apr 16 '21 at 14:58
  • see [OpenGL 3D-raypicking with high poly meshes](https://stackoverflow.com/a/51764105/2521214) especially the last sublinks in there in edit2 ... – Spektre Apr 17 '21 at 08:19

0 Answers0