Program1:Implement Brenham's line drawing algorithm for all types of slope.
#include <GL/glut.h>
#include <stdio.h>
int x1, y1, x2, y2;
void myInit()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);
}
void draw_pixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void draw_line(int x1, int x2, int y1, int y2)
{
int dx, dy, i, e;
int incx, incy, inc1, inc2;
int x,y;
dx = x2-x1;
dy = y2-y1;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
incx = 1;
if (x2 < x1) incx = -1;
incy = 1;
if (y2 < y1) incy = -1;
x = x1; y = y1;
if (dx > dy)
{
draw_pixel(x, y);
e = 2 * dy-dx;
inc1 = 2*(dy-dx);
inc2 = 2*dy;
for (i=0; i<dx; i++) {
if (e >= 0) {
y += incy;
e += inc1;
}
else
e += inc2;
x += incx;
draw_pixel(x, y);
}
}
else
{
draw_pixel(x, y);
e = 2*dx-dy;
inc1 = 2*(dx-dy);
inc2 = 2*dx;
for (i=0; i<dy; i++) {
if (e >= 0) {
x += incx;
e += inc1;
}
else
e += inc2;
y += incy;
draw_pixel(x, y);
}
}
}
void myDisplay()
{
draw_line(x1, x2, y1, y2);
glFlush();
}
int main(int argc, char **argv) {
printf( "Enter (x1, y1, x2, y2)\n");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
Output:
2 & 4. To demonstrate the basic geometric operations/Transformation on 2D objects
#include<GL/glut.h>
#include<math.h>
double parr[8];
void init()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,0,0,1);
glColor3f(1,0,1);
gluOrtho2D(-500,500,-500,500); // Left,right,bottom,top
// Polygon Defaut
parr[0] = 10; //x
parr[1] = 10; //y
parr[2] = 200;
parr[3] = 10;
parr[4] = 200;
parr[5] = 200;
parr[6] = 10;
parr[7] = 200;
}
double degreeToRad(double deg)
{
return 3.14*(deg/180);
}
void polygon()
{
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);
glVertex2f(parr[0],parr[1]);
glVertex2f(parr[2],parr[3]);
glVertex2f(parr[4],parr[5]);
glVertex2f(parr[6],parr[7]);
glEnd();
glFlush();
}
void drawCorodinates()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
glPointSize(4);
glBegin(GL_LINES);
glVertex2f(-500,0);
glVertex2f(500,0);
glVertex2f(0,500);
glVertex2f(0,-500);
glEnd();
glColor3f(1,0,0);
glBegin(GL_POINTS);
glVertex2f(0,0);
glEnd();
glFlush();
}
// Translation
void translate2d()
{
// 40px in x
// 50px in y
int i = 0;
int x = 40, y = 50;
for(i= 0;i<7;i=i+2)
{
parr[i] = parr[i] + x;
}
for(i = 0;i<7;i=i+2)
{
parr[i] = parr[i] + y;
}
polygon();
}
// Rotation
void rotation()
{
double angle = 270,rad,x,y;
rad = degreeToRad(angle);
for(int i=0;i<8;i=i+2)
{
x = parr[i]*cos(rad) - parr[i+1]*sin(rad);
y = parr[i]*sin(rad) + parr[i+1]*cos(rad);
parr[i] = x;
parr[i+1] = y;
}
polygon();
}
// Scaling
void scaling2d()
{
// 2 unit in x
// 2 unit in y
int i = 0;
double x = 0.5, y = 0.5;
for(i= 0;i<7;i=i+2)
{
parr[i] = parr[i] * x;
parr[i+1] = parr[i+1] * y;
}
polygon();
}
void menu(int ch)
{
drawCorodinates();
switch(ch)
{
case 1: polygon();
break;
case 2: translate2d();
break;
case 3: scaling2d();
break;
case 4: rotation();
break;
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("2D Transformation");
init();
glutDisplayFunc(drawCorodinates);
glutCreateMenu(menu);
glutAddMenuEntry("1 Display Polygon",1);
glutAddMenuEntry("2 Translate",2);
glutAddMenuEntry("3 Scaling",3);
glutAddMenuEntry("4 Rotation",4);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}
3&5. To demonstrate the basic geometric operations/Transformation on 3D objects.
#include<stdlib.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 normals[][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},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.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}};
GLubyte CubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};
void polygon(int a,int b,int c,int d)
{
glColor3f(1.0,0.0,0.0);
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);
}
static GLfloat theta[]={0.0,0.0,0.0};
static GLint axis=2;
static GLdouble viewer[]={0.0,0.0,5.0};
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2],0.0,0.0,0.0,0.0,1.0,0.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);
//glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,CubeIndices);
//glBegin(GL_LINES);
//glVertex3f(0.0,0.0,0.0);
//glVertex3f(1.0,1.0,1.0);
colorcube();
//glEnd();
glFlush();
glutSwapBuffers();
}
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;
theta[axis]+=2.0;
if(theta[axis]>360.0)theta[axis]-=360.0;
display();
}
void keys(unsigned char key,int x,int y)
{
if(key=='x') viewer[0]-=1.0;
if(key=='X') viewer[0]+=1.0;
if(key=='y') viewer[1]-=1.0;
if(key=='Y') viewer[1]+=1.0;
if(key=='z') viewer[2]-=1.0;
if(key=='Z') viewer[2]+=1.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);
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("colorcube viewer");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
OUTPUT
Rotation about X-axis
Rotation about Y-axis
Rotation about Z-axis
6. develop a program to demonstrate animation effects on simple objects
// OpenGL animation for revolution
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
// global declaration
int x, y;
float i, j;
// Initialization function
void myInit (void)
{
// Reset background color with black (since all three argument is 0.0)
glClearColor(0.0, 0.0, 0.0, 1.0);
// Set picture color to green (in RGB model)
// as only argument corresponding to G (Green) is 1.0 and rest are 0.0
glColor3f(0.0, 1.0, 0.0);
// Set width of point to one unit
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set window size in X- and Y- direction
gluOrtho2D(-780, 780, -420, 420);
}
// Function to display animation
void display (void)
{
// Outer loop to make figure moving
// loop variable j iterated up to 10000,
// indicating that figure will be in motion for large amount of time
// around 10000/6.29 = 1590 time it will revolve
// j is incremented by small value to make motion smoother
for (j = 0; j < 10000; j += 0.01)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
// Iterate i up to 2*pi, i.e., 360 degree
// plot point with slight increment in angle,
// so, it will look like a continuous figure
// Loop is to draw outer circle
for (i = 0;i < 6.29;i += 0.001)
{
x = 200 * cos(i);
y = 200 * sin(i);
glVertex2i(x, y);
// For every loop, 2nd glVertex function is
// to make smaller figure in motion
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
// 7 loops to draw parallel latitude
for (i = 1.17; i < 1.97; i += 0.001)
{
x = 400 * cos(i);
y = -150 + 300 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 1.07; i < 2.07; i += 0.001)
{
x = 400 * cos(i);
y = -200 + 300 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 1.05; i < 2.09; i += 0.001)
{
x = 400 * cos(i);
y = -250 + 300 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 1.06; i < 2.08; i += 0.001)
{
x = 400 * cos(i);
y = -300 + 300 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 1.10; i < 2.04; i += 0.001)
{
x = 400 * cos(i);
y = -350 + 300 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 1.16; i < 1.98; i += 0.001)
{
x = 400 * cos(i);
y = -400 + 300 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 1.27; i < 1.87; i += 0.001)
{
x = 400 * cos(i);
y = -450 + 300 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
// Loop is to draw vertical line
for (i = 200; i >=- 200; i--)
{
glVertex2i(0, i);
glVertex2i(-600 * cos(j), i / 2 - 100 * sin(j));
}
// 3 loops to draw vertical ellipse (similar to longitude)
for (i = 0;i < 6.29; i += 0.001)
{
x = 70 * cos(i);
y = 200 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 0; i < 6.29; i += 0.001)
{
x = 120 * cos(i);
y = 200 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
for (i = 0; i < 6.29; i += 0.001)
{
x = 160 * cos(i);
y = 200 * sin(i);
glVertex2i(x, y);
glVertex2i(x / 2 - 600 * cos(j), y / 2 - 100 * sin(j));
}
// Loop to make orbit of revolution
for (i = 0; i < 6.29; i += 0.001)
{
x = 600 * cos(i);
y = 100 * sin(i);
glVertex2i(x, y);
}
glEnd();
glFlush();
}
}
// Driver Program
int main (int argc, char** argv)
{
glutInit(&argc, argv);
// Display mode which is of RGB (Red Green Blue) type
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Declares window size
glutInitWindowSize(1360, 768);
// Declares window position which is (0, 0)
// means lower left corner will indicate position (0, 0)
glutInitWindowPosition(0, 0);
// Name to window
glutCreateWindow("Revolution");
// Call to myInit()
myInit();
glutDisplayFunc(display);
glutMainLoop();
}