You are on page 1of 24

Chapter Two

Introduction to rendering process with openGL


Agendas:
 Coordinate system
 OpenGL primitives and attributes
 OpenGL rendering pipeline
Coordinate System Basics
The default coordinate space is stretch from -1 to +1 in x,
y and z directions. Where
x- is the horizontal axis
y- is the vertical axis
z- comes out of screen towards you
Window-Viewport Mapping
gluOrtho2D (left, right, bottom, top)
Sets up a 2-D orthographic viewing region Defined by two
vertical clipping planes left and right and two horizontal
clipping planes bottom and top. The default is (-1,1,-1,1).
glViewport (x, y, width, height)
The viewport defines the area within the window in actual
screen coordinates that OpenGL can use to draw in.
By default, it uses the whole graphics area of the interface
window.
Default viewport
void draw()
{
glClearColor( 0, 0, 0 );
glClear ( GL_COLOR_BUFFER_BIT );
gluOrtho2D(0.0,50.0,-10.0,40.0);
glColor3f( 0, 0, 1 );
glbegin(GL_LINES); //draw a blue line
----------
----------
glEnd();
single viewport
void draw()
{
glClearColor( 100, 100, 0, 0 );
glClear ( GL_COLOR_BUFFER_BIT );
glviewport (0,0,125,125);
gluOrtho2D(0.0,50.0,-10.0,40.0);
glColor3f( 0, 0, 1 );
glbegin(GL_LINES); //draw a blue line
----------
----------
glEnd();
Two viewport
void draw() {
glClearColor( 100, 100, 0, 0 );
glClear ( GL_COLOR_BUFFER_BIT );
glviewport (0,0,125,125);
gluOrtho2D(0.0,50.0,-10.0,40.0);
glColor3f( 0, 0, 1 );
glbegin(GL_LINES); //draw a blue line
----------
glEnd();
glviewport (0,0,125,125);
gluOrtho2D(0.0,50.0,-10.0,40.0);
glbegin(GL_POLYGON); //draw a blue line
-----------
glEnd();
OpenGL Primitives: Drawing Points, Lines and
Polygons with glVertex
A primitive in OpenGL is simply a collection of vertices,
hooked together in a predefined way.
A single point for example is a primitive that requires
exactly one vertex. Another example is a triangle, a
primitive made up of three vertices. The type of a primitive
is defined by the number of vertices it is constructed from.
A point has only one vertex, a line has two, a triangle has
three…….
The process..
 Declare what kind of primitive we are drawing.
glBegin(mode);
mode is basically the type of primitive we’re drawing.
Note: All constants in OpenGL use the prefx “GL_” i.e.
glBegin(GL_MODE);
 Declare our vertices.
i.e. glVertex3f(float x, float y);
 Declare that we are finished
i.e. glEnd();
OpenGL organizes vertices into primitives based upon
which type is passed into glBegin(). The possible types are:
GL_POINTS GL_LINE_STRIP
GL_LINES GL_LINE_LOOP
GL_POLYGON GL_TRIANGLE_STRIP
GL_TRIANGLES GL_TRIANGLE_FAN
GL_QUADS GL_QUAD_STRIP
GL_POINTS
Simply draws the points in the order you pass them in.
Draws a point at each of the n vertices.
glBegin(GL_POINTS);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, 3.0);
glVertex2f(4.0, 3.0);
glVertex2f(6.0, 1.5);
glVertex2f(4.0, 0.0);
glEnd();
GL_LINES
Draws a series of unconnected line segments. Segments
are drawn between v0 and v1, between v2 and v3, and so
on. If n is odd, the last segment is drawn between vn-3
and vn-2, and vn-1 is ignored.
GL_LINE_STRIP:
series of connected line segments
Draws a line segment from v0 to v1, then from v1 to v2, and
so on, finally drawing the segment from vn-2 to vn-1. Thus, a
total of n-1 line
segments are drawn. Nothing is drawn unless n is larger than
1. There are no restrictions on the vertices describing a line
strip (or a line loop);
the lines can intersect arbitrarily.
GL_LINE_LOOP
same as above, with a segment added between last and first
vertices.
Bresenham's Line Algorithm
• Bresenham's Algorithm is efficient (fast) and accurate.
• The key part of Bresenham's algorithm lies in the determination
of which pixel to turn on based on whether the line equation
would cause the line to lie more near one pixel or the other.
Bresenham's Line Algorithm
• Think of a line whose slope is >0, but < 1. These are first octant lines (the
area of the coordinate plane between the positive x-axis and the line y=x.)
• Assume we have the pixel at (xk, yk) turned on and we're trying to determine
between the following:
– Should (xk+1, yk) or (xk+1, yk+1) be on?
– That is, the choice is between either the pixel one to the right of (xk, yk)
or the pixel one to the right and one up.
• The decision can be made based on whether dlower - dupper is positive or not.
• The goal is to come up with an inexpensive way to determine the sign of

(dlower – dupper )
Bresenham's Line Algorithm
• The y coordinate on the line is calculated by: y = m(xk + 1) + b
and
• dlower = y – yk

• dupper = yk+1 – y
– dupper = 2m(xk + 1) – 2yk + 2b –
dlower
• Since 1m is < 1 (recall it's in the 1st octant) it is a non-integer. That's not good so,
• replace it with dy/dx, where dy is the change in y endpoints and dx is the change in
x endpoints – these will be integer values divided --- but we'll get rid of the division.
• – dupper = 2 ( dy / dx )(xk + 1) – 2yk + 2b –
dlower
1 both sides by dx and get:
• Multiply
• – dupper ) = 2 dy (xk + 1) – 2 dx yk + 2 dx b –
dx (dlower
• dx
• 2
dxdy – bdupper
+ 2 dx
(dlower – dx) = is
2 a constant
dy xk –which
2 dx yisk + 2 dy + 2 dxofb each
independent – iteration
dx
Bresenham's Line Algorithm
• So we end up with
• – dupper ) = 2 dy xk – 2 dx yk + c
dx (dlower
• We only need to determine the sign of
– dupper )
(d
dxlower
is positive so it doesn't have an effect on the sign (so now there's no need to
• divide) --- does that make sense?
• Letpk = dx (dlower is the decision parameter
– dupper )
pk
• So, we check if 2 dy xk – 2 dx yk + c < 0 and if it is we turn on the lower pixel,
so

(xk+1, yk) should be the pixel turned on



otherwise (xk+1, yk+1) is turned on

As the algorithm iterates though it needs to then calculate the next value of p,
which is pk+1
Bresenham's Line Algorithm
• Recall that to calculate pk we had: pk = 2 dy xk – 2 dx yk + c

• So, pk+1 = 2 dy xk+1 – 2 dx yk+1 + c

• If we subtract pk+1 – pk = 2dy ( xk+1 – xk ) – 2dx ( yk+1 – yk )

• But, xk+1= xk + 1 because the first octant slope always increment x by 1

• So, we have pk+1 – pk = 2dy – 2dx ( yk+1 – yk )

• Then, pk+1 = pk + 2dy – 2dx ( yk+1 – yk )

• Where ( yk+1 – yk ) is either 1 or 0 (why?) depending on the sign of pk . So,


• peither
k+1 = pk + 2dy (when pk < 0)

• pk+1 = pk + 2dy – 2dx (when pk >= 0)


Initial decision variable value
• The initial value of p in the Bresenham line algorithm
is calculated by using the equation for p with (x0, y0)
• Derivation:
pk = 2dyxk – 2dxyk + c
where c = 2dy + dx(2b-1)
and since yk=mxk+b, b = yk–mxk and m =
dy/dx

pk = 2dyxk – 2dxyk + 2dy + dx(2(yk– (dy/dx)xk)– 1)


multiplied out:
pk = 2dyxk – 2dxyk + 2dy + 2dxyk– 2dyxk – dx
= 2dy – dx
Bresenham's Line Algorithm
/* Bresenham line-drawing procedure for |m| < 1.0. */ setPixel (x, y);
void lineBres (int x0, int y0, int xEnd, int yEnd) while (x < xEnd) {
{ x++;
int dx = fabs (xEnd - x0), dy = fabs(yEnd - y0); if (p < 0)
int p = 2 * dy - dx; p += twoDy;
int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx); else {
int x, y; y++;
p +=
/* Determine which endpoint to use as start position. */ twoDyMinu
sDx;
if (x0 > xEnd) {
}
x = xEnd; y
setPixel (x,
= yEnd; y);
xEnd = x0; }
} }
else { /* From Hearn & Baker's Computer Graphics with
x = x0; OpenGL, 3rd Edition */
y = y0;
}

You might also like