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!

  • March 25, 2010
  • 12