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;