An Overview of Opengl: I Iti Primitives
An Overview of Opengl: I Iti Primitives
An Overview of OpenGL
One of the first things we need to do when creating a picture is to describe the component parts
of the scene to be displayed.
For each type of scene, we need to describe the structure of the individual objects and their
coordinate locations within the scene.
Those functions in graphics package that we use to describe the various picture components are
called the graphics output primitives, or simply primitives.
The output primitives describing the geometry of objects are typically referred to as geometric
primitives.
i iti
Point positions and straight-line segments are the simplest geometric primitives.
Overview of OpenGL
After the geometry of a picture has been specified within a selected coordinate reference
frame, the output primitives are projected to a two dimensional plane, corresponding to
the display area of an output device, and scan converted into integer pixel positions within
the frame buffer.
To describe
d
b a picture, we first
f decide
d d upon a convenient Cartesian coordinate
d
system,
called world coordinate reference frame (2D or 3D).
These coordinate positions are stored in the scene description along with other information
about the objects, such as their color and their coordinate extents which are minimum
and maximum x, y, and z values for each object (bounding box or bounding
rectangle).
Objects are then displayed by passing the scene information to the viewing routines
routines, which
identify visible surfaces and ultimately map the objects to positions on the video screen.
Overview of OpenGL
9/21/2015
Overview of OpenGL
Screen Coordinates
Locations on a video monitor are references in integer screen coordinates, which corresponds
to the pixel positions in the frame buffer.
Pixel coordinate values give the scan line number (the y value) and the column number (the x value
along a scan line).
Hardware processes, such as screen refreshing, typically address pixel positions with respect to
the top-left corner of the screen.
Scan-line algorithms for the graphics primitives use the defining coordinate descriptions to
determine the locations of pixels that are to be displayed.
displayed
Once pixel positions have been identifies for an object, the appropriate color values must be
stored in the frame buffer.
Overview of OpenGL
9/21/2015
glCopyPixels,
glPolygonMode
Certain functions require that one (or more) of their argument be assigned a symbolic constant
specifying, for instance, a parameter name, a value for a parameter, or a particular mode. All
such constants begin with the uppercase letter GL.
GL_2D,,
Overview of OpenGL
Related Libraries
The OpenGL Utility (GLU) provides routines for setting up viewing and projection matrices,
describing complex objects with line and polygon approximations, ..... , processing surface
rendering tasks.
y and all GLU function names start with
Everyy OpenGL implementation includes the GLU library,
the prefix glu.
To create a graphics display using OpenGL, we first need to set up a display window on our video
screen. There are several window-system libraries that support OpenGL functions for a variety
of machines.
The Opengl Extension to the X Windows System (GLX) provides a set of routines that are
prefixed with the letter glX. For Microsoft Windows systems, the WGL routines provides a
Wi d
Windows-to-OpenGL
t O
GL interface.
i
f
The OpenGL Utility Toolkit (GLUT) provides a library of functions for interacting with any
screen-windowing system. The GLUT library functions are prefixed with glut.
Here is where to obtain glut: https://www.opengl.org/resources/libraries/glut/
6
Overview of OpenGL
9/21/2015
Header Files
We need to include the header file for the OpenGL core library. For most applications we
will also need GLU. We need to also include the header file for the window system. For
instance, with Microsoft Windows, the header file that access the WGL routines is
windows.h .
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
In most cases, we can use GLUT to handle the window-managing operations and it may not
necessary to include gl.h and glu.h since GLUT ensures that these will be included
correctly.
#include <GL/glut.h>
In addition, we will often need to include other header files such as:
#include <stdio.h>
#include <math.h>
7
Overview of OpenGL
Overview of OpenGL
9/21/2015
But the display windows is not on the screen. We need one more GLUT function to complete the
window-processing operations. After execution of the following statement, all display windows that
we have created, including their graphic content, are activated.
glutMainLoop ( );
This function must be the last one in our program. It displays the initial graphics and puts the
program into an infinite loop that checks for input from devices such as a mouse or keyboard.
Although the display windows that we have created will be in some default location and size, we can
set these parameters using additional GLUT functions.
We use the glutInitWindowPosition function to give an initial location for the top left corner of the
display window. For example, the following statement specifies that the top-left corner of the display
window should be placed 50 pixels to the right of the left edge of the screen and 100 pixels down
from the top edge of the screen.
glutInitWindowPosition (50, 100);
Overview of OpenGL
Similarly, the glutInitWindowSize function is used to set the initial pixel width and height of the
display window. For example, we can specify a display windows with an initial width of 400
pixels and a height of 300 pixels:
glutInitWindowSize (400, 300);
Other examples such as choice of buffering and a choice of color modes can be specify through
glutInitDisplayMode function. Arguments for this routine are assigned symbolic GLUT
constants. For example, the following command specifies that a single refresh buffer is to be
used for the display window and that the RGB (red, green, blue) color mode is to be used for
selecting color values.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB );
There are other display functions for color mode, double buffering for animation and selecting
parameters for viewing three-dimensional scenes.
10
Overview of OpenGL
9/21/2015
11
Overview of OpenGL
Although glClearColor command assigns a color to the display window, it does not put the display
windows on the screen. To get the assigned window color displayed, we need to invoke the following
OpenGL function.
glClear (GL_COLOR_BUFFER_BIT);
The argument is an OpenGL symbolic constant specifying that it is the bit values in the color buffer
(refresh
f h buffer)
b ff that
h are to be
b set to the
h values
l indicated
d
d in the
h glClearColor function.
f
In addition to setting the background color for the display window, we can choose a variety of color
schemes for the objects we want to display in a scene. For example, one can simply set object color
to be red.
glColor3f ( 1.0, 0.0, 0.0);
The suffix 3f on the glColor function indicates that we are specifying the three RGB color components
using floating-point (f) values.
12
Overview of OpenGL
9/21/2015
For two dimensional objects, we need to tell OpenGL how we want to project our picture onto the
display windows because generating two dimensional picture is treated by OpenGl as a special case of
three-dimensional viewing.
We set the projection type (mode) and other viewing parameters that we need with the following two
functions.
glMatrixMode (GL_PROJECTION) ;
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
This specifies that an orthogonal projection is to be used to map the contents of a two-dimensional
(2D) rectangular area of the world coordinates to the screen, and that the x-coordinate values within
this rectangle range from 0.0 to 200.0 with y-coordinate values ranging from 0.0 To 150.0. Whatever
objects we define within this world-coordinate rectangle will be shown within the display window.
Anything outside this coordinate range will not be displayed.
Therefore, the GLU function gluOrtho2D defines the coordinate reference frame within the display
window to be (0.0, 0.0) at the lower-left corner of the display window and (200.0, 150.0) at the
upper-right window corner.
13
Overview of OpenGL
Finally, we need to call the appropriate OpenGL routines to create, for example, a line
segment. The following code defines a two-dimensional, straight-line segment with integer,
Cartesian endpoint coordinates (180, 15) and (10, 145).
glBegin (GL_LINES);
glVertex2i (180, 15);
glVertex2i
lV t 2i (10,
(10 145);
145)
glEnd ( );
The following OpenGL program is organized into three procedures. All the initializations and
related one-time parameter settings are in procedure init.
Geometric description of the picture than needs to be displayed is in procedure lineSegment,
which is the procedure that will be referenced by the GLUT function glutDisplayFunc.
The main procedure contains the GLUT functions for setting up the display window and getting
our line segment onto the screen.
14
Overview of OpenGL
9/21/2015
15
Overview of OpenGL
16
Overview of OpenGL