1. Program to implement openGL primitives(points, lines, triangle, triangle strip, triangle fan, polygon,
quad)
#include "stdafx.h"
#include <GL\glut.h>
void points(void);
void lines(void);
void triangle(void);
void triangle_strip(void);
void triangle_fan(void);
void quads(void);
void poly(void);
void init(void);
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("MCA426:Point Plotting");
init();
glutDisplayFunc(points);
glutCreateWindow("MCA426:Triangle Strip Plotting");
init();
glutDisplayFunc(triangle_strip);
glutCreateWindow("MCA426:Triangle Fan Plotting");
init();
glutDisplayFunc(triangle_fan);
glutCreateWindow("MCA426:Triangle Plotting");
init();
glutDisplayFunc(triangle);
glutCreateWindow("MCA426:Line Plotting");
init();
glutDisplayFunc(lines);
glutCreateWindow("MCA426:Quad Plotting");
init();
glutDisplayFunc(quads);
glutCreateWindow("MCA426:polygon Plotting");
init();
glutDisplayFunc(poly);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(1.0f,1.0f,1.0f,1.0f);
glColor3f(0.0,0.0,1.0);
glPointSize(10.0);
gluOrtho2D(0,600,0,400);
}
void points(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glColor3f(0.0,0.0,1.0);
glVertex2f(200.0,100.0);
glVertex2f(300.0,100.0);
glVertex2f(400.0,200.0);
glVertex2f(300.0,300.0);
glVertex2f(200.0,300.0);
glVertex2f(100.0,200.0);
glEnd();
glutSwapBuffers();
}
void lines(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(1.0,0.0,0.0);
glVertex2f(200.0,100.0);
glVertex2f(300.0,100.0);
glVertex2f(400.0,200.0);
glVertex2f(300.0,300.0);
glVertex2f(200.0,300.0);
glVertex2f(100.0,200.0);
glEnd();
glutSwapBuffers();
}
void triangle(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(200.0,100.0);
glVertex2f(350.0,100.0);
glVertex2f(430.0,180.0);
glVertex2f(280.0,200.0);
glVertex2f(400.0,200.0);
glVertex2f(180.0,130.0);
glEnd();
glutSwapBuffers();
}
void triangle_strip(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0.0,1.0,0.0);
//v6,v5,v4
glVertex2f(115.0,47.0);
glVertex2f(154.0,114.0);
glVertex2f(196.0,50.0);
glColor3f(1.0,0.0,0.0);
//v5,v4,v3
glVertex2f(154.0,114.0);
glVertex2f(196.0,50.0);
glVertex2f(236.0,117.0);
glColor3f(0.0,0.0,1.0);
//v4,v3,v2
glVertex2f(196.0,50.0);
glVertex2f(236.0,117.0);
glVertex2f(275.0,46.0);
glColor3f(1.0,1.0,0.0);
//v3,v2,v1
glVertex2f(236.0,117.0);
glVertex2f(275.0,46.0);
glVertex2f(306.0,121.0);
glColor3f(1.0,.0,1.0);
//v2,v1,v0
glVertex2f(275.0,46.0);
glVertex2f(306.0,121.0);
glVertex2f(335.0,46.0);
glEnd();
glutSwapBuffers();
}
void triangle_fan(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
/* Draw points */
glColor3f(0.0,0.0,1.0);
glVertex2f(130.0,73.0);
glVertex2f(293.0,55.0);
glVertex2f(290.0,95.0);
glColor3f(0.0,1.0,0.0);
glVertex2f(130.0,73.0);
glVertex2f(290.0,95.0);
glVertex2f(284.0,136.0);
glColor3f(1.0,0.0,0.0);
glVertex2f(130.0,73.0);
glVertex2f(284.0,136.0);
glVertex2f(256.0,177.0);
glColor3f(0.0,1.0,0.0);
glVertex2f(130.0,73.0);
glVertex2f(256.0,177.0);
glVertex2f(220.0,204.0);
glEnd();
glutSwapBuffers();
}
void quads(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0,0.0,1.0);
glVertex2f(119.0,89.0);
glVertex2f(111.0,116.0);
glVertex2f(152.0,139.0);
glVertex2f(154.0,75.0);
glColor3f(1.0,0.0,1.0);
glVertex2f(231.0,79.0);
glVertex2f(274.0,168.0);
glVertex2f(328.0,141.0);
glVertex2f(319.0,80.0);
glEnd();
glutSwapBuffers();
}
void poly(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(200.0,100.0);
glVertex2f(400.0,100.0);
glVertex2f(400.0,100.0);
glVertex2f(480.0,200.0);
glVertex2f(480.0,200.0);
glVertex2f(400.0,300.0);
glVertex2f(400.0,300.0);
glVertex2f(200.0,300.0);
glVertex2f(200.0,300.0);
glVertex2f(100.0,200.0);
glVertex2f(200.0,100.0);
glVertex2f(400.0,100.0);
glEnd();
glutSwapBuffers();
}
MCA426:Triangle
Strip
Plotting
-
7
zz
MCA426:polygon
Plotting
-
OE
MCA426:Triangle
Fan
Plotting
-
7
MCA426:Quad
Plotting
-
a
1. Draw the front/ perspective view of your home using primitives.
#include "stdafx.h"
#include<gl\glut.h>
void init(void);
void display(void);
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("MCA426:HOME");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
glLineWidth(3.0);
gluOrtho2D(0,600,0,400);
}
void display(void)
{
glBegin(GL_QUADS);
glColor3f(0.5,0.5,0.0);
glVertex2f(75.0,65.0);
glVertex2f(409.0,65.0);
glVertex2f(409.0,207.0);
glVertex2f(75.0,207.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.5,0.0);
glVertex2f(238.0,56.0);
glVertex2f(334.0,56.0);
glVertex2f(334.0,207.0);
glVertex2f(238.0,207.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(346.0,106.0);
glVertex2f(346.0,176.0);
glVertex2f(384.0,176.0);
glVertex2f(384.0,106.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(182.0,125.0);
glVertex2f(220.0,125.0);
glVertex2f(220.0,170.0);
glVertex2f(182.0,170.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(95.0,126.0);
glVertex2f(132.0,126.0);
glVertex2f(132.0,171.0);
glVertex2f(95.0,171.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(339.0,200.0);
glVertex2f(339.0,196.0);
glVertex2f(234.0,196.0);
glVertex2f(234.0,200.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
glVertex2f(235.0,207.0);
glVertex2f(235.0,213.0);
glVertex2f(340.0,213.0);
glVertex2f(340.0,207.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.5,0.0);
glVertex2f(239.0,214.0);
glVertex2f(335.0,214.0);
glVertex2f(335.0,339.0);
glVertex2f(239.0,339.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(268.0,255.0);
glVertex2f(305.0,255.0);
glVertex2f(305.0,325.0);
glVertex2f(268.0,325.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
glVertex2f(234.0,340.0);
glVertex2f(234.0,347.0);
glVertex2f(340.0,347.0);
glVertex2f(340.0,340.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.5,0.5,0.0);
glVertex2f(75.0,339.0);
glVertex2f(239.0,339.0);
glVertex2f(239.0,217.0);
glVertex2f(75.0,217.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,1.0);
glVertex2f(67.0,339.0);
glVertex2f(232.0,339.0);
glVertex2f(239.0,352.0);
glVertex2f(67.0,352.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glVertex2f(234.0,347.0);
glVertex2f(339.0,347.0);
glVertex2f(314.0,371.0);
glVertex2f(261.0,371.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glVertex2f(88.0,373.0);
glVertex2f(220.0,373.0);
glVertex2f(239.0,352.0);
glVertex2f(66.0,352.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,1.0);
glVertex2f(74.0,207.0);
glVertex2f(74.0,216.0);
glVertex2f(238.0,216.0);
glVertex2f(238.0,207.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(220.0,315.0);
glVertex2f(180.0,315.0);
glVertex2f(180.0,266.0);
glVertex2f(220.0,266.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(109.0,315.0);
glVertex2f(163.0,315.0);
glVertex2f(163.0,217.0);
glVertex2f(109.0,217.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glVertex2f(163.0,315.0);
glVertex2f(163.0,217.0);
glVertex2f(141.0,217.0);
glVertex2f(141.0,300.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0,0.0,0.0);
glVertex2f(259.0,169.0);
glVertex2f(313.0,169.0);
glVertex2f(313.0,65.0);
glVertex2f(259.0,65.0);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glVertex2f(313.0,169.0);
glVertex2f(290.0,154.0);
glVertex2f(290.0,65.0);
glVertex2f(313.0,65.0);
glEnd();
glBegin(GL_LINES);
glColor3f(0.0,0.0,0.0);
glVertex2f(70.0,218.0);
glVertex2f(70.0,254.0);
glVertex2f(70.0,254.0);
glVertex2f(240.0,254.0);
glVertex2f(70.0,218.0);
glVertex2f(240.0,218.0);
glVertex2f(240.0,254.0);
glVertex2f(240.0,218.0);
glVertex2f(144.0,254.0);
glVertex2f(144.0,218.0);
glVertex2f(70.0,218.0);
glVertex2f(144.0,254.0);
glVertex2f(70.0,254.0);
glVertex2f(144.0,218.0);
glVertex2f(144.0,218.0);
glVertex2f(240.0,254.0);
glVertex2f(240.0,218.0);
glVertex2f(144.0,254.0);
glEnd();
glutSwapBuffers();
}
Program to draw lines of different slopes (+,-.0,1,∞ etc)using DDA line drawing algorithm
// line1.cpp : Defines the entry point for the console application.
//
// line.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// HOME.cpp : Defines the entry point for the console application.
//
// GLTRIANGLES.cpp : Defines the entry point for the console application.
//
#include<math.h>
#include<gl\glut.h>
void init(void);
void display(void);
void dda(int,int,int,int);
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("MCA426:LINE USING DDA");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
gluOrtho2D(0,600,0,400);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glColor3f(1.0,0.0,0.0);
dda(112,250,10,386);
dda(350,390,200,350);
dda(148,61,254,167);
dda(130,68,12,186);
glEnd();
glutSwapBuffers();
}
void dda(int x0,int y0,int xend,int yend)
{
int x=x0,y=y0;
int dx=xend-x0;
int dy=yend-y0,steps,k;
float xinc,yinc;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/steps;
yinc=dy/steps;
glBegin(GL_POINTS);
x=int (x+0.5);
y=int (y+0.5);
glVertex2f(x,y);
for(k=0;k<steps;k++)
{
x=x+xinc;
y=y+yinc;
x=int (x+0.5);
y=int (y+0.5);
glVertex2f(x,y);
}
}
Program to draw lines of different slopes (+,-.0,1,∞ etc)using
Bresenham’s line drawing algorithm
// line.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// HOME.cpp : Defines the entry point for the console application.
//
// GLTRIANGLES.cpp : Defines the entry point for the console application.
//
#include<math.h>
#include<gl\glut.h>
void init(void);
void display(void);
void dda(int,int,int,int);
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("MCA426:BRESENHAMS LINE DRAWING");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(10.0);
gluOrtho2D(0,600,0,400);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
dda(0,0,0,0);
glutSwapBuffers();
}
void dda(int x1,int y1,int x2,int y2)
{
int x=x1,y=y1;
int dx=x2-x1;
int dy=y2-y1;
int d=2*dy-dx;
glBegin(GL_LINES);
glVertex2f(112.0,250.0);
glVertex2f(10.0,386.0);
glVertex2f(446.0,248.0);
glVertex2f(553.0,391.0);
glVertex2f(250.0,100.0);
glVertex2f(100.0,100.0);
glVertex2f(500.0,50.0);
glVertex2f(500.0,150.0);
while(x<=x2)
{
x++;
if(d<0)
d=d+2*dy;
else
{
d=d+2*(dy-dx);
y=y+1;
}
}
glEnd();
}
Program to draw concentric circles using midpoint circle algorithm
// circle1.cpp : Defines the entry point for the console application.
//
// line.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// HOME.cpp : Defines the entry point for the console application.
//
// GLTRIANGLES.cpp : Defines the entry point for the console application.
//
#include<math.h>
#include<gl\glut.h>
void init(void);
void display();
void midpoint(int);
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
//glutCreateWindow("point plotting");
glutCreateWindow("MCA426_CIRCLE USING MIDPOINT ALGORITHM");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(4.0);
gluOrtho2D(0,600,0,400);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
midpoint(10);
midpoint(40);
midpoint(60);
midpoint(80);
midpoint(100);
midpoint(120);
glColor3f(1.0,0.0,0.0);
glutSwapBuffers();
}
void midpoint(int r)
{
int x=0,y=r;
int d=1-r;
while(x<=y)
{
glBegin(GL_POINTS);
glVertex2f(x+200,y+200);
glVertex2f(x+200,-y+200);
glVertex2f(-x+200,y+200);
glVertex2f(-x+200,-y+200);
glVertex2f(y+200,x+200);
glVertex2f(y+200,-x+200);
glVertex2f(-y+200,x+200);
glVertex2f(-y+200,-x+200);
glEnd();
if(d<0)
d=d+2*x+3;
else
{
d=d+2*(x-y)+5;
y--;
}
x++;
}
}
Program to draw circle using Bresenham’s circle drawing algorithm.
// circle2.cpp : Defines the entry point for the console application.
//
// circle1.cpp : Defines the entry point for the console application.
//
// line.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// HOME.cpp : Defines the entry point for the console application.
//
// GLTRIANGLES.cpp : Defines the entry point for the console application.
//
#include<math.h>
#include<gl\glut.h>
void init(void);
void display();
void midpoint(int);
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
//glutCreateWindow("point plotting");
glutCreateWindow("MCA426_BRESENHAM'S CIRCLE");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(4.0);
gluOrtho2D(0,600,0,400);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
midpoint(50);
glColor3f(1.0,0.0,0.0);
glutSwapBuffers();
}
void midpoint(int r)
{
int x=0,y=r;
int d=3-2*r;
while(x<=y)
{
glBegin(GL_POINTS);
glVertex2f(x+200,y+200);
glVertex2f(x+200,-y+200);
glVertex2f(-x+200,y+200);
glVertex2f(-x+200,-y+200);
glVertex2f(y+200,x+200);
glVertex2f(y+200,-x+200);
glVertex2f(-y+200,x+200);
glVertex2f(-y+200,-x+200);
glEnd();
if(d<0)
d=d+4*x+6;
else
{
d=d+4*(x-y)+10;
y--;
}
x++;
}
}
Program to implement basic transformations
// tran.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<gl/glut.h>
#include<stdio.h>
float tx=0.0f; //for translation
float ty=0.0f;
float angle=180.0f; //for rotation
float sx=1.0f;
float sy=1.0f;
float sz=1.0f;
void init();
void update(int);
void init()
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}
void display()
{
init();
//TRANSORMATIONS..........
/*glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
glVertex3f(0.3f,-0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();*/
glTranslatef(tx,ty,0.0);
glRotatef(angle,0.0,0.0,1.0);
glScalef(sx,sy,sz);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
//glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.3f,-0.3f,0.0f);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.3f,0.3f,0.0f);
glEnd();
glutSwapBuffers();
}
void rotate()
{
init();
//TRANSORMATIONS..........
/*glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
glVertex3f(0.3f,-0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();*/
glTranslatef(0.5,0.0,0.0);
glRotatef(angle,0.0,0.0,1.0);
glScalef(sx,sy,sz);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
//glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.3f,-0.3f,0.0f);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.3f,0.3f,0.0f);
glEnd();
glutSwapBuffers();
}
void scaling()
{
init();
//TRANSORMATIONS..........
/*glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
glVertex3f(0.3f,-0.3f,0.0f);
glVertex3f(0.0f,0.3f,0.0f);
glEnd();*/
glTranslatef(0.5,0.0,0.0);
glRotatef(angle,0.0,0.0,1.0);
glScalef(sx,sy,sz);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
//glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.3f,-0.3f,0.0f);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.3f,0.3f,0.0f);
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
if(x!= 0 || y!= 0)
switch(key)
{
case 'r': angle-=5;
break;
case 'R': angle+=5;
break;
case 'T': tx=tx+.2;
break;
case 't': ty=ty+.2;
break;
case 'M': tx=tx-.2;
break;
case 'm': ty=ty-.2;
break;
case 'S': angle=angle+10;
break;
case 's': angle=angle-10;
break;
case 'd': sx=sx+2;
sy=sy+2;
sz=sz+2;
break;
case 'e': sx=sx-2;
sy=sy-2;
sz=sz-2;
break;
}
}
void update(int value)
{
angle+=2;
if(angle>360)
angle=0;
glutTimerFunc(5,update,0);
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutCreateWindow("MCA426_Transformation1");
glutDisplayFunc(display);
// glutCreateWindow("MCA427_Transformation1");
// glutDisplayFunc(scaling);
// glutCreateWindow("MCA427_Transformation1");
// glutDisplayFunc(rotate);
glutKeyboardFunc(keyboard);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
MCA426_
TRANSFORMATION
-
©
MCA426
TRANSLATION
-
©
Program to rotate two triangles in opposite directions (implementation of Keyboard event)
// TIMERTRANS.cpp : Defines the entry point for the console application.
//
// transform.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<gl/glut.h>
float tx=0.0F;//translation
float ty=0.0F;
float angle=180.0F;//rotation
float sx=0.0F;//for scaling
float sy=0.0F;
float sz=0.0F;
void init();
void update(int);
void init()
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}
void display()
{
init();
//glTranslatef(0.6,0.0,0.0);
glRotatef(angle,0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.3f,-0.3f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.3f,0.3f,0.0f);
glEnd();
glLoadIdentity();
glTranslatef(0.0,0.8,0.0);
glRotatef(-angle,0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.3f,-0.3f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.3f,-0.3f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.3f,0.3f,0.0f);
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case 'r':
//angle-=5;
glutTimerFunc(50,update,0);
break;
case 'R':
angle+=5;
break;
}
}
void update(int value)
{
angle+=2;
if (angle>360)
angle=0;
glutTimerFunc(5,update,0);
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutCreateWindow("MCA426_OPPOSITE ROTATION");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(display);
glutMainLoop();
glutTimerFunc(50,update,0);
return 0;
}
Program to animate a triangle (implementation of timer)
// blaaaaa.cpp : Defines the entry point for the console application.
//
// TIMERTRANS.cpp : Defines the entry point for the console application.
//
// transform.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<Windows.h>
#include<stdio.h>
#include<gl/glut.h>
float tx=0.0F;//translation
float ty=0.0F;
float angle=0.0F;//rotation
float sx=0.0F;//for scaling
float sy=0.0F;
float sz=0.0F;
int s=5;
void init();
void update(int);
void init()
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLineWidth(5);
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}
void display()
{
init();
//glTranslatef(0.6,0.0,0.0);
glRotatef(angle,0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,1.0f);
glVertex2f(0.0f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex2f(-0.5f,0.1f);
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(-0.5f,-0.1f);
glEnd();
glLoadIdentity();
//glTranslatef(0.0,0.8,0.0);
glRotatef(angle,0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,1.0f);
glVertex2f(0.0f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex2f(0.5f,0.1f);
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(0.5f,-0.1f);
glEnd();
glLoadIdentity();
glRotatef(angle,0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,1.0f);
glVertex2f(0.0f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex2f(0.1f,0.5f);
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(-0.1f,0.5f);
glEnd();
glLoadIdentity();
glBegin(GL_LINES);
glColor3f(0.0f,0.0f,0.0f);
glVertex2f(0.0,0.0);
glVertex2f(0.0,-0.8);
glEnd();
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case 'r':
//angle-=5;
//s+=2;
//update(s);
glutTimerFunc(5,update,0);
break;
case 's':
//angle+=5;
//break;
Sleep(5000);
break;
case 't':
//glutTimerFunc(5,update,0);
s-=2;
update(s);
break;
}
}
void update(int value)
{
angle+=s;
if (angle>360)
angle=0;
glutTimerFunc(5,update,0);
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400,400);
glutCreateWindow("MCA426_FAN");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(display);
glutMainLoop();
//update(50);
glutTimerFunc(5,update,0);
return 0;
}
Program to draw and animate a cube with different colors on its sides (implementation of special keys)
// cube.cpp : Defines the entry point for the console application.
//
// blaaaaa.cpp : Defines the entry point for the console application.
//
// TIMERTRANS.cpp : Defines the entry point for the console application.
//
// transform.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<gl/glut.h>
float tx=0.0F;//translation
float ty=0.0F;
float angle=180.0F;//rotation
float sx=0.0F;//for scaling
float sy=0.0F;
float sz=0.0F;
int rx=0;
int ry=0;
int rz=0;
void init();
void update(int);
void init()
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}
void display()
{
init();
//glTranslatef(0.6,0.0,0.0);
glRotatef(rx,1.0,0.0,0.0);
glRotatef(ry,0.0,1.0,0.0);
glRotatef(rz,0.0,0.0,1.0);
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.5f,0.5f,-0.5f);
//glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-0.5f,0.5f,-0.5f);
//glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-0.5f,0.5f,0.5f);
//glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.5f,0.5f,0.5f);
//glEnd();
//glLoadIdentity();
//glTranslatef(0.0,0.8,0.0);
//glRotatef(angle,0.0,0.0,1.0);
//glBegin(GL_POLYGON);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.5f,-0.5f,0.5f);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-0.5f,-0.5f,0.5f);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-0.5f,-0.5f,-0.5f);
//glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.5f,-0.5f,-0.5f);
//glEnd();
//glLoadIdentity();
//glRotatef(angle,0.0,0.0,1.0);
//glBegin(GL_POLYGON);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.5f,0.5f,0.5f);
//glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.5f,0.5f,0.5f);
// glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-0.5f,-0.5f,0.5f);
//glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.5f,-0.5f,0.5f);
//glEnd();
//glLoadIdentity();
//glRotatef(angle,0.0,0.0,1.0);
// glBegin(GL_POLYGON);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.5f,-0.5f,-0.5f);
//glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-0.5f,-0.5f,-0.5f);
//glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-0.5f,0.5f,-0.5f);
//glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.5f,0.5f,-0.5f);
//glEnd();
//glLoadIdentity();
//glRotatef(angle,0.0,0.0,1.0);
// glBegin(GL_POLYGON);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.5f,0.5f,0.5f);
//glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.5f,0.5f,-0.5f);
//glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.5f,-0.5f,-0.5f);
//glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.5f,-0.5f,0.5f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(0.5f,0.5f,-0.5f);
//glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.5f,0.5f,0.5f);
//glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.5f,-0.5f,0.5f);
//glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.5f,-0.5f,-0.5f);
glEnd();
//glLoadIdentity();
glutSwapBuffers();
}
void specialkey(int key,int x,int y)
{
if(key==GLUT_KEY_RIGHT)
ry+=5;
//glutTimerFunc(5,update,0);
else if(key==GLUT_KEY_LEFT)
ry-=5;
//glutTimerFunc(5,update,0);
else if(key==GLUT_KEY_UP)
rx+=5;
//glutTimerFunc(5,update,0);
else if(key==GLUT_KEY_DOWN)
rx-=5;
//glutTimerFunc(5,update,0);
glutPostRedisplay();
}
void update(int value)
{
angle+=2;
if (angle>360)
angle=0;
glutTimerFunc(5,update,0);
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("MCA426_CUBE");
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
glEnable(GL_DEPTH_TEST);
glutIdleFunc(display);
glutMainLoop();
//update(50);
glutTimerFunc(5,update,0);
return 0;
}
ce
MCA426_CUBE
MCA426_CUBE
oa
oa
Program to animate a pyramid (implementation of mouse
event)
#include "stdafx.h"
#include<Windows.h>
#include<GL\glut.h>
#include<stdio.h>
float angle=180.0f;//for rotaton
//float tx=0.0f;float ty=0.0f;float tz=0.0f;
float sx=1.0;float sy=1.0;float sz=1.0;
void display();
void init()
{
glClearColor(1.0f,1.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}
void update(int value)
{
angle=angle+1.0;
glutTimerFunc(5,update,0);
}
void display(void)
{
init();
glScalef(sx,sy,sz);
//glTranslatef(tx,ty,tz);
glRotatef(angle,0.0,1.0,0.0);
glBegin(GL_TRIANGLES);
glColor3f( 0.0, 0.0, 1.0);
glVertex3f( 0.0,0.0,0.0 );
glVertex3f( -0.5,-0.5,0.5 );
glVertex3f( 0.5,-0.5,0.5 );
glEnd();
//glScalef(sx,sy,sz);
//glRotatef(angle,0.0,1.0,0.0);
//glTranslatef(tx,ty,tz);
glBegin(GL_TRIANGLES);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 0.0,0.0,0.0 );
glVertex3f( -0.5,-0.5,-0.5 );
glVertex3f( -0.5,-0.5,0.5);
glEnd();
//glScalef(sx,sy,sz);
//glRotatef(angle,0.0,1.0,0.0);
//glTranslatef(tx,ty,tz);
glBegin(GL_TRIANGLES);
glColor3f( 0.0, 1.0, 1.0 );
glVertex3f( 0.0,0.0,0.0 );
glVertex3f( -0.5,-0.5,-0.5 );
glVertex3f( 0.5,-0.5,-0.5 );
glEnd();
//glScalef(sx,sy,sz);
//glTranslatef(tx,ty,tz);
//glRotatef(angle,0.0,1.0,0.0);
glBegin(GL_TRIANGLES);
glColor3f( 1.0, 1.0, 0.0 );
glVertex3f( 0.0,0.0,0.0 );
glVertex3f( 0.5,-0.5,0.5 );
glVertex3f( 0.5,-0.5,-0.5);
glEnd();
//glScalef(sx,sy,sz);
//glTranslatef(tx,ty,tz);
//glRotatef(angle,0.0,1.0,0.0);
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( -0.5,-0.5,-0.5 );
glVertex3f( -0.5,-0.5,0.5 );
glVertex3f( 0.5,-0.5,0.5 );
glVertex3f( 0.5,-0.5,-0.5 );
glEnd();
glutSwapBuffers();
}
void mouse(int b,int s,int x,int y)
{
if(b==GLUT_LEFT_BUTTON && s==GLUT_DOWN)
{
sx=sx-0.5;
sy=sy-0.5;
sz=sz-0.5;
}
if(b==GLUT_LEFT_BUTTON && s==GLUT_UP)
{
sx=sx+0.5;
sy=sy+0.5;
sz=sz+0.5;
}
if(b==GLUT_RIGHT_BUTTON && s==GLUT_DOWN)
{
//tx=tx+0.3;
//ty=ty+0.3;
//tz=tz+0.3;
glRotatef(angle,0.0,1.0,0.0);
glutTimerFunc(5,update,0);
}
if(b==GLUT_RIGHT_BUTTON && s==GLUT_UP)
{
//tx=tx-0.3;
//ty=ty-0.3;
//tz=tz-0.3;
Sleep(1000);
//glRotatef(-angle,0.0,1.0,0.0);
//glutTimerFunc(5,update,0);
}
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutInitWindowPosition(50,50);
glutCreateWindow("MCA426:PYRAMID");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutIdleFunc(display);
//glutTimerFunc(5,update,0);
glutMainLoop();
return 0;
}