Improve CMake checks for the OpenGL availability

Issue 2868