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

# What is OpenGL? Callback function

Computer Graphics · Chapter 9: Introduction to Open GL

- Asked in: 2074 Asoj, 4+2 marks; 2071 Chaitra, 4+2 marks; 2075 Asoj, 2+2 marks; 2073 Shrawan, 6 marks; 2073 Chaitra, 8 marks (asked 5 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/opengl-callback-function
- Whole chapter: https://www.bctengineer.com/md/semester_5/computer_graphics/chapter-9

## Question

What is OpenGL (why is OpenGL required)? Explain the callback function.

## Short answer

OpenGL is a platform-independent API for hardware-accelerated 2D/3D rendering, needed for easy, fast, and portable graphics programming. Callback functions are user-defined routines registered with GLUT (e.g., glutDisplayFunc) that are automatically invoked on events like redraw, key press, or mouse click, enabling event-driven interaction.

## Answer

#### OpenGL
**OpenGL** is a cross-platform API of about 250 functions for rendering 2D and 3D graphics using the graphics hardware. It is required because it makes graphics programming hardware-independent, easy and fast: drawing primitives, transformations, lighting, texturing and hidden surface removal can be done with simple function calls, and the same program works on different systems.

#### Callback function
A **callback function** is a function written by the programmer that is **registered** with the system (GLUT) and called automatically by the library when a particular event occurs. OpenGL/GLUT programs are event driven: after registration, `glutMainLoop()` waits for events and calls the matching callback.

| Registration function | Called when |
|---|---|
| glutDisplayFunc(f) | The window must be redrawn |
| glutReshapeFunc(f) | The window is resized |
| glutKeyboardFunc(f) | A key is pressed |
| glutMouseFunc(f) | A mouse button is pressed/released |
| glutMotionFunc(f) | The mouse moves with a button pressed |
| glutIdleFunc(f) | No other events (used for animation) |
| glutTimerFunc(ms, f, v) | After a given time |

**Example**

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

void display(void) {              // display callback
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 1, 0);
    glBegin(GL_POLYGON);
        glVertex2f(-0.5f, -0.5f); glVertex2f(0.5f, -0.5f);
        glVertex2f(0.5f, 0.5f);   glVertex2f(-0.5f, 0.5f);
    glEnd();
    glFlush();
}
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);
    glutCreateWindow("Callbacks");
    glutDisplayFunc(display);      // register callbacks
    glutKeyboardFunc(keyboard);
    glutMainLoop();                // wait for events
    return 0;
}
```

Here GLUT calls `display` whenever the window needs redrawing, and `keyboard` when a key is pressed (the program exits on 'q').

## Check yourself

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

1. What is a callback function in GLUT?
   A. A function called directly by main in sequence
   B. A programmer-written function registered with GLUT that is called automatically when an event occurs
   C. A function that returns a window handle
   D. A built-in OpenGL drawing routine
   Answer: B. The library calls it when the event happens. (Key point: Definition)

2. What does glutMainLoop() do?
   A. Draws the scene once and exits
   B. Registers the display callback
   C. Waits for events and calls the matching callbacks
   D. Creates the window
   Answer: C. After registration the program is event driven. (Key point: Event driven)

3. When is the function registered with glutDisplayFunc called?
   A. When a key is pressed
   B. When the mouse moves
   C. After a fixed time only
   D. When the window must be redrawn
   Answer: D. It holds the drawing code. (Key point: Display callback)

4. Which callback is typically used for animation?
   A. glutIdleFunc
   B. glutKeyboardFunc
   C. glutReshapeFunc
   D. glutMouseFunc
   Answer: A. The idle callback runs when there are no other events. (Key point: Idle callback)

## Related in Computer Graphics

- [Callback function with example](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Acallback-function-opengl-example) — Computer Graphics, chapter 9, asked 3×
- [What is OpenGL and why needed](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Awhat-is-opengl-why-required) — Computer Graphics, chapter 9, asked 8×
