Emscripten GLUT/OpenGLES test with fix for browser resizing
This is a modified version of Emscripten’s test, hello_world_gles.c, fixed to handle browser window resizing.
Emscripten issues logged here and here.
Issue:
Fix #1:
Fix #2:
To build:
emcc hello_world_gles.c -s WASM=0 -o hello_world_gles.js
emcc hello_world_gles_resizable_CLIENT_fix.c -s WASM=0 -o hello_world_gles_resizable_CLIENT_fix.js
The issue is that the glutReshapeFunc() callback is not being called when the browser window is resized.
The client-side fix is to detect when the HTML canvas has changed size in the glutDisplayFunc() callback, and explicitly call glutReshapeWindow() with the updated canvas size. This then invokes glutReshapeFunc() callback with the updated canvas size. Note this solution doesn’t handle fullscreen correctly.
The GLUT fix is to add a resize event listener and call glutReshapeWindow on resize. Resize events due to entering/exiting fullscreen have to be excluded, and this is done two ways. On entering fullscreen, fullscreen document state is checked and the resize event listener is removed. On exiting fullscreen, the resize event listener is restored. This solution does handle fullscreen correctly, so long as the ‘Resize canvas’ box is checked (the original and client-side fix do not work even if Resize canvas is checked).
Also note the default html generated by Emscripten (-o hello_world_gles.html) does not show this issue because the canvas is not resizable. To demonstrate the issue, HTML which stretches the canvas to fill the entire browser window is provided.