02 - Primitives
02 - Primitives
INTRODUCTION
(REVIEW)
CRT Displays
• CRT = Cathode Ray Tube
Shadow Mask
Electron Guns
Red Input
Green
Input
Blue Input
Deflection
Yoke Red, Blue,
and Green
Phosphor Dots
Types of Graphics Display Devices
CRT displays can work in one of two ways:
• Random scan devices:
– Vector displays
– CRT electron beam directed
only to areas of screen where
picture is to be displayed
– Picture displayed as a set of
‘primitives’ (lines, vectors)
– although some early video
games did use random scan
technology. These days random
scan is only really used by some
hard-copy plotters.
• Raster scan devices:
– Object converted into set of
dots, stored in a frame buffer
– CRT electron beam swept
across screen line-by-line
Raster Scan: Terminology
16
Using OpenGL with Dev-C++
• File menu, New, Project …
17
Adding Libraries in Dev-C++
18
Before displaying any graphics primitives we
always need to perform some basic initialisation
tasks, including creating a window for display.
The following lines initialise the glut library,
define a frame buffer and create a window for
display.
glutInit(&argc, argv)
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB);
glutCreateWindow(“GLUT Points demo”);
19
•glutInit(&argc, argv);
This line initialises the glut library. We must call this function
before any others in our glut/OpenGL programs.
•glutInitWindowSize(640,480);
This function call sets the size of the display window in pixels.
•glutInitWindowPosition(10,10);
This line sets the position of the top-left corner of the display
window, measured in pixels from the top-left corner of the
monitor screen.
•glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
This line defines the type of frame buffer we want to have. In
this case, we are asking for a single RGB (i.e. colour) frame
buffer.
•glutCreateWindow(“GLUT Points demo”);
Finally, this function call creates the display window with
the attributes defined by the previous calls. 20
The OpenGL Event Loop
• OpenGL is an event-driven package. This means
that it always executes code in response to
events. The code that is executed is known as a
callback function, and it must be defined by the
programmer.
• It has a callback function-which is a code that is
executed when that event happens
21
Cont…
• OpenGL will continually detect a number
of standard events in an event loop, and
execute the associated callback functions.
glutMainLoop();
• For example, mouse clicks and keyboard
presses are considered as OpenGL events.
The most important event is the display event
Therefore all OpenGL programs consist of a
number of parts:
• Perform any initialisation tasks such as
creating the display window.
• Define any required callback functions and
associate them with the appropriate
events.
• Start the event loop.
23
OpenGL Initialisation Routines
• The first thing we need to understand is that
the OpenGL graphics package is a state system.
This means that it maintains a list of state
variables, or state parameters, which will be
used to define how primitives will be drawn.
• These state variables are specified separately,
before the primitives are drawn.
• example, if we want to draw a blue point on our display
window, we first set the drawing colour state variable to
blue, and then we draw the point at the specified location
Cont…
• before we actually draw any primitives, we need to set
the values of some state variables
• we draw the points we assign values for three state
variables. at the beginning of the myInit function):
• glClearColor (1.0, 1.0, 1.0, 0.0);
glColor3f(0, 0,0);
glPointSize(4.0);
• In the first line we are defining the background colour
to have an RGB value of (1,1,1), which means white.
When we clear the screen later on it will be cleared to
white.
• The fourth argument to glClearColor is the alpha
value of the background colour. The alpha value
indicates the opacity of the colour (i.e. is it a semi-
transparent colour?): a value of 1 means that it is
completely opaque, or the transparency is zero.
• The second line listed above defines the current
drawing colour to be (0,0,0), or black. Any
subsequently drawn primitive will be drawn in this
colour.
• The final line sets the point size to four pixels. Any
subsequent point primitives drawn will be drawn with
this size.
OpenGL Coordinate Systems
• 2-D Primitives:
– Points
– Lines, polylines
– Circles
• 3-D Primitives
– Points
– Lines, polylines
– Spheres
– Polyhedra
Graphics Coordinate Systems
OpenGL Point Drawing Primitives
Desired Line
(xi+1, Round(yj+m))
(xi, yj)
(xi+1, yj+m)
(xi, Round(yj))
y2
y1
x1 x2
Example 1
• Let us consider an example of applying the
DDA algorithm for drawing a straight-line
segment. Referring to see the below given, we
first compute a value for the gradient m:
Now, because |m| ≤ 1, we compute δx
and δy as follows:
δx = 1
δy = 0.6
•The DDA algorithm is simple and easy to
implement, but it does involve floating point
operations to calculate each new point.
•Floating point operations are time-consuming
when compared to integer operations. Since line-
drawing is a very common operation in computer
graphics, it would be nice if we could devise a
faster algorithm which uses integer operations
only
Line Drawing-DDA Algorithm(class
work)
(X0,Y0)=(0,0) (Xend,Yend)=(5,3) Pixel (Xk,Yk)
M = 0.6 δx=1 δy=0.6
7
6 Yk
5
4
3
Xk
2
1
0
0 1 2 3 4 5 6 7
Line Drawing: Bresenham’s Algorithm
• Compute (dlower-dupper)
• If (dlower-dupper) is positive, then we choose A as the
next position in the line, otherwise choose B
Decision variable:
pk=2Δy.xk - 2Δx.yk + d
where d = 2Δy – 2cΔx - Δx
pk has the same sign as
(dlower-dupeer), and can be
calculated using integer
operations only
Example 3
0
0 1 2 3 4 5 6 7
OpenGL Line Functions
• The first and last vertices are only joined to one other vertex.
Finally, GL_LINE_LOOP is the same as GL_LINE_STRIP except
that the last point is joined to the first one to create a loop.
Circle Drawing Algorithms
• Equation of a circle: (Cartesian coordinate)
– (x - xc)2 + (y - yc)2 = r2
(xc,yc = centre of the circle)
• Alternatively (using polar coordinates r,Θ):
– x = xc + r.cos Θ
– y = yc + r.sin Θ
Circle Drawing Algorithms
(-b,-a) (b,-a)
9 4
8 3
7 2
Y=X
6 1
5 0
4 0 1 2 3 4
3
2 Y=0
1
0
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Ellipse-Drawing Algorithms
E1 = (1,0,0) E2 = (0,1,0)
Cross-product = u|E1||E2|Sin θ
(0 ≤ θ ≤ π)
Direction of u determined
using right-hand rule
Normal vector = (0,0,1)
OpenGL Fill-Area Routines
• OpenGL provides a variety of routines to draw fill-
area polygons.
• In all cases these polygons must be convex.
• In all, there are seven different ways to draw
polygons in OpenGL:
• glRect*
• Six different symbolic constants for use with glBegin
… glEnd
• We will now consider each of these techniques in
turn.
• glRect*
Some OpenGL Fill-Area Routines
• End of today class