You are on page 1of 44

K.S.

SCHOOL OF ENGINEERING AND MANAGEMENT, BANGALORE - 560109


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT – 18CSL67

LAB MANUAL

2020-2021
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT


(Effective from the academic year 2018 -
2019) SEMESTER – VI
Course Code 18CSL67 CIE Marks 40
Number of Contact Hours/Week 0:2:2 SEE Marks 60
Total Number of Lab Contact Hours 36 Exam Hours 03
Credits – 2
Course Learning Objectives: This course (18CSL67) will enable students to:
 Demonstrate simple algorithms using OpenGL Graphics Primitives and attributes.
 Implementation of line drawing and clipping algorithms using OpenGL functions
 Design and implementation of algorithms Geometric transformations on both 2D and 3D
objects.
Descriptions (if any): --
Installation procedure of the required software must be demonstrated, carried out
in groups and documented in the journal.
Programs List:
PART A
Design, develop, and implement the following programs using
OpenGL API
1. Implement Brenham’s line drawing algorithm for all types of
slope. Refer:Text-1: Chapter 3.5
Refer:Text-2: Chapter 8
2. Create and rotate a triangle about the origin and a fixed point.
Refer:Text-1: Chapter 5-4
3. Draw a colour cube and spin it using OpenGL transformation matrices.
Refer:Text-2: Modelling a Coloured Cube
4. Draw a color cube and allow the user to move the camera suitably to
experiment with perspective viewing.
Refer:Text-2: Topic: Positioning of Camera
5. Clip a lines using Cohen-Sutherland algorithm
Refer:Text-1: Chapter 6.7
Refer:Text-2: Chapter 8
6. To draw a simple shaded scene consisting of a tea pot on a table. Define suitably
the position and properties of the light source along with the properties of the
surfaces of thesolid object used in the scene.
Refer:Text-2: Topic: Lighting and Shading
7. Design, develop and implement recursively subdivide a tetrahedron to form 3D
sierpinski gasket. The number of recursive steps is to be specified by the user.
Refer: Text-2: Topic: sierpinski gasket.
8. Develop a menu driven program to animate a flag using Bezier Curve algorithm
Refer: Text-1: Chapter 8-10
9. Develop a menu driven program to fill the polygon using scan line algorithm
PART B MINI PROJECT
Student should develop mini project on the topics mentioned below or similar applications
using Open GL API. Consider all types of attributes like color, thickness, styles, font,
background, speed etc., while doing mini project.
(During the practical exam: the students should demonstrate and answer
Viva-Voce)Sample Topics:
Simulation of concepts of OS, Data structures, algorithms etc.
Laboratory Outcomes: The student should be able to:
 Apply the concepts of computer graphics

Department of CSE,KSSEM Page 1


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

 Implement computer graphics applications using OpenGL


 Animate real world problems using OpenGL
Conduct of Practical Examination:
 Experiment distribution
o For laboratories having only one part: Students are allowed to pick one
experiment from the lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick
one experiment from PART A and one experiment from PART B, with equal
opportunity.
 Change of experiment is allowed only once and marks allotted for procedure to be
made zero ofthe changed part only.
 Marks Distribution (Courseed to change in accoradance with university regulations)
o) For laboratories having only one part – Procedure + Execution + Viva-Voce:
15+70+15 =
100 Marks
p) For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks
ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks

Department of CSE,KSSEM Page 2


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

INTRODUCTION TO OPENGL

OpenGL is a low -level graphics library specification. It makes available to the programmer a
small set of geometric primitives -points, lines, polygons, images, and bitmaps. OpenGL
provides a set of commands that allow the specification of geometric objects in two or three
dimensions, using the provided primitives, together with commands that control how these
objects are rendered (drawn).

Since OpenGL drawing commands are limited to those that generate simple geometric
primitives (points, lines, and polygons), the OpenGL Utility Toolkit (GLUT) has been created to
aid in the development of more complicated three-dimensional objects such as a sphere, a torus,
and even a teapot. GLUT may not be satisfactory for full-featured OpenGL applications, but it is
a useful starting point for learning OpenGL.

GLUT is designed to fill the need for a window system independent programming
interface for OpenGL programs. The interface is designed to be simple yet still meet the needs
of useful OpenGL programs. Removing window system operations from OpenGL is a sound
decision because it allows the OpenGL graphics system to be retargeted to various systems
including powerful but expensive graphics workstations as well as mass-production graphics
systems like video games, set -top boxes for interactive television, and PCs.

Graphics Pipeline

A pipeline, in computing terminology, refers to a series of processing stages in which the output
from one stage is fed as the input of the next stage, similar to a factory assembly line or water/oil
pipe. With massive parallelism, pipeline can greatly improve the overall throughput. In computer
graphics, rendering is the process of producing image on the display from model description.
The 3D Graphics Rendering Pipeline accepts description of 3D objects in terms of vertices of
primitives (such as triangle, point, line and quad), and produces the color-value for the pixels on
the display.

The 3D graphics rendering pipeline consists of the following main stages:

1. Vertex Processing: Process and transform individual vertices.


