0% found this document useful (0 votes)
334 views45 pages

CGV 18cs67 Lab Manual

This lab manual outlines experiments to be conducted using OpenGL for a Computer Graphics lab course. It includes a list of 9 experiments involving line drawing algorithms, 3D transformations, clipping, shading, and more. It also describes a mini-project for students to develop an application using OpenGL. The document provides background on OpenGL including its hierarchy, functions, conventions and how to set up an OpenGL program with GLUT. It explains concepts such as the model-view and projection matrices, and notes OpenGL must be run within an event loop to handle user input events.

Uploaded by

Nagamani D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
334 views45 pages

CGV 18cs67 Lab Manual

This lab manual outlines experiments to be conducted using OpenGL for a Computer Graphics lab course. It includes a list of 9 experiments involving line drawing algorithms, 3D transformations, clipping, shading, and more. It also describes a mini-project for students to develop an application using OpenGL. The document provides background on OpenGL including its hierarchy, functions, conventions and how to set up an OpenGL program with GLUT. It explains concepts such as the model-view and projection matrices, and notes OpenGL must be run within an event loop to handle user input events.

Uploaded by

Nagamani D
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

18CSL67

BRINDAVAN COLLEGE OF ENGINEERING


DWARAKANAGAR, BANGALORE – 560063

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


COMPUTER GRAPHICS LABORATORY WITH MINI PROJECT

LAB MANUAL
18CSL67

PREPARED BY:

PADMAVATHI H G Associate Professor


R.MEENATCHI Assistant Professor
BHARATHI J Assistant Professor

PART A
Design, develop, and implement the following programs in C/C++ using OpenGL API
S.NO LIST OF EXPRIEMENTS
Implement Brenham's line drawing algorithm for all types of
1
slope.
2 Create and rotate a triangle about the origin and a xed point.

3 Draw a colour cube and spin it using OpenGL transformation


matrices.
4 Draw a color cube and allow the user to move the camera suitably
to experiment with perspective viewing.
5 Clip a line using Cohen-Sutherland algorithm

6 To draw a simple shaded scene consisting of a tea pot on a


table. Dene suitably the position and properties of the light
source along with the properties of the surfaces of the solid
object used in the scene
7 Design, develop and implement recursively subdivide a
tetrahedron to form 3D sierpinski gasket. The number of
recursive steps is to be specied by the user.
8 Develop a menu driven program to animate image using Bezier
Curve algorithm

1
Dept Of CSE , BRCE
18CSL67

9 Develop a menu driven program to ll the polygon using scan line


algorithm

PART B (MINI-PROJECT)

Students should develop a mini project on the topics metioned below or similar
applica-tions using OpenGL API. Consider all types of attributes like color,
thickness, styles, font, background, speed etc., while doing mini project.
During the practical exam, students should demonstrate and answer viva-voce.
Simulation of concepts of OS, Data Structres, Algorithms etc.

COURSE OBJECTIVES:-

This course will enable students to

• Demonstrate simple algorithms using OpenGL 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.

COURSE OUTCOMES:-
The students should be able to

• Apply the concepts of computer graphics

• Implement computer graphics applications using OpenGL

• Animate real world problems using OpenGL

2
Dept Of CSE , BRCE
18CSL67

OpenGL Hierarchy
• Several levels of abstraction are provided
• GL
Lowest level: vertex, matrix manipulation
glVertex3f(point.x, point.y, point.z)
• GLU
Helper functions for shapes, transformations
gluPerspective( fovy, aspect, near, far )
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
• GLUT
Highest level: Window and interface management
glutSwapBuffers()
glutInitWindowSize (500, 500);
OpenGL Implementations
• OpenGL IS an API (think of as collection of .h files):
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
• Windows, Linux, UNIX, etc. all provide a platform specific implementation.
• Windows: opengl32.lib glu32.lib glut32.lib
• Linux: -l GL -l GLU –l GLUT

OpenGL: Camera
• Two things to specify:
Physical location of camera in the scene (MODELVIEW matrix in OpenGL).
Projection properties of the camera (PROJECTION matrix in OpenGL):

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom,GLdouble top, GLdouble


near, GLdouble far);

