Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Computer Graphics

Chapter 6

2D Viewing

Nguyen Huu Cuong, Ph.D.


6.1 The 2D viewing pipeline

• A section of a 2D scene that is selected for display is called a clipping window


• Only the part of the scene that shows up on the screen is what is inside the
clipping window.
• The clipping window is alluded to as the world window or the viewing window.
• Objects inside the clipping window are mapped to a viewport. The viewport
indicates where it is to be viewed on the output device.
6.1 The 2D viewing pipeline

• By changing the position of a viewport, we can view objects at different positions


on the display area of an output device.
• The mapping of a 2D, world coordinate scene description to device coordinates is
called a 2D viewing transformation (window-to-viewport transformation or
windowing transformation).

Construct world-
Convert world- Transform viewing Map normalized
WC coordinate scene WC coordinates to
VC coordinates to
NC coordinates to DC
using modeling-
viewing normalized device
coordinate
coordinates coordinates coordinates
transformations
6.2 OpenGL 2D viewing functions

• Basic OpenGL library has no functions for 2D viewing. But the core library contains
a viewport function.
• The OpenGL Utility (GLU) does provide a 2D function for specifying the clipping
window, and GLUT functions for handling display windows.
OpenGL projection mode
• Before select a clipping window and a viewport in OpenGL, we need to establish
the appropriate mode for constructing the matrix to transform from world
coordinates to screen coordinates.
• To designate the project matrix as the current matrix

glMatrixMode(GL_PROJECTION);

GLU clipping-window function


• To define a 2D clipping window
gluOrtho2D(xwmin, xwmax, ywmin, ywmax);
OpenGL viewport function

• Specify the viewport parameters


glViewport(xvmin, yvmin, vpWidth, vpHeight);
• Obtain the parameters for the currently active viewport
glGetIntegerv(GL_VIEWPORT, vpArray);

Creating a GLUT display window


glutInit(&argc, argv);

glutInitWindowPosition(xTopLeft, yTopLeft);
glutInitWindowSize(dwWidth, dwHeight);
glutCreateWindow("Title of Display Window");
Setting the GLUT display-window mode and color

glutInitDisplayMode (mode);

glClearColor (red, green, blue, alpha);

glClearIndex (index);

GLUT display window identifier


Multiple display windows can be created for an application, each is assigned
a positive integer display window identifier, starting with the value 1
windowID = glutCreateWindow("A Display Window");

Deleting a GLUT display window


glutDestroyWindow (windowID);
Current GLUT display window

glutSetWindow (windowID);

currentWindowID = glutGetWindow();

Relocating and resizing a GLUT display window


Reset the screen location for the current display window
glutPositionWindow(xNewTopLeft, yNewTopLeft);

Reset the size of the current display window


glutReshapeWindow(dwNewWidth, dwNewHeight);

Expand the current display window to fill the screen


glutFullScreen();

Whenever the size of a display window is changed, we adjust for a change


in display window dimensions
glutReshapeFunc(winReshapeFcn);
Managing multiple GLUT display window
Convert the current display window to an icon in form of a small picture or
symbol representing the window
glutIconifyWindow ();

Change the name of the display window


glutSetWindowTitle("New Window Name");

With multiple display windows open on the screen, to choose any display
window to be in front of all other windows
glutSetWindow(windowID);
glutPopWindow();

To choose any display window to be behind all other windows


glutSetWindow(windowID);
glutPushWindow();

Take the current window off the screen


glutHideWindow ();

Return a “hidden” display window


glutShowWindow ();
GLUT subwindows
Within a selected display window, we can set up any number of second-
level display windows, called subwindows
glutCreateSubWindow(windowID, xBottomLeft, yBottomLeft,
width, height);

Selecting a display-window screen-cursor shape


glutSetCursor (shape);

Viewing graphics objects in a GLUT display window


glutDisplayFunc (pictureDescrip);

Executing the application program


glutMainLoop ();

You might also like