0% found this document useful (0 votes)
52 views

program 1 - Bresenham's Line Drawing Algorithm: / To Compile GCC Lab - Name.c - LGL - lGLU - Lglut

The document contains code for 5 OpenGL programs: 1. Bresenham's line drawing algorithm 2. Rotating a triangle around the origin and a fixed point 3. Drawing a color cube 4. Perspective viewing of a color cube 5. Line clipping algorithm The programs demonstrate various OpenGL graphics concepts like drawing lines and shapes, applying transformations like translation, rotation, and perspective, and clipping lines against screen boundaries.
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)
52 views

program 1 - Bresenham's Line Drawing Algorithm: / To Compile GCC Lab - Name.c - LGL - lGLU - Lglut

The document contains code for 5 OpenGL programs: 1. Bresenham's line drawing algorithm 2. Rotating a triangle around the origin and a fixed point 3. Drawing a color cube 4. Perspective viewing of a color cube 5. Line clipping algorithm The programs demonstrate various OpenGL graphics concepts like drawing lines and shapes, applying transformations like translation, rotation, and perspective, and clipping lines against screen boundaries.
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/ 30

/* To compile gcc lab_name.

c -lGL -lGLU -lglut*/


//Program 1 - Bresenham's Line Drawing Algorithm
#include<GL/glut.h>

#include<stdio.h>

int x1, y1, x2, y2;

void draw_pixel(int x, int y)

glColor3f(1.0,0.0,0.0);

glPointSize(1);

glBegin(GL_POINTS);

glVertex2i(x, y);

glEnd();

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

int dx = x2 - x1;

int dy = y2 - y1;

int slope = dy/dx;

if(slope < 1)

int dp = 2*dy - dx;

int x = x1;

int y = y1;

if(dx < 0)

x = x2;

y = y2;

x2 = x1;

draw_pixel(x, y);

while(x < x2)


{

if(dp >= 0)

x = x+1;

y = y+1;

dp=dp + 2*dy - 2*dx;

else

x = x+1;

y = y;

dp = dp + 2*dy;

draw_pixel(x, y);

else if(slope > 1)

int dp = 2*dx - dy;

int x = x1;

int y = y1;

if(dy < 0)

x = x2;

y = y2;

y2 = y1;

draw_pixel(x, y);

while(y < y2)

if(dp >= 0)
{

x = x+1;

y = y+1;

dp=dp + 2*dx - 2*dy;

else

y = y+1;

x = x;

dp = dp + 2*dx;

draw_pixel(x, y);

else if (slope == 1)

int x = x1;

int y = y1;

draw_pixel(x, y);

while(x < x2)

x = x+1;

y = y+1;

draw_pixel(x, y);

void init()

glClearColor(1,1,1,1);

gluOrtho2D(0.0, 500.0, 0.0, 500.0);


}

void display()

glClear(GL_COLOR_BUFFER_BIT);

bresenhams_line_draw(x1, y1, x2, y2);

glFlush();

int 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(220, 200);

glutCreateWindow("Bresenham's Line Drawing");

init();

glutDisplayFunc(display);

glutMainLoop();

//Program 2 - Rotating a triangle around origin and a


fixed point
#include <GL/glut.h>

#include <stdlib.h>

#include <stdio.h>

#include <math.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)

trX=0.0; trY=0.0; th+=0.1;

draw_pixel(0.0,0.0);

if(rFlag==2)

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;

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

//Program 3 - Color Cube


#include<stdio.h>

#include<GL/glut.h>

GLfloat vertices[][3]=

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

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

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

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

};

GLfloat colors[][3]=

{0.0,0.0,0.0},{0,0.0,0.0},

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

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

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

};

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

glBegin(GL_POLYGON);

glColor3fv(colors[a]);
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(2,3,7,6);

polygon(0,4,7,3);

polygon(1,2,6,5);

polygon(4,5,6,7);

polygon(0,1,5,4);

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

static GLint axis=2;

void display(void)

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]+=0.1;

if(theta[axis]>360.0) theta[axis]-=360.0;

glutPostRedisplay();

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

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(500,500);
glutCreateWindow("color cube viewer");

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glutMouseFunc(mouse);

glutIdleFunc(spinCube);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