void glOrtho(GLdouble left, GLdouble right, GLdouble bottom,


GLdouble top, GLdouble near, GLdouble far);
3
Dept Of CSE , BRCE
18CSL67

OpenGL Conventions
Many functions have multiple forms:
glVertex2f, glVertex3i, glVertex4dv, etc.
Number indicates number of arguments
Letters indicate type
f: float, d: double, ub: unsigned byte, etc.
“v‟ (if present) indicates a single pointer argument

Required files for Windows


• In the System Directory
– glut32.dll
• In the C++ Include Directory
– gl\glut.h (includes both gl.h and glu.h)
• In the C++ Library Directory
– gl\glut32.lib

Setting up GLUT - main()


GLUT provides high-level utilities to simplify OpenGL programming, especially in interacting
with the
Operating System (such as creating a window, handling key and mouse inputs). The following
GLUT
functions were used in the above program:
glutInit: initializes GLUT, must be called before other GL/GLUT functions. It takes the same
arguments as the main().
void glutInit(int *argc, char **argv)
glutCreateWindow: creates a window with the given title.
int glutCreateWindow(char *title)
glutInitWindowSize: specifies the initial window width and height, in pixels.
void glutInitWindowSize(int width, int height)
glutInitWindowPosition: positions the top-left corner of the initial window at (x, y).
The coordinates (x, y), in term of pixels, is measured in window coordinates, i.e., origin
(0, 0) is at the top-left corner of the screen; x-axis pointing right and y-axis pointing
down.
void glutInitWindowPosition(int x, int y)
glutDisplayFunc: registers the callback function (or event handler) for handling
window-paint event. The OpenGL graphic system calls back this handler when it
receives a window re-paint request. In the example, we register the function display() as
the handler.
void glutDisplayFunc(void (*func)(void)) 4
Dept Of CSE , BRCE
18CSL67

glutMainLoop: enters the infinite event-processing loop, i.e, put the OpenGL graphics
system to wait for events (such as re-paint), and trigger respective event handlers (such
as display()).
void glutMainLoop()

glutInitDisplayMode: requests a display with the specified mode, such as color mode
(GLUT_RGB, GLUT_RGBA, GLUT_INDEX), single/double buffering (GLUT_SINGLE,
GLUT_DOUBLE), enable depth (GLUT_DEPTH), joined with a bit OR '|'.
void glutInitDisplayMode(unsigned int displayMode)
void glMatrixMode (GLenum mode);
The glMatrixMode function specifies which matrix is the current matrix.
Void
glOrtho(GLdoubleleft,GLdoubleright,GLdoublebottom,GLdoubletop,GLdoublezNear,G
Ldou blezFar);
The glOrtho function multiplies the current matrix by an orthographic matrix.
void glPointSize (GLfloat size);
The glPointSize function specifies the diameter of rasterized points.
Void glutPostRedisplay(void);
glutPostRedisplay marks the current window as needing to be redisplayed.
void glPushMatrix (void);
void glPopMatrix (void);
The glPushMatrix and glPopMatrix functions push and pop the current matrix stack.
GLintglRenderMode (GLenum mode);
The glRenderMode function sets the rasterization mode.
void glRotatef (GLfloat angle, GLfloat x, GLfloat y, GLfloat);
The glRotatef functions multiply the current matrix by a rotation matrix.
void glScalef (GLfloat x, GLfloat y, GLfloat z);
The glScalef functions multiply the current matrix by a general scaling matrix.
void glTranslatef (GLfloat x, GLfloat y, GLfloat z);
The glTranslatef functions multiply the current matrix by a translation matrix.
void glViewport (GLint x, GLint y, GLsizei width, GLsizei height);
The glViewport function sets the viewport.
void glEnable, glDisable();
The glEnable and glDisable functions enable or disable OpenGL capabilities.
glutBitmapCharacter();
The glutBitmapCharacter function used for font style.

5
Dept Of CSE , BRCE
18CSL67

OpenGL Hierarchy:
Several levels of abstraction are provided
GL
Lowest level: vertex, matrix manipulation
glVertex3f(point.x, point.y, point.z)
GLU
Helper functions for shapes, transformations
gluPerspective( fovy, aspect, near, far )
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
GLUT
Highest level: Window and interface management
glutSwapBuffers()
glutInitWindowSize (500, 500);
OpenGL Implementations :
OpenGL IS an API (think of as collection of .h files):
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
Windows, Linux, UNIX, etc. all provide a platform specific implementation.
Windows: opengl32.lib glu32.lib glut32.lib
Linux: -l GL -l GLU –l GLUT