2. Rasterization: Convert each primitive (connected vertices) into a set of fragments. A
fragment can be treated as a pixel in 3D spaces, which is aligned with the pixel grid, with
attributes such as position, color, normal and texture.
3. Fragment Processing: Process individual fragments.
4. Output Merging: Combine the fragments of all primitives (in 3D space) into 2D color-
pixel for the display

Department of CSE,KSSEM Page 3


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

In modern GPUs, the vertex processing stage and fragment processing stage are
programmable. You can write programs, known as vertex shader and fragment shader to
perform your custom transform for vertices and fragments. The shader programs are written in
C-like high level languages such as GLSL (OpenGL Shading Language), HLSL (High-Level
Shading Language for Microsoft Direct3D), or Cg (C for Graphics by NVIDIA).On the other
hand, the rasterization and output merging stages are not programmable, but configurable - via
configuration commands issued to the GPU.

Libraries

OpenGL provides a powerful but primitive set of rendering command, and all higher-level
drawing must be done in terms of these commands. There are several libraries that allow you to
simplify your programming tasks, including the following

OpenGL Utility Library (GLU) contains several routines that use lower-level OpenGL
commands to perform such tasks as setting up matricesfor specific viewing orientations and
projections and rendering surfaces.

OpenGL Utility Toolkit (GLUT) is a window-system -independent toolkit, written by


Mark Kilgard, to hide the complexities of differing window APIs.

Include Files

For all OpenGL applications, you want to include the gl.h header file in every file. Almost all
OpenGL applications use GLU, the aforementioned OpenGL Utility Library, which also requires
inclusion of the glu.h header file. So almost every OpenGL source file begins with:

#include<GL/gl.h>
#include <GL/glu.h>

Department of CSE,KSSEM Page 4


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
If you are using the OpenGL Utility Toolkit (GLUT) for managing your window
manager tasks, you should include:

#include <GL/glut.h>

Note that glut.h guarantees that gl.h and glu.h are properly included for you so including
these three files is redundant. To make your GLUT programs portable, include glut.h and do not
include gl.h or glu.h explicitly.

Compiling OpenGL/GLUT Programs

1. Create a new project:


 choose File | New from the File Menu
 select the Projects tab
 choose Win32 Console Application, select Empty Project
 fill in your Project name
2. Add/Create files to the project:
 Right click on sources, Select Add New Item , select new c++ file, give file
name
3. Write the code, then Build and Execute.

Department of CSE,KSSEM Page 5


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

1. Implement Brenham’s line drawing algorithm for all types of slope.

Description:

Bresenham's line algorithm is an algorithm that determines the points of an n-


dimensional raster that should be selected in order to form a close approximation to a straight
line between two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a
computer screen), as it uses only integer addition, sub-traction and bit shifting, all of which are
very cheap operations in standard computer architectures. It is an incremental error algorithm. It
is one of the earliest algorithms developed in the field of computer graphics. An extension to the
original algorithm may be used for drawing circles.

While algorithms such as Wu's algorithm are also frequently used in modern computer
graphics because they can support antialiasing, the speed and simplicity of Bresenham's line
algorithm means that it is still important. The algorithm is used in hardware such as plotters and
in the graphics chips of modern graphics cards. It can also be found in many software graphics
libraries. Because the algorithm is very simple, it is often implemented in either the firmware or
the graphics hardware of modern graphics cards.

#include<GL/glut.h>
#include<stdio.h>
int x1, y1, x2, y2;

void draw_pixel(int x, int y)


{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

void brenhams_line_draw(int x1, int y1, int x2, int y2)


{
int dx=x2-x1,dy=y2-y1;
int p=2*dy*dx;
int twoDy=2*dy;
int twoDyMinusDx=2*(dy-dx); // paranthesis are required
int x=x1,y=y1;
if(dx<0)
{
x=x2;
y=y2;
x2=x1;
}
draw_pixel(x, y);
while(x<x2)

Department of CSE,KSSEM Page 6


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
{
x++;
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyMinusDx;
}
draw_pixel(x, y);
}
}
void myInit()
{
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
brenhams_line_draw(x1, y1, x2, y2);
glFlush();
}

void main(int argc, char **argv)


