Programs
Programs
#include <GL/glut.h>
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); /* clear all pixels */
glColor3f (1.0, 0.0, 1.0);
glBegin(GL_POLYGON); /* draw magenta polygon (rectangle)*/
glVertex3f (-0.15, -0.15, 0.0); /*with corners at (0.25,0.25,0.0) */
glVertex3f (0.65, -0.15, 0.0); /*(0.75, 0.75, 0.0) */
glVertex3f (0.65, 0.65, 0.0);
glVertex3f (-0.15, 0.65, 0.0);
glEnd();
glColor3f (1.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex3f(0.70f, 0.50f, -0.60f);
glEnd( );
glColor3f (0.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(-0.10f, -0.50f, 0.90f); // origin of the line
glVertex3f(0.90f, -0.80f, -0.80f); // ending point of the line
glEnd( );
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.50f, -0.25f, 0.0f);
glVertex3f(-0.55f, 0.30f, 0.0f);
glVertex3f(0.15f,- 0.50f, 0.0f);
glEnd( );
glFlush ();
glutMainLoop();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /*Declare initial
window display mode (single buffer and RGBA*/
glutInitWindowSize (650, 550); // Declare initial window size
glutInitWindowPosition (200, 200); // Declare initial window position
glutCreateWindow ("Simple Demo"); //set window title
glutDisplayFunc(display); //Register callback function to display graphics
glutMainLoop(); // Enter main loop and process events.
return 0;
#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);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void bresenhams_line_draw(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1; // x difference
int dy = y2 - y1; // y difference
int m = dy/dx; // slope
if (m < 1)
{
int decision_parameter = 2*dy - dx;
int x = x1; // initial x
int y = y1; // initial y
if (dx < 0) // decide the first point and second point
{
x = x2; // making second point as first point
y = y2;
x2 = x1;
}
draw_pixel (x, y); // plot a point
while (x < x2) // from 1st point to 2nd point
{
if (decision_parameter >= 0)
{
x = x+1;
y = y+1;
decision_parameter = decision_parameter + 2*dy - 2*dx * (y+1 - y);
}
else
{
x = x+1;
y = y;
decision_parameter = decision_parameter + 2*dy - 2*dx * (y - y);
}
draw_pixel (x, y);
}
}else if (m > 1)
{
int decision_parameter = 2*dx - dy;
int x = x1; // initial x
int y = y1; // initial y
if (dy < 0)
{
x = x2;
y = y2;
y2 = y1;
}
draw_pixel (x, y);
while (y < y2)
{
if (decision_parameter >= 0)
{
x = x+1;
y = y+1;
decision_parameter = decision_parameter + 2*dx - 2*dy * (x+1 - x);
}
else
{
y = y+1;
x = x;
decision_parameter = decision_parameter + 2*dx - 2*dy * (x- x);
}
draw_pixel(x, y);
}
}
else if (m == 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); // left ->0, right ->500, bottom ->0, top ->500
}
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); // 1st point from user
printf( "Enter End Points (x2,y2)\n");
scanf("%d %d", &x2, &y2); // 2nd point from user
glutInit(&argc, argv); // initialize graphics system
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); //single buffered mode with RGB colour
variants
glutInitWindowSize(500, 500); // 500 by 500 window size
glutInitWindowPosition(220, 200); // where do you wanna see your window
glutCreateWindow("Bresenham's Line Drawing"); // the title of your window
init(); // initialize the canvas
glutDisplayFunc(display); // call display function
glutMainLoop(); // run forever
}
#include<GL/glut.h>
#include<stdio.h>
int x,y;
int where_to_rotate=0; // don't rotate initially
float rotate_angle=0; // initial angle
float translate_x=0,translate_y=0; // initial translation
void draw_pixel(float x1, float y1)
{
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(x1,y1); // plot a single point
glEnd();
}
void triangle(int x, int y)
{
glColor3f(1,0,0);
glBegin(GL_POLYGON); // drawing a Triangle
glVertex2f(x,y);
glVertex2f(x+400,y+300);
glVertex2f(x+300,y+0);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1,1,1); // mark origin point as white dot
draw_pixel(0,0); // plot origin - white colour
if (where_to_rotate == 1) // rotate around origin
{
translate_x = 0; // no translation for rotation around origin
translate_y = 0;
rotate_angle += 1; // the amount of rotation angle
}
if (where_to_rotate == 2) // rotate around Fixed Point
{
translate_x = x; // SET the translation to wherever the customer says
translate_y = y;
rotate_angle += 1; // the amount of rotation angle
glColor3f(0,0,1); // mark the customer coordinate as blue dot
draw_pixel(x,y); // plot the customer coordinate - blue colour
}
glTranslatef(translate_x, translate_y, 0); // ACTUAL translation +ve
glRotatef(rotate_angle, 0, 0, 1); // rotate
glTranslatef(-translate_x, -translate_y, 0); // ACTUAL translation -ve
triangle(translate_x,translate_y); // what to rotate? – TRIANGLE boss
glutPostRedisplay(); // call display function again and again
glutSwapBuffers(); // show the output
}
void init()
{
glClearColor(0,0,0,1); //setting to black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-800, 800, -800, 800);
glMatrixMode(GL_MODELVIEW);
}
void rotateMenu (int option)
{
if(option==1)
where_to_rotate=1; // rotate around origin
if(option==2)
where_to_rotate=2; // rotate around customer's coordinates
if(option==3)
where_to_rotate=3; // stop rotation
}
int main(int argc, char **argv)
{
printf( "Enter Fixed Points (x,y) for Rotation: \n");
scanf("%d %d", &x, &y); // getting the user's coordinates to rotate
glutInit(&argc, argv); // initialize the graphics system
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // SINGLE also works
glutInitWindowSize(800, 800); // 800 by 800 size..you can change it
glutInitWindowPosition(0, 0); // where do you wanna see your window
glutCreateWindow("Create and Rotate Triangle"); // title
init(); // initialize the canvas
glutDisplayFunc(display); // call display function
glutCreateMenu(rotateMenu); // menu items
glutAddMenuEntry("Rotate around ORIGIN",1);
glutAddMenuEntry("Rotate around FIXED POINT",2);
glutAddMenuEntry("Stop Rotation",3
);
glutAttachMenu
(
GLUT_RIGHT_BUTTON
);
Write a program to create and fill two dimensional object by using boundary
fill algorithm.
#include <GL/glut.h>
#include<math.h>
#include<stdio.h>
int ww = 600, wh = 500;
float fillCol[3] = {0.4,0.0,0.0};
float borderCol[3] = {0.0,0.0,0.0};
void setPixel(int, int, float[]);
void getPixel(int, int, float[]);
void resize(int, int);
void drawPolygon(int, int, int, int);
void boundaryFill4(int,int,float[],float[]);
void display();
void mouse(int, int, int, int);
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(ww,wh);
glutInitWindowPosition(500, 50);
glutCreateWindow("Bountry-Fill-Recursive");
glutDisplayFunc(display);
glutMouseFunc(mouse);
//calls whenever frame buffer window is resized
glutReshapeFunc(resize);
glutMainLoop();
return 0;
}
void setPixel(int pointx, int pointy, float f[3])
{
glBegin(GL_POINTS);
glColor3fv(f);
glVertex2i(pointx,pointy);
glEnd();
glFlush();
}
void getPixel(int x, int y, float pixels[3])
{
glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
}
void resize(int w, int h)
{
glMatrixMode(GL_PROJECTION); //set projection paramaters
glLoadIdentity();
gluOrtho2D(0.0,w,0.0,h);
glutReshapeWindow(ww, wh);
glViewport(0.0, 0.0, w, h);
}
void drawPolygon(int x1, int y1, int x2, int y2)
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2,y2);
glVertex2i(x2,y1);
glEnd();
glFlush();
}
void display()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
//If you modify following values, u should change condition in mouse() also.
drawPolygon(150,250,200,300);
glFlush();
}
void boundaryFill4(int x,int y,float fillColor[3],float borderColor[3])
{
float interiorColor[3];
getPixel(x,y,interiorColor);
if(((interiorColor[0]!=borderColor[0] && (interiorColor[1])!=borderColor[1]
&& (interiorColor[2])!=borderColor[2]) && (interiorColor[0]!=fillColor[0]
&& (interiorColor[1])!=fillColor[1] && (interiorColor[2])!=fillColor[2])))
{
setPixel(x,y,fillColor);
boundaryFill4(x+1,y,fillColor,borderColor);
boundaryFill4(x-1,y,fillColor,borderColor);
boundaryFill4(x,y+1,fillColor,borderColor);
boundaryFill4(x,y-1,fillColor,borderColor);
}
}
void mouse(int btn, int state, int x, int y)
{
//This check is based on size of the polygon mentioned in display() function
if(((x<150 || x>200) ||( y<200 || y>250)))
printf("Invalid click !!\n");
else
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
int xi = x;
int yi = (wh-y);
boundaryFill4(xi,yi,fillCol,borderCol);
}
}
glutReshapeFunc(resize);
glutMainLoop(); //display everything and wait
}
void resize(int w, int h)
{
glMatrixMode(GL_MODELVIEW); //set projection paramaters
glLoadIdentity();
//change first three values to see the cube from different directions
gluLookAt(3,3,6, 0,0, 0, 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //reset to identity matrix
gluPerspective(45,(float)w/h, 1, 100);
glViewport(0,0,w,h);
}
V translate(V point, V offset)
{
point.x +=offset.x;
point.y +=offset.y;
point.z +=offset.z;
return point;
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_MODELVIEW);
V p0 = { -0.5, -0.5, -0.5};
V amountX = {1, 0, 0}; //matrix values needed for translation
V amountY ={0, 1, 0};
V amountZ = {0, 0, 1};
V p1 = translate(p0, amountX);
V p2 = translate(p1, amountY);
V p3 = translate(p0, amountY);
glBegin(GL_LINE_LOOP); //draw one surface of cube
glVertex3f(p0.x, p0.y, p0.z);
glVertex3f(p1.x, p1.y, p1.z);
glVertex3f(p2.x, p2.y, p2.z);
glVertex3f(p3.x, p3.y, p3.z);
glEnd();
//calculate and draw second surface parallel to first surface using translation
V p4 = translate(p0, amountZ);
V p5 = translate(p1, amountZ);
V p6 = translate(p2, amountZ);
V p7 = translate(p3, amountZ);
glBegin(GL_LINE_LOOP);
glVertex3f(p4.x, p4.y, p4.z);
glVertex3f(p5.x, p5.y, p5.z);
glVertex3f(p6.x, p6.y, p6.z);
glVertex3f(p7.x, p7.y, p7.z);
glEnd();
//draw remaining lines to join two surface, to create a cube
glBegin(GL_LINES);
glVertex3f(p0.x, p0.y, p0.z);
glVertex3f(p4.x, p4.y, p4.z);
glVertex3f(p1.x, p1.y, p1.z);
glVertex3f(p5.x, p5.y, p5.z);
glVertex3f(p2.x, p2.y, p2.z);
glVertex3f(p6.x, p6.y, p6.z);
glVertex3f(p3.x, p3.y, p3.z);
glVertex3f(p7.x, p7.y, p7.z);
glEnd();
glFinish();
}
Write a Program to create (without using built-in function) and rotate (1. given angle, 2. around X and
Y axis) a triangle by implementing rotation algorithm.
#include <GL/glut.h>
#include<math.h>
#include<stdio.h>
#define PI 3.1416
float angle=10;
float theta;
typedef struct Point
{
float x, y, z;
};
void rotate_x(float);
void rotate_y(float);
void init();
Point p[3]={ {3.0, 0, -0.50}, {3.0, 0, -1.50}, {3.0, 1, -1.0}};
void drawTriangle(Point p[3])
{
glColor3f(0.3, 0.6, 0.9);
glLineWidth(2.0);
glBegin(GL_LINES);
glVertex3f(p[0].x, p[0].y, p[0].z);
glVertex3f(p[1].x, p[1].y, p[1].z);
glEnd();
glColor3f(0.6, 0.9, 0.3);
glBegin(GL_LINES);
glVertex3f(p[1].x, p[1].y, p[1].z);
glVertex3f(p[2].x, p[2].y, p[2].z);
glEnd();
glColor3f(0.9, 0.3, 0.6);
glBegin(GL_LINES);
glVertex3f(p[0].x, p[0].y, p[0].z);
glVertex3f(p[2].x, p[2].y, p[2].z);
glEnd();
glFlush();
}
void display()
{
init();
int opt;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(7, 0, 0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,3,0);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,3);
glEnd();
glRasterPos3f(7, 0, 0);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'X');
glRasterPos3f(0, 3, 0);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'Y');
glRasterPos3f(0, 0, 3);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'Z');
glFinish();
drawTriangle(p);
printf("************ Traingle Rotation ***************");
printf("\n1. Rotate around x-axis \n 2. Rotate around y-axis \n");
printf("Enter your option:");
scanf("%d", & opt);
printf("\n Enter value for theta: ");
scanf("%f", &theta);
switch(opt)
{
case 1: rotate_x(theta);
break;
case 2: rotate_y(theta);
break;
}
glFlush();
}
void rotate_x(float theta)
{
int i;
Point new_p[3];
for(i=0;i<3;i++)
{
new_p[i].x= p[i].x;
new_p[i].y = p[i].y * cos(theta * PI/180.0) –
p[i].z * sin(theta*PI/180.0);
new_p[i].z = p[i].y * sin(theta * PI/180.0) +
p[i].z * cos(theta*PI/180.0);
}
drawTriangle(new_p);
}
void rotate_y(float theta)
{
int i;
Point new_p[3];
for(i=0;i<3;i++)
{
new_p[i].x = p[i].z * sin(theta * PI/180.0) +
p[i].x * cos(theta*PI/180.0);
new_p[i].y= p[i].y;
new_p[i].z = p[i].z * cos(theta * PI/180.0) –
p[i].x * sin(theta*PI/180.0);
}
drawTriangle(new_p);
}
void init(void)
{
glMatrixMode(GL_MODELVIEW); //set projection parameters
glLoadIdentity();
gluLookAt(10,1,1, 0,0, 0, 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //reset to identity matrix
gluPerspective(45, 1, 1, 100);
}
void main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(500,100);
glutCreateWindow(" Triangle Rotation");
init();
glutDisplayFunc(display);
glutMainLoop();
}
Program to clip a line using Cohen-Sutherland line-clipping algorithm.