Event Loop:
• OpenGL programs often run in an event loop: 6
Dept Of CSE , BRCE
18CSL67

– Start the program


– Run some initialization code
– Run an infinite loop and wait for events such as
• Key press
• Mouse move, click
• Reshape window
• Expose event

OpenGL Command Syntax (1) :


• OpenGL commands start with “gl”
• OpenGL constants start with “GL_”
• Some commands end in a number and one, two or three letters at the end (indicating number
and type of
arguments)
• A Number indicates number of arguments
• Characters indicate type of argument
OpenGL Command Syntax (2)
– `f' float
– `d' double float
– `s' signed short integer
– `i' signed integer
– `b' character
– `ub' unsigned character
– `us' unsigned short integer
– `ui' unsigned integer

7
Dept Of CSE , BRCE
18CSL67

Projections in OpenGL

Perspective projection:

8
Dept Of CSE , BRCE
18CSL67

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom,GLdouble top,


GLdouble near, GLdouble far);

Orthographic projection:

Bresenham's Line Algorithm:-

1. Bresenham's Line Algorithm use fixed point, i.e., Integer Arithmetic


2. Bresenham's Line Algorithm uses only subtraction and addition its operation
3. Bresenham's Algorithm is faster than DDA Algorithm in line because it involves only
addition & subtraction in its calculation and uses only integer arithmetic.
4. Bresenham's Line Algorithm is more accurate and efficient at DDA Algorithm. 9
Dept Of CSE , BRCE
18CSL67

5. Bresenham's Line Algorithm can draw circle and curves with more accurate than DDA
Algorithm.

Bresenham's Line Algorithm


This algorithm is used for scan converting a line. It was developed by Bresenham. It is an
efficient method because it involves only integer addition, subtractions, and multiplication
operations. These operations can be performed very rapidly so lines can be generated quickly.

In this method, next pixel selected is that one who has the least distance from true line.

The method works as follows:

Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work our may to the night, one
pixel position at a time in the horizontal direction toward P2'(x2',y2').

Once a pixel in choose at any step

The next pixel is

Either the one to its right (lower-bound for the line)

One top its right and up (upper-bound for the line)

The line is best approximated by those pixels that fall the least distance from the path between
P1',P2'.

10
Dept Of CSE , BRCE
18CSL67

To chooses the next one between the bottom pixel S and top pixel T.
If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1

The actual y coordinates of the line at x = xi+1is


y=mxi+1+b

Bresenham's Line Algorithm


The distance from S to the actual line in y direction
s = y-yi

The distance from T to the actual line in y direction


t = (yi+1)-y

Now consider the difference between these 2 distance values


s-t

When (s-t) <0 ⟹ s < t


The closest pixel is S

11
Dept Of CSE , BRCE
18CSL67

When (s-t) ≥0 ⟹ s < t

The closest pixel is T

This difference is
s-t = (y-yi)-[(yi+1)-y]
= 2y - 2yi -1

Bresenham's Line Algorithm


Substituting m by Bresenham's Line Algorithm and introducing decision variable
di=△x (s-t)
di=△x (2 Bresenham's Line Algorithm (xi+1)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.xi-2△x.yi+c

Where c= 2△y+△x (2b-1)

We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)

Since x_(i+1)=xi+1,we have


di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Special Cases

If chosen pixel is at the top pixel T (i.e., di≥0)⟹ yi+1=yi+1


di+1=di+2△y-2△x

If chosen pixel is at the bottom pixel T (i.e., di<0)⟹ yi+1=yi


di+1=di+2△y

Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]

Since mx1+b-yi=0 and m = Bresenham's Line Algorithm, we have


d1=2△y-△x

Advantage:
1. It involves only integer arithmetic, so it is simple.

12
Dept Of CSE , BRCE
18CSL67

2. It avoids the generation of duplicate points.

3. It can be implemented using hardware because it does not use multiplication and
division.

4. It is faster as compared to DDA (Digital Differential Analyzer) because it does not


involve floating point calculations like DDA Algorithm.

