Tinywow - Computer Graphics Experiments (2) - 41880454
Tinywow - Computer Graphics Experiments (2) - 41880454
Lab File
(2023 - 2024)
For
5th Semester
BASICS OF OPENGL........................................................................................I
Experiment 1
Objective: Basic of Opengl
Implementation:
1. Draw the following
a. Points
b. Line
c. Rectangle
d. Polyline using Line_Strip
e. Closed Polyline using Line_Loop
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void setup(){
glClearColor(1.0f, 0.0f, 0.0f, 0.0f); // backgrouning
glPointSize(20);
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f); // coloring rect
glRectf(-0.75f, 0.75f, 0.75f, -0.75f); // draw rect
glBegin( GL_POINTS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(0.6f, -0.6f);
glVertex2f(0.5f, -0.6f);
glVertex2f(0.4f, -0.6f);
glEnd();
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f); // coloring rect
glVertex2f(.25,0.25);
glVertex2f(.75,.75);
glEnd();
glBegin(GL_POLYGON);
glColor3f(0.0f, 1.0f, 0.0f);
Jeetesh Saini-211436
glVertex3f(-0.1, -0.1,0.0);
glVertex3f(-0.6, -0.1,0.0);
glVertex3f(-0.8,-0.3,0.0);
glVertex3f(-0.6,-0.6,0.0);
glVertex3f(-0.1,-0.6,0.0);
glVertex3f(-.0,-0.3,0.0);
glEnd();
glFlush();
glutSwapBuffers();
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(400, 300); // horizontal, vertical
glutInitWindowPosition(200, 100); // horizontal, vertical
glutCreateWindow("Hello World"); // title of the window
setup();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
const int numCircleSegments = 100; // Number of segments for the circle approximation
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(-0.5f, 0.5f, 0.0f);
drawTriangle();
glPopMatrix();
glFlush();
}
return 0;
}
Output:
Jeetesh Saini-211436
Experiment 2
Objective: Implement DDA Line drawing algorithm.
Implementation:
#include <windows.h>
#include <iostream>
#include <GL/glut.h>
using namespace std;
void init(){
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);
}
void dda_line(int x1, int y1, int x2, int y2){
int dx =x2 -x1; int dy= y2-y1;
int steps =1, length,x =x1,y=y1;
length =max(abs(dx), abs(dy));
int xinc = dx/length, yinc= dy/length;
while(steps<=length){
glBegin(GL_POINTS);
glVertex2d(x,y);
glEnd();
x+= xinc; y+=yinc;
steps++;
}
glutSwapBuffers();
}
void display(){
Jeetesh Saini-211436
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 0);
glPointSize(2.0);
dda_line(0,0,250,250);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(400, 300);
glutInitWindowPosition(200, 100);
glutCreateWindow("DDA LINE DRWAING");
glutDisplayFunc(display);
init();
glutMainLoop();
return 1;
}
Output:
Jeetesh Saini-211436
Jeetesh Saini-211436
Experiment 3
Objective: Bresenham’s Line Drawing Algorithm
Implementation:
#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();
}
printf("%0.2f %0.2f\n",i,j);
glVertex3f ((i/100), (j/100), 0.0);
p=p+(2*dy);
}
}
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
Jeetesh Saini-211436
}
int main(int argc, char** argv)
{
Output:
Jeetesh Saini-211436
Experiment 4
Objective:Circle Midpoint Algorithm
A:Display a circle
Implementation:
#include <GL/glut.h>
#include <cmath>
while (x <= y) {
plotPixel(centerX + x, centerY + y);
plotPixel(centerX - x, centerY + y);
plotPixel(centerX + x, centerY - y);
plotPixel(centerX - x, centerY - y);
plotPixel(centerX + y, centerY + x);
plotPixel(centerX - y, centerY + x);
plotPixel(centerX + y, centerY - x);
plotPixel(centerX - y, centerY - x);
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glFlush();
}
Output:
Jeetesh Saini-211436
while (x <= y) {
plotPixel(centerX + x, centerY + y);
plotPixel(centerX - x, centerY + y);
plotPixel(centerX + x, centerY - y);
plotPixel(centerX - x, centerY - y);
plotPixel(centerX + y, centerY + x);
plotPixel(centerX - y, centerY + x);
plotPixel(centerX + y, centerY - x);
plotPixel(centerX - y, centerY - x);
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
Jeetesh Saini-211436
glFlush();
}
Output:
while (x <= y) {
plotPixel(centerX + x, centerY + y);
plotPixel(centerX - x, centerY + y);
plotPixel(centerX + x, centerY - y);
plotPixel(centerX - x, centerY - y);
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glFlush();
Jeetesh Saini-211436
Output:
Jeetesh Saini-211436
Experiment 5
Objective: Circle Bresenham’s Drawing Algorithm
Implementation:
1.
#include <stdio.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
int s1 = 250;
int s2 = 250;
int radius = 80;
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void drawCircle(int s1, int s2, int radius) {
int x = 0, y = radius;
int d = 3 - 2 * radius;
while (x <= y) {
drawPixel(s1 + x, s2 + y);
drawPixel(s1 - x, s2+ y);
drawPixel(s1 + x, s2 - y);
drawPixel(s1 - x, s2 - y);
drawPixel(s1 + y, s2 + x);
drawPixel(s1 - y, s2 + x);
drawPixel(s1+ y, s2 - x);
drawPixel(s1 - y,s2 - x);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
Jeetesh Saini-211436
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
drawCircle(s1, s2, radius);
glFlush();
}
void init() {
glClearColor(1.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
}
return 0;
}
output:
Jeetesh Saini-211436
2:
#include <stdio.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
int c1 = 280;
int c2 = 200;
int c3 = 240;
int c4 = 230;
int c5 = 310;
int c6 = 230;
int c7 = 280;
int c8 = 270;
int radius = 100;
int radius1 = 15;
int radius2 = 15;
int radius3 = 15;
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void drawCircle(int c1, int c2, int radius) {
int x = 0, y = radius;
int d = 3 - 2 * radius;
while (x <= y) {
drawPixel(c1 + x, c2 + y);
drawPixel(c1 - x, c2 + y);
drawPixel(c1 + x, c2 - y);
drawPixel(c1 - x, c2 - y);
drawPixel(c1 + y, c2 + x);
drawPixel(c1 - y, c2 + x);
Jeetesh Saini-211436
drawPixel(c1 + y, c2 - x);
drawPixel(c1 - y, c2 - x);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * x - 4*y + 10;
y--;
x++;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2.0);
glFlush();
}
void init() {
glMatrixMode(GL_PROJECTION);
Jeetesh Saini-211436
glLoadIdentity();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
3:
#include <stdio.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
int s1 = 270;
int s2 = 270;
int radius = 80;
Jeetesh Saini-211436
while (x <= y) {
drawPixel(s1 + x, s2 + y);
drawPixel(s1 - x, s2+ y);
drawPixel(s1 + x, s2 - y);
drawPixel(s1 - x, s2 - y);
drawPixel(s1 + y, s2 + x);
drawPixel(s1 - y, s2 + x);
drawPixel(s1+ y, s2 - x);
drawPixel(s1 - y,s2 - x);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
drawCircle(s1, s2, radius);
drawCircle(220, 220, radius);
drawCircle(300, 220, radius);
glFlush();
}
void init() {
glClearColor(1.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Circle Drawing
Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
Experiment 6
Objective:Ellipse mid point drawing algorithm
Implementation
A.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
int x = 0;
int y = radiusY;
int p;
int px = 0;
int py = twoRadiusX2 * y;
// Region 1
p = round(radiusY2 - radiusX2 * radiusY + 0.25 * radiusX2);
while (px < py) {
x++;
px += twoRadiusY2;
if (p < 0) {
p += radiusY2 + px;
} else {
y--;
py -= twoRadiusX2;
p += radiusY2 + px - py;
}
plotEllipsePoints(x, y, centerX, centerY);
}
Jeetesh Saini-211436
// Region 2
p = round(radiusY2 * (x + 0.5) * (x + 0.5) + radiusX2 * (y - 1) * (y - 1) - radiusX2 * radiusY2);
while (y > 0) {
y--;
py -= twoRadiusX2;
if (p > 0) {
p += radiusX2 - py;
} else {
x++;
px += twoRadiusY2;
p += radiusX2 - py + px;
}
plotEllipsePoints(x, y, centerX, centerY);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
// Call the ellipse drawing function with center (400, 300), radii 200 and 100
drawEllipse(400, 300, 200, 100);
glFlush();
}
Output:
Jeetesh Saini-211436
b
.#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
int x = 0;
int y = radiusY;
int p;
int px = 0;
int py = twoRadiusX2 * y;
} else {
y--;
py -= twoRadiusX2;
p += radiusY2 + px - py;
}
plotEllipsePoints(x, y, centerX, centerY);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
glFlush();
}
Output:
Jeetesh Saini-211436
C.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
int x = 0;
int y = radiusY;
int p;
int px = 0;
Jeetesh Saini-211436
int py = twoRadiusX2 * y;
// Region 1
p = round(radiusY2 - radiusX2 * radiusY + 0.25 * radiusX2);
while (px < py) {
x++;
px += twoRadiusY2;
if (p < 0) {
p += radiusY2 + px;
} else {
y--;
py -= twoRadiusX2;
p += radiusY2 + px - py;
}
plotEllipsePoints(x, y, centerX, centerY);
plotEllipsePoint1(x, y, centerX, centerY);
}
// Region 2
p = round(radiusY2 * (x + 0.5) * (x + 0.5) + radiusX2 * (y - 1) * (y - 1) - radiusX2 * radiusY2);
while (y > 0) {
y--;
py -= twoRadiusX2;
if (p > 0) {
p += radiusX2 - py;
} else {
x++;
px += twoRadiusY2;
p += radiusX2 - py + px;
}
plotEllipsePoints(x, y, centerX, centerY);
plotEllipsePoint1(x, y, centerX, centerY);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
glFlush();
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Midpoint Ellipse Drawing Algorithm");
glClearColor(0.0, 0.0, 0.0, 1.0);
gluOrtho2D(0, 800, 0, 600);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
Experiment: 7
Objective: BOUNDARY FILLING ALGORITHM
Implementation:
#include <windows.h>
#include <gl/glut.h>
#include <stdio.h>
} pixel;
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
pixel c;
return c;
boundary.blue)
{
Jeetesh Saini-211436
setPixel(x, y, fill);
void drawSquare()
glBegin(GL_LINE_LOOP);
glVertex2i(100, 100);
glVertex2i(300, 100);
glVertex2i(300, 300);
glVertex2i(100, 300);
glEnd();
void drawCircle()
int x = 0;
int y = radius;
int p = 1 - radius;
glBegin(GL_POINTS);
while (x <= y)
Jeetesh Saini-211436
if (p < 0)
p += 2 * x + 1;
else
y--;
p += 2 * (x - y) + 1;
x++;
glEnd();
void fillCircle()
fill.red = 255;
fill.green = 0;
fill.blue = 0;
boundary.red = 255;
boundary.green = 255;
boundary.blue = 0;
Jeetesh Saini-211436
void mydisplay()
glClear(GL_COLOR_BUFFER_BIT);
drawSquare();
drawCircle();
fillCircle();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(540, 320);
glClear(GL_COLOR_BUFFER_BIT);
glutDisplayFunc(mydisplay);
glutMainLoop();
return 0;
Output:
Jeetesh Saini-211436
Experiment: 8
Objective: Flood FILLING ALGORITHM
Implementation:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cmath>
#include <stdio.h>
int screen[400][400];
void floodFill(int x, int y, int oldColor, int newColor) {
if (x < 0 || x >= 400 || y < 0 || y >= 400) {
return;
}
if (screen[x][y] != oldColor) {
return; // Color already changed or not the target color
}
glFlush();
Jeetesh Saini-211436
Output:
Jeetesh Saini-211436
Experiment: 9
Objective: Sutherland line clipping ALGORITHM
Implementation::
#include <GL/glut.h>
#include <GL/gl.h>
#include <iostream>
#include <cmath>
if (x < x_min) {
code |= LEFT;
} else if (x > x_max) {
code |= RIGHT;
}
if (y < y_min) {
code |= BOTTOM;
} else if (y > y_max) {
code |= TOP;
}
return code;
}
while (true) {
Jeetesh Saini-211436
if (code1 != 0) {
code_out = code1;
} else {
code_out = code2;
}
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
glBegin(GL_LINES);
glVertex2d(x1, y1);
glVertex2d(x2, y2);
glEnd();
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
Jeetesh Saini-211436
glFlush();
}
Output:
Jeetesh Saini-211436
Experiment 10
}
//window to viewport mappings
double sx= (xvmax-xvmin)/(xmax-xmin);// scale parameters
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;
// draw a red colored viewport
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); //draw blue colored clip line
glBegin(GL_LINES);
glVertex2d(vx0,vy0);
glVertex2f(vx1,vy1);
glEnd();
}
void display()
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0); //draw a line with red color
glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glEnd();
glColor3f(0.0,0.0,1.0); //draw a blue color window
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
liangbaraskylineclipanddraw(x0,y0,x1,y1);
glFlush();
}
void myinit()
{ glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
int main(int argc, char** argv)
Jeetesh Saini-211436
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("liang barsky line clipping algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
void drawRectangle() {
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
drawRectangle();
// Implement your clipping logic and draw the clipped line here
// You can replace the clipping functions with OpenGL drawing functions
// For example, drawLine(clippedX1, clippedY1, clippedX2, clippedY2);
glFlush();
}
exit(0);
break;
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Line Clipping");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
#include <math.h>
#include <time.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <list>
void init(){
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);
float** pts;
class points{
int x;
int y;
public:
this->x = x;
Jeetesh Saini-211436
this->y = y;
int getx(){
return x;
int gety(){
return y;
};
class tryo{
int x;
int y;
public:
};
points *s,*p;
while(goal>clock());
void drawPolygon(){
glBegin(GL_LINE_LOOP);
sz = in.size();
while(in.size()>0){
Jeetesh Saini-211436
pts[in.size()-1][0] = temp->getx();
pts[in.size()-1][1] = temp->gety();
glVertex2i(temp->getx(),temp->gety());
in.pop_front();
glEnd();
glFlush();
void redraw(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(pts[i][0],pts[i][1]);
glEnd();
glFlush();
glColor3f(0,0,0);
glBegin(GL_LINE_LOOP);
glVertex2i(xmin,ymin);
glVertex2i(xmin,ymax);
glVertex2i(xmax,ymax);
glVertex2i(xmax,ymin);
glEnd();
glFlush();
glColor3f(1,0,0);
glLineWidth(1.0);
switch(clip_edge){
if((P->getx()-S->getx())==0)
m = 0;
else
m = (float) (P->gety()-S->gety())/(P->getx()-S->getx());
points(x,y));}
points(x,y));}
sz = out.size();
while(out.size()>0){
inner[out.size()-1][0] = temp->getx();
inner[out.size()-1][1] = temp->gety();
Jeetesh Saini-211436
out.pop_front();
out.empty();
return inner;
s = new points(inva[sz-1][0],inva[sz-1][1]);
p = new points(inva[j][0],inva[j][1]);
if(inside(p->getx(),p->gety(),clip_edge))
if(inside(s->getx(),s->gety(),clip_edge)){
out.push_front(new points(p->getx(),p->gety()));
else{ // case 4
out.push_front(temp);
out.push_front(p);
out.push_front(temp);
else{
s = p;
inva = out_to_in(inva,out);
return inva;
}
Jeetesh Saini-211436
if(key_t=='d'){
enter = -1;
drawPolygon();
in.empty();
if(key_t=='c'){
pts = SHPC(pts,outer,1);
pts = SHPC(pts,outer,2);
pts = SHPC(pts,outer,3);
pts = SHPC(pts,outer,4);
redraw(); }}
y = 480-y;
in.push_front(temp); }}
y = 480-y;
if(enter==-1&&st_flag){
xmin = x;
ymin = y;
st_flag = 0;
else{
xmax = x;
ymax = y;
redraw();
y = 480-y;
if(enter==-1&&st_flag==0){
xmax = x;
ymax = y;
st_flag = 1;
redraw();}}
void world(){
glPointSize(2);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(200,200);
glutCreateWindow("polygon clipping");
glutDisplayFunc(world);
glutMouseFunc(mouse);
glutMotionFunc(drag_start);
glutPassiveMotionFunc(drag_end);
glutKeyboardFunc(key);
init();
glutMainLoop();
return 0;
Output:
Jeetesh Saini-211436
int n;
int i,j;
PT p1,p2,p[20],pp[20];
void left() // left clipper
{
i=0;j=0;
for(i=0;i<n;i++)
{if(p[i].x<p1.x && p[i+1].x>=p1.x) //Case-1: outside to inside{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y;
// save point of intersection}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
pp[j].x=p[i+1].x; // save that point that lie
inside our clipping window // consult theory
pp[j].y=p[i+1].y;
Jeetesh Saini-211436
j++;
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}
for(i=0;i<n;i++)
{
if(p[i].x>p2.x && p[i+1].x<=p2.x) //Case-1: outside to inside
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y;
// save point of intersection
}
else
Jeetesh Saini-211436
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
pp[j].x=p[i+1].x; // save that
point that lie inside our clipping window // consult theory
pp[j].y=p[i+1].y;
j++;
}
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
for(i=0;i<n;i++)
{
if(p[i].y>p2.y && p[i+1].y<=p2.y) //Case-1: outside to inside
Jeetesh Saini-211436
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x;
// save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
pp[j].x=p[i+1].x; // save that point
that lie inside our clipping window // consult theory
pp[j].y=p[i+1].y;
j++;
}
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
}
Jeetesh Saini-211436
for(i=0;i<n;i++)
{
if(p[i].y<p1.y && p[i+1].y>=p1.y) // Case-1: outside to inside
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x;
// save point of intersection
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
pp[j].x=p[i+1].x; // save that
point that lie inside our clipping window // consult theory
pp[j].y=p[i+1].y;
j++;
}
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
Jeetesh Saini-211436
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){
Jeetesh Saini-211436
for(i=0;i<n;i++){
printf("\nEnter V%d(x%d,y%d):\n" , i+1, i+1, i+1);
scanf("%f", &p[i].x); // if you don't know what value should be given:
enter V1(100,110), V2(340,210), V3(300,380)
scanf("%f", &p[i].y);}
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("Sutherland Hodgman Polygon Clipping Algorithm ");
init();
glutDisplayFunc(display);
glutMouseFunc(myMouse); // notice mouse movement and
call user defined function
glFlush();
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
#include<windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(tx,ty,0);
glBegin(GL_TRIANGLES);
for (int i = 0; i < sides; i++) {
float angle = 2 * 3.14 * i / sides;
int x = centerX + radius * cos(angle);
int y = centerY + radius * sin(angle);
glVertex2i(x, y);
}
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex2i(100, 100);
Jeetesh Saini-211436
glVertex2i(200, 100);
glVertex2i(200, 200);
glVertex2i(100, 200);
glEnd();
glPopMatrix();
glFlush();
}
void init(void)
{
glClearColor (1.0, 1.0, 1.0, 1.0);
glOrtho(-200.0, 200.0, -200.0, 200.0, -2.0, 2.0);
}
void spe_key(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
glColor3f(0,0,0);
tx -=5;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
glColor3f(0,0,0);
tx +=5;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
glColor3f(0,0,0);
ty +=5;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
glColor3f(0, 0, 0);
ty -=5;
glutPostRedisplay();
break;
default:
break;
}
}
int main()
{
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow ("2D Transformation");
init();
glutDisplayFunc(display);
glutSpecialFunc(spe_key);
glutMainLoop();
Jeetesh Saini-211436
return 0;
}
Output:
#include <iostream>
#include <windows.h>
#include <gl/glut.h>
#include <stdio.h>
#include <math.h>
#include <bits/stdc++.h>
void display_before(void) {
glClear(GL_COLOR_BUFFER_BIT);
int poly_size = 3;
int poly_points[20][2] = {{0, 0}, {40, 90}, {80, 0}};
draw_polygon(poly_points, poly_size);
glutSwapBuffers();
}
void display_after(void) {
glClear(GL_COLOR_BUFFER_BIT);
int poly_size = 3;
int poly_points[20][2] = {{0, 0}, {40, 90}, {80, 0}};
int tx = 20;
int ty = 100;
int scale_factor = 2;
for (int i = 0; i < poly_size; i++) {
poly_points[i][0] *= scale_factor;
poly_points[i][1] *= scale_factor;
}
translation(poly_points, poly_size, tx, ty);
glutSwapBuffers();
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glPointSize(2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 400.0, 0.0, 400.0, 0, 50);
}
glutInitWindowSize(400, 400);
glutInitWindowPosition(50, 50);
glutCreateWindow("BEFORE");
init();
glutDisplayFunc(display_before);
glutInitWindowSize(400, 400);
glutInitWindowPosition(450, 50);
glutCreateWindow("AFTER");
init();
glutDisplayFunc(display_after);
glutMainLoop();
Jeetesh Saini-211436
return 0;
}
#include<iostream>
#include <windows.h>
#include <gl/glut.h>
#include <stdio.h>
#include <math.h>
#include <bits/stdc++.h>\
}
glEnd();
}
void translation(int poly_points[][2], int poly_size,int tx,int ty, int sx,int sy)
{
int new_poly[poly_size][2];
int i;
for(i=0;i<poly_size;i++)
{
new_poly[i][0]=(poly_points[i][0] + tx) *sx;
new_poly[i][1]=(poly_points[i][1] + ty) *sy;
}
draw_polygon(new_poly,poly_size);
}
void display_before(void)
{
glClear(GL_COLOR_BUFFER_BIT);
int poly_size = 4;
int poly_points[20][2] = {{0,0},{0,90} ,{80,90},{80,0}};
draw_polygon(poly_points,poly_size);
glutSwapBuffers();
}
Jeetesh Saini-211436
void display_after(void)
{
glClear(GL_COLOR_BUFFER_BIT);
int poly_size = 4;
int poly_points[20][2] = {{0,0},{0,90} ,{80,90},{80,0}};
int tx=15;
int ty=10;
int sx=2;
int sy=1;
translation(poly_points,poly_size,tx,ty,sx,sy);
glutSwapBuffers();
void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glPointSize(2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 200.0, 0.0, 200.0, 0, 50);
}
glutInitWindowSize(400, 400);
glutInitWindowPosition(50, 50);
glutCreateWindow("BEFORE ");
init();
glutDisplayFunc(display_before);
glutInitWindowSize(400, 400);
glutInitWindowPosition(450, 50);
glutCreateWindow("AFTER");
init();
glutDisplayFunc(display_after);
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
void drawTriangle() {
glBegin(GL_TRIANGLES);
glVertex2f(triangleVertices[0], triangleVertices[1]);
glVertex2f(triangleVertices[2], triangleVertices[3]);
glVertex2f(triangleVertices[4], triangleVertices[5]);
glEnd();
}
void before_rotation() {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
drawTriangle();
glPopMatrix();
glutSwapBuffers();
}
void after_rotation() {
glClear(GL_COLOR_BUFFER_BIT);
Jeetesh Saini-211436
glPushMatrix();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glColor3f(0.0f, 0.0f, 0.0f);
drawTriangle();
glPopMatrix();
glutSwapBuffers();
}
glutInitWindowPosition(450,50);
glutCreateWindow("Triangle Rotation");
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glutDisplayFunc(after_rotation);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glutMainLoop();
return 0;
}
Output:
Jeetesh Saini-211436
void drawSquare() {
glBegin(GL_QUADS);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glEnd();
}
Jeetesh Saini-211436
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Original square
glColor3f(0.0, 1.0, 0.0);
glPushMatrix();
drawSquare();
glPopMatrix();
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-2.5, 2.5, -2.5, 2.5); // Adjusted viewing window
}
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output:
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawTriangle();
glFlush();
}
void display_after_shearing()
{
glClear(GL_COLOR_BUFFER_BIT);
drawTriangle_after_Shearing();
glFlush();
}
glutCreateWindow("Triangle");
glutDisplayFunc(display);
glutCreateWindow("Shearing Transformation");
glutDisplayFunc(display_after_shearing);
glutMainLoop();
return 0;
}
Jeetesh Saini-211436
void drawTriangle_after_Scaling_and_Translation() {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0, 0);
GLfloat vertices[3][3] = {
{0, 0, 0},
{1, 0, 0},
{0, 1, 0}
};
glVertex3fv(scaledResult);
}
glEnd();
}
void display() {
Jeetesh Saini-211436
glClear(GL_COLOR_BUFFER_BIT);
drawTriangle();
glFlush();
}
void display_after_Scaling() {
glClear(GL_COLOR_BUFFER_BIT);
drawTriangle_after_Scaling_and_Translation();
glFlush();
}
glutCreateWindow("Triangle");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
output:
Implementation:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
Jeetesh Saini-211436
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1, 0, 0);
glPushMatrix();
glTranslated(0, 1.2, -6);
glRotated(angleX, 1, 0, 0); // Apply X-axis rotation
glRotated(angleZ, 0, 0, 1); // Apply Z-axis rotation
glutSolidCone(1, 1, slices, stacks);
glPopMatrix();
glPushMatrix();
glTranslated(0, -1.2, -6);
glRotated(angleX, 1, 0, 0); // Apply X-axis rotation
glRotated(angleZ, 0, 0, 1); // Apply Z-axis rotation
glutWireCone(1, 1, slices, stacks);
glPopMatrix();
glutSwapBuffers();}
static void key(unsigned char key, int x, int y){
switch (key){
case 27:
case 'q':
exit(0);
break;
case '+':
slices++;
stacks++;
break;
case '-':
Jeetesh Saini-211436
glutPostRedisplay();
}
glutCreateWindow("GLUT Shapes");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glClearColor(1, 1, 1, 1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
const GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
Jeetesh Saini-211436