{
printf( "Enter Start Points (x1,y1)\n");
scanf("%d %d", &x1, &y1);
printf( "Enter End Points (x2,y2)\n");
scanf("%d %d", &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInit WindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(display);
glutMainLoop();
}

Department of CSE,KSSEM Page 7


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
OUTPUT :

Department of CSE,KSSEM Page 8


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

2. Create and rotate a triangle about the origin and a fixed point.

In linear algebra, a rotation matrix is a matrix that is used to perform a rotation in Euclidean
space. For example the matrix rotates points in the xy-Cartesian plane counterclockwise through
an angle _ about the origin of the Cartesian coordinate system. To perform the rotation using a
rotation matrix R, the position of each point must be represented by a column vector v,
containing the coordinates of the point. A rotated vector is obtained by using the matrix
multiplication Rv. Since matrix multiplication has no e_ect on the zero vector (i.e., on the
coordinates of the origin), rotation matrices can only be used to describe rotations about the
origin of the coordinate system. Rotation matrices provide a simple algebraic description of such
rotations, and are used extensively for computations in geometry, physics, and computer
graphics. In 2-dimensional space, a rotation can be simply described by an angle _ of rotation,
but it can be also represented by the 4 entries of a rotation matrix with 2 rows and 2 columns. In
3-dimensional space, every rotation can be interpreted as a rotation by a given angle about a
single _xed axis of rotation (see Euler's rotation theorem), and hence it can be simply described
by an angle and a vector with 3 entries. However, it can also be represented by the 9 entries of a
rotation matrix with 3 rows and 3 columns. The notion of rotation is not commonly used in
dimensions higher than 3; there is a notion of a rotational displacement, which can be
represented by a matrix, but no associated single axis or angle.

#include<GL/glut.h>
#include<stdio.h>
int x,y;
int rFlag=0;
void draw_pixel(float x1,float y1)
{
glColor3f(0.0,0.0,1.0);
glPointSize(5.0);
glBegin(GL_POINTS);
glVertex2f(x1,y1);
glEnd();
}

void triangle()
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(100,100);
glVertex2f(250,400);
glVertex2f(400,100);
glEnd();
}

float th=0.0;
float trX=0.0,trY=0.0;

Department of CSE,KSSEM Page 9


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

if(rFlag==1) //Rotate Around origin


{
trX=0.0;
trY=0.0;
th+=0.1;
draw_pixel(0.0,0.0);
}

if(rFlag==2) //Rotate Around Fixed Point


{
trX=x;
trY=y;
th+=0.1;
draw_pixel(x,y);
}
glTranslatef(trX,trY,0.0);
glRotatef(th,0.0,0.0,1.0);
glTranslatef(-trX,-trY,0.0);
triangle();
glutPostRedisplay();
glutSwapBuffers();

void myInit()
{
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-500.0, 500.0, -500.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}

void rotateMenu (int option)


{
if(option==1)
rFlag=1;
if(option==2)
rFlag=2;
if(option==3)
rFlag=3;
}

Department of CSE,KSSEM Page 10


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

void main(int argc, char **argv)


{
printf( "Enter Fixed Points (x,y) for Roration: \n");
scanf("%d %d", &x, &y);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Create and Rotate Triangle");
myInit();
glutDisplayFunc(display);
glutCreateMenu(rotateMenu);
glutAddMenuEntry("Rotate around ORIGIN",1);
glutAddMenuEntry("Rotate around FIXED POINT",2);
glutAddMenuEntry("Stop Rotation",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}

OUTPUT:

Department of CSE,KSSEM Page 11


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

Rotation Around the ORIGIN

Rotation Around the FIXED POINT

Department of CSE,KSSEM Page 12


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
3) Program to draw a color cube and spin it using openGL transformation matrices.

/* Rotating cube with color interpolation */

/* Demonstration of use of homogeneous coordinate transformations and simple data structure


for representing cube from Chapter 4 */

/*Both normals and colors are assigned to the vertices */ /*Cube is centered at origin so
(unnormalized) normals are the same as the vertex values */

#include <stdlib.h>
#include <GL/glut.h>

GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},


{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},


{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},


{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};
void polygon(int a, int b, int c , int d)