Disadvantage:
1. This algorithm is meant for basic line drawing only Initializing is not a part of
Bresenham's line algorithm. So to draw smooth lines, you should want to look into a
different algorithm.

Bresenham's Line Algorithm:


Step1: Start Algorithm

Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy

Step3: Enter value of x1,y1,x2,y2


Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point

Step4: Calculate dx = x2-x1


Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx

Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2

Step6: Generate point at (x,y)coordinates.

Step7: Check if whole line is generated.


If x > = xend 13
Dept Of CSE , BRCE
18CSL67

Stop.

Step8: Calculate co-ordinates of the next pixel


If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1

Step9: Increment x = x + 1

Step10: Draw a point of latest (x, y) coordinates

Step11: Go to step 7

Step12: End of Algorithm

Example: Starting and Ending position of the line are (1, 1) and (8, 5). Find
intermediate points.

Solution: x1=1
y1=1
x2=8
y2=5
dx= x2-x1=8-1=7
dy=y2-y1=5-1=4
I1=2* ∆y=2*4=8
I2=2*(∆y-∆x)=2*(4-7)=-6
d = I1-∆x=8-7=1

x y d=d+I1 or I2

1 1 d+I2=1+(-6)=-5

2 2 d+I1=-5+8=3

3 2 d+I2=3+(-6)=-3

4 3 d+I1=-3+8=5

5 3 d+I2=5+(-6)=-1
14
Dept Of CSE , BRCE
18CSL67

6 4 d+I1=-1+8=7

7 4 d+I2=7+(-6)=1

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

#include<glut.h>
#include<stdio.h>

int x1, y1, x2, y2;

void drawpixel(int x, int y)


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

void brenhamslinedraw(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;
15
Dept Of CSE , BRCE
18CSL67

}
drawpixel(x, y);
while(x<x2)
{
x++;
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyMinusDx;
}
drawpixel(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);
brenhamslinedraw(x1, y1, x2, y2);
glFlush();
}

16
Dept Of CSE , BRCE
18CSL67

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);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(display);
glutMainLoop();
}

OUTPUT :

17
Dept Of CSE , BRCE
18CSL67

2. Create and rotate a triangle about the origin and a fixed point.
#include<glut.h>
#include<stdio.h>

int x,y;
int rFlag=0;

void drawpixel(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;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

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


{
trX=0.0;
trY=0.0;
th+=0.1;
drawpixel(0.0,0.0);
}
if(rFlag==2) //Rotate Around Fixed Point
{
trX=x;
trY=y;
th+=0.1;
drawpixel(x,y);
}
glTranslatef(trX,trY,0.0);
glRotatef(th,0.0,0.0,1.0);
glTranslatef(-trX,-trY,0.0);
triangle();
glutPostRedisplay();
glutSwapBuffers();
}

18
Dept Of CSE , BRCE
18CSL67

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;
}
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 :

4
Dept Of CSE , BRCE
18CSL67

Rotation Around the ORIGIN

Rotation Around the FIXED POINT

5
Dept Of CSE , BRCE
18CSL67

3. Program to draw a color cube and spin it using OpenGL transformation matrices.

#include <stdlib.h>
#include <glut.h>
GLfloat vertices[][3] = {{-1,-1,-1},{1,-1,-1},{1,1,-1},{-1,1,-1},
{-1,-1,1},{1,-1,1}, {1,1,1},{-1,1,1}};
GLfloat colors[][3] = {{1,0,0},{1,1,0},{0,1,0},{0,0,1},
{1,0,1},{1,1,1},{0,1,1},{0.5,0.5,0.5}};

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


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

void colorcube(void)
{
polygon(0,3,2,1);
polygon(0,4,7,3);
polygon(5,4,0,1);
polygon(2,3,7,6);
polygon(1,2,6,5);
polygon(4,5,6,7);
}

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


GLint axis = 2;

void display(void)
{
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();
glutSwapBuffers();
}

void spinCube()
{
theta[axis] += 1.0;
if( theta[axis] > 360.0 )
theta[axis] -= 360.0;
glutPostRedisplay();
}

6
Dept Of CSE , BRCE
18CSL67

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;
}

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);
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 :

7
Dept Of CSE , BRCE
18CSL67

