Académique Documents
Professionnel Documents
Culture Documents
Practical # 01
Objective:
Installaion of Visual Studio 2010.
Steps:
1. Download the file glut-3.7.6-bin fromhere.Download the file glut-3.7.6-bin fromhere.
http://www.microsoft.com/en-pk/download/details.aspx?id=23691
1
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
2
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Next, we get a license page, as well as an overview of what is going to be installed. The key
components are:
3
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Now the actual installation begins and we can see a more complete list of all the components that
will be installed. For completeness, here’s the full list:
VC 9.0 Runtime
VC 10.0 Runtime
Microsoft .NET Framework 4 Beta 1
Microsoft Help 3.0 Beta 1
Microsoft Visual Studio Macro Tools
Microsoft Visual Studio 2010 Professional Beta 1
Microsoft Web Deployment Tool
Visual Studio Tools for the Office System 4.0 Runtime
4
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
5
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
6
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
7
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
You’ll get a warning dialog, indicating that SQL Server 2008 has compatibility issues on
Windows 7 and suggesting that you install SP1.
8
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
I just clicked the Run Program button and proceeded with the install. A little bit later, I got a
second compatibility warning dialog, also mentioning SQL Server 2008. An external DOS
window was also spawned, running a setup.exe command.
9
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
10
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
After the install completes, we get the main auto run window again and the link for checking for
service releases is now active.
11
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
If you click the Check for Service Releases link, you’ll be redirected to an update web page,
which in turn allows firing up the Windows Update applet.
12
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
As with earlier versions, when you start Visual Studio for the first time, you’re asked to choose a
language, which dictates how the environment is set up. I’m a C# guy.
When things finally start up, we see the new Start Page for the first time.
13
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
14
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
That’s it. Click finish button. You are ready to explore the mush waited and fully loaded
visual Studio 2010.
Name: _____
Roll #: _____
Date: _____
15
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 02
Objective:
To draw a line using Digital Differential Analyser (DDA) algorithm.
Theory:
1. Line:
A line is produced by means of illuminating a set of intermediary pixels between the two
endpoints. We can say that a straight line segment is defined by the coordinate position for the
end points of the segment i-e Points (x1, y1) and (x2, y2).
All line drawing algorithms make use of the fundamental equations:
Slope m = y2 − y1 / x2 − x1 = Δy / Δx
y-intercept b = y1 − m.x1
x-interval→Δx = Δy / ma
y-interval→ Δy = m Δx
DDA is a line algorithm Based on calculating either Δy or Δx using the above Equations.
It uses simple division operations. It is faster than the direct use of line Equations and also
eliminates the multiplication in line Equation.
16
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
ALGORITHM:
17
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
18
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
OUTPUT:
Name: _____
Roll #: _____
Date: _____
Sig: of LAB Engineer
19
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 03
Objective:
To draw a line using Bresenham’s Algorithm.
Theory:
Bresenham’s Line Algorithm:
It is an efficient raster line generation algorithm. It can be adapted to display circles
and other curves. It uses simple addition and subtraction operations and is faster than the DDA
line algorithm. DDA line algorithm draw lines with no accuracy as compared to Bresenham’s line
algorithm so it is more sufficient and much simpler than the DDA as it doesn’t use division or
multiplication operations.
In Bresenham’s Algorithm, after plotting a pixel position (xk, yk) we have two choices for
next pixel:
(xk +1, yk)
For this assistance we calculate a decision parameter to calculate which pixel is next closer to
line.
So initial decision parameter p0 can be evaluated as:
p0 = 2Δy − Δx
pk+1 = pk + 2Δy
20
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
ALGORITHM:
21
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
22
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
OUTPUT:
REVIEW QUESTIONS:
1. What is difference between DDA and Bresenham’s Line algorithm?
_____ _____ _____
_____ _____ _____
_____ _____ _____
_____ _____ _________________
Name: _____
Roll #: _____
Date: _____ Sig: of LAB Engineer
23
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 04
Objective:
To draw a circle using Mid point Algorithm.
Theory:
Circle:
A circle is defined as the set of points that are all at a given distance r from a center point
(xc, yc). For any circle point (x, y), this distance is expressed by the Equation;
(x − xc)2 + (y − yc)2 = r 2
The midpoint method calculates pixel positions along the circumference of a circle using integer
additions and subtractions, assuming that the circle parameters are specified in screen coordinate.
We can summarize the steps in the midpoint circle algorithm as follows.
24
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
ALGORITHM:
Mid-Point Circle (Xc, Yc, R):
Description: Here Xc and Yc denote the x – coordinate and y – coordinate of the center of the
circle. R is the radius.
1. Set X = 0 and Y = R
2. Set P = 1 – R
3. Repeat While (X < Y)
4. Call Draw Circle(Xc, Yc, X, Y)
5. Set X = X + 1
6. If (P < 0) Then
7. P = P + 2X + 6
8. Else
9. Set Y = Y – 1
10. P = P + 2(X – Y) + 1
[End of If]
11. Call Draw Circle(Xc, Yc, X, Y)
[End of While]
12. Exit
25
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
#include <stdio.h>
#include <dos.h>
#include “graphics.h”
void circleMidpoint(int, int, int);
void drawCircle(int, int, int, int);
void main()
{
int xc, yc, r;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleMidpoint(xc, yc, r);
getch();
}
void circleMidpoint(int xc, int yc, int r)
{
int x = 0, y = r;
int di = 1 - r;
while (x < y)
{
drawCircle(xc, yc, x, y);
x++;
if (di < 0)
di = di + 2 * x + 1;
else
{
y--;
di = di + 2 * (x - y) + 1;
}
drawCircle(xc, yc, x, y);
delay(100);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
}
26
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
OUTPUT:
Name: _____
Roll #: _____
Date: _____
Sig: of LAB Engineer
27
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 05
Objective:
To draw an Ellipse using Mid point Algorithm.
Theory:
Ellipse:
An Ellipse is the locus of points the sum of whose distance to the two given points is
constant. An Ellipse is a planar curve which in some Cartesian system of coordinates and
the equation for the ellipse shown in Fig. can be written in terms of the ellipse center
coordinates and parameters rx and ry as:
28
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
The midpoint ellipse method is applied throughout the first quadrant in two parts.
Figure 3-25 shows the division of the first quadrant according to the slope of an ellipse with rx <
ry.
We can start at position (0, ry) and step clockwise along the elliptical path in the
first quadrant, shifting from unit steps in x to unit steps in y when the slope
becomes less than −1.0.
29
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
ALGORITHM:
30
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include "graphics.h"
void ellipse(int, int, int, int);
void drawellipse(int, int, int, int);
void main ()
{
int gd=DETECT, gm;
initgraph(&gd, &gm, "");
int xc,yc,rx,ry;
printf("enter xc=");
scanf("%d", &xc);
printf("enter the value of yc=");
scanf("%d", &yc);
printf("enter the value of rx=");
scanf("%d", &rx);
printf("enter the value of ry=");
scanf("%d", &ry);
ellipse(xc, yc, rx, ry);
getch();
}
void ellipse(int xc,int yc,int rx,int ry)
{
int x=0, y=ry; //region1
int p=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);
while((2*ry*ry*x)<(2*rx*rx*y))
{
drawellipse(xc, yc, x, y);
x++;
if(p<0)
p=p+(2*ry*ry*x)+(ry*ry);
else
{
y--;
p=p+(2*ry*ry*x)-(2*ry*ry*y)+(ry*ry);
}
drawellipse(xc, yc, x, y);
delay(50);
}
31
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
//region 2
p=(ry*ry*(x+(1/2))*(x+(1/2)))+(rx*rx*(y-1)*(y-1))-(rx*rx*ry*ry);
while(y>0)
{
drawellipse(xc, yc, x, y);
y--;
if(p<0)
{
x++;
p=p+(2*ry*ry*x)-(2*rx*rx*y)+(rx*rx);
}
else
{
p=p-(2*rx*rx*y)+(rx*rx);
}
drawellipse(xc,yc,x,y);
delay(50);
}
}
void drawellipse(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
}
OUTPUT:
32
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 06
Objective:
Introduction to OpenGL and Setting up OpenGL.
I. Setting up
Steps:
1. Download the file glut-3.7.6-bin from here.
http://dl.dropbox.com/u/3563445/lec/OPENGL/glut-3.7.6-bin.rar
2. Extract the downloaded file glut-3.7.6-bin to the folder X:\path\glut-3.7.6-
bin
3. Open the folder glut-3.7.6-bin where files are located. In the folder you will see 3
subfolders, namely dll, include, and library.
33
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Note: For Windows 7, 8 Professional 64 bits, we need the additional step as follows.
Now copy all files from glut-3.7.6-bin\dll →C:\Windows\SysWOW64.
II. Checking
1. Create an empty C++ Console Application Project.
File New, Project, and selectWin32 Console Application
Name: enter a project name (e.g., ex1) Location: select a location to store the project.
(e.g., C:\Users\username\Documents\Visual Studio 2010\Projects\icg5)
For Microsoft Visual Studio 2010 Express Edition, you will see the following screen.
34
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
For Microsoft Visual Studio 2010 Professional, you will see the following screen.
35
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Click Finish.
2. Copy and paste the following code to the opening screen (i.e., ex1.cpp) and run it (F5).
#include"stdafx.h"
#include<glut.h>
void Draw(void);
int main(int argc, char* argv[])
{
glutCreateWindow("OpenGL glut configuration is done!");
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
Void Draw(void)
{
// Origin (0, 0) located at centre of display window
glBegin(GL_LINES);
// draw a horizontal line (i.e., x-axis)
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.75, 0.0, 0.0);
// draw a vertical line (i.e., y-axis)
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.75, 0.0);
glEnd();
glFlush();
}
36
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
OUTPUT:
Review Question/Assignment
Write a program that will display first character of your name.
(Note: Use Simple GLUT_LINES functions to draw first character of your name, give your
answer with output).
Remarks:
Add extra sheet if necessary.
Name: _____
Roll #: _____
Date: _____
37
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 07
Objective:
To draw various lines with different width and stippled(dotted, dashed) lines.
Theory:
Line Details:
With OpenGL, you can specify lines with different widths and lines that are
stippled in various ways—dotted, dashed, drawn with alternating dots and dashes, and
so on.
glBegin(GL_LINES);
glEnd();
Wide Lines:
Void glLineWidth(GLfloat width); Sets the width, in pixels, for rendered lines;
Width must be greater than 0.0 and by default is 1.0.
The actual rendering of lines is affected if either anti aliasing or multisampling is
enabled. Without antialiasing, widths of 1, 2, and 3 draw lines 1, 2, and 3 pixels wide.
With antialiasing enabled, non integer line widths are possible, and pixels on the
boundaries are typically drawn at less than full intensity.
glLineWidth(GLfloat Width);
Stippled Lines:
To make stippled (dotted or dashed) lines, you use the command
glLineStipple() to define the stipple pattern, and then you enable line stippling with
glEnable().
Syntax:
Example:
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x3F07);
With the preceding example and the pattern 0x3F07 (which translates to
0011111100000111 in binary), a line would be drawn with 3 pixels on, then 5 off, 6
on, and 2 off. If factor had been 2, the pattern would have been elongated: 6 pixels
on, 10 off, 12 on, and 4 off.
38
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
#include "stdafx.h"
#include <glut.h>
#define drawOneLine(x1,y1,x2,y2)\
glBegin(GL_LINES); glVertex2f((x1),(y1));glVertex2f((x2),(y2));glEnd();
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display(void)
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
/* select white for all lines */
glColor3f(1.0, 1.0, 1.0);
/* in 1st row, 3 lines, each with a different stipple */
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x0101); /* dotted */
drawOneLine(50.0, 125.0, 150.0, 125.0);
glLineStipple(1, 0x00FF); /* dashed */
drawOneLine(150.0, 125.0, 250.0, 125.0);
glLineStipple(1, 0x1C47); /* dash/dot/dash */
drawOneLine(250.0, 125.0, 350.0, 125.0);
/* in 2nd row, 3 wide lines, each with different stipple */
glLineWidth(5.0);
glLineStipple(1, 0x0101); /* dotted */
drawOneLine(50.0, 100.0, 150.0, 100.0);
glLineStipple(1, 0x00FF); /* dashed */
drawOneLine(150.0, 100.0, 250.0, 100.0);
glLineStipple(1, 0x1C47); /* dash/dot/dash */
drawOneLine(250.0, 100.0, 350.0, 100.0);
glLineWidth(1.0);
/* in 3rd row, 6 lines, with dash/dot/dash stipple */
/* as part of a single connected line strip */
glLineStipple(1, 0x1C47); /* dash/dot/dash */
glBegin(GL_LINE_STRIP);
for (i = 0; i < 7; i++)
glVertex2f(50.0 + ((GLfloat) i * 50.0), 75.0);
glEnd();
/* in 4th row, 6 independent lines with same stipple */
for (i = 0; i < 6; i++) {
drawOneLine(50.0 + ((GLfloat) i * 50.0), 50.0,
50.0 + ((GLfloat)(i+1) * 50.0), 50.0);
}
/* in 5th row, 1 line, with dash/dot/dash stipple */
/* and a stipple repeat factor of 5 */
glLineStipple(5, 0x1C47); /* dash/dot/dash */
drawOneLine(50.0, 25.0, 350.0, 25.0);
glDisable(GL_LINE_STIPPLE);
glFlush();
}
39
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
OUTPUT:
Name: _____
Roll #: _____
Date: _____
Sig: of LAB Engineer
40
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 08
Objective:
Two Dimensional Transformation
Translation
Scaling, &
Rotation.
Theory:
2D Transformations:
Transformations are a fundamental part of computer graphics. Transformations are used to
position objects, to shape objects, to change viewing positions, and even to change how
something is viewed.
One of the most common and important tasks in computer graphics is to transform the coordinates
(position, orientation, and size) of either objects within the graphical scene or the camera that is
viewing the scene. It is also frequently necessary to transform coordinates from one coordinate
system to another, (e.g. world coordinates to viewpoint coordinates to screen coordinates).
1. Translation:
Syntax:
void glTranslatef (TYPEx, TYPEy, TYPEz);
Translation in OpenGL is done with glTranslatef (dx, dy, dz) where the parameters specify the
amount of displacement in the x, y, and z directions. By setting the third parameter, dz, to 0, we
get a 2D translation. The parameters are of type float, as indicated by the "f" at the end of
"translatef". If you want to use parameters of type double, you can use gl.translated. The names
for the other transform methods work in the same way.
Translation is movement along any of the three axis in a 3D scene, for example, moving
something the left is a translation on the X axis if you are looking straight on. In OpenGL, the
length of translation is called units, and a unit has no definitive length.
x' x t x
P' P T
y ' y t y
P' P T
41
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
42
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
2. Scaling:
Syntax:
glScalef(TYPE x, TYPE y, TYPE z);
Scaling can be done with glScalef (sx, sy, sz), where the parameters specify the scaling factor in
the x, y, and z directions. To avoid changing the scale in the z direction, the third parameter, sz,
should be 1.
glScale*() is the only one of the three modeling transformations that changes the apparent size of
an object: Scaling with values greater than 1.0 stretches an object, and using values less than 1.0
shrinks it. Scaling with a 1.0 value reflects an object across an axis.
43
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
44
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
OUTPUT:
3. Rotation:
Syntax:
glRotatef (GLfloat angle, TYPE x, TYPE y, TYPE z);
In two dimensions, rotation is rotation about a point, and by default, the rotation is about the
origin, (0, 0).Rotating an object follows as similar scheme. We want a matrix R that when applied
to all vertices v in an object will rotate it. The following example, shown in Fig(c) rotates 50
degrees counter clockwise around the z-axis.
45
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
46
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
OUTPUT:
Review Questions:
Write a program to Translate, Scale and Rotate (120 degree) a Triangle, Quadrilateral
and Polygon.
Remarks:
Use extra sheet if necessary.
Name: _____
Roll #: _____
Date: _____
47
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 09
Objective:
Three Dimensional Transformation
Translation, Scaling, & Rotation.
To be familiar with following Functions:
glPushMatrix(),glPopMatrix(), glutWireSphere(), glViewPort(), gluLookAt().
Write a Program to Build a Solar System.
3D-Transformation:
1. Translation:
Translation is defined as moving the object from one position to another position along
straight line path. We can move the objects based on translation distances along x and y
axis. tx denotes translation distance along x-axis and ty denotes translation distance
along y axis.
Translation Distance: It is nothing but by how much units we should shift the object
from one location to another along x, y-axis. Consider (x, y) are old coordinates of a point.
Then the new coordinates of that same point (X’, Y’) can be obtained as follows:
X’=x+tx
Y’=y+ty
Z’=z+tz
We denote translation transformation as P’. We can write the above equations in matrix
form as follows:
x' 1 0 0 tx x
y ' 0 1 0 t y y
z ' 0 0 1 tz z
1 0 0 0 1 1
P' T P
48
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
2. Scaling: scaling refers to changing the size of the object either by increasing or decreasing.
We will increase or decrease the size of the object based on scaling factors along x and y -
axis. If (x, y) are old coordinates of object, then new coordinates of object after applying
scaling transformation are obtained as:
X’=x*sx
Y’=y*sy.
Z’=z*sz.
sx ,sy and sz are scaling factors along x-axis, y-axis and z-axis. We can write the above
equations in matrix form as follows:
x' s x 0 0 0 x
y ' 0 sy 0 0 y
z' 0 0 sz 0 z P' S P
1 0 0 0 1 1
3. Rotation: A rotation repositions all points in an object along a circular path in the plane
centered at the pivot point. We rotate an object by an angle theta. The Transformation
matrices for above 3D transformations are given below:
49
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
copies the current matrix and adds the copy to the top of the stack, and glPopMatrix(), which
discards the top matrix on the stack, as shown in Figure.
50
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
viewport rectangle. By default, the initial viewport values are (0, 0, winWidth, winHeight), where
winWidth and winHeight are the size of the window.
We set the position and orientation of the OpenGL camera, as shown in Figure, using
gluLookAt().
51
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Syntax:
void gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
GLdouble centery, GLdouble centerz, Ldouble upx, GLdouble upy, GLdouble upz );
The position of the camera in space – sometimes also called the eyepoint – is given by (eyex,
eyey, eyez). (centerx, centery, centerz) specifies a look point for the camera to “look at”, and a
good choice for this would a point of interest in the scene, and often the center of the scene is
used. Together, the points (eyex, eyey, eyez) and (centerx, centery,centerz) define a view
vector. The last set of gluLookAt()’s arguments specify the up vector of the camera. This
defines the camera’s orientation at the eyepoint.
4. Sphere:
Syntax
Void glutWireSphere( GLdoubleradius, GLintslices, GLintstacks);
glutWireSphere() draws a sphere, of radius radius, centred on (0,0,0) in object coordinates.
Slices is the number of subdivisions around the Zaxis (like lines of longitude); stacks is the
number of subdivisions along the Zaxis (like lines of latitude). Solid version: glutSolidSphere().
52
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
PROGRAM:
// planetsystem.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <glut.h>
static int year = 0, day = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glColor3f(1.0, 1.0, 0.0);
glutSolidSphere(1.0, 20, 16); /* draw sun */
glRotatef((GLfloat) year, 0.0, 1.0, 0.0);
glTranslatef(2.0, 0.0, 0.0);
glRotatef((GLfloat) day, 0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 1.0);
glutWireSphere(0.3, 10, 8); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'd':
day = (day + 10) % 360;
glutPostRedisplay();
break;
case 'D':
day = (day - 10) % 360;
glutPostRedisplay();
break;
case 'y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
53
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
case 'Y':
year = (year - 5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
OUTPUT:
54
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 10
Objective:
Animation using OpenGL.
Theory:
Animation:
One of the most exciting things you can do on a graphics computer is draw pictures that
move. Whether you’re an engineer trying to see all sides of a mechanical part you’re
designing, a pilot learning to fly an airplane using a simulation, or merely a computer-game
aficionado, it’s clear that animation is an important part of computer graphics. In a movie
theater, motion is achieved by taking a sequence of pictures and projecting them at 24 per second
on the screen. Each frame is moved into position behind the lens, the shutter is opened,
and the frame is displayed. The shutter is momentarily closed while the film is advanced
to the next frame, then that frame is displayed, and so on. Although you’re watching 24
different frames each second, your brain blends them all into a smooth animation.
Program:
#include "stdafx.h"
#include <glut.h>
static GLfloat spin = 0.0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}
55
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
56
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Output:
Name: _____
Roll #: _____
Date: _____ Sig: of LAB Engineer
57
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 11
Objective:
Basic Lighting Effects in OpenGL.
Theory:
OpenGL Lighting:
In this lab, we use OpenGL to demonstrate the local illumination model that we covered in our
previous lab about 3D rendering. Here we explain the ambient, diffuse, and specular components
of the lighting model and the type of effects that each represents.
In OpenGL, we set lighting and material properties in order to illuminate polygonal surfaces. To
calculate the lighting at a vertex, we need the position that we view from, the normal vector, an
active light source, lighting properties, material properties, and to have lighting enabled. To
simply the settings, we use a single square as our polygonal surface for this lesson. The square is
actually a grid of squares to reflect light more accurately; the reasoning become more apparent
when we cover shading.
58
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Although OpenGL allows for multiple light sources, we use a single light source for simplicity
(GL_LIGHT0). The ambient component is set to .2, which correspond to a dimly light
environment. The diffuse component is .8—typically ambient and diffuse components sum to 1,
unless a dimmer environment is required. If the ambient and diffuse components sum to more
than 1, then saturation will occur and give an unrealistic appearance. The specular component is
set to 1. These components have separate red, green, and blue color channels. However, it is
usually best to leave them all equal as this represents white light, which is what most light sources
are. The ambient, diffuse, and specular components are set via calls to glLight(), as is the light’s
position.
59
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Before any lighting takes place, the lighting model needs to be enabled via a call to glEnable()
with the parameter GL_LIGHTING. Lights must also be enabled with glEnable(), and we use this
to enable GL_LIGHT0: the first light. We set the lighting model to use a local viewer via a call to
glLightModel().
Program:
#include "stdafx.h"
#include <glut.h>
void Draw() {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
// Lighting set up
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
60
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Output:
Conclusion:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Name: _____
Roll #: _____
Date: _____
Sig: of LAB Engineer
61
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Practical # 12
Objective:
Rendering an object with lighting effects.
Theory:
OpenGL Lighting:
When you look at a physical surface, your eye's perception of the color depends on the
distribution of photon energies that arrive and trigger your cone cells. Those photons come from a
light source or combination of sources, some of which are absorbed and some of which are
reflected by the surface. In addition, different surfaces may have very different properties - some
are shiny and preferentially reflect light in certain directions, while others scatter incoming light
equally in all directions. Most surfaces are somewhere in between.
OpenGL approximates light and lighting as if light can be broken into red, green, and blue
components. Thus, the color of light sources is characterized by the amount of red, green, and
blue light they emit, and the material of surfaces is characterized by the percentage of the
incoming red, green, and blue components that is reflected in various directions.
In the OpenGL lighting model, the light in a scene comes from several light sources that can be
individually turned on and off. Some light comes from a particular direction or position, and some
light is generally scattered about the scene.
The OpenGL lighting model considers the lighting to be divided into four independent
components: emissive, ambient, diffuse, and specular. All four components are computed
independently and then added together.
The diffuse component is the light that comes from one direction, so it's brighter if it comes
squarely down on a surface than if it barely glances off the surface. Once it hits a surface,
however, it's scattered equally in all directions, so it appears equally bright, no matter where the
eye is located. Any light coming from a particular position or direction probably has a diffuse
component.
62
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Finally, specular light comes from a particular direction, and it tends to bounce off the surface in
a preferred direction. A well-collimated laser beam bouncing off a high-quality mirror produces
almost 100 percent specular reflection. Shiny metal or plastic has a high specular component, and
chalk or carpet has almost none. You can think of specularity as shininess.
Although a light source delivers a single distribution of frequencies, the ambient, diffuse, and
specular components might be different. For example, if you have a white light in a room with red
walls, the scattered light tends to be red, although the light directly striking objects is white.
OpenGL allows you to set the red, green, and blue values for each component of light
independently.
Specifies a current material property for use in lighting calculations. face can be GL_FRONT,
GL_BACK, or GL_FRONT_AND_BACK to indicate which face of the object the material
should be applied to. The particular material property being set is identified by pname and the
desired values for that property are given by param, which is either a pointer to a group of values
(if the vector version is used) or the actual value (if the non vector version is used). The non
vector version works only for setting GL_SHININESS. The possible values for pname are shown
in Table. Note that GL_AMBIENT_AND_DIFFUSE allows you to set both the ambient and
diffuse material colors simultaneously to the same RGBA value.
Note that most of the material properties set with glMaterial*() are (R, G, B, A) colors. regardless
of what alpha values are supplied for other parameters, the alpha value at any particular vertex is
the diffuse-material alpha value.
63
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Program:
// lighting Effect.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <glut.h>
#include <gl.h>
#include <glu.h>
void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(1.0, 20, 16);
glFlush ();
}
64
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
65
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH
DEPARTMENT OF COMPUTER SYSTEMS ENGINEERING
Output:
Name: _____
Roll #: _____
Date: _____
Sig: of LAB Engineer
66