{/* draw a polygon via list of vertices */

glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glNormal3fv(normals[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();

Department of CSE,KSSEM Page 13


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
void colorcube(void)
{

/* map vertices to faces */

polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;

void display(void)
{
/* display callback, clear frame buffer and z buffer,
rotate cube and draw, swap buffers */

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void spinCube()
{

/* Idle callback, spin cube 2 degrees about selected axis */

theta[axis] += 1.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
/* display(); */
glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y)
{
/* mouse callback, selects an axis about which to rotate */

if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;


if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}

Department of CSE,KSSEM Page 14


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Rotating a Color Cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glutMainLoop();
}

Output:

Department of CSE,KSSEM Page 15


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
4) Program to draw a color cube and allow the user to move the camera suitably to
experiment with perspective viewing. Use openGL functions.

/* We use the Lookat function in the display callback to point the viewer, whose position can be
altered by the x,X,y,Y,z, and Z keys.The perspective view is set in the reshape callback */

#include <stdlib.h>
#include <GL/glut.h>

GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},


{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},


{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},


{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};

void polygon(int a, int b, int c , int d)


{
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glNormal3fv(normals[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();
}
void colorcube()
{
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}

Department of CSE,KSSEM Page 16


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

static GLfloat theta[] = {0.0,0.0,0.0};


static GLint axis = 2;
static GLdouble viewer[]= {0.0, 0.0, 5.0}; /* initial viewer location */

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* Update viewer position in modelview matrix */

glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

/* rotate cube */

glRotatef(theta[0], 1.0, 0.0, 0.0);


glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);

colorcube();

glFlush();
glutSwapBuffers();
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
theta[axis] += 2.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
display();
}
void keys(unsigned char key, int x, int y)
{
/* Use x, X, y, Y, z, and Z keys to move viewer */

if(key == 'x') viewer[0]-= 1.0;


if(key == 'X') viewer[0]+= 1.0;
if(key == 'y') viewer[1]-= 1.0;
if(key == 'Y') viewer[1]+= 1.0;
if(key == 'z') viewer[2]-= 1.0;
if(key == 'Z') viewer[2]+= 1.0;

display();
}

Department of CSE,KSSEM Page 17


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

void myReshape(int w, int h)


{
glViewport(0, 0, w, h);

/* Use a perspective view */

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);

/* Or we can use gluPerspective */


/* gluPerspective(45.0, w/h, -10.0, 10.0); */
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Colorcube Viewer");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
Output:

Department of CSE,KSSEM Page 18


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
5) Program to implement the Cohen-Sutherland line clipping algorithm. Make provision to
specify the input line, window for clipping and viewport for displaying the clipped image.

#include <stdio.h>
#include <GL\glut.h>
double xmin=50,ymin=50, xmax=100,ymax=100;
double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
const int RIGHT = 8;
const int LEFT = 2;
const int TOP = 4;
const int BOTTOM = 1;
int ComputeOutCode (double x, double y)
{
int code = 0;
if (y > ymax) //above the clip window
code |= TOP;
else if (y < ymin) //below the clip window
code |= BOTTOM;
if (x > xmax) //to the right of clip window
code |= RIGHT;
else if (x < xmin) //to the left of clip window
code |= LEFT;
return code;
}

void CohenSutherland(double x0, double y0,double x1, double y1)


{
int outcode0, outcode1, outcodeOut;
bool accept = false, done = false;
outcode0 = ComputeOutCode (x0, y0);
outcode1 = ComputeOutCode (x1, y1);
do{
if (!(outcode0 | outcode1))
{
accept = true;
done = true;
}
else if (outcode0 & outcode1)
done = true;
else {
double x, y;
outcodeOut = outcode0? outcode0: outcode1;
if (outcodeOut & TOP)
{
x = x0 + (x1 - x0) * (ymax - y0)/(y1 - y0);
y = ymax;
}

Department of CSE,KSSEM Page 19


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
else if (outcodeOut & BOTTOM)
{
x = x0 + (x1 - x0) * (ymin - y0)/(y1 - y0);
y = ymin;
}
else if (outcodeOut & RIGHT)
{
y = y0 + (y1 - y0) * (xmax - x0)/(x1 - x0);
x = xmax;
}

else
{
y = y0 + (y1 - y0) * (xmin - x0)/(x1 - x0);
x = xmin;
}
if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode (x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode (x1, y1);
}
}
}while (!done);

if (accept)
{
double sx=(xvmax-xvmin)/(xmax-xmin);
double sy=(yvmax-yvmin)/(ymax-ymin);
double vx0=xvmin+(x0-xmin)*sx;
double vy0=yvmin+(y0-ymin)*sy;
double vx1=xvmin+(x1-xmin)*sx;
double vy1=yvmin+(y1-ymin)*sy;
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);

Department of CSE,KSSEM Page 20


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
glVertex2d (vx0, vy0);
glVertex2d (vx1, vy1);
glEnd();
}
}

void display()
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
glVertex2d (x0, y0);
glVertex2d (x1, y1);
glEnd();
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherland(x0,y0,x1,y1);
glFlush();
}

void myinit()
{
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0,0.0,500.0);
glMatrixMode(GL_MODELVIEW);
}

void main(int argc, char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Cohen Suderland Line Clipping Algorithm");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}

Department of CSE,KSSEM Page 21


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

OUTPUT :

Department of CSE,KSSEM Page 22


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
6. Program to draw a simple shaded scene consisting of a tea pot on a table. Define suitably
the position and properties of the light source along with the properties of the properties of
the surfaces of the solid object used in the scene.

#include<GL/glut.h>
void teapot(GLfloat x,GLfloat y,GLfloat z)
{
glPushMatrix();
glTranslatef(x,y,z);
glutSolidTeapot(0.1);
glPopMatrix();
}
void tableTop(GLfloat x,GLfloat y,GLfloat z)
{
glPushMatrix();
glTranslatef(x,y,z);
glScalef(0.6,0.02,0.5);
glutSolidCube(1.0);
glPopMatrix();
}
void tableLeg(GLfloat x,GLfloat y,GLfloat z)
{
glPushMatrix();
glTranslatef(x,y,z);
glScalef(0.02,0.3,0.02);
glutSolidCube(1.0);
glPopMatrix();
}

void wall(GLfloat x,GLfloat y,GLfloat z)


{
glPushMatrix();
glTranslatef(x,y,z);
glScalef(1.0,1.0,0.02);
glutSolidCube(1.0);
glPopMatrix();
}

void light()
{
GLfloat mat_ambient[]={1.0,1.0,1.0,1.0};
GLfloat mat_diffuse[]={0.5,0.5,0.5,1.0};
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
Department of CSE,KSSEM Page 23
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
GLfloat light_position[]={2.0,6.0,3.0,1.0};
GLfloat lightIntensity[]={0.7,0.7,0.7,1.0};
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightIntensity);
}