return 0;

//Program 4 - Perspective Viewing of a Color Cube


#include<GL/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}};

GLdouble viewer[]={0,0,4};

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

glBegin(GL_QUADS);

glColor3fv(vertices[a]);

glVertex3fv(vertices[a]);

glColor3fv(vertices[b]);

glVertex3fv(vertices[b]);

glColor3fv(vertices[c]);

glVertex3fv(vertices[c]);

glColor3fv(vertices[d]);

glVertex3fv(vertices[d]);

glEnd();

void drawCube()

quad(0,3,2,1);

quad(2,3,7,6);
quad(0,4,7,3);

quad(1,2,6,5);

quad(4,5,6,7);

quad(0,1,5,4);

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

static GLint axis=2;

void display(void)

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

gluLookAt(viewer[0],viewer[1],viewer[2],0.5,0.5,0.5,0,1,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);

drawCube();

glFlush();

void Keys(unsigned char key,int x,int y)

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

if(key=='X') viewer[0]+=0.25;

if(key=='y') viewer[1]-=0.25;

if(key=='Y') viewer[1]+=0.25;

if(key=='z') viewer[2]-=0.25;

if(key=='Z') viewer[2]+=0.25;

glutPostRedisplay();

void mouse(int btn,int state,int x,int y)

if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)


axis=0;

if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)

axis=1;

if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)

axis=2;

theta[axis]+=2.0;

if(theta[axis]>360.0)

theta[axis]-=360.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);

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DEPTH);

glutInitWindowSize(500,500);

glutCreateWindow("color cube perpective viewing");

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glutKeyboardFunc(Keys);
glutMouseFunc(mouse);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

return 0;

//Program 5 - Line Clipping


#include<stdio.h>

#include<stdbool.h>

#include<GL/glut.h>

#define outcode int

double xmin = 50, ymin = 50, xmax = 100, ymax = 100;

double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300;

double x0, y0, x1, y1;

const int RIGHT = 1;

const int LEFT = 2;

const int TOP = 8;

const int BOTTOM = 4;

int computeOutcode(double x, double y)

outcode code = 0;

if (y > ymax)

code = TOP;

else if(y < ymin)

code = BOTTOM;

if (x > xmax)

code = RIGHT;

else if(x < xmin)

code = LEFT;

return code;

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

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

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

glVertex2d(vx0,vy0);

glVertex2d(vx1, vy1);

glEnd();

void display()

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);

glBegin(GL_LINES);

glVertex2d(x0, y0);

glVertex2d(x1, y1);

glEnd();

glColor3f(0.0, 0.0, 1.0);

glBegin(GL_LINE_LOOP);

glVertex2f(xmin, ymin);

glVertex2f(xmax, ymin);

glVertex2f(xmax, ymax);

glVertex2f(xmin, ymax);
glEnd();

cohenSutherland(x0,y0,x1,y1);

glFlush();

void myInit()

glClearColor(1.0, 1.0, 1.0, 1.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, 499.0, 0.0, 499.0);

void main(int argc, char **argv)

printf("Enter the end points of the line: ");

scanf("%lf %lf %lf %lf", &x0, &y0, &x1, &y1);

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(500, 500);

glutInitWindowPosition(0,0);

glutCreateWindow("Cohen Sutherland Line Clipping");

glutDisplayFunc(display);

myInit();

glutMainLoop();

//Program 6 - Teapot on a Table


#include<GL/glut.h>

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

glPushMatrix();

glTranslatef(x, y, z);

glutSolidTeapot(0.1);

glPopMatrix();
}

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

glPushMatrix();

glTranslatef(x, y, z);

glScalef(0.6, 0.02, 0.5);

glutSolidCube(1);

glPopMatrix();

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

glPushMatrix();

glTranslatef(x, y, z);

glScalef(0.02, 0.3, 0.02);

glutSolidCube(1);

glPopMatrix();

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

glPushMatrix();

glTranslatef(x, y, z);

glScalef(1, 1, 0.02);

glutSolidCube(1);

glPopMatrix();

void light()

GLfloat mat_ambient[] = {1, 1, 1, 1};

GLfloat mat_diffuse[] = {0.5, 0.5, 0.5, 1};

