0% found this document useful (0 votes)
25 views14 pages

CG Lab

Cg Lab Fr study
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views14 pages

CG Lab

Cg Lab Fr study
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Program 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

You might also like