4. Program to draw a color cube and allow the user to move the camera suitably to
experiment with perspective viewing.

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

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


{-1,-1,1},{1,-1,1}, {1,1,1},{-1,1,1}};
GLfloat colors[][3] = {{1,0,0},{1,1,0},{0,1,0},{0,0,1},
{1,0,1},{1,1,1},{0,1,1},{0.5,0.5,0.5}};

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


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

void colorcube(void)
{
polygon(0,3,2,1);
polygon(0,4,7,3);
polygon(5,4,0,1);
polygon(2,3,7,6);
polygon(1,2,6,5);
polygon(4,5,6,7);
}

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


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

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
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();
}

8
Dept Of CSE , BRCE
18CSL67

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)


{
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();
}

void myReshape(int w, int h)


{
glViewport(0, 0, w, h);
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);
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();
}

9
Dept Of CSE , BRCE
18CSL67

OUTPUT :

10
Dept Of CSE , BRCE
18CSL67

5. Program to clip a lines using Cohen-Sutherland line-clipping algorithm.

#include <stdio.h>
#include <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;
}
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;
}

11
Dept Of CSE , BRCE
18CSL67

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);
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);

12
Dept Of CSE , BRCE
18CSL67

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();
}

OUTPUT :

13
Dept Of CSE , BRCE
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<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);
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);
}

14
Dept Of CSE , BRCE
18CSL67

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);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}

15
Dept Of CSE , BRCE
18CSL67

OUTPUT :

16
Dept Of CSE , BRCE
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

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

float point v[][3]={{0.0, 0.0, 0.0}, {0.0, 1.0, -1.0},{-1.0, -1.0, -1.0},
{1.0, -1.0, -1.0}};
int n;

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


{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

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


{
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);
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)


{
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();
}

17
Dept Of CSE , BRCE
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)


{
printf(" Enter the Number of Divisions ? ");
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 :

18
Dept Of CSE , BRCE
18CSL67

8. Develop a menu driven program to animate a flag using Bezier curve algorithm.

#include<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);
computeBezPt(u, &bcPt, cPt, pt1, C);
glVertex2f(bcPt.x, bcPt.y);
}
glEnd();
delete[]C;
}

19
Dept Of CSE , BRCE
18CSL67

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);
glVertex2f(100,40);
glEnd();

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

20
Dept Of CSE , BRCE
18CSL67

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 :

21
Dept Of CSE , BRCE
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 <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);
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);
}
}

22
Dept Of CSE , BRCE
18CSL67

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);
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();
}

23
Dept Of CSE , BRCE
18CSL67

OUTPUT :

24
Dept Of CSE , BRCE
18CSL67

PART B (MINI-PROJECT)

Students should develop a mini project on the topics metioned below or similar applica-tions using OpenGL
API. Consider all types of attributes like color, thickness, styles, font, background, speed etc., while doing
mini project. During the practical exam, students should demonstrate and answer viva-voce. Simulation of
concepts of OS, Data Structres, Algorithms etc.

Viva questions and answers


1. What is Computer Graphics? Answer: Computer graphics are graphics created using
computers and, more generally, the representation and manipulation of image data by a
computer.
2. What is OpenGL? Answer: OpenGL is the most extensively documented 3D graphics API
(Application Program
Interface) to date. It is used to create Graphics.
3. What is GLUT? Answer: The OpenGL Utility Toolkit (GLUT) is a library of utilities for
OpenGL programs, which
primarily perform system-level I/O with the host operating system.
4. What are the applications of Computer Graphics? Answer: Gaming Industry, Animation
Industry and Medical
Image Processing Industries. The sum total of these industries is a Multi Billion Dollar
Market. Jobs will continue to
increase in this arena in the future.
5. Explain in breif 3D Sierpinski gasket? Answer: The Sierpinski triangle (also with the
original orthography
Sierpinski), also called the Sierpinski gasket or the Sierpinski Sieve, is a fractal named after
the Polish mathematician
Waclaw Sierpinski who described it in 1915. Originally constructed as a curve, this is one of
the basic
examples of self-similar sets, i.e. it is a mathematically generated pattern that can be
reproducible at any
magnifcation or reduction.
6. What is Liang-Barsky line clipping algorithm? Answer: In computer graphics, the Liang-
25
Dept Of CSE , BRCE
18CSL67

