19. OpenGL Fullscreen Mode (Version 2.0)
Introduction
Fullscreen mode in OpenGL enhances the gaming and graphical experience by allowing the application to take over the entire screen. This mode can also help boost performance by reducing distractions and maximizing frame rates.
In this tutorial, you’ll learn how to enable fullscreen mode using GLUT, set the resolution, color depth, and refresh rate, and exit fullscreen mode cleanly. By the end, you’ll have a solid understanding of how to use GLUT’s game mode effectively.
Setting Fullscreen Mode
To enable fullscreen mode, GLUT provides a feature called Game Mode. This involves two primary functions:
- glutGameModeString: Sets the desired resolution, color depth, and refresh rate.
- glutEnterGameMode: Switches the application to fullscreen mode.
Here’s an example:
glutGameModeString("1024x768:32@75"); // Set resolution, color depth, and refresh rate glutEnterGameMode(); // Enter fullscreen mode
In the glutGameModeString
, the parameters are:
– Resolution: 1024×768, 800×600, or other supported values.
– Color depth: 32, 24, 16, or 8 bits per pixel.
– Refresh rate: 75Hz, 60Hz, or other monitor-supported rates.
Exiting Fullscreen Mode
When you’re done with fullscreen mode, you can return to the windowed mode by calling:
glutLeaveGameMode(); // Exit fullscreen mode and restore the previous state
Tutorial Code
Here’s the complete example demonstrating how to set up and use fullscreen mode in OpenGL:
#include <GL/gl.h> #include <GL/glut.h> // Camera and rotation parameters float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 90, angle = 0.0; // Draw cubes void cube(void) { for (int i = 0; i < 50; i++) { glTranslated(1, 0, 1); glPushMatrix(); glutSolidCube(2); // Draw a cube glPopMatrix(); } } // Initialize settings void init(void) { glEnable(GL_DEPTH_TEST); // Enable depth testing glEnable(GL_LIGHTING); // Enable lighting glEnable(GL_LIGHT0); // Enable diffuse light glShadeModel(GL_SMOOTH); // Smooth shading } // Camera control void camera(void) { glRotatef(xrot, 1.0, 0.0, 0.0); glRotatef(yrot, 0.0, 1.0, 0.0); glTranslated(-xpos, -ypos, -zpos); } // Display callback void display(void) { glClearColor(0.0, 0.0, 0.0, 1.0); // Clear the screen to black glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // Camera setup camera(); cube(); // Draw the cubes glutSwapBuffers(); angle++; // Increment angle } // Reshape callback void reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); } // Keyboard controls void keyboard(unsigned char key, int x, int y) { if (key == 'q') { xrot += 1; if (xrot > 360) xrot -= 360; } if (key == 'z') { xrot -= 1; if (xrot < -360) xrot += 360; } if (key == 'w') { float xrotrad = xrot / 180 * 3.141592654f; float yrotrad = yrot / 180 * 3.141592654f; xpos += sin(yrotrad); zpos -= cos(yrotrad); ypos -= sin(xrotrad); } if (key == 's') { float xrotrad = xrot / 180 * 3.141592654f; float yrotrad = yrot / 180 * 3.141592654f; xpos -= sin(yrotrad); zpos += cos(yrotrad); ypos += sin(xrotrad); } if (key == 'd') { yrot += 1; if (yrot > 360) yrot -= 360; } if (key == 'a') { yrot -= 1; if (yrot < -360) yrot += 360; } if (key == 27) { // Escape key glutLeaveGameMode(); // Exit fullscreen mode exit(0); // Quit program } } // Main function int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutGameModeString("1024x768:32@75"); // Fullscreen mode settings glutEnterGameMode(); // Enter fullscreen mode init(); // Initialize settings glutDisplayFunc(display); // Display callback glutIdleFunc(display); // Idle callback glutReshapeFunc(reshape); // Reshape callback glutKeyboardFunc(keyboard); // Keyboard callback glutMainLoop(); // Enter the GLUT event loop return 0; }
If you have any questions or run into issues, feel free to email me at swiftless@gmail.com. Happy coding!
In my case, to set proper fullscreen (without window frame and etc.) I use:
glutGameModeString(“1366×768:32@60”);
glutEnterGameMode();
glutFullScreen();
Why not use:
glutFullScreenToggle();
It will go into full screen if we’re currently in windowed mode and to windowed mode if we’re in full screen.
Not sure how to change the resolution of the full screen mode, but should be the same as changing it for the window.
while i was executing the program it is giving the compile errors
Hi mr.jagan,
What are the compile errors? Some people have mentioned in other comments on this page that they have had to make changes. I’m guessing GLUT and FreeGLUT have an incompatibility here.
Cheers,
Swiftless
It’s working for me Swiftless and your tutorial is really helpful now that i deal with openGL, keep at it! 🙂
Unrecognized game mode string error comes up.. 🙁
Hey Nishant,
Have you tried changing the game mode string, eg: “990×768:32@75” to something that your monitor supports? This was done when I had my old CRT monitory and most LCD displays don’t use 75Hz.
Cheers,
Swiftless
Seems like it’s not working for me.
I get the error
“Unrecognized game mode string word: 1200î800:32@60”
My game mode string is “1200×800:32@60”. what to do now? :O
Other than that, your tutorials are really really really awesome! Keep em coming!
I got the same error – check “1200×800:32@60″ string and be sure “x” is x 🙂 delete and press “x” from keyboard….
Thanks to your solution! My code is now working by retyping ‘x’!
Huum, been having a play around with this, though it dosent seem to want to go fullscreen with a single buffer, and when i use a double buffer it seems to stop my update calls.
glutIdleFunc (display); dosent seem to work, and as b4
glutPostRedisplay(); stops working with a double buffer.
As far as I’m aware, if you’re using duble buffers, make sure you’ve got “glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);” and “glutSwapBuffers();” inside your ‘display’ function and that should work, if you’ve got both and its still happenning, then idk lol.