Unit 1 PDF
Unit 1 PDF
•OpenGL basic library are prefixed with gl, and each component word within a
function name has its first letter capitalized.
ex:glBegin, glClear, glCopyPixels, glPolygonMode etc…
•All such constants begin with the Uppercase letters GL.In addition ,
component words within a constant name are written in capital letters,and the
underscore(_) is used as a separator between all component words in the name.
ex: GL_2D,GL_RGB,GL_COLOR_BUFFER_BIT etc…
•OpenGL uses special built-in, data-type names,
each data type name begins with the capital
letters GL, and the remainder of the name is a
standard data type designation written in
lowercase letters.
ex:GLbyte, GLshort, GLfloat, GLdouble etc..
Related Libraries
•The OpenGL basic library, there are a number of associated
libraries for handling special operations.
•The OpenGL Utility(GLU) provides routines for setting up
viewing and projection matrices, describing complex objects with
line and polygon approximation, displaying quadrics and B-
splines using linear approximation, processing the surface-
rendering operations, and other complex tasks.
•GLU function name start with the prefix glu. There is also an
object-oriented toolkit based on OpenGL called Open Inventor,
which provides routines and predfined object shapes for
interactive three-dimensional applications.This toolkit is written
in C++.
There are several window –system libraries that support OpenGL functions
for a variety of machines.
•The OpenGL Extension to the X Window System(GLX) provides a set of
routines that are prefixed with the letters glx.
•Apple systems can use the Apple GL(AGL) interface for window-
management operations. Function names for this library are prefixed with agl.
•Microsoft windows systems, the WGL routines provide a Windows-to-
OpenGL inetrface.These routines are prefixed with the letters wgl.
•The Presentation Manager to OpenGL(PGL) is an interface for the IBM
OS/2, which uses the prefix pgl for the library routines.
•The OpenGL Utility Toolkit(GLUT) provides a library fo functions are
prefixed with glut, and this library also contains methods for describing and
rendering quadric curves and surfaces.
•Its main objective is to connect the graphics package with the corresponding
OS.
Note : To create a graphics display using OpenGL we first need to set up a
display window on our video screen. This is simply the rectangular area of the
screen in which our picture will be displayed.
Library Organization of Open GL
Header Files
•The Header file for the window system.For
instance, with Microsoft windows, the header files
that accesses the WGL routines is windows.h.
•This header file must be listed before the OpenGL
and GLU header files because it contains macros
needed by the Microsoft Windows version of the
OpenGL libraries. So the source file begin with
#include<windows.h>
#include<GL/gl.h>
#include<GL/glu.h>
Continued…..
•GLUT to handle the window-managing operations, we do not need
to include gl.h and glu.h because GLUT ensures that these will be
included correctly so we can replace the header files for OpenGL
and GLU with
#include<GL/glut.h>
•On some systems ,the header files for OpenGL and GLUT routines
are found in different places in the file system.First instance , on
Apple OS x systems, the header file inclusion statement as follows
#include<GLUT/glut.h>
•In addition , we will need to include header files that are required
by the c++ code.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
Display-Window management using GLUT
Control functions supported by Open GL:
•Consider a simplified, minimal number of operations for displaying
a picture. The OpenGL Utility Toolkit first step is to initialize GLUT.
glutInit (&argc,argv);
Syntax : glutInit(int *argc, char **argv);
• The 2 arguments enables the programmer to pass command line
arguments.
glutDisplayFunc (lineSegment);
Syntax: glutDisplayFunc(void (*func)(void));
glutMainLoop();
Syntax:glutMainLoop(void);
•One more function is used to set the initial pixel width and height of the
display window. Thus , we specify a display window with an initial width of
400 pixels and a height of 300 pixels as shown in below statement
glMatrixMode (Gl_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0 );
#include<stdio.h>
Glenum errorCheck ( )
{
GLenum code;
const GLubyte *string;
5
4
3
2
1
0
0 1 2 3 4 5 x
Pixel positions referenced with respect to
the lower-left corner of a screen area.
•Once pixel positions have been identified for an
object , the appropriate color values must be
stored in the buffer.
setPixel(x,y);
•This procedure stores the current color setting
into the frame buffer at integer
position(x,y),relative to the selected position of
the screen-coordinate origin.To retrieve the
current frame-buffer setting for a pixel location
with color using following function:
getPixel(x,y,color);
•In this function , parameter color receives an
integer value corresponding to the combined
•Absolute Coordinate Specification
This means that the values specified are the actual positions
within the coordinate system in use.
•Relative Coordinate Specification
Some graphics package also allow positions to be specified using
relative coordinates. This method is useful for various graphics
applications, such as producing drawings with pen plotters, artist’s
drawing and painting systems, and graphics packages for
publishing and printing applications. Specify a coordinate position
as an offset from the last position that was referenced is called
current position.
Specifying a Two –Dimensional World-Coordinate Reference
Frame in OpenGL :
•The gluOrtho2D command is a function use to set up any two
dimensional Cartesian reference frame.The arguments for this
function are the four values defining the x and y coordinate limits
for the picture to display.
•The gluOrtho2D function specifies an orthogonal projection,by
specifyning the coordinate values are placed in the identify matrix as
the projection matrix.
•Assign the identity matrix as the projection matrix before defining
the world-coordinate range.
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluOrtho2D(xmin,xmax,ymin,ymax);
•The display window referenced by coordinates(xmin,ymin) at
the lower-left corner and by coordinates(xmax,ymax) at the
upper-left corner as shown in figure.
•The geometric describing a picture, all positions for the openGL
primitives must be given in absolute coordinates,with respect to
the reference frame defined in the gluOrtho2D function.
Video Screen
ymax
Display
Window
ymin
xmin
xmax
OpenGL Geometric Primitives
GL_LINES
GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP
GL_POINTS
GL_TRIANGLES
GL_QUADS
GL_QUAD_STRIP
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
OpenGL Point Functions
•To specify the geometry of a point, give a coordinate
position in the world reference frame.
•OpenGL function to state the coordinate values for a
single position:
glVertex*( );
•Where-> the asterisk(*) indicates that suffix codes are
required for this function.These suffix codes are used to
identify the spatial dimension ,the numerical data type to
be used for the coordinate values,and a possible vector
form for the coordinate specification.
•This single function is used for point,line and polygon
specification.
•glVertex functions must be placed between a glBegin( ) and
glEnd( ) as shown below syntax:
glBegin(GL_POINTS);
glVertex*( );
glEnd( );
glBegin(GL_POINTS);
glVertex2i (50,100);
glVertex2i(75,150);
glVertex2i(100,200); y
glEnd( );
glVertex2iv(p1); p1
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5); p4
p2
glEnd( );
glBegin(GL_LINE_STRIP); p3
glVertex2iv(p1);
glVertex2iv(p2); p5
p1
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
p4
p2
glEnd( );
glBegin(GL_LINE_LOOP);
glVertex2iv(p1); p3
glVertex2iv(p2);
p5 p1
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glEnd( ); p2
p4
glBegin(GL_POINTS);
glVertex3f(-78.05,909.72,14.60);
glVertex3f(261.91,-5200.67,188.33);
glEnd( );
Class wcPtd2D {
public:
Glfloat x,y;
};
wcPt2D pointPos;
pointPos.x = 120.75;
pointPos.y = 45.30;
glBegin(GL_POINTS);
glVertex2f(pointPos.x,pointPos.y);
glEnd();
Line Drawing Algorithms
•A straight-line segment in a scene is defined by
the coordinate positions for the endpoints of the
segment.
•To display the line on a raster monitor, the
graphics first project the endpoints to integer
screen coordinates and determine the nearest
pixel positions along the line path between the
two endpoints.
•Horizontal and vertical lines to be displayed
with a stair-step appearance(known as “the
jaggies”).
DDA[Digital Differential Analyzer] Algorithm
Step-1 :-Get the input of two end points(x0,y0) and (x1,y1)
Step-2:-Calculate the difference between two end points.
dx=x1-x0
dy=y1-y0
Step-3:-Based on the calculated difference in step-2,to identify the number of steps to put
pixel. If dx>dy, then we need more steps in x coordinate, otherwise in y-coordinate
if(dx>dy)
steps=absolute(dx);
else
steps=absolute(dy);
Step-4:-Calculate the increment in x-coordinate and y-coordinate
xincrement=dx/(float) steps;
yincrement=dy/(float) steps;
Step-5:Put the pixel by successfully incrementing x and y coordinates accordingly and complete the drawing of the line
for (int v=o; v<steps; v++)
{
x=x+xincrement;
y=y+yincrement;
putPixel(x,y);
}
Bresenham’s Line Algorithm
•An accurate and efficient raster line-generating algorithm, developed by
Bresenham.
•To determine at the next sample position whether to plot the pixel at
position(11,11) or the one at (11,12).
•To illustrate Bresenham’s approach, first consider the scan-conversion process for
lines with positive slope less than 1.0.
•Starting from the left endpoint(x0,y0) of a given line , determined that the pixel at
(xk,yk) and to decide which pixel to plot in column xk+1=xk+1 choices are the pixels at positions(xk+1,yk) and
(xk+1,yk+1).
yk+3 y = mx+b
yk+2
yk+1
yk
xk xk+1 xk+2 xk+3 x
•At pixel position xk+1,label vertical pixel separations from the line path as dlower and dupper.The y coordinate on the line at
pixel column position xk+1 is calculated as
y
y=m(xk+1)+b
then
yk+1
dlower=y-yk dupper
yk+1-yk =m(xk+1)+b-yk
and yk dlower
dupper=(yk+1)-y
xk+1
x
=yk+1-m(xk+1)-b
•To determine which of the two pixels is closest to the line path, set up an efficient test that is based on the difference between the two
pixel separations as follows:
dlower-dupper=2m(xk+1)-2yk+2b-1
•A decision parameter pk for the kth step in the line algorithm by substituting m=∆y/∆x
Where: ∆y and ∆x are the vertical and horizontal separations of the endpoint positions, and defining the decision parameter as
pk= ∆x(dlower-dupper)
=2∆y.xk-2∆x.yk+c
Where: Parameter c is constant,which is independent of the pixel position and will be eliminated in the recursive calculations for pk.
•If the pixel at yk is “closer” to the line path than the pixel at yk+1(that is dlower<dupper),then decision parameter pk is negative. In that
case plot the lower pixel; otherwise, plot the upper pixel.
•Coordinate changes along the line occur in unit steps in
either the x or y direction. At step k+1, the decision
parameter is evaluated as follows:
pk+1=2∆y.xk+1-2∆x.yk+1+c
Subtracting from pk+1 to pk
pk+1-pk=2∆y(xk+1-xk)-2∆x(yk+1-yk)+c
Step-1:Input radius r and circle center(xc , yc), then set the coordinates for the first point on the
circumference of a circle centered on the origin as
(x0,y0)=(0,r)
Step-2:calculate the initial value of the decision parameter as
p0=5/4-r
Step-3:At each xk position , starting at k=0, perform the following test:
If pk<0, the next point along the circle centered on (0,0) is (xk+1,yk) and
pk+1=pk+2xk+1+1
Otherwise, the next point along the circle is (xk+1,yk-1) and
pk+1=pk+2xk+1+1-2yk+1
where 2xk+1=2xk+2 and 2yk+1=2yk-2
Step-4:Determine symmetry points in the other seven octants.
Step-5:Move each calculated pixel position (x , y) onto the circular path centered at (xc , yc) and plot the coordinate values as
follows:
x=x + xc
y=y + yc
Step-6:Repeat steps-3 through 5 until x ≥ y
Ellipse-Generating Algorithms
Ellipse – A modified circle whose radius varies from a
maximum value in one direction (major axis) to a minimum
value in the perpendicular direction (minor axis).
d1
F1 P=(x,y)
d2
F2
The sum of the two distances d1 and d2, between the fixed positions F1 and F2 (called the foci of the
ellipse) to any point P on the ellipse, is the same value, i.e.
d1 + d2 = constant
•Symmetry between quadrants
•Not symmetric between the two octants of a quadrant
•Thus, we must calculate pixel positions along the elliptical
arc
through one quadrant and then we obtain positions in the
remaining
3 quadrants by symmetry
(-x, y) (x, y)
ry
rx
Slope = -1
1
ry 2
rx
Slope = -1
1
ry 2
rx
Midpoint
yi
yi-1
xi xi + xi +
1 2
Assuming that we have just plotted the pixels at (xi , yi).
The next position is determined by:
OR
where yi+1 = yi
or yi+1 = yi – 1
Decision Parameter (Region 1)
Decision parameters are incremented by:
Midpoint
yi
yi-1
xi xi + xi +
1 2
Decision parameter:
OR
where xi+1 = xi
or xi+1 = xi + 1
Decision Parameter (Region 2)
Decision parameters are incremented by:
1.At each xi position, starting at i = 0, if p1i < 0, the next point along the
ellipse centered on (0, 0) is (xi + 1, yi) and
1.At each yi position, starting at i = 0, if p2i > 0, the next point along the ellipse
centered on (0, 0) is (xi, yi – 1) and
6
5
4
3
2
1 71
1.Write an algorithm to draw a line using DDA algorithm. Trace the
algorithm with the end points (20,10) and (30,18). 10 Marks
2.Write an algorithm to draw a line using Bresenham’s principle. Trace the
algorithm with the end points (20,10) and (30,18). 10 Marks
3.Explain Midpoint Circle drawing algorithm giving all the decision
parameters used. Trace the algorithm to draw a circle with radius r=10. List
all the Pixel position on the circumference of the circle – centered at origin.
10 Marks
4.Explain Ellipse Midpoint drawing algorithm giving all the decision
parameters used. Trace the algorithm to draw a circle with radius r=8 and r =
6 List all the Pixel position on the circumference of the circle – centered at
origin. 10 Marks
•Color model based on three primary colors. Green
•Three primary color are three axis of a cube. C
•Red, Green, Blue
Y
Blue M
Red
OpenGL Fill-Area Attribute Functions
glPloygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_POLYGON);
glVertex3fv(v1);
glEdgeFlag(GL_FALSE);
glVertex3fv(v2);
glEdgeFlag(GL_TRUE);
glVertex3fv(v3);
glEnd();
OpenGL Front-Face Function
•glFrontFace( vertexOrder );
or
•glFrontFace( faceOrder ); // GL_CW or GL_CCW
Color model based on three primary colors. Green
Three primary color are three axis of a cube. C
Red, Green, Blue Y
Blue M
Red
OpenGL Fill-Area Attribute Functions
glPloygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_POLYGON);
glVertex3fv(v1);
glEdgeFlag(GL_FALSE);
glVertex3fv(v2);
glEdgeFlag(GL_TRUE);
glVertex3fv(v3);
glEnd();
OpenGL Front-Face Function
glFrontFace( vertexOrder );
or
glFrontFace( faceOrder ); // GL_CW or GL_CCW
Polygon Fill Areas
Convex Concave
E5
(E1xE2)z>0
(E2xE3)z>0
<180o (E3xE4)z<0 E4
(E4xE5)z>0 E6
(E5xE6)z>0 >180o E3
E1 E2
Splitting concave polygon
•Vector Method
The cross product E j × Ek for two successive edge vectors is a vector perpendicular
to the xy plane with z component equal to Ejx Eky − EkxEjy:
•Rotational Method
Inside-Outside Testing
–1 –1 +1 +1
Polygon Tables
E V3
E E
2
V2 4 5
V4
Plane Equations
(x,y,z)
Surface Normal
x
z
General Scan-Line Polygon-Fill Algorithm
This algorithm works by intersecting scanline with polygon edges and fills the
polygon between pairs of intersection.
Step 1:Find out the ymin and ymax from the given polygon.
Step 2:Scanline intersects with each edge of the polygon from ymin to ymax.name each intersection point of the polygon.as per
the above figure , they are named as P0,P1,P2,P3.
Step 3:Sort the intersection point in the increasing order of x coordinate,that is
(P0,P1),(P1,P2),(P2,P3).
Step 4:Fill all those pair of coordinates that are inside polygons and ignore the alternate pairs.
Fill methods for areas with Irregular boundaries
Start point
4-connected 8-connected
void boundaryFill4 (int x, int y, int fillColor, int borderColor)
{
int interiorColor;
/* Set current color to fillColor, then perform following oprations. */
getPixel (x, y, interiorColor);
if ((interiorColor != borderColor) && (interiorColor != fillColor))
{
setPixel (x, y); // Set color of pixel to fillColor.
boundaryFill4 (x + 1, y , fillColor, borderColor);
boundaryFill4 (x - 1, y , fillColor, borderColor);
boundaryFill4 (x , y + 1, fillColor, borderColor);
boundaryFill4 (x , y - 1, fillColor, borderColor)
}
}
Flood Fill Algorithm
Sometimes we come across an object where we want to fill the area and its boundary
with different colors.
This algorithm replaces the interior color of the object with fill color.It is looking for
all adjacent pixels that are a part of the interior,when no more pixels of the original
interior color exist, the algorithm is completed.
void floodFill4 (int x, int y, int fillColor, int interiorColor)
{
int color;
/* Set current color to fillColor, then perform following operations. */
getPixel (x, y, color);
if (color = interiorColor)
{
setPixel (x, y); // Set color of pixel to fillColor.
floodFill4 (x + 1, y, fillColor, interiorColor);
floodFill4 (x - 1, y, fillColor, interiorColor);
floodFill4 (x, y + 1, fillColor, interiorColor);
floodFill4 (x, y - 1, fillColor, interiorColor)
}
}