Barsky algorithm is a line


clipping algorithm. The Liang-Barsky algorithm uses the parametric equation of a line and
inequalities describing
the range of the clipping box to determine the intersections between the line and the clipping
box. With these
intersections it knows which portion of the line should be drawn.
7. Explain in brief Cohen-Sutherland line-clipping algorithm? Answer: The Cohen-Sutherland
line clipping algorithm
quickly detects and dispenses with two common and trivial cases. To clip a line, we need to
consider only its
endpoints. If both endpoints of a line lie inside the window, the entire line lies inside the
window. It is trivially
accepted and needs no clipping. On the other hand, if both endpoints of a line lie entirely to
one side of the
window, the line must lie entirely outside of the window. It is trivially rejected and needs to be
neither clipped nor
displayed.
8. Explain in brief scan-line area flling algorithm? Answer: The scanline fll algorithm is an
ingenious way of flling in
irregular polygons. The algorithm begins with a set of points. Each point is connected to the
next, and the line
between them is considered to be an edge of the polygon. The points of each edge are adjusted
to ensure that
the point with the smaller y value appears frst. Next, a data structure is created that contains a
list of edges that
begin on each scanline of the image. The program progresses from the frst scanline upward.
For each line, any
pixels that contain an intersection between this scanline and an edge of the polygon are flled
in. Then, the
algorithm progresses along the scanline, turning on when it reaches a polygon pixel and
turning of when it
reaches another one, all the way across the scanline.
9. Explain Midpoint Line algorithm Answer: The Midpoint line algorithm is an algorithm
which determines which
points in an n-dimensional raster should be plotted in order to form a close approximation to a
straight line between
two given points. It is commonly used to draw lines on a computer screen, as it uses only
integer addition, subtraction
and bit shifting, all of which are very cheap operations in standard computer architectures.
10. What is a Pixel? Answer: In digital imaging, a pixel (or picture element) is a single point in
a raster image. The
Pixel is the smallest addressable screen element; it is the smallest unit of picture which can 26
be
Dept Of CSE , BRCE
18CSL67

controlled. Each Pixel


has its address. The address of Pixels corresponds to its coordinate. Pixels are normally
arranged in a 2-dimensional
grid, and are often represented using dots or squares.
11. What is Graphical User Interface? Answer: A graphical user interface (GUI) is a type of
user interface item
that allows people to interact with programs in more ways than typing such as computers;
hand-held devices such as
MP3 Players, Portable Media Players or Gaming devices; household appliances and ofce
equipment with images
rather than text commands.
12. What is the general form of an OpenGL program? Answer: There are no hard and fast
rules. The following
pseudocode is generally recognized as good OpenGL form. program entrypoint { // Determine
which depth or pixel
format should be used. // Create a window with the desired format. // Create a rendering
context and make it current
with the window. // Set up initial OpenGL state. // Set up callback routines for window resize
and window refresh. }
handle resize { glViewport(...); glMatrixMode(GL PROJECTION); glLoadIdentity(); // Set
projection transform with
glOrtho, glFrustum, gluOrtho2D, gluPerspective, etc. } handle refresh { glClear(...);
glMatrixMode(GL MODELVIEW);
glLoadIdentity(); // Set view transform with gluLookAt or equivalent // For each object (i) in
the scene that needs to be
rendered: // Push relevant stacks, e.g., glPushMatrix, glPushAttrib. // Set OpenGL state specifc
to object (i). // Set
model transform for object (i) using glTranslatef, glScalef, glRotatef, and/or equivalent. //
Issue rendering commands
for object (i). // Pop relevant stacks, (e.g., glPopMatrix, glPopAttrib.) // End for loop. // Swap
bufers. }
13. What support for OpenGL does Open,Net,FreeBSD or Linux provide? Answer: The X
Windows
implementation, XFree86 4.0, includes support for OpenGL using Mesa or the OpenGL
Sample Implementation.
XFree86 is released under the XFree86 license. http://www.xfree86.org/
14. What is the AUX library? Answer: The AUX library was developed by SGI early in
OpenGL's life to ease
creation of small OpenGL demonstration programs. It's currently neither supported nor
maintained. Developing
OpenGL programs using AUX is strongly discouraged. Use the GLUT instead. It's more
fexible and powerful and is 27
Dept Of CSE , BRCE
18CSL67