void display()
{
GLfloat teapotP=-0.07,tabletopP=-0.15,tablelegP=0.2,wallP=0.5;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(-2.0,2.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
light(); //Adding light source to your project
teapot(0.0,teapotP,0.0); //Create teapot
tableTop(0.0,tabletopP,0.0); //Create table’s top
tableLeg(tablelegP,-0.3,tablelegP); //Create 1st leg
tableLeg(-tablelegP,-0.3,tablelegP); //Create 2nd leg
tableLeg(-tablelegP,-0.3,-tablelegP); //Create 3rd leg
tableLeg(tablelegP,-0.3,-tablelegP); //Create 4th leg
wall(0.0,0.0,-wallP); //Create 1st wall
glRotatef(-90.0,1.0,0.0,0.0);
wall(0.0,0.0,-wallP); //Create 2nd wall
glRotatef(90.0,0.0,1.0,0.0);
wall(0.0,0.0,wallP); //Create 3rd wall
glFlush();
}

void myinit()
{
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,10.0);
glMatrixMode(GL_MODELVIEW);
}

void main(int argc,char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Teapot on a table");
myinit();
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
Department of CSE,KSSEM Page 24
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}

OUTPUT :

Department of CSE,KSSEM Page 25


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
7. Program to recursively subdivide a tetrahedron to from 3D Sierpinski gasket. The
number of recursive steps is to be specified by the user

/* Recursive subdivision of tetrahedron to form 3D Sierpinski gasket */ //tetra_vtu.cpp

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>

typedef float point[3];

/* initial tetrahedron */

point v[]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333},


{-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};

static GLfloat theta[] = {0.0,0.0,0.0};

int n;

void triangle( point a, point b, point c)

/* display one triangle using a line loop for wire frame, a single
normal for constant shading, or three normals for interpolative shading */
{
glBegin(GL_POLYGON);
glNormal3fv(a);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

void divide_triangle(point a, point b, point c, int m)


{

/* triangle subdivision using vertex numbers


righthand rule applied to create outward pointing faces */

point v1, v2, v3;


int j;
if(m>0)
{
for(j=0; j<3; j++) v1[j]=(a[j]+b[j])/2;
for(j=0; j<3; j++) v2[j]=(a[j]+c[j])/2;
for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2;
divide_triangle(a, v1, v2, m-1);

Department of CSE,KSSEM Page 26


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
divide_triangle(c, v2, v3, m-1);
divide_triangle(b, v3, v1, m-1);
}
else(triangle(a,b,c)); /* draw triangle at end of recursion */
}

void tetrahedron( int m)


{
/* Apply triangle subdivision to faces of tetrahedron */
glColor3f(1.0,0.0,0.0);
divide_triangle(v[0], v[1], v[2], m);
glColor3f(0.0,1.0,0.0);
divide_triangle(v[3], v[2], v[1], m);
glColor3f(0.0,0.0,1.0);
divide_triangle(v[0], v[3], v[1], m);
glColor3f(0.0,0.0,0.0);

divide_triangle(v[0], v[2], v[3], m);


}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(n);
glFlush();
}

void myReshape(int w, int h)


{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,2.0 * (GLfloat) h / (GLfloat) w, -10.0,
10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0,
-10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}

void main(int argc, char **argv)


{
//n=atoi(argv[1]);
printf(" No. of Divisions ? ");

Department of CSE,KSSEM Page 27


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
scanf("%d",&n);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor (1.0, 1.0, 1.0, 1.0);
glutMainLoop();
}

Output:

No of divisions:2

Department of CSE,KSSEM Page 28


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
8. Develop a menu driven program to animate a flag using Bezier curve algorithm.

#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1416
typedef struct point
{
GLfloat x, y, z;
};

void bino(int n, int *C)


{
int k, j;
for(k=0;k<=n;k++)
{
C[k]=1;
for(j=n;j>=k+1; j--)
C[k]*=j;
for(j=n-k;j>=2;j--)
C[k]/=j;
}
}

void computeBezPt(float u, point *pt1, int cPt, point *pt2, int *C)
{
int k, n=cPt-1;
float bFcn;
pt1 ->x =pt1 ->y = pt1->z=0.0;
for(k=0; k< cPt; k++)
{
bFcn = C[k] * pow(u, k) * pow( 1-u, n-k);
pt1 ->x += pt2[k].x * bFcn;
pt1 ->y += pt2[k].y * bFcn;
pt1 ->z += pt2[k].z * bFcn;
}
}

void bezier(point *pt1, int cPt, int bPt)


{
point bcPt;
float u;
int *C, k;
C= new int[cPt];
bino(cPt-1, C);
glBegin(GL_LINE_STRIP);
for(k=0; k<=bPt; k++)
{
u=float(k)/float(bPt);

Department of CSE,KSSEM Page 29


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
computeBezPt(u, &bcPt, cPt, pt1, C);
glVertex2f(bcPt.x, bcPt.y);
}
glEnd();
delete[]C;
}

