Improve CMake checks for the OpenGL availability

Issue #2868
Merge pull request #758 from taka-no-me:do_not_use_opengl_without_ui