available on a wide range of platforms. Very important: Don't use AUX. Use GLUT instead.
15. How does the camera work in OpenGL? Answer: As far as OpenGL is concerned, there is
no camera. More
specifcally, the camera is always located at the eye space coordinate (0., 0., 0.). To give the
appearance of moving
the camera, your OpenGL application must move the scene with the inverse of the camera
transformation.
16. How do I implement a zoom operation? Answer: A simple method for zooming is to use a
uniform scale on the
ModelView matrix. However, this often results in clipping by the zNear and zFar clipping
planes if the model is scaled
too large. A better method is to restrict the width and height of the view volume in the
Projection matrix.
17. What are OpenGL coordinate units? Answer: Depending on the contents of your geometry
database, it may
be convenient for your application to treat one OpenGL coordinate unit as being equal to one
millimeter or one parsec
or anything in between (or larger or smaller). OpenGL also lets you specify your geometry
with coordinates of difering
values. For example, you may fnd it convenient to model an airplane's controls in centimeters,
its fuselage in meters,
and a world to fy around in kilometers. OpenGL's ModelView matrix can then scale these
diferent coordinate systems
into the same eye coordinate space. It's the application's responsibility to ensure that the
Projection and ModelView
matrices are constructed to provide an image that keeps the viewer at an appropriate distance,
with an appropriate
feld of view, and keeps the zNear and zFar clipping planes at an appropriate range. An
application that displays
molecules in micron scale, for example, would probably not want to place the viewer at a
distance of 10 feet with a 60
degree feld of view.
18. What is Microsoft Visual Studio? Answer: Microsoft Visual Studio is an integrated
development environment
(IDE) for developing windows applications. It is the most popular IDE for developing
windows applications or windows
based software.
19. What does the .gl or .GL fle format have to do with OpenGL? Answer: .gl fles have
nothing to do with
OpenGL, but are sometimes confused with it. .gl is a fle format for images, which has no
relationship to OpenGL.
20. Who needs to license OpenGL? Who doesn't? Is OpenGL free software? Answer: 28
Dept Of CSE , BRCE
18CSL67

Companies which will be


creating or selling binaries of the OpenGL library will need to license OpenGL. Typical
examples of licensees include
hardware vendors, such as Digital Equipment, and IBM who would distribute OpenGL with
the system software on
their workstations or PCs. Also, some software vendors, such as Portable Graphics and
Template Graphics, have a
business in creating and distributing versions of OpenGL, and they need to license OpenGL.
Applications developers
do NOT need to license OpenGL. If a developer wants to use OpenGL that developer needs to
obtain copies of a
linkable OpenGL library for a particular machine. Those OpenGL libraries may be bundled in
with the development
and/or run-time options or may be purchased from a third-party software vendor, without
licensing the source code or
use of the OpenGLtrademark.
21. How do we make shadows in OpenGL? Answer: There are no individual routines to
control neither shadows
nor an OpenGL state for shadows. However, code can be written to render shadows.
22. What is the use of Glutinit? Answer: void glutInit(int *argcp, char **argv); glutInit will
initialize the GLUT library
and negotiate a session with the window system. During this process, glutInit may cause the
termination of the GLUT
program with an error message to the user if GLUT cannot be properly initialized.
23. Describe the usage of glutInitWindowSize and glutInitWindowPosition? Answer: void
glutInitWindowSize(int
width, int height); void glutInitWindowPosition(int x, int y); Windows created by
glutCreateWindow will be requested to
be created with the current initial window position and size. The intent of the initial window
position and size values is to provide a suggestion to the window system for a window's initial
size and position. The window system is not
obligated to use this information. Therefore, GLUT programs should not assume the window
was created at the
specifed size or position. A GLUT program should use the window's reshape callback to
determine the true size of the
window.
24. Describe the usage of glutMainLoop? Answer: void glutMainLoop(void); glutMainLoop
enters the GLUT event
processing loop. This routine should be called at most once in a GLUT program. Once called,
this routine will never
return. It will call as necessary any callbacks that have been registered.
29
Dept Of CSE , BRCE
18CSL67

30
Dept Of CSE , BRCE

You might also like