float theta = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int nCtrlPts = 4, nBCPts =20;
point ctrlPts[4] = {{100, 400, 0}, {150, 450, 0}, {250, 350, 0},
{300, 400, 0}};
ctrlPts[1].x +=50*sin(theta * PI/180.0);
ctrlPts[1].y +=25*sin(theta * PI/180.0);
ctrlPts[2].x -= 50*sin((theta+30) * PI/180.0);
ctrlPts[2].y -= 50*sin((theta+30) * PI/180.0);
ctrlPts[3].x -= 25*sin((theta) * PI/180.0);
ctrlPts[3].y += sin((theta-30) * PI/180.0);
theta+=0.2;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(5);
glPushMatrix();
glLineWidth(5);
glColor3f(1, 0.4, 0.2); //Indian flag: Orange color code
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(1, 1, 1); //Indian flag: white color code
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(0, 1, 0); //Indian flag: green color code
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glPopMatrix();
glColor3f(0.7, 0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(100,400);
Department of CSE,KSSEM Page 30
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
glVertex2f(100,40);
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}

void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}

void main(int argc, char **argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500,500);
glutCreateWindow("Bezier Curve");
init();
glutDisplayFunc(display);
glutMainLoop();
}

OUTPUT :

Department of CSE,KSSEM Page 31


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
9. Develop a menu driven program to fill any given polygon using scan-line area filling
algorithm.

#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
float x1,x2,x3,x4,y1,y2,y3,y4;
int fillFlag=0;
void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
{
float mx,x,temp;
int i;
if((y2-y1)<0){
temp=y1;y1=y2;y2=temp;
temp=x1;x1=x2;x2=temp;
}
if((y2-y1)!=0)
mx=(x2-x1)/(y2-y1);
else
mx=x2-x1;
x=x1;
for(i=y1;i<=y2;i++)
{
if(x<(float)le[i])
le[i]=(int)x;
if(x>(float)re[i])
re[i]=(int)x;
x+=mx;
}
}

void draw_pixel(int x,int y)


{
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)
{
int le[500],re[500];
int i,y;
for(i=0;i<500;i++)
{
le[i]=500;
re[i]=0;
}
edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);
Department of CSE,KSSEM Page 32
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
edgedetect(x3,y3,x4,y4,le,re);
edgedetect(x4,y4,x1,y1,le,re);
for(y=0;y<500;y++)
{
for(i=(int)le[y];i<(int)re[y];i++)
draw_pixel(i,y);
}
}

void display()
{
x1=200.0;y1=200.0;x2=100.0;y2=300.0;x3=200.0;y3=400.0;x4=300.0;y4=300.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
if(fillFlag==1)
scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
glFlush();
}

void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void fillMenu(int option)
{
if(option==1)
fillFlag=1;
if(option==2)
fillFlag=2;
display();
}

void main(int argc, char* argv[])


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
Department of CSE,KSSEM Page 33
COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
glutCreateWindow("Filling a Polygon using Scan-line Algorithm");
init();
glutDisplayFunc(display);
glutCreateMenu(fillMenu);
glutAddMenuEntry("Fill Polygon",1);
glutAddMenuEntry("Empty Polygon",2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}

OUTPUT :

Department of CSE,KSSEM Page 34


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67

VIVA Questions

Define Computer graphics.

Computer graphics may be defined as a pictorial representation or graphical


representation of objects in a computer.

Define Random scan/Raster scan displays?


Random scan is a method in which the display is made by the electronic beam
which is directed only to the points or part of the screen where the picture is to
be drawn. Picture is drawn as series of lines.
The Raster scan system is a scanning technique in which the electrons sweep
from top to bottom and from left to right. The intensity is turned on or off to
light and unlight the pixel.

List out the merits and demerits of Penetration techniques?


The merits and demerits of the Penetration techniques are as follows
• It is an inexpensive technique
• It has only four colors
• The quality of the picture is not good when it is compared to other techniques
• It can display color scans in monitors
• Poor limitation etc.

Define pixel.
Pixel is shortened forms of picture element. Each screen point is referred to as pixel or pel.

What is frame buffer?


Picture definition is stored in a memory area called frame buffer or refresh buffer.

DDA algorithm?
Digital differential analyzer is a scan conversion line algorithm based on
calculating either dx or dy at unit intervals in one coordinate and determine
corresponding integer values nearest to the line path for the other
coordinate.

Merits and de-merits of DDA:

Merit:
1. It is a faster method for calculating pixel positions than direct use.
2. It eliminates multiplication by making use of raster characteristics.

De-merits:

Department of CSE,KSSEM Page 35


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
1. It is time consuming
2. It is difficult to perform rounding and arithmetic operations.

What is Need for Bresenham’s algorithm?


Since this algorithm uses only incremental integer calculations we need to
decide which of the two possible pixel positions is closer to the line path at
each sample step.

What is Decision parameter and its need?


Decision parameter is an integer parameter whose value is proportional to the
difference between the separations of two pixel positions from the actual line
path. It is used to decide which of the two possible pixel positions is closer to
the line path at each sample step.

Properties of circle
1. Circle type
2. Width
3. Color
4. dot-dash patterns
5. Pen or brush options

Attribute primitive
The parameter that affects the way a primitive is to be displayed is referred to
as attribute parameters. Eg. Color, size that determine fundamental
characteristics of a primitive.

Attributes of a line
1. Line type
2. Line width
3. Pen and brush options
4. Line color

