Copy of Lab Report CSE-802
Copy of Lab Report CSE-802
LAB REPORT
Submitted By Submitted To
Table of Contents
2
Translation of Star Shape by taking user input of Tx & Ty
5
DDA Line Drawing Algorithm by taking user input
6
Bresenham Line Drawing Algorithm
7
Cohen Sutherland Line Clipping Algorithm
8
Sutherland Hodgman Polygon Clipping Algorithm
Experiment No: 01
Experiment Name: Star Shape Drawing.
Objectives:
❖ To write an OpenGL programme that draws a star form.
❖ To get experience with the fundamental OpenGL functions and creating primitives.
❖ To comprehend the idea of giving vertices in a 2D space their coordinates.
Code:
#include <GL/gl.h>
#include <GL/glut.h>
void display(void)
{
/* clear all pixels */
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glEnd();
/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush();
}
3
Output:
Experiment No: 02
Experiment Name: Translation of Star Shape by taking user input of Tx & Ty Source Code
Objectives:
Code:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;
float Tx = 0.0;
float Ty = 0.0;
void translation(void)
{
glBegin(GL_POLYGON);
glVertex2f(2,2);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(5,0);
glVertex2f(2,2);
glVertex2f(2,-2);
glEnd();
5
glBegin(GL_POLYGON);
glVertex2f(0,5);
glVertex2f(-2,2);
glVertex2f(2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-5,0);
glVertex2f(-2,-2);
glVertex2f(-2,2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0,-5);
glVertex2f(2,-2);
glVertex2f(-2,-2);
glEnd();
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.22,0.99,0.11);
glLoadIdentity();
glTranslatef(Tx,Ty,0);
translation();
glFlush();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15,15,-15,15,-15,15);
glMatrixMode(GL_MODELVIEW);
}
Output:
Experiment No: 03
7
Experiment Name: Rotations of Star Shape by taking user input of angle source code
Objectives:
Code:
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
//Initializes 3D rendering
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //Enable color
glClearColor(0.7f, 0.9f, 1.0f, 1.0f); //Change the background to sky blue
}
float _angle;
float _cameraAngle = 0.0f;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, -5.0f);
glPushMatrix();
glTranslatef(0.0f, -1.0f, 0.0f);
glRotatef(_angle, 0.0f, 0.0f, -1.0f);
glBegin(GL_POLYGON);
glVertex2f(0.2,0.2);
glVertex2f(0.2,-0.2);
glVertex2f(-0.2,-0.2);
glVertex2f(-0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.5,0.0);
glVertex2f(0.2,0.2);
glVertex2f(0.2,-0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.0,0.5);
glVertex2f(-0.2,0.2);
glVertex2f(0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(-0.5,0.0);
glVertex2f(-0.2,-0.2);
glVertex2f(-0.2,0.2);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(0.0,-0.5);
glVertex2f(0.2,-0.2);
glVertex2f(-0.2,-0.2);
glEnd();
9
glPopMatrix();
glutSwapBuffers();
}
glutMainLoop();
return 0;
}
10
Output:
Experiment No: 04
Experiment Name: Reflection of Star Shape source code
Objectives:
Code:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include<bits/stdc++.h>
using namespace std;
void reflections() {
glBegin(GL_POLYGON);
glVertex2f(3, 6); // Top right
glVertex2f(5, 6); // Top left
glVertex2f(5, 4); // Bottom left
glVertex2f(3, 4); // Bottom right
glEnd();
glBegin(GL_POLYGON);
glVertex2f(4, 7.5);
glVertex2f(5, 6);
glVertex2f(3, 6);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(5, 6);
glVertex2f(6.5, 5);
glVertex2f(5, 4);
glEnd();
11
glBegin(GL_POLYGON);
glVertex2f(5, 4);
glVertex2f(4, 2.5);
glVertex2f(3, 4);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(3, 4);
glVertex2f(1.5, 5);
glVertex2f(3, 6);
glEnd();
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();
reflections();
glPushMatrix();
glScalef(1.0, -1.0, 1.0); // Reflect vertically
glColor3f(0.0, 1.0, 0.0); // Green color
reflections();
glPopMatrix();
glPushMatrix();
glScalef(-1.0, -1.0, 1.0); // Reflect both horizontally and vertically
glColor3f(0.0, 0.0, 1.0); // Blue color
reflections();
glPopMatrix();
glFlush();
}
12
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Output:
Experiment No: 05
Experiment Name: DDA Line Drawing Algorithm by taking user input source code
Objectives:
Code:
#include<stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
13
float x1,y1,x2,y2,m,i,j;
float dx,dy;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glEnd();
}
}
else if(m>1)
{
while(x1<=x2 && y1<=y2)
{
x1=x1+(1/m);
y1=y1+1;
glVertex3f(x1/100,y1/100,0.0);
printf("%f %f\n",x1,y1);
}
}
glEnd();
glFlush ();
}
void init (void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
15
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("DDA Line Drawing Algo 2018331502");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Experiment No: 06
Experiment Name: Bresenham Line Drawing Algorithm Source Code
Objectives:
Code:
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
float x1,y1,x2,y2,m,i,j,p;
int dx=0,dy=0;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glEnd();
glFlush ();
}
void init (void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
scanf("%f %f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("bresenham line drawing algo 2018331502");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Experiment No: 07
Experiment Name: Cohen Sutherland Line Clipping Algorithm Source Code
Objectives:
Code:
#include<windows.h>
#include<GL/glu.h>
#include<GL/glut.h>
GLfloat xMin=-0.5,xMax=0.5,yMin=-0.5,yMax=0.5;
GLfloat x1=-0.8,y1=-0.6,x2=0.7,y2=0.4;
int Left=1,Right=2,Bot=4,Top=8;
int C1,C2;
int Clip_Flag = 0, Flag = 1;;
19
void Clip()
{
int C;
GLfloat x,y;
if(C1)
C = C1;
else
C = C2;
if(C == C1)
{
x1 = x;
y1 = y;
}
else
{
x2 = x;
y2 = y;
}
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
glBegin(GL_LINE_LOOP);
glVertex2f(xMin,yMin);
glVertex2f(xMax,yMin);
glVertex2f(xMax,yMax);
glVertex2f(xMin,yMax);
glEnd();
21
glColor3f(1,0,0);
if(Flag == 1)
{
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
if((C1|C2) == 0)
break;
else if((C1&C2)!=0)
{
Flag = 0;
break;
}
else
Clip();
}
glFlush();
}
glutInit(&argC,argV);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Cohen-Sutherland Algorithm 2018331502");
glutDisplayFunc(Draw);
glutKeyboardFunc(Key);
glutMainLoop();
return 0;
}
Output:
Experiment No: 8
Experiment Name: Sutherland Hodgman Polygon Clipping Algorithm
Objectives:
Code:
#include<stdio.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<math.h>
typedef struct
{
float x;
float y;
} PT;
23
int n;
int i,j;
PT p1,p2,p[20],pp[20];
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}
void drawpolygon()
{
glColor3f(1.0,0.0,0.0);
for(i=0; i<n-1; i++)
{
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[i+1].x,p[i+1].y);
glEnd();
}
glBegin(GL_LINES);
glVertex2d(p[i].x,p[i].y);
glVertex2d(p[0].x,p[0].y);
glEnd();
}
glBegin(GL_LINE_LOOP);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
left();
right();
top();
bottom();
drawpolygon();
}
glFlush();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.4,1.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p1.x,p2.y);
glEnd();
drawpolygon();
glFlush();
}
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); // clear screen usually black
gluOrtho2D(0,500,0,500);
}
p[i].x=p[0].x;
p[i].y=p[0].y;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Sutherland Hodgman Polygon Clipping Algorithm 2018331502");
init();
glutDisplayFunc(display);
glutMouseFunc(myMouse);
glFlush();
glutMainLoop();
return 0;
Output:
Experiment No: 09
Experiment Name: Weiler-Atherton Clipping Algorithm
Objectives:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
typedef struct
{
float x;
float y;
} Vertex;
int in_out(float x, float y, int x1, int y1, int x2, int y2)
{
float p = (y - y1) * (x2 - x1) - (x - x1) * (y2 - y1);
if (p < 0)
return 0; // for out
return 1; // for in
}
32
void intersection_lineseg(float *x, float *y, int x1, int y1, int x2, int y2, int xa, int ya, int xb, int yb)
{
*x = -1;
*y = -1;
if (x2 == x1 && xb == xa)
return;
else if (x2 == x1)
{
float m2 = (float)(yb - ya) / (xb - xa);
*x = x1;
*y = ya - m2 * (xa - x1);
}
else if (xb == xa)
{
float m1 = (float)(y2 - y1) / (x2 - x1);
*x = xa;
*y = y1 + m1 * (xa - x1);
}
else
{
float m1 = (float)(y2 - y1) / (x2 - x1);
float m2 = (float)(yb - ya) / (xb - xa);
if (m1 == m2)
return;
*x = (ya - y1 + m1 * x1 - m2 * xa) / (m1 - m2);
*y = (m1 * m2 * (xa - x1) + m2 * y1 - m1 * ya) / (m2 - m1);
}
if ((x1 >= x2 && (*x < x2 || *x > x1)) || (x2 >= x1 && (*x > x2 || *x < x1)) || (y1 >= y2 && (*y < y2
|| *y > y1)) || (y2 >= y1 && (*y > y2 || *y < y1)) || (xa >= xb && (*x < xb || *x > xa)) || (xb >= xa &&
(*x > xb || *x < xa)) || (ya >= yb && (*y < yb || *y > ya)) || (yb >= ya && (*y > yb || *y < ya)))
{
*x = -1;
*y = -1;
}
}
void wa_clip()
{
Vertex tempcw[40], tempsp[40];
int tag_sp[40], tag_cw[40], trav_sp[40], trav_cw[40];
float x, y;
int entry_list[10]; // saves indexes only
33
int e = -1;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (cw[(i + 1) % n_cw].x < cw[i].x)
{
// decreasing x sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x > tempi[max_idx][0].x)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (cw[(i + 1) % n_cw].y > cw[i].y)
{
// increasing y sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y < tempi[min_idx][0].y)
min_idx = m;
}
float temp = tempi[min_idx][0].x;
tempi[min_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
35
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else
{
// decreasing y sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y > tempi[max_idx][0].y)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
for (int k = 0; k <= ti; k++) // put sorted intersection points in cw array
{
kc++;
tempcw[kc].x = tempi[k][0].x;
tempcw[kc].y = tempi[k][0].y;
tag_cw[kc] = tempi[k][1].x;
trav_cw[kc] = 0;
}
}
kc++;
tempcw[kc].x = cw[(i + 1) % n_cw].x;
36
tempi[k][0].x = temp;
temp = tempi[min_idx][0].y;
tempi[min_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[min_idx][1].x;
tempi[min_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (sp[(i + 1) % n_sp].x < sp[i].x)
{
// decreasing x sort
int max_idx;
for (int k = 0; k < ti; k++)
{
max_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].x > tempi[max_idx][0].x)
max_idx = m;
}
float temp = tempi[max_idx][0].x;
tempi[max_idx][0].x = tempi[k][0].x;
tempi[k][0].x = temp;
temp = tempi[max_idx][0].y;
tempi[max_idx][0].y = tempi[k][0].y;
tempi[k][0].y = temp;
temp = tempi[max_idx][1].x;
tempi[max_idx][1].x = tempi[k][1].x;
tempi[k][1].x = temp;
}
}
else if (sp[(i + 1) % n_sp].y > sp[i].y)
{
// increasing y sort
int min_idx;
for (int k = 0; k < ti; k++)
{
min_idx = k;
for (int m = k + 1; m < ti + 1; m++)
{
if (tempi[m][0].y < tempi[min_idx][0].y)
min_idx = m;
}
38
for (int k = 0; k <= ti; k++) // put sorted intersection points in sp array
{
ks++;
tempsp[ks].x = tempi[k][0].x;
tempsp[ks].y = tempi[k][0].y;
tag_sp[ks] = tempi[k][1].x;
trav_sp[ks] = 0;
}
}
39
ks++;
tempsp[ks].x = sp[(i + 1) % n_sp].x;
tempsp[ks].y = sp[(i + 1) % n_sp].y;
tag_sp[ks] = -1;
trav_sp[ks] = 0;
}
kc++;
tempcw[kc].x = cw[0].x;
tempcw[kc].y = cw[0].y;
tag_cw[kc] = -1;
trav_cw[kc] = 0;
ks++;
tempsp[ks].x = sp[0].x;
tempsp[ks].y = sp[0].y;
tag_sp[ks] = -1;
trav_sp[ks] = 0;
n_cw = kc + 1;
n_sp = ks + 1;
// Traversal
for (int i = 0; i <= e; i++)
{
int done = 0;
int j = entry_list[i];
while (!done)
{
if (trav_sp[j] == 1)
done = 1;
else if (tag_sp[j] == 1 || tag_sp[j] == -1)
{
glBegin(GL_LINES);
glVertex2f(tempsp[j].x, tempsp[j].y);
glVertex2f(tempsp[(j + 1) % n_sp].x, tempsp[(j + 1) % n_sp].y);
glEnd();
trav_sp[j] = 1;
j++;
}
else if (tag_sp[j] == 0)
{
trav_sp[j] = 1;
40
// Swap
int k;
for (k = 0; k < n_cw; k++) // find location to switch to
{
if (tempcw[k].x == tempsp[j].x && tempcw[k].y == tempsp[j].y)
{
j = k;
break;
}
}
int n = n_cw;
n_cw = n_sp;
n_sp = n;
}
}
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
41
glFlush();
}
// Accept user input for the clipping window and subject polygon
printf("Enter no. of vertices in the clipping window: ");
scanf("%d", &n_cw);
printf("Enter vertices (x, y) clockwise for the clipping window:\n");
for (int i = 0; i < n_cw; i++)
{
scanf("%f %f", &cw[i].x, &cw[i].y);
}
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
42
Output:
Experiment No: 10
Experiment Name: Bresenham Circle Drawing Algorithm Source Code
Objectives:
Code:
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
int radius,centerX,centerY;
void bresenhamCircle() {
int x = 0;
int y = radius;
int decision = 3 - 2 * radius;
while (x <= y) {
drawCircle(x, y);
if (decision < 0) {
decision += 4 * x + 6;
43
x++;
} else {
decision += 4 * (x - y) + 10;
x++;
y--;
}
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
bresenhamCircle();
glFlush();
}
Output:
44
Experiment No: 11
Experiment Name: Mid point Circle Drawing Algorithm
Objectives:
Code:
45
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
void midpointCircle()
{
int x = 0;
int y = radius;
int decision = 1 - radius;
while (x <= y)
{
drawCircle(x, y);
if (decision < 0)
{
decision += 2 * x + 3;
x++;
}
else
{
decision += 2 * (x - y) + 5;
x++;
y--;
}
}
}
void display()
46
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
midpointCircle();
glFlush();
}
Output:
47