Page 82 - Computer Graphics Handout
P. 82
void demo_menu(int id)
{
switch(id)
{
case 1:
exit(0);
break;
case 2:
glutIdleFunc(idle);
break;
case 3:
glutIdleFunc(NULL);
break;
}
glutPostRedisplay();
}
The call to glutPostRedisplay requests a redraw through the glutDisplayFunc callback, so that the screen is drawn again without the
menu. GLUT also supports hierarchical menus, as shown in Figure 2.47. For example, suppose that we want the main menu that we
create to have only two entries. The first entry still causes the program to terminate, but now the second causes a submenu to pop
up. The submenu contains the two entries for turning the rotation on and off. The following code for the menu (which is in main)
should be clear:
sub_menu = glutCreateMenu(rotation_menu);
glutAddMenuEntry("start rotation", 2);
glutAddMenuEntry("stop rotation", 3);
glutCreateMenu(top_menu);
glutAddMenuEntry("Quit", 1);
glutAddSubMenu("start/stop rotation", sub_menu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
Writing the callback functions, rotation_menu and top_menu, should be a simple exercise.
2.13 Scan Conversion and Drawing Algorithms
Scan conversion is a fundamental process in computer graphics that converts geometric descriptions of objects into a raster
representation suitable for display on pixel-based devices. Since display screens are composed of discrete pixels, efficient drawing
algorithms are required to approximate continuous geometric entities such as lines, circles, and curves. This section presents the
essential drawing algorithms used in raster graphics systems and introduces techniques for improving visual quality.
2.13.1 Line Drawing Algorithms (DDA, Bresenham)
Line drawing is one of the most basic operations in computer graphics. Two widely used algorithms for raster line generation are
the Digital Differential Analyzer (DDA) and Bresenham’s Line Algorithm.
The DDA algorithm generates a line by incrementally calculating intermediate points between the start and end coordinates using
floating-point arithmetic. At each step, one coordinate is incremented by a fixed amount, and the corresponding pixel position is
determined by rounding the calculated values.
Bresenham’s Line Algorithm improves upon DDA by using only integer arithmetic to determine the nearest pixel to the ideal line
path. This approach significantly reduces computational overhead while maintaining high accuracy, making Bresenham’s algorithm
well suited for real-time and hardware-based graphics systems.
2.13.2 Circle and Curve Drawing
Drawing circles and curves on raster displays requires specialized algorithms due to their non-linear nature.
82