What is PHIGS?
PHIGS is Programmers hierarchical interactive graphics standard. It is the
second software standard to be developed and approved by standard
organization. It is an extension of graphical kernel system (GKS). It increases
capabilities for object modeling, color specifications, surface rendering and
picture manipulations.

Give some examples for computer graphics standards.


i. CORE – The Core graphics standard

Department of CSE,KSSEM Page 36


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
j. GKS -- The Graphics Kernel system
k. PHIGS – The Programmers Hierarchical Interactive Graphics System.
l. GSX – The Graphics system extension
m. NAPLPS – The North American presentation level protocol syntax.

What are the different types of line type attributes?


Solid lines
Dotted lines
Dashed lines

What is pixel mask?


It is a string containing the digits 1 and 0, to indicate which positions to plot along the line
path.

How will you specify the line width for lines with |m| < 1?
For lines with slope magnitude less than 1, we can modify a line drawing
routine to display thick lines by plotting a vertical span of pixels at each x
position along the line. The no. of pixels in each span is set equal to the
integer magnitude of parameter lw.

How will you specify the line width for lines with |m| >1?
For lines with slope magnitude greater than 1, we can modify a thick line with
horizontal spans alternatively picking up pixels to the right and left of the line
path.

What is a Look-up table?


It is a scheme for storing color values in a color look-up table where frame
buffer values are now used as indices into the color table. This scheme
would allow the user to select any 256 colors for simultaneous display from
a palette of nearly 17 million colors.

What are the area-fill attributes?


1. Fill styles
2. Pattern fill
3. Soft fill

How areas are displayed with 3 basic fill styles?


1. Hollow with a color border- using only the boundary outline
2. Filled with solid color- displayed in a single color including the borders
3. Filled with specified pattern or design

Give types of algorithms for filling object with colors?


Boundary fill Algorithm Flood Fill Algorithm
Scanline Polygon fill Algorithm

What is Transformation?

Department of CSE,KSSEM Page 37


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
Transformation is the process of introducing changes in the shape size and
orientation of the object using scaling rotation reflection shearing & translation etc.

What is translation?
Translation is the process of changing the position of an object in a straight-line path
from one coordinate location to another.

What is rotation?
A 2-D rotation is done by repositioning the coordinates along a circular path, in the
x-y plane by making an angle with the axes.

What is scaling?
The scaling transformations changes the shape of an object and can be carried out by
multiplying each vertex (x,y) by scaling factor Sx,Sy where Sx is the scaling factor of
x and Sy is the scaling factor of y.

What is shearing?
The shearing transformation actually slants the object along the X direction or the
Y direction as required.ie, this transformation slants the shape of an object along a
required plane.

What is reflection?
The reflection is actually the transformation that produces a mirror image of an
object. For this use some angles and lines of reflection.

What is the need of homogeneous coordinates?


To perform more than one transformation at a time, use homogeneous coordinates
or matrixes. They reduce unwanted calculations intermediate steps saves time and
memory and produce a sequence of transformations.

Distinguish between uniform scaling and differential scaling.


When the scaling factors sx and sy are assigned to the same value, a uniform scaling
is produced that maintains relative object proportions. Unequal values for sx and sy
result in a differential scaling that is often used in design application.

What is fixed point scaling?


The location of a scaled object can be controlled by a position called the fixed
point that is to remain unchanged after the scaling transformation.

Distinguish between window port & view port.


A portion of a picture that is to be displayed by a window is known as window
port. The display area of the part selected or the form in which the selected part is

Department of CSE,KSSEM Page 38


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
viewed is known as view port.

Define clipping.
Clipping is the method of cutting a graphics display to neatly fit a predefined graphics region
or the view port.

What is covering (exterior clipping)?


This is just opposite to clipping. This removes the lines coming inside the windows
and displays the remaining. Covering is mainly used to make labels on the complex
pictures.

List out the various Text clipping?


_ All-or-none string clipping - if all of the string is inside a clip window, keep it otherwise
discards.
_ All-or-none character clipping – discard only those characters that are not
completely inside the window. Any character that either overlaps or is outside a
window boundary is clipped.
_ Individual characters – if an individual character overlaps a clip window
boundary, clip off the parts of the character that are outside the window.

What are the various representation schemes used in three dimensional objects?
Boundary representation (B-res) – describe the 3 dimensional objects as a set of
surfaces that separate the object interior from the environment.
Space- portioning representation – describe interior properties, by partitioning the
spatial region containing an object into a set of small, no overlapping, contiguous
solids.

What is Polygon mesh?


Polygon mesh is a method to represent the polygon, when the object surfaces are
tiled, it is more convenient to specify the surface facets with a mesh function. The
various meshes are Triangle strip – (n-2) connected triangles Quadrilateral mesh –
generates (n-1)(m-1) Quadrilateral

What are the advantages of rendering polygons by scan line method?


i. The max and min values of the scan were easily found.
ii. The intersection of scan lines with edges is easily calculated by a simple incremental
method.
iii. The depth of the polygon at each pixel is easily calculated by an incremental method.
What is a Blobby object?
Some objects do not maintain a fixed shape, but change their surface characteristics
in certain motions or when in proximity to other objects. That is known as blobby
objects. Example – molecular structures, water droplets.

