- Asked 5/25
- 2074 Asoj · 4+2 marks
- 2071 Chaitra · 4+2 marks
- 2075 Asoj · 2+2 marks
- 2073 Shrawan · 6 marks
- 2073 Chaitra · 8 marks
What is OpenGL? 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.
What is OpenGL (why is OpenGL required)? Explain the callback function.
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
#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').
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Callback function with example · Computer Graphics, chapter 9 · asked 3×
- What is OpenGL and why needed · Computer Graphics, chapter 9 · asked 8×