Properly restore all the OpenGL states properly and added a sanity check

Also set the proper viewport value at startup.