CG Unit 1
CG Unit 1
Computer graphics is an art of drawing pictures, lines, charts, etc. using computers with
the help of programming. Computer graphics image is made up of number of pixels. Pixel
is the smallest addressable graphical unit represented on the computer screen.
Introduction
Computer is information processing machine. User needs to communicate with
computer and the computer graphics is one of the most effective and commonly used
ways of communication with the user.
It displays the information in the form of graphical objects such as pictures, charts,
diagram and graphs.
Graphical objects convey more information in less time and easily
understandable formats for example statically graph shown in stock exchange.
In computer graphics picture or graphics objects are presented as a collection of
discrete pixels.
We can control intensity and color of pixel which decide how picture look like.
The special procedure determines which pixel will provide the best
approximation to the desired picture or graphics object this process is known as
Rasterization.
The process of representing continuous picture or graphics object as a
collection of discrete pixels is called Scan Conversion.
A pixel is a point of light. It is just one tiny dot on the raster displays. Though it has no
structure, it is definitely a building block and hence it can be considered as the graphics
primitive. The resolution of CRT is related to the dot size, the diameter of a single dot. A
resolution of 100 dots lines/inch implies a dot size of 0.01 inch. The ratio of the distance
between the centres of two adjacent horizontal pixels to that of the vertical ones is called the
pixel ratio. Pixel ratio should be considered in line-generating algorithms.
Line
Line, especially straight lines, constitute the basic building block of Line graphs, bar and pie
charts, two and three-dimensional graphs of mathematical functions, engineering drawings and
architectural plans. In computer graphics, straight line is so basic in creating images that we
call it a graphics primitive. Straight lines can be developed in two different ways. A structural
method determines which pixels should be set before drawing the line; a conditional
method tests certain conditions to find which pixel should be set next.
Polygon
A polygon is a closed area of image bounded by straight or curved lines and filled with one
solid color. Since images are two dimensional, a polygon is a closed planar figure.A polygon,
is an important graphics primitive. So often we want to handle polygon as a single entity, as
images of objects from the real world consist in large, part of polygons.
Display file
A display file is a set of uncorrelated data, such as a histogram array or bivariate array. The
arrays are filled event by event from a list data in order to create a display. The saved arrays
usually take up far less disk space, but can the data can no longer be gated.
Frame buffer
The video output device which drives a video display from the memory buffer containing a
complete set of data is known as frame buffer. The image is stored in terms of pixel by pixel.
The memory can be discs, Integrated circuits etc.,
Resolution
the term resolution refers to the total number of count of pixels in an digital image. For
example. If an image has M rows and N columns, then its resolution can be defined as M X
N.
If we define resolution as the total number of pixels, then pixel resolution can be defined with
set of two numbers. The first number the width of the picture, or the pixels across columns,
and the second number is height of the picture, or the pixels across its width.
We can say that the higher is the pixel resolution, the higher is the quality of the image.
e.g. You have probably seen that in your own computer settings, you have monitor resolution
of 800 x 600, 640 x 480 e.t.c
Aspect ratio
Aspect ratio is the ratio between width of an image and the height of an image. It is
commonly explained as two numbers separated by a colon (8:9). This ratio differs in different
images, and in different screens. The common aspect ratios are:
1.33:1, 1.37:1, 1.43:1, 1.50:1, 1.56:1, 1.66:1, 1.75:1, 1.78:1, 1.85:1, 2.00:1, e.t.c
Advantage
Aspect ratio maintains a balance between the appearance of an image on the screen, means it
maintains a ratio between horizontal and vertical pixels. It does not let the image to get
distorted when aspect ratio is increased.
Display Devices
The display device is an output device used to represent the information in the form of images
(visual form). Display systems are mostly called a video monitor or Video display unit (VDU).
Display devices are designed to model, display, view, or display information. The purpose of
display technology is to simplify information sharing.
There are some display devices given below:
1. Cathode-Ray Tube(CRT)
2. Color CRT Monitor
3. Liquid crystal display(LCD)
4. Light Emitting Diode(LED)
5. Direct View Storage Tubes(DVST)
6. Plasma Display
7. 3D Display
Description pending……………………
Introduction to OpenGL
OpenGL is a software interface that allows the programmer to create 2D and 3D graphics
images. OpenGL is both a standard API and the implementation of that API. You can call
the functions that comprise OpenGL from a program you write and expect to see the same
results no matter where your program is running. OpenGL is independent of the hardware,
operating, and windowing systems in use. The fact that it is windowing-system
independent, makes it portable. OpenGL program must interface with the windowing
system of the platform where the graphics are to be displayed.
Therefore, a number of windowing toolkits have been developed for use with OpenGL.
OpenGL functions in a client/server environment. That is, the application program
producing the graphics may run on a machine other
than the one on which the graphics are displayed. The server part of OpenGL, which runs
on the workstation where the graphics are displayed, can access whatever physical
graphics device or frame buffer is available
on that machine.
OpenGL's rendering commands, however are "primitive". You can tell theprogram to draw
points, lines, and polygons, and you have to build more complex entities upon these. There are
no special-purpose functions that you can call to create graphs, contour plots, maps, or any of
the other elements we are used to getting from "old standby programs”. With OpenGL, you
have to build these
things up yourself.
With OpenGL any commands that you execute are executed immediately. That is, when you
tell the program to draw something, it does it right away. You also have the option of putting
commands into display lists. A display list is a noteditable list of OpenGL commands stored
for later execution. You can execute the same display list more than once. For example, you
can use display lists to redraw the graphics whenever the user resizes the window. You can use
a display list to draw the same shape more than once if it repeats as an element of the picture.
OpenGL has been designed using a client/server paradigm, allowing the client application and
the graphics server controlling the display hardware to exist on the same or separate machines.
The network is transparent to the application.
OpenGL Pipeline
The OpenGL architecture is structured as a state-based pipeline. Below is a simplified diagram
of this pipeline. Commands enter the pipeline from the left.
Fig Diagram open Gl architecture Structure as a state based pipeline
Commands may either be accumulated in display lists, or processed immediately through the
pipeline. Display lists allow for greater optimization and command reuse, but not all
commands can be put in display lists.
The first stage in the pipeline is the evaluator. This stage effectively takes any polynomial
evaluator commands and evaluates them into their corresponding vertex and attribute
commands.
The second stage is the per-vertex operations, including transformations, lighting, primitive
assembly, clipping, projection, and viewport mapping.
The third stage is rasterization. This stage produces fragments, which are series of framebuffer
addresses and values, from the viewport-mapped primitives as well as bitmaps and pixel
rectangles.
The fourth stage is the per-fragment operations. Before fragments go to the framebuffer, they
may be subjected to a series of conditional tests and modifications, such as blending or z-
buffering.
Parts of the framebuffer may be fed back into the pipeline as pixel rectangles. Texture memory
may be used in the rasterization process when texture mapping is enabled.
OpenGL supports several basic primitive types, including points, lines, quadrilaterals, and
geneneral polygons. All of these primitives are specified using a sequence of vertices. The
diagram below shows the basic primitive types, where the numbers indicate the order in which
the vertices have been specified. Note that for the GL_LINES primitive only every second vertex
causes a line segment to be drawn. Similarly, for the GL_TRIANGLES primitive, every third
vertex causes a triangle to be drawn. Note that for
the GL_TRIANGLE_STRIP and GL_TRIANGLE_FAN primitives, a new triangle is produced for
every additional vertex. All of the closed primitives shown below are solid-filled, with the
exception of GL_LINE_LOOP, which only draws lines connecting the vertices.
Assigning Colours
OpenGL maintains a current drawing colour as part of its state information.
The glColor() function calls are used to change the current drawing colour. assigned using the
glColor function call. Like glVertex(), this function exists in various instantiations. Colour
components are specified in the order of red, green, blue. Colour component values are in the
range [0...1], where 1 corresponds to maximum intensity. For unsigned bytes, the range
corresponds to [0...255]. All primitives following the fragment of code given below would be
drawn in green, assuming no additional glColor() function calls are used.
GLfloat mycolour[3] = {0,0,1}; // blue
glColor3fv( mycolour ); // blue using vector of floats
glColor3f(1.0, 0.0, 0.0); // red using floats
glColor3ub(0,255,0); // green using unsigned bytes
If desired, a polygon can be smoothly shaded to interpolate colours between vertices. This is
accomplished by using the GL_SMOOTH shading mode (the OpenGL default) and by
assigning a desired colour to each vertex, as shown in the following example.
glShadeModel(GL_SMOOTH); // as opposed to GL_FLAT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0); // red
glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0); // green
glVertex3f(1.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0); // blue
glVertex3f(1.0, 1.0, 0.0);
glEnd();
A fourth value called alpha is often appended to the colour vector. This can be used assign a
desired level of transparency to a primitive and finds uses in compositing multiple images
together. An alpha value of 0.0 defines an opaque colour, while an alpha value of 1.0
corresponds to complete transparency.
The screen can be cleared to a particular colour as follows:
glClearcolor(1.0, 1.0, 1.0, 0.0); // sets the clear colour to white and opaque
glClear( GL_COLOR_BUFFER_BIT); // clears the colour frame buffer
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
closegraph();
}
From the above illustration, the y coordinate on the mathematical line at xk+1 is:
𝑌=(𝑋𝑘+1)+𝑏
So, dupper and dlower are given as follows: 𝑑𝑙𝑜𝑤𝑒𝑟 =𝑦−𝑦𝑘
= (𝑋𝑘+1)+𝑏− 𝑌𝑘
and
𝑑𝑢𝑝𝑝𝑒𝑟 =(𝑦𝑘+1)−𝑦
= 𝑌𝑘+1−(𝑋𝑘+1)−𝑏
You can use these to make a simple decision about which pixel is closer to the mathematical
line. This simple decision is based on the difference between the two pixel positions.
𝑑𝑙𝑜𝑤𝑒𝑟− 𝑑𝑢𝑝𝑝𝑒𝑟 =2(𝑥𝑘+1)−2𝑦𝑘+2𝑏−1
Let us substitute m with dy/dx where dx and dy are the differences between the end-points.
(𝑑𝑙𝑜𝑤𝑒𝑟− 𝑑𝑢𝑝𝑝𝑒𝑟) =𝑑𝑥(2𝑑𝑦𝑑𝑥(𝑥𝑘+1)−2𝑦𝑘+2𝑏−1)
=2𝑑𝑦∙𝑥𝑘 −2𝑑𝑥∙𝑦𝑘+2𝑑𝑦+(2𝑏−1) =2𝑑𝑦∙𝑥𝑘 −2𝑑𝑥∙𝑦𝑘+𝐶
So, a decision parameter pk for the kth step along a line is given by
: 𝑝𝑘=𝑑𝑥(𝑑𝑙𝑜𝑤𝑒𝑟− 𝑑𝑢𝑝𝑝𝑒𝑟)
=2𝑑𝑦∙𝑥𝑘 −2𝑑𝑥∙𝑦𝑘+𝐶
The sign of the decision parameter pk is the same as that of dlower – dupper.
If pk is negative, then choose the lower pixel, otherwise choose the upper pixel.
Remember, the coordinate changes occur along the x axis in unit steps, so you can do
everything with integer calculations. At step k+1, the decision parameter is given as:
𝑝𝑘+1=2𝑑𝑦∙𝑥𝑘+1 −2𝑑𝑥∙𝑦𝑘+1+𝐶
Subtracting pk from this we get: 𝑝𝑘+1−𝑝𝑘 =2(𝑥𝑘+1−𝑥𝑘) −2𝑑𝑥(𝑦𝑘+1−𝑦𝑘)
But, xk+1 is the same as xk+1. So: 𝑝𝑘+1=𝑝𝑘+ 2𝑑𝑦−2(𝑦𝑘+1−𝑦𝑘)
Where, yk+1 – yk is either 0 or 1 depending on the sign of pk.
The first decision parameter p0 is evaluated at (x0, y0) is given as: 𝑝0=2𝑑𝑦−𝑑𝑥
Now, keeping in mind all the above points and calculations,
here is the Bresenham algorithm for slope m < 1:
Step 1: Input the two end-points of line, storing the left end-point in (x0, y0).
Step 2: Plot the point (x0, y0).
Step 3: Calculate the constants dx, dy, 2dy, and (2dy – 2dx)
and get the first value for the decision parameter as: 𝑝0=2𝑑𝑦−𝑑𝑥
Step 4: At each xk along the line, starting at k = 0,
perform the following test:
If pk < 0, the next point to plot is (xk+1, yk) and
𝑝𝑘+1=𝑝𝑘+2𝑑𝑦
Otherwise, the next point to plot is (xk+1, yk+1) and 𝑝𝑘+1=𝑝𝑘+2𝑑𝑦−2𝑑𝑥
Step 5: Repeat step 4 (dx – 1) times.
For m > 1, find out whether you need to increment x while incrementing y each time. After
solving, the equation for decision parameter pk will be very similar, just the x and y in the
equation gets interchanged.
Advantages of Bresenham Line Drawing Algorithm-
The advantages of Bresenham Line Drawing Algorithm are-
It is easy to implement.
It is fast and incremental.
It executes fast but less faster than DDA Algorithm.
The points generated by this algorithm are more accurate than DDA
Algorithm.
It uses fixed points only.
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
return 0;
}
1.DDA
Draws a circle using DDA Algorithm. Takes the circle parameters (centre and radius)from the
user to plot the desired circle.The program calculates each successive pixel that lies on the
circle using DDA Algorithm.
Program
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
int main()
{
int xc,yc,r,gd,gm;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(10);
//drawing the co-ordinate axes
line(0,240,640,240);
line(320,0,320,480);
setcolor(5);
getch();
return 0;
/*
Function Definition for DDA circle
Function: dda_circle() Draws the circle with given parameters using DDA algorithm.
Input: Takes three input
1) x co-ordinate for centre
2) y co-ordinate for centre
3) radius of the circle
*/
void dda_circle(int xc,int yc,int r)
{
float xc1,xc2,yc1,yc2,eps,sx,sy;
int val,i;
xc1=r;
yc1=0;
sx=xc1;
sy=yc1;
i=0;
do{
val=pow(2,i);
i++;
}while(val<r);
eps = 1/pow(2,i-1);
do{
putpixel(xc+xc2,yc-yc2,3);
xc1=xc2;
yc1=yc2;
}while((yc1-sy)<eps || (sx-xc1)>eps);
Now, we will see how to calculate the next pixel location from a previously known pixel
location (x, y). In Bresenham’s algorithm at any point (x, y) we have two option either to
choose the next pixel in the east i.e. (x+1, y) or in the south east i.e. (x+1, y-1).
x++;
// driver function
int main()
{
int xc = 50, yc = 50, r2 = 30;
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}
Advantages
It is simple algorithm.
It can be implement easily
It is totally based on the equation of circle i.e. x2 +Y2 =R2
Disadvantages
There is the problem of accuracy while generating points.
This algorithm is not suitable for the complex and high graphic images..
Like Mid Point Algorithm, accuracy of the generating points is an issue in this
algorithm.
This algorithm suffers when used to generate complex and high graphical images.
There is no significant enhancement with respect to performance.
#include<iostream.h>
#include<graphics.h>
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
{
y += 1;
err += 2*y + 1;
}
if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
return 0;
}
Output