GLfloat mat_specular[] = {1, 1, 1, 1};

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, 6, 3, 1};

GLfloat light_intensity[] = {0.7, 0.7, 0.7, 1};

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glLightfv(GL_LIGHT0, GL_DIFFUSE, light_intensity);

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, 2, 5, 0, 0, 0, 0, 1, 0);

light();

teapot(0, teapotP, 0);

tableTop(0, tabletopP, 0);

tableLeg(tablelegP, -0.3, tablelegP);

tableLeg(-tablelegP, -0.3, tablelegP);

tableLeg(-tablelegP, -0.3, -tablelegP);

tableLeg(tablelegP, -0.3, -tablelegP);

wall(0, 0, -wallP);

glRotatef(90, 1, 0, 0);

wall(0, 0, wallP);

glRotatef(90, 0, 1, 0);

wall(0, 0, wallP);

glFlush();

void init()

{
glClearColor (0, 0, 0, 1);

glMatrixMode (GL_PROJECTION);

glLoadIdentity ();

glOrtho (-1, 1, -1, 1, -1, 10);

glMatrixMode (GL_MODELVIEW);

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

init();

glutDisplayFunc(display);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glShadeModel(GL_SMOOTH);

glEnable(GL_NORMALIZE);

glEnable(GL_DEPTH_TEST);

glutMainLoop();

//Program 7 - 3D gasket or Recursive division of a


tetrahedron
#include <stdio.h>

#include <GL/glut.h>

typedef float point[3];

point v[] = { {0.0,0.0,-0.0},

{0.0,0.9,-1.0},

{-0.8,-0.4,-1.0},

{0.8,-0.4,-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;

v2[j] = (a[j] + c[j]) / 2;

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

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

void myReshape(int w, int h)

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

if (w <= h)

glOrtho(-2.0, 2.0, -2.0*(GLfloat)h / (GLfloat)w,2.0*(GLfloat)h /


(GLfloat)w,-10.0,10.0);

else

glOrtho(-2.0*(GLfloat)w / (GLfloat)h,2.0*(GLfloat)w / (GLfloat)h, -2.0,


2.0, -10.0, 10.0);

glMatrixMode(GL_MODELVIEW);

glutPostRedisplay();

int main(int argc,char **argv)


{

printf("Enter the number of Divisions:\n");

scanf("%d",&n);

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(500, 500);

glutInitWindowPosition(0,0);

glutCreateWindow("3D Gasket");

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glEnable(GL_DEPTH_TEST);

glClearColor(1.0,1.0,1.0,1.0);

glutMainLoop();

return 0;

//Program 8 - Bezier Curve (only lab program with .cpp


extension and compiled using g++ instead of gcc)
#include<GL/glut.h>

#include<stdio.h>

#include<math.h>

#define PI 3.1416

float theta = 0;

struct pt

GLfloat x, y, z;

};

int factorial (int n)

if (n<=1)

return(1);

else

n = n * factorial( n-1 );
return n;

void computeNcR (int n, int *hold_ncr_values)

int r;

for (r=0; r<=n; r++)

hold_ncr_values [r] = factorial(n)/( factorial (n-r) * factorial (r));

void computeBezierPoints (float t, pt *actual_bezier_point, int


number_of_control_points, pt *control_points_array, int *hold_ncr_values)

int i, n = number_of_control_points - 1;

float bernstein_polynomial;

actual_bezier_point -> x = 0;

actual_bezier_point -> y = 0;

actual_bezier_point -> z = 0;

for( i=0; i<=number_of_control_points; i++ )

bernstein_polynomial = hold_ncr_values [i] * pow(t, i) * pow( 1-t, n-i);

actual_bezier_point->x += bernstein_polynomial * control_points_array


[i].x;

actual_bezier_point->y += bernstein_polynomial * control_points_array


[i].y;

actual_bezier_point->z += bernstein_polynomial * control_points_array


[i].z;

void Bezier (pt *control_points_array, int number_of_control_points, int


number_of_bezier_points)

pt actual_bezier_point;
float t;

int *hold_ncr_values, i;

hold_ncr_values = new int [number_of_control_points];

computeNcR (number_of_control_points - 1, hold_ncr_values);

glBegin(GL_LINE_STRIP);

for(i=0; i<=number_of_bezier_points; i++)

t=float (i) / float (number_of_bezier_points);

computeBezierPoints( t, &actual_bezier_point,
number_of_control_points,control_points_array, hold_ncr_values );

glVertex2f(actual_bezier_point.x, actual_bezier_point.y);

glEnd ();

delete [] hold_ncr_values;

void display()

glClear (GL_COLOR_BUFFER_BIT);

int number_of_control_points= 4, number_of_bezier_points= 20;

pt control_points_array[4]= {{100, 400, 0}, {150, 450, 0}, {250,


350,0},{300, 400, 0}};

control_points_array[1].x += 50 * sin (theta * PI/180.0);

control_points_array[1].y += 25 * sin (theta * PI/180.0);

control_points_array[2].x -= 50 * sin ((theta+30) * PI/180.0);

control_points_array[2].y -= 50 * sin ((theta+30) * PI/180.0);

control_points_array[3].x -= 25 * sin ((theta-30) * PI/180.0);

control_points_array[3].y += sin ((theta-30) * PI/180.0);

theta += 2;

glPushMatrix();

glPointSize(5);

glColor3f (1, 0.4, 0.2);

for(int i=0; i<=50; i++)


{

glTranslatef(0, -0.8, 0 );

Bezier(control_points_array, number_of_control_points,
number_of_bezier_points);

glColor3f(1, 1, 1);

for(int i=0; i<=50; i++)

glTranslatef(0, -0.8, 0);

Bezier(control_points_array, number_of_control_points,
number_of_bezier_points);

glColor3f(0, 1, 0);

for(int i=0; i<=50; i++)

glTranslatef(0, -0.8, 0);

Bezier(control_points_array, number_of_control_points,
number_of_bezier_points);

glPopMatrix();

glLineWidth(5);

glColor3f(0.7, 0.5,0.3);

glBegin(GL_LINES);

glVertex2f(100,400);

glVertex2f(100,40);

glEnd();

glutPostRedisplay();

glutSwapBuffers();

void init()

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(0,500,0,500);

int main(int argc, char ** argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowPosition(0, 0);

glutInitWindowSize(500,500);

glutCreateWindow("BezierCurve");

init();

glutDisplayFunc(display);

glutMainLoop();

//Program 9 - Scan Line Algorithm


#include<stdio.h>

#include<GL/glut.h>

float x1,x2,x3,x4,y1,y2,y3,y4;

int fillflag=0;

void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)

float mx,x,temp;

int i;

if((y2-y1)<0)

temp=y1;

y1=y2;

y2=temp;

temp=x1;

x1=x2;

x2=temp;

}
if((y2-y1)!=0){

mx=(x2-x1)/(y2-y1);

else{

mx=x2-x1;

x=x1;

for(i=y1;i<=y2;i++){

if(x<(float)le[i]){

le[i]=(int)x;

if(x>(float)re[i]){

re[i]=(int)x;

x+=mx;

void draw_pixel(int x,int y){

glColor3f(1.0,1.0,0.0);

glBegin(GL_POINTS);

glVertex2i(x,y);

glEnd();

void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float


x4,float y4){

int le[500],re[500];

int i,y;

for(i=0;i<500;i++){

le[i]=500;

re[i]=0;

edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);

edgedetect(x3,y3,x4,y4,le,re);

edgedetect(x4,y4,x1,y1,le,re);

for(y=0;y<500;y++){

for(i=(int)le[y];i<(int)re[y];i++){

draw_pixel(i,y);

void display(){

x1=200.0;y1=200.0;x2=100.0;y2=300.0;x3=200.0,y3=400.0;x4=300.0;y4=300.0;

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0,0.0,1.0);

glBegin(GL_LINE_LOOP);

glVertex2f(x1,y1);

glVertex2f(x2,y2);

glVertex2f(x3,y3);

glVertex2f(x4,y4);

glEnd();

if(fillflag==1){

scanfill(x1,y1,x2,y2,x3,y3,x4,y4);

glFlush();

void init(){

glClearColor(0.0,0.0,0.0,1.0);

glColor3f(1.0,0.0,0.0);

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

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

}
--------------------------THE END--------------------------

You might also like