A) Program To Implement A FLYING KITE
A) Program To Implement A FLYING KITE
A) Program To Implement A FLYING KITE
#include<glut.h>
float i=0,j=-50,x=1;
int ctr=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex2f(0+i,-20+j);
glVertex2f(-10+i,0+j);
glVertex2f(0+i,10+j);
glVertex2f(10+i,0+j);
glEnd();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2f(0+i,10+j);
glVertex2f(0+i,-20+j);
glVertex2f(-10+i,0+j);
glVertex2f(10+i,0+j);
glColor3f(1,0,0);
glVertex2f(0+i,-20+j);
glVertex2f(-5*x+i,-32+j);
glVertex2f(-5*x+i,-32+j);
glVertex2f(5*x+i,-44+j);
glEnd();
glFlush();
}
void myInit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100,100,-100,100);
glMatrixMode(GL_MODELVIEW);
glClearColor(1,1,1,1);
void idle()
{
j+=0.01;
ctr++;
if(j>100)
{
i=0;
j=-50;
}
if(ctr>5000)
{
ctr=0;
x*=-1;
}
if(ctr<2500)
i+=0.01;
else
i-=0.01;
display();
}
void main()
{
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("KITE");
glutDisplayFunc(display);
glutIdleFunc(idle);
myInit();
glutMainLoop();
}
#include <glut.h>
float v[3][2]={{-1,-0.5},{1.0,-0.5},{0,1.1}};
int n=5;
float t=0;
void disp()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,1);
glColor3f(0,1,0);
glBegin(GL_POLYGON);
glVertex2f(cos(t),sin(t));
glVertex2f(-sin(t),cos(t));
glVertex2f(-cos(t),-sin(t));
glVertex2f(sin(t),-cos(t));
glEnd();
//glFlush();
glutSwapBuffers();
}
void idle()
{
if(t>=360)
t=t-360;
t+=0.001;
disp();
}
void mouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(idle);
if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(0);
if(button==GLUT_MIDDLE_BUTTON)
exit(0);
}
void main()
{
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(0,0);
glutInitWindowSize(800,800);
glutCreateWindow("square rotation");
//glutIdleFunc(idle);
glutMouseFunc(mouse);
glutDisplayFunc(disp);
glutMainLoop();
}
2. b) Create 3D Sierpinski gasket by recursive subdivision of tetrahedron.
#include <glut.h>
#include <stdio.h>
float v[4][3]={{1.0,-1.0,0.0},{0.0,0.5,0.0},{-1.0,-1.0,0.0},{0.0,-1.5,-1.0}};
float colors[4][3]={{0.0,0.0,1.0},{1.0,0.0,0.0},{1.0,0.0,0.0},{0.0,0.0,0.0}};
int n;
int s=25;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,1);
glFlush();
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,600,600,0);
glMatrixMode(GL_MODELVIEW);
}
void main()
{
glutInitWindowSize(600,600);
glutInitWindowPosition(0,0);
glutCreateWindow("mouse click point square");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(draw);
init();
glutMainLoop();
}
double xmin=50,ymin=50,xmax=100,ymax=100;
double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
const int RIGHT=8;
const int LEFT=2;
const int TOP=4;
const int BOTTOM=1;
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(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);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin,yvmin);
glVertex2f(xvmax,yvmin);
glVertex2f(xvmax,yvmax);
glVertex2f(xvmin,yvmax);
glEnd();
glColor3f(0,0,1);
glBegin(GL_LINES);
glVertex2d(vx0,vy0);
glVertex2d(vx1,vy1);
glEnd();
}
}
void display()
{
double x0=60,y0=20,x1=80,y1=120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glEnd();
glColor3f(0,0,1);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin,ymin);
glVertex2f(xmax,ymin);
glVertex2f(xmax,ymax);
glVertex2f(xmin,ymax);
glEnd();
cohensutherlandlineclipanddraw(x0,y0,x1,y1);
glFlush();
}
void myinit()
{
glClearColor(1,1,1,1);
glColor3f(1,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}
void main()
{
glutInitWindowSize(500,500);
glutCreateWindow("Cohen Sutherland");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
4 a) Write a program to handle mouse events. Draw rectangles using 2 points, at top left and
bottom right on mouse left-button clicks. On right click, clear window. Use reshape callback to
avoid distortion on window resize.
#include <glut.h>
int s=25;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,0,0,0);
glFlush();
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,600,600,0);
glMatrixMode(GL_MODELVIEW);
}
void main()
{
glutInitWindowSize(600,600);
glutInitWindowPosition(0,0);
glutCreateWindow("mouse click draw square");
glutDisplayFunc(display);
glutMouseFunc(mouse);
init();
glutMainLoop();
}
4 b. Write a Program to demonstrate the usage of display lists. the list should define
varios objects like square, rectangle, triangle.
#include <stdlib.h>
#include <glut.h>
void init (void)
{ // to draw rectangle
glNewList (1, GL_COMPILE);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 1.0);
glBegin (GL_QUADS);
glVertex2f (-.5,-.5);
glVertex2f (.5,-.5);
glVertex2f (.5,0.2);
glVertex2f (-.5,0.2);
glEnd ();
glEndList ();
// to draw triangle
glNewList (2, GL_COMPILE);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glBegin (GL_TRIANGLES);
glVertex2f (0,0);
glVertex2f (1,0);
glVertex2f (0,1);
glEnd ();
glEndList ();
// to draw square
glNewList (3, GL_COMPILE);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 0.0);
glBegin (GL_QUADS);
glVertex2f (-0.5,-0.5);
glVertex2f (0.5,-0.5);
glVertex2f (0.5,0.5);
glVertex2f (-0.5,0.5);
glEnd ();
glEndList ();
glNewList (4, GL_COMPILE);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0,1,1);
glutSolidTeapot(1);
glEndList ();
}
void display(void)
{
//glClear (GL_COLOR_BUFFER_BIT);
glFlush ();
}
void obj(int i)
{
switch(i)
{
case 2:
glCallList (1);
break;
case 3:
glCallList (2);
break;
case 4:
glCallList (3);
break;
case 5:
glCallList (4);
break;
}
glFlush();
}
void mmenu(int i)
{
switch(i)
{
case 1:exit(0);
}
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
if(w<=h)
gluOrtho2D(-2,2,-2*(float)h/(float)w,2*(float)h/(float)w);
else
gluOrtho2D(-2*(float)w/(float)h,2*(float)w/(float)h,-2,2);
glMatrixMode(GL_MODELVIEW);
}
void main()
{
int smenu1;
glutInitWindowSize(500,500);
glutCreateWindow("Display List");
smenu1=glutCreateMenu(obj);
glutAddMenuEntry("Rectangle",2);
glutAddMenuEntry("Triangle",3);
glutAddMenuEntry("Square",4);
glutAddMenuEntry("Teapot",5);
glutCreateMenu(mmenu);
glutAddMenuEntry("quit",1);
glutAddSubMenu("Objects",smenu1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
}
5 a) Write a program to create a font for the letter S of user input thickness.
#include <glut.h>
#include <math.h>
void ourfont(char c)
{
float i;
float angle;
glColor3f(1,0,0);
glBegin(GL_QUAD_STRIP);
for(i=1;i<=9;i=i+0.5)
{
angle=3.14159/6*i;
glVertex2f(0.4*cos(angle)+0.5,0.4*sin(angle)+0.5);
glVertex2f(0.5*cos(angle)+0.5,0.5*sin(angle)+0.5);
}
glEnd();
glTranslatef(-0.1,0.12,0.0);
glBegin(GL_QUAD_STRIP);
for(i=9;i!=18;i=i+0.5)
{
angle=3.14159/6*i;
glVertex2f(0.4*cos(angle)+0.5,-(0.4*sin(angle)+0.5));
glVertex2f(0.5*cos(angle)+0.5,-(0.5*sin(angle)+0.5));
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
ourfont('s');
glFlush();
}
void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2,2,-2*(GLfloat)h/(GLfloat)w,2*(GLfloat)h/(GLfloat)w,-
10,10);
else
glOrtho(-2*(GLfloat)w/(GLfloat)h,2*(GLfloat)w/(GLfloat)h,-2,2,-
10,10);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void main()
{
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("font S");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1,1,1,1);
glutMainLoop();
}
#include <glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
glRotatef(theta[0], 1,0,0);
glRotatef(theta[1], 0,1,0);
glRotatef(theta[2], 0,0,1);
glColorPointer(3,GL_FLOAT, 0, colors);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
glutSwapBuffers();
}
void spinCube()
{
void main()
{
6 a) Write a program to create a font for the letter O of user input thickness.
#include <glut.h>
#include <math.h>
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
glClearColor(1,1,1,1);
glColor3f(1,0,0);
}
void display()
{
float angle;
float i;
glTranslatef(250,250,0);
glBegin(GL_QUAD_STRIP);
for(i=0;i<=16;i=i+0.5)
{
angle=3.1415/6.0*i+(3.1415/2);
glVertex2f(40*cos(angle),40*sin(angle));
glVertex2f(50*cos(angle),50*sin(angle));
}
glEnd();
glFlush();
}
void main()
{
glutInitWindowSize(500,500);
glutCreateWindow("letter O");
glutDisplayFunc(display);
myinit();
glClearColor(1,1,1,1);
glutMainLoop();
}
6 b)Write a program to draw a color cube and allow the user to move the camera
suitably to experiment with perspective viewing. Use OpenGL functions.
#include <glut.h>
unsigned char
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};
float theta[] = {0,0,0};
int axis = 2;
float viewer[3]={1,1,1};
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2],0,0,0,0,1,0);
glRotatef(theta[0], 1,0,0);
glRotatef(theta[1], 0,1,0);
glRotatef(theta[2], 0,0,1);
glColorPointer(3,GL_FLOAT, 0, colors);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
glutSwapBuffers();
}
void spinCube()
{
void main()
{
#include <glut.h>
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,40,0,40);
glMatrixMode(GL_MODELVIEW);
}
void disp()
{
int x=0,y=0;
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0,0,0,1);
for(y=0;y<8;y++)
{
for(x=0;x<8;x++)
{
if(x%2==y%2) glColor3f(1,1,1);
else glColor3f(0,0,0);
glBegin(GL_POLYGON);
glVertex2f(x*5,y*5);
glVertex2f(x*5,(y+1)*5);
glVertex2f((x+1)*5,(y+1)*5);
glVertex2f((x+1)*5,y*5);
glEnd();
}
}
glFlush();
}
void main()
{
glutInitWindowPosition(0,0);
glutInitWindowSize(800,800);
glutCreateWindow("ChessBoard");
init();
glutDisplayFunc(disp);
glutMainLoop();
}
7 b) Write a program, using OpenGL functions, to demonstrate properties of the light source along
with properties of the surfaces of the solid object
#include <glut.h>
float xv=0,yv=0;
8 a) Write a program to create a font for the letter C of user input thickness.
#include <glut.h>
#include <math.h>
void ourfont(char c)
{
float i;
float angle;
glColor3f(1,0,0);
glBegin(GL_QUAD_STRIP);
for(i=2;i<=10.5;i=i+0.5)
{
angle=3.14159/6*i;
glVertex2f(0.4*cos(angle)+0.5,0.4*sin(angle)+0.5);
glVertex2f(0.5*cos(angle)+0.5,0.5*sin(angle)+0.5);
}
glEnd();
glTranslatef(-0.1,0.12,0.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
ourfont('s');
glFlush();
}
void myreshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
glOrtho(-2,2,-2*(GLfloat)h/(GLfloat)w,2*(GLfloat)h/(GLfloat)w,-
10,10);
else
glOrtho(-2*(GLfloat)w/(GLfloat)h,2*(GLfloat)w/(GLfloat)h,-2,2,-
10,10);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void main()
{
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("font C");
glutReshapeFunc(myreshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor(1,1,1,1);
glutMainLoop();
}
8 b) Program to fill any given polygon using scan-line area filling algorithm.
(Use appropriate data structures.)
#include<stdio.h>
#include<glut.h>
float x1,x2,x3,x4,y1,y2,y3,y4;
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 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++)
{
if(le[y]<=re[y])
for(i=(int)le[y];i<(int)re[y];i++)
draw_pixel(i,y);
}
void display()
{
x1=200;y1=200;x2=100;y2=300;x3=200;y3=400;x4=300;y4=300;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,1);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
glFlush();
}
void myinit()
{
glClearColor(1,1,1,1);
glColor3f(1,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
}
void main()
{
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Filling a Polygon using Scan-line Algorithm");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}
9 a) Write a program to create a rotating wheel
#include <glut.h>
#include <math.h>
float t=0,t1=360;
float xc=0,yc=0,r=0.5,i=-0.5;
void disp()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,1);
glColor3f(0,0,1);
for(t=0;t<360;t=t+0.001)
{
glBegin(GL_POINTS);
glVertex2f((xc+i+r*cos(t)),(yc+r*sin(t)));
glEnd();
}
glBegin(GL_LINES);
glVertex2f(i+cos(t1)/2,sin(t1)/2);
glVertex2f(-cos(t1)/2+i,-sin(t1)/2);
glVertex2f(sin(t1)/2+i,-cos(t1)/2);
glVertex2f(-sin(t1)/2+i,cos(t1)/2);
glEnd();
glFlush();
}
void idle()
{
if(i<1)
i=i+0.05;
else
i=-0.5;
t1-=0.01;
disp();
}
void main()
{
glutInitWindowPosition(0,0);
glutInitWindowSize(800,800);
glutCreateWindow("wheel rotation");
glutDisplayFunc(disp);
glutIdleFunc(idle);
glutMainLoop();
}
9 b)Write a program to draw a square. Use popup menu with options to quit or resize the square.
The resize option has submenu with increase size and decrease size as options. The increase size
must double the size of the square and the decrease size option must reduce the size by 2. (half
size) . Min size is 1.
#include <glut.h>
float s=5;
int submenu;
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-40,40,-40,40);
glMatrixMode(GL_MODELVIEW);
}
void disp()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,1);
glColor3f(0,0,1);
glBegin(GL_POLYGON);
glVertex2f(+s,+s);
glVertex2f(-s,+s);
glVertex2f(-s,-s);
glVertex2f(+s,-s);
glEnd();
glFlush();
}
void s_menu(int id)
{
switch(id)
{
case 1: s=s*2;
disp();
break;
case 2: s=s/2;
disp();
break;
}
}
void m_menu(int id)
{
if(id==3)
exit(0);
}
void main()
{
glutInitWindowPosition(0,0);
glutInitWindowSize(800,800);
glutCreateWindow("Menu");
init();
glutDisplayFunc(disp);
submenu=glutCreateMenu(s_menu);
glutAddMenuEntry("Increase",1);
glutAddMenuEntry("Decrease",2);
glutCreateMenu(m_menu);
glutAddMenuEntry("Quit",3);
glutAddSubMenu("Resize",submenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
10 a) Write a program to display a hierarchical menu as below using popup menu. When an option
is selected, display the option on output window.
#include <stdio.h>
#include <glut.h>
void disp()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void demomenu(int id)
{
switch(id)
{
case 1:
exit(0);
}
}
void sizemenu(int id)
{
switch(id)
{
case 2:
printf("normal\n");
break;
case 3:
printf("print\n");
break;
case 4:
printf("web\n");
break;
}
}
void editmenu(int id)
{
switch(id)
{
case 5:
printf("cut\n");
break;
case 6:
printf("copy\n");
break;
case 7:
printf("paste\n");
break;
case 8:
printf("cgv\n");
break;
}
}
void main()
{
int submenu1,submenu2;
glutInitWindowPosition(20,20);
glutInitWindowSize(500,500);
glutCreateWindow("just menu");
submenu1=glutCreateMenu(sizemenu);
glutAddMenuEntry("normal",2);
glutAddMenuEntry("print",3);
glutAddMenuEntry("web",4);
submenu2=glutCreateMenu(editmenu);
glutAddMenuEntry("cut",5);
glutAddMenuEntry("copy",6);
glutAddMenuEntry("paste",7);
glutAddMenuEntry("cgv",8);
glutCreateMenu(demomenu);
glutAddMenuEntry("quit",1);
glutAddSubMenu("view",submenu1);
glutAddSubMenu("edit",submenu2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(disp);
glutMainLoop();
}
10 b) Write a program to create a house like figure and rotate it about a given fixed point using
OpenGL functions.
#include <stdio.h>
#include <math.h>
#include < glut.h>
float house[3][9]={{100.0,100.0,175.0,250.0,250.0,150.0,150.0,200.0,200.0},
{100.0,300.0,400.0,300.0,100.0,100.0,150.0,150.0,100.0},
{1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}};
float rot_mat[3][3]={{0},{0},{0}};
float result[3][9]={{0}, {0}, {0}};
float h=100.0; // Pivot point
float k=100.0;
float theta;
void multiply()
{ // Rotation MATRIX and Object Matrix => Resultant Transformed House
int i,j,l;
for(i=0;i<3;i++)
for(j=0;j<9;j++)
{
result[i][j]=0;
for(l=0;l<3;l++)
result[i][j]=result[i][j]+rot_mat[i][l]*house[l][j];
}
}
void rotate()
{
float m,n; // Build the rotation matrix
m=-h*(cos(theta)-1)+k*(sin(theta));
n=-k*(cos(theta)-1)-h*(sin(theta));
rot_mat[0][0]=cos(theta);
rot_mat[0][1]=-sin(theta);
rot_mat[0][2]=m;
rot_mat[1][0]=sin(theta);
rot_mat[1][1]=cos(theta);
rot_mat[1][2]=n;
rot_mat[2][0]=0;
rot_mat[2][1]=0;
rot_mat[2][2]=1;
//multiply the two matrices: Rotation Matrix * Objet Matrix(house)
multiply();
}
void drawhouse(float house[3][9])
{ glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][0],house[1][0]);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][3],house[1][3]);
glVertex2f(house[0][4],house[1][4]);
glEnd();
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][5],house[1][5]);
glVertex2f(house[0][6],house[1][6]);
glVertex2f(house[0][7],house[1][7]);
glVertex2f(house[0][8],house[1][8]);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(house[0][1],house[1][1]);
glVertex2f(house[0][2],house[1][2]);
glVertex2f(house[0][3],house[1][3]);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawhouse(house);
rotate();
drawhouse(result);
glFlush();
}
void myinit()
{
glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-500,500,-500,500);
glMatrixMode(GL_MODELVIEW);
}
void main()
{
printf("Enter the rotation angle\n");
scanf("%f", &theta);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("house rotation");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}