Define Octrees.
Hierarchical tree structures called octrees are used to represent solid objects in some
graphics systems. Medical imaging and other applications that require displays of

Department of CSE,KSSEM Page 39


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
object cross sections commonly use octree representation.

Define B-Spline curve.


A B-Spline curve is a set of piecewise (usually cubic) polynomial segments that pass
close to a set of control points. However the curve does not pass through these control
points, it only passes close to them.

What is a Blobby object?


Some objects do not maintain a fixed shape, but change their surface Characteristics
in certain motions or when in proximity to other objects. That is known as blobby
objects. Example – molecular structures, water droplets.

What are the steps involved in 3D transformation pipeline?


• Modeling Transformation
• Viewing Transformation
• Projection Transformation
• Workstation Transformation

What do you mean by view plane?


A view plane is nothing but the film plane in camera which is positioned and
oriented for a particular shot of the scene.

Define projection.
The process of converting the description of objects from world coordinates to
viewing coordinates is known as projection

What you mean by parallel projection?


Parallel projection is one in which z coordinates is discarded and parallel lines from
each vertex on the object are extended until they intersect the view plane.

What do you mean by Perspective projection?


Perspective projection is one in which the lines of projection are not parallel.
Instead, they all converge at a single point called the center of projection.

What is Projection reference point?


In Perspective projection, the lines of projection are not parallel. Instead, they all
converge at a single point called Projection reference point.

What is the use of Projection reference point?


In Perspective projection, the object positions are transformed to the view plane
along these converged projection line and the projected view of an object is
determined by calculating the intersection of the converged projection lines with the

Department of CSE,KSSEM Page 40


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
view plane.

What are the different types of parallel projections?


The parallel projections are basically categorized into two types, depending on the
relation between the direction of projection and the normal to the view plane. They
are orthographic parallel projection and oblique projection.

What is orthographic parallel projection?


When the direction of the projection is normal (perpendicular) to the view plane
then the projection is known as orthographic parallel projection

What is orthographic oblique projection?


When the direction of the projection is not normal (not perpendicular) to the view
plane then the projection is known as oblique projection.

What is an axonometric orthographic projection?


The orthographic projection can display more than one face of an object. Such
an orthographic projection is called axonometric orthographic projection.

What is cavalier projection?


The cavalier projection is one type of oblique projection, in which the direction of
projection makes a 45-degree angle with the view plane.

What is vanishing point?


The perspective projections of any set of parallel lines that are not parallel to the
projection plane converge to appoint known as vanishing point.

What do you mean by principle vanishing point?


The vanishing point of any set of lines that are parallel to one of the three
principle axes of an object is referred to as a principle vanishing point or axis
vanishing point.

What is view reference point?


The view reference point is the center of the viewing coordinate system. It is
often chosen to be close to or on the surface of the some object in the scene.

What is the use of control points?


Spline curve can be specified by giving a set of coordinate positions called control
points, which indicates the general shape of the curve, can specify spline curve.
What is Bezier Basis Function?
Bezier Basis functions are a set of polynomials, which can be used instead of the
primitive polynomial basis, and have some useful properties for interactive curve

Department of CSE,KSSEM Page 41


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
design.

What is surface patch?


A single surface element can be defined as the surface traced out as two parameters
(u, v) take all possible values between 0 and 1 in a two-parameter representation.
Such a single surface element is known as a surface patch.

Define B-Spline curve.


A B-Spline curve is a set of piecewise (usually cubic) polynomial segments that
pass close to a set of control points. However the curve does not pass through
these control points, it only passes close to them.

What is a spline?
To produce a smooth curve through a designed set of points, a flexible strip
called spline is used. Such a spline curve can be mathematically described with a
piecewise cubic polynomial function whose first and second derivatives are
continuous across various curve section.

What are the different ways of specifying spline curve?


• Using a set of boundary conditions that are imposed on the spline.
• Using the state matrix that characteristics the spline
• Using a set of blending functions that calculate the positions along the curve path by
specifying combination of geometric constraints on the curve

What are the important properties of Bezier Curve?


• It needs only four control points
• It always passes through the first and last control points
• The curve lies entirely within the convex half formed by four control points.

Differentiate between interpolation spline and approximation spline.


When the spline curve passes through all the control points then it is called
interpolate. When the curve is not passing through all the control points then that
curve is called approximation spline.

Define computer graphics animation.


Computer graphics animation is the use of computer graphics equipment where the
graphics output presentation dynamically changes in real time. This is often also
called real time animation. Visual changes of scene over screen with respect to time.

Define frame.
One of the shape photographs that a film or video is made of is known as frame.

Department of CSE,KSSEM Page 42


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT 18CSL67
What is key frame?
One of the shape photographs that a film or video is made of the shape of an
object is known initially and for a small no of other frames called key frame

Give the steps for designing an animation sequence?

The steps are:


1. Story board layout
2. Object parameters
3. Key frame generation
4. In-between generation

Give computer animation languages?

C, LISP, PASCAL, FORTRAN, FLASH, MAYA etc

Department of CSE,KSSEM Page 43

You might also like