---
title: "Callback function with example"
subject: "Computer Graphics"
chapter: 9
key: "semester_5/computer_graphics:callback-function-opengl-example"
source: https://www.bctengineer.com/q/semester_5/computer_graphics/callback-function-opengl-example
site: BCT Engineering
---

# Callback function with example

Computer Graphics · Chapter 9: Introduction to Open GL

- Asked in: 2074 Chaitra, 5 marks; 2076 Chaitra, 4 marks; 2070 Chaitra, 3 marks (asked 3 times)
- Video: [Online Graphics OpenGL 1: Window System and Callbacks](https://www.youtube.com/watch?v=k72Rp9CN-Ik) — raviramamoorthi
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_5/computer_graphics/callback-function-opengl-example
- Whole chapter: https://www.bctengineer.com/md/semester_5/computer_graphics/chapter-9

## Question

Explain the callback function with example in OpenGL.

## Short answer

Callback functions are registered via GLUT (e.g., glutDisplayFunc, glutKeyboardFunc) and called automatically on events. In an example, a display callback redraws a colored square, a keyboard callback exits on 'q', and a mouse callback changes the color and requests redisplay with glutPostRedisplay, driven by glutMainLoop.

## Answer

A **callback function** is a user-defined function that is registered with GLUT (`glutXxxFunc`) and is called automatically by the library when a specific event happens (redraw, key press, mouse click, window resize, idle time). Because of this, an OpenGL program is event driven: `glutMainLoop()` waits for events and calls the proper callback.

Common callbacks: `glutDisplayFunc` (draw), `glutReshapeFunc` (resize), `glutKeyboardFunc` (keys), `glutMouseFunc` (mouse buttons), `glutIdleFunc` (animation), `glutTimerFunc`.

**Example:** a square that changes colour with a mouse click and closes with the 'q' key

```c
#include <GL/glut.h>
#include <stdlib.h>

float r = 1, g = 0, b = 0;

void display(void) {                       // display callback
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(r, g, b);
    glBegin(GL_QUADS);
        glVertex2f(-0.5f, -0.5f); glVertex2f(0.5f, -0.5f);
        glVertex2f(0.5f, 0.5f);   glVertex2f(-0.5f, 0.5f);
    glEnd();
    glFlush();
}
void mouse(int button, int state, int x, int y) {   // mouse callback
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        r = (float)rand() / RAND_MAX;
        g = (float)rand() / RAND_MAX;
        b = (float)rand() / RAND_MAX;
        glutPostRedisplay();               // ask GLUT to call display again
    }
}
void keyboard(unsigned char key, int x, int y) {    // keyboard callback
    if (key == 'q') exit(0);
}
int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutCreateWindow("Callback demo");
    glutDisplayFunc(display);              // register callbacks
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}
```

Each left click calls `mouse`, which changes the colour and requests a redraw; GLUT then calls `display`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a callback function?
   A. A function that GLUT defines for drawing
   B. A function that stops the program
   C. A user-defined function registered with GLUT and called automatically when an event occurs
   D. A function that must be called from main
   Answer: C. It handles events like redraw, key press and click. (Key point: Meaning)

2. How is the mouse callback registered in the example?
   A. glutMotionFunc(mouse)
   B. glutDisplayFunc(mouse)
   C. glutKeyboardFunc(mouse)
   D. glutMouseFunc(mouse)
   Answer: D. Registration uses glutXxxFunc. (Key point: Registration)

3. What does glutPostRedisplay() do in the mouse callback?
   A. Asks GLUT to call the display callback again
   B. Draws the square directly
   C. Closes the window
   D. Resets the colour
   Answer: A. After changing colour, a redraw is requested. (Key point: glutPostRedisplay)

4. What happens when the user presses 'q' in the example?
   A. The colour changes
   B. The keyboard callback calls exit(0)
   C. The window resizes
   D. Nothing happens
   Answer: B. The keyboard callback checks for 'q' and exits. (Key point: Keyboard)

## Related in Computer Graphics

- [What is OpenGL? Callback function](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Aopengl-callback-function) — Computer Graphics, chapter 9, asked 5×
