Professional Documents
Culture Documents
6th Sem CG Manual
6th Sem CG Manual
SYLLABUS
Design, develop, and implement the following programs using OpenGL API
PART –B ( MINI-PROJECT)
10. 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 PROGRAMS
This section covers simple programs to draw points, lines, triangles, and polygons. The aim of
these programs is to introduce the basic functions and structure of OpenGL programs.
Source Code:
#include <GL/glut.h>
void display()
glClear(GL_COLOR_BUFFER_BIT);
/* Draw points */
glBegin(GL_POINTS);
glVertex2f(0.0,0.0);
glVertex2f(100.0,100.0);
glVertex2f(110.0,10.0);
glVertex2f(400.0,500.0);
glVertex2f(500.0,500.0);
glVertex2f(550.0,550.0);
glEnd();
/* Clear buffers */
glFlush();
void myinit()
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
gluOrtho2D(0.0,550.0,0.0,550.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Points");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Output:
Exercise:
1. Change the color of each point (set yellow color for origin & green for dagonally opposite point)
2. Change the backgorund color
3. Change the size of the window
4. What happens if glutMainLoop () function is not specified?
Program to draw lines
Source Code:
#include <GL/glut.h>
void display(void)
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(50,50);
glVertex2i(400,400);
glEnd();
glColor3f(0.0,0.0,0.0);
glRasterPos2i(190, 200);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,'L');
/* Clear buffers */
glFlush();
void myinit()
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,500.0,0.0,500.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Lines");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Output:
Exercise:
1. Add two or more line segments in different colors
2. What happens when negative coordinates are given for lines?
3. Print the word ‘LINE A’, LINE ‘B’,… along each line
4. What happens if glutInitWindowSize and glutCreateWindow functions are not included?
Program to draw triangle
Source Code:
#include <GL/glut.h>
void display(void)
glClear(GL_COLOR_BUFFER_BIT);
/* Draw triangle */
glBegin(GL_TRIANGLES);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glEnd();
/* Clear buffers */
glFlush();
void myinit()
glMatrixMode (GL_PROJECTION);
glutInit(&argc,argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Triangles");
glutDisplayFunc(display);
myinit();
glutMainLoop();
Output:
Exercise:
1. Add two or more triangles in different colors
2. Draw triangles using macros for lines and line loops
3. What is the difference in the triangles formed using three different macros
4. What happens if glutDisplayFunc function is not included?
Source Code:
#include <GL/glut.h>
void display(void)
glClear(GL_COLOR_BUFFER_BIT);
/* Draw Polygon */
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
/* Clear buffers */
glFlush();
void myinit()
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutDisplayFunc(display);
myinit();
glutMainLoop();
Output:
Exercise:
1. Add two or more rectangles in different colors
2. Draw rectangles using macros for lines and line loops
3. Change the size of the clipping window and redraw the rectangles
4. What happens if macros in glutInitDisplayMode are entered wrong?
PROGRAM 1
Question 1.
#include <GL/glut.h>
#include <math.h>
int ww = 600, wh = 400;
int first = 0;
int xi, yi, xf, yf;
void putPixel (int x, int y)
{
glColor3f (0.3, 0.2, 0.0); // activate the pixel by setting the point color to white
glBegin (GL_POINTS);
glVertex2i (x, y); // set the point
glEnd ();
glFlush (); // process all openGL routines as quickly as possible
}
void display()
{
glClearColor(0.4, 0.7, 0.5, 1.0);
glColor3f(0.2, 0.3, 0.3);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void bresenhamAlg (int x0, int y0, int x1, int y1)
{
int dx = abs (x1 - x0);
int dy = abs (y1 - y0);
int x, y;
if (dx >= dy)
{
int d = 2*dy-dx;
int ds = 2*dy;
int dt = 2*(dy-dx);
case 0:
xi = x;
yi = (wh-y);
first = 1;
break;
case 1:
xf = x;
yf = (wh-y);
bresenhamAlg ( xi, yi, xf, yf);
first = 0;
break;
}
}
}
void myinit()
{
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(ww,wh);
glutCreateWindow("Bresenham Line Algorithm");
glutDisplayFunc(display);
myinit();
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Question 2
Write a program to Create and rotate a triangle about the origin and a fixed point.
PROGRAM:
#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;
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;
}
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();
}
Sample Output:
PROGRAM 3
Question 3.
Program to draw a color cube and spin it using OpenGL transformation matrices.
Y – Axis
(-1,-1,-1) 0 1 (1,-1,-1)
5 (1,-1, 1)
(-1,-1, 1) 4
X – Axis
Z – Axis
The cube is rotated around the x-axis, y-axis, and z-axis when the left button, middle button, or right
button is clicked on the mouse respectively. The cube is rotated by an angle of 0.05º around the selected
axis. The angle of rotation around a given axis is stored in a static variable. Assume that the cube was
rotated by 30º around x – axis first. If it is then starts rotating about y – axis, then it will remain at 30º
with respect to x – axis. The angle of rotation about y – axis is incremented.
The faces of the cube are based on the right hand thumb rule. The rule states that if the right hand
thumb points outwards, the direction in which the fingers are encircled gives the sequence for the outward
face of the cube.
Each vertex of the cube is assigned a color. A given face of the cube is colored by bilinear
interpolation of the colors of each vertex of that face of the cube.
Source Code:
#include <stdlib.h>
#include <GL/glut.h>
{-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},
{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}};
/* Declare static variables to store the rotation angle around each axis */
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(void)
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
void display(void)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Draw cube */
colorcube();
/* Clear buffers */
glFlush();
/* Swap buffers */
glutSwapBuffers();
void spinCube()
theta[axis] += 0.05;
/* If the rotation angle has reached 360 degrees, then restart from 0 degrees */
glutPostRedisplay();
/* Choose x - axis */
/* Choose y - axis */
/* Choose z - axis */
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
else
glMatrixMode(GL_MODELVIEW);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
Output:
Exercise:
1. Which algorithm is used for depth test?
2. What happens if the matrix mode is not set to modelview?
3. What happens if the faces of the polygon are not defined properly?
4. How is the depth buffer clearly? What happens if depth buffer is not cleared?
PROGRAM 4
Question 4.
Program to draw a color cube and allow the user to move the camera suitably to experiment
with perspective viewing. Use OpenGL functions.
The color cube drawn in this program uses the same fundamental as in program 3. Here, the cube
is moved only when the user clicks the mouse button or presses specified keys on the keyboard. Left
button, rmiddle button, and right button move the cube about x-axis, y-axis, and z-axis respectively. The
movement of the cube along each of these axes is stored in three different static variables.
Additionally, viewer can be moved about by pressing the keys on the keyboard. ‘x’ and ‘X’ move
the viewer in negative direction and positive direction along the x-axis respectively. ‘y’ and ‘Y’ move the
viewer in negative direction and positive direction along the y-axis. ‘z’ and ‘Z’ move the viewer in
negative and positive direction along the z-axis.
Source Code:
#include <GL/glut.h>
{-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},
{1.0,1.0,1.0},{-1.0,1.0,1.0}};
{1.0,1.0,1.0},{-1.0,1.0,1.0}};
/* Define static variables for angles of roatation, axis, and the viewer position */
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);
void display(void)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/* Draw cube */
colorcube();
/* Clear buffers */
glFlush();
/* Swap buffers */
glutSwapBuffers();
/* Choose x - axis */
/* Choose y - axis */
/* Choose z - axis */
theta[axis] += 2.0;
glutPostRedisplay();
glutPostRedisplay();
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glutInit(&argc, argv);
/* Set double buffer, RGB color mode, and enable depth buffer */
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();}
Output:
PROGRAM 5
Question 5.
Source Code:
#include <stdio.h>
#include <GL/glut.h>
double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
double x0,y0,x1,y1;
/* Bit codes for right, left, top, & bottom boundaries of clipping window */
/* Function to implement Cohen Sutherland line clipping algorithm and to draw the clipped line */
do{
if (!(outcode0 | outcode1))
accept = true;
done = true;
done = true;
/* If the line is not trivially accepted or trivially rejected, then it intersects the clippping
window on some boundary. The following procedure finds the clipped window */
double x, y;
/* Choose the outcode for the point that lies outside the boundary */
y = ymax;
y = ymin;
x = xmax;
else
x = xmin;
if (outcodeOut == outcode0)
x0 = x;
y0 = y;
else
x1 = x;
y1 = y;
}while (!done);
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glEnd();
outcode code = 0;
/* Set TOP bit if point is above top boundary of the clipping window */
if (y > ymax)
code |= TOP;
/* Set BOTTOM bit if point is below bottom boundary of the clipping window */
code |= BOTTOM;
/* Set RIGHT bit if point lies beyond right boundary of the clipping window */
if (x > xmax)
code |= RIGHT;
/* Set LEFT bit if point lies before left boundary of the clipping window */
code |= LEFT;
return code;
void display()
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherlandLineClipAndDraw(x0,y0,x1,y1);
/* Clear buffers */
glFlush();
void myinit()
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,499.0,0.0,499.0);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutDisplayFunc(display);
myinit();
glutMainLoop();
Output:
Enter point (x0, y0): 10 40
PROGRAM 6
Question 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 the solid object used in the scene.
This program uses the built in functions to create teapot and cubes. Cube is scaled and translated to form
wall, table top, and table legs. Light 0 is enabled.
Source Code:
/* A program to draw a simple shaded scene consisting of a teapot on a table in a room */
#include <GL/glut.h>
glPushMatrix();
glutSolidCube (1.0);
glPopMatrix();
glPushMatrix();
glutSolidCube (1.0);
glPopMatrix();
void table (double topWid, double topThick, double legThick, double legLen)
glPushMatrix();
glutSolidCube (1.0);
glPopMatrix();
glPushMatrix();
glTranslated(0, 0, -2*dist);
glPopMatrix();
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);
glLightfv (GL_LIGHT0,GL_POSITION,light_position);
glLightfv (GL_LIGHT0,GL_DIFFUSE,lightIntensity);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-winHt * 64/48.0,winHt*64/48.0,-winHt,winHt,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated (0.5,0.40,0.5);
glRotated (50,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
/* Draw table */
glPushMatrix();
glTranslated(0.4,0,0.4);
table (0.6,0.02,0.02,0.3);
glPopMatrix();
/* Draw floor */
wall(0.02);
glPushMatrix();
glRotated(90.0,0.0,0.0,1.0);
wall(0.02);
glPopMatrix();
glPushMatrix();
wall(0.02);
glPopMatrix();
/* Clear buffers */
glFlush();
glutInit(&argc,argv);
/* Set single buffer, RGB color mode, and enable depth buffer */
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutDisplayFunc(displaySolid);
/* Enable lighting */
glEnable(GL_LIGHTING);
/* Enable light 0 */
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutMainLoop();
Output:
Exercise:
1. What happens if other light sources are enabled and light 0 is disabled?
2. Rotate the teapot by 180º.
3. Rotate the table by 90º.
4. Add a stool to the scene
PROGRAM 7
Question 7.
Tetrahedron is composed of four triangular faces. Three faces meet at each vertex. A tetrahedron
is recursively subdivided to form a three dimensional Sierpinski gasket.
The below diagram shows the way the vertices are named in the program.
mid3 mid0
mid4
a
mid1
mid2
c d
mid5
Figure 1 Tetrahedron
2. If n > 0,
a. Find the midpoint for each edge of the tetrahedron
b. Call ‘divide_tetra’ function to define new tetrahedra. The vertices used for the same
are as below
i. Tetrahedron 1 – a, mid0, mid1, mid2
ii. Tetrahedron 2 – mid0, b, mid3, mid4
iii. Tetrahedron 3 – mid1, mid3, c, mid5
iv. Tetrahedron 4 – mid2, mid4, mid5, d
3. Else if n = 0,
a. Draw the tetrahedrons
b. Exit when all tetrahedra are drawn
4. Reduce the number of subdivisions by 1
5. Repeat step 2
Each tetrahedron is divided into five smaller tetrahedra. One tetrahedron at each vertex and
another tetrahedron formed by connecting the midpoints. The tetrahedron formed by midpoints is ignored.
For additional levels of recursion, the tetrahedra formed at the vertices are considered as separate
tetrahedron and the same procedure is repeated for each additional level.
Source Code:
/* This is a program to recursively subdivide a tetrahedron to form a 3D Sierpinski Gasket. The number of recursive
steps is specified by the user */
#include <stdio.h>
#include <GL/glut.h>
/* Initial tetrahedron */
{-0.82,-0.47,-0.33},{0.82,-0.47,-0.33}};
{0.0,0.0,1.0},{0.0,0.0,0.0}};
int n;
glVertex3fv(va);
glVertex3fv(vb);
glVertex3fv(vc);
glColor3fv(colors[0]);
triangle(a,b,c);
glColor3fv(colors[1]);
triangle(a,c,d);
glColor3fv(colors[2]);
triangle(a,d,b);
glColor3fv(colors[3]);
triangle(b,d,c);
GLfloat mid[6][3];
int j;
if (m > 0)
divide_tetra(a,mid[0],mid[1],mid[2],m-1);
divide_tetra(mid[0],b,mid[3],mid[4],m-1);
divide_tetra(mid[1],mid[3],c,mid[5],m-1);
divide_tetra(mid[2],mid[4],mid[5],d,m-1);
else
tetra(a,b,c,d);
void display(void)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
divide_tetra(vertices[0],vertices[1],vertices[2],vertices[3],n);
glEnd();
/* Clear buffers */
glFlush();
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
else
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
//n = 3;
scanf("%d",&n);
glutInit(&argc,argv);
/* Set single buffer, RGB color mode, and enable depth buffer */
glutInitDisplayMode (GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glutMainLoop();
Output:
Enter number of recursive steps to divide tetrahedron: 2
Exercise:
1. What happens if depth buffer is not initialized or if depth test is not enabled?
2. What is the purpose of myReshape function?
3. glOrtho function is defined in myReshape function. What is the default viewport size?
4. What happens if macros in glutInitDisplayMode are entered wrong?
PROGRAM 8
Question 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
GLsizei winWidth = 600, winHeight = 600;
GLfloat xwcMin = 0.0, xwcMax = 130.0;
GLfloat ywcMin = 0.0, ywcMax = 130.0;
typedef struct wcPt3D
{
GLfloat x, y, z;
};
void bino(GLint n, GLint *C)
{
GLint 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(GLfloat u, wcPt3D *bezPt, GLint nCtrlPts, wcPt3D *ctrlPts, GLint
*C)
{
GLint k, n=nCtrlPts-1;
GLfloat bezBlendFcn;
bezPt ->x =bezPt ->y = bezPt->z=0.0;
for(k=0; k< nCtrlPts; k++)
{
bezBlendFcn = C[k] * pow(u, k) * pow( 1-u, n-k);
bezPt ->x += ctrlPts[k].x * bezBlendFcn;
bezPt ->y += ctrlPts[k].y * bezBlendFcn;
bezPt ->z += ctrlPts[k].z * bezBlendFcn;
}
}
void bezier(wcPt3D *ctrlPts, GLint nCtrlPts, GLint nBezCurvePts)
{
wcPt3D bezCurvePt;
GLfloat u;
GLint *C, k;
C= new GLint[nCtrlPts];
bino(nCtrlPts-1, C);
glBegin(GL_LINE_STRIP);
for(k=0; k<=nBezCurvePts; k++)
{
u=GLfloat(k)/GLfloat(nBezCurvePts);
computeBezPt(u, &bezCurvePt, nCtrlPts, ctrlPts, C);
glVertex2f(bezCurvePt.x, bezCurvePt.y);
}
glEnd();
delete[]C;
}
void displayFcn()
{
GLint nCtrlPts = 4, nBezCurvePts =20;
static float theta = 0;
wcPt3D ctrlPts[4] = {
{20, 100, 0},
{30, 110, 0},
{50, 90, 0},
{60, 100, 0}};
ctrlPts[1].x +=10*sin(theta * PI/180.0);
ctrlPts[1].y +=5*sin(theta * PI/180.0);
ctrlPts[2].x -= 10*sin((theta+30) * PI/180.0);
ctrlPts[2].y -= 10*sin((theta+30) * PI/180.0);
ctrlPts[3].x-= 4*sin((theta) * PI/180.0);
ctrlPts[3].y += sin((theta-30) * PI/180.0);
theta+=0.1;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(5);
glPushMatrix();
glLineWidth(5);
glColor3f(255/255, 153/255.0, 51/255.0); //Indian flag: Orange color code
for(int i=0;i<8;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBezCurvePts);
}
glColor3f(1, 1, 1); //Indian flag: white color code
for(int i=0;i<8;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBezCurvePts);
}
glColor3f(19/255.0, 136/255.0, 8/255.0); //Indian flag: green color code
for(int i=0;i<8;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBezCurvePts);
}
glPopMatrix();
glColor3f(0.7, 0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(20,100);
glVertex2f(20,40);
glEnd();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void winReshapeFun(GLint newWidth, GLint newHeight)
{
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xwcMin, xwcMax, ywcMin, ywcMax);
glClear(GL_COLOR_BUFFER_BIT);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("Bezier Curve");
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFun);
glutMainLoop();
}
PROGRAM 9
Question 9.
Develop a menu driven program to fill the polygon using scan line algorithm.
This program can be used to fill convex polygons of upto ten sides. Each line in the display window
is scanned to check if any of the edges intersect that line. If the edges do intersect that line, the points
between the first edge and the second edge that intersect are colored.
Source Code:
#include <stdio.h>
#include <GL/glut.h>
float x[10],y[10];
int n;
float mx,x,temp;
int i;
if((y2-y1)<0)
if((y2-y1)!=0)
mx=(x2-x1)/(y2-y1);
else
mx=x2-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;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
int le[500],re[500];
GLint i,j;
le[i]=500;
re[i]=0;
glBegin(GL_LINE_LOOP);
edgedetect(x[i],y[i],x[(i+1)%n],y[(i+1)%n],le,re);
glEnd();
/* For every line, color pixels that lie between left and right edge */
glColor3f(1.0,0.0,1.0);
for(j=0;j<500;j++)
if(le[j]<=re[j])
for(i=(int)le[j];i<=(int)re[j];i++)
draw_pixel(i,j);
glFlush();
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x[i],y[i]);
glEnd();
scanfill(x,y,n);
/* Clear buffers */
glFlush();
void myinit()
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,499.0,0.0,499.0);
scanf("%d",&n);
scanf("%f %f",&x[i],&y[i]);
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutDisplayFunc(display);
myinit();
glutMainLoop();
Output:
Enter number of vertices(n<=10) for polygon: 5
Exercise: