Skip to main content

3D Cube Transformations

 

Group C  

Practical 6 b  

Problem Statement :  Write C++ program to draw 3-D cube and perform following transformations on it using OpenGL i) Scaling ii) Translation iii) Rotation about an axis (X/Y/Z).

Check Out Code Here 👇

#include<iostream>
#include<math.h>
#include<GL/glut.h>
using namespace std;
typedef float Matrix4 [4][4];
Matrix4 theMatrix;
static GLfloat input[8][3]=
{
{40,40,-50},{90,40,-50},{90,90,-50},{40,90,-50},
{30,30,0},{80,30,0},{80,80,0},{30,80,0}
};
float output[8][3];
float tx,ty,tz;
float sx,sy,sz;
float angle;
int choice,choiceRot;
void setIdentityM(Matrix4 m)
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
m[i][j]=(i==j);
}
void translate(int tx,int ty,int tz)
{
for(int i=0;i<8;i++)
{
output[i][0]=input[i][0]+tx;
output[i][1]=input[i][1]+ty;
output[i][2]=input[i][2]+tz;
}
}
void scale(int sx,int sy,int sz)
{
theMatrix[0][0]=sx;
theMatrix[1][1]=sy;
theMatrix[2][2]=sz;
}
void RotateX(float angle) //Parallel to x
{
angle = angle*3.142/180;
theMatrix[1][1] = cos(angle);
theMatrix[1][2] = -sin(angle);
theMatrix[2][1] = sin(angle);
theMatrix[2][2] = cos(angle);
}
void RotateY(float angle) //parallel to y
{
angle = angle*3.14/180;
theMatrix[0][0] = cos(angle);
theMatrix[0][2] = -sin(angle);
theMatrix[2][0] = sin(angle);
theMatrix[2][2] = cos(angle);
}
void RotateZ(float angle) //parallel to z
{
angle = angle*3.14/180;
theMatrix[0][0] = cos(angle);
theMatrix[0][1] = sin(angle);
theMatrix[1][0] = -sin(angle);
theMatrix[1][1] = cos(angle);
}
void multiplyM()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
output[i][j]=0;
for(int k=0;k<3;k++)
{
output[i][j]=output[i][j]+input[i][k]*theMatrix[k][j];
}
}
}
}
void Axes(void)
{
glColor3f (0.0, 0.0, 0.0); // Set the color to BLACK
glBegin(GL_LINES); // Plotting X-Axis
glVertex2s(-1000 ,0);
glVertex2s( 1000 ,0);
glEnd();
glBegin(GL_LINES); // Plotting Y-Axis
glVertex2s(0 ,-1000);
glVertex2s(0 , 1000);
glEnd();
}
void draw(float a[8][3])
{
glBegin(GL_QUADS);
glColor3f(0.7,0.4,0.5); //behind
glVertex3fv(a[0]);
glVertex3fv(a[1]);
glVertex3fv(a[2]);
glVertex3fv(a[3]);
glColor3f(0.8,0.2,0.4); //bottom
glVertex3fv(a[0]);
glVertex3fv(a[1]);
glVertex3fv(a[5]);
glVertex3fv(a[4]);
glColor3f(0.3,0.6,0.7); //left
glVertex3fv(a[0]);
glVertex3fv(a[4]);
glVertex3fv(a[7]);
glVertex3fv(a[3]);
glColor3f(0.2,0.8,0.2); //right
glVertex3fv(a[1]);
glVertex3fv(a[2]);
glVertex3fv(a[6]);
glVertex3fv(a[5]);
glColor3f(0.7,0.7,0.2); //up
glVertex3fv(a[2]);
glVertex3fv(a[3]);
glVertex3fv(a[7]);
glVertex3fv(a[6]);
glColor3f(1.0,0.1,0.1);
glVertex3fv(a[4]);
glVertex3fv(a[5]);
glVertex3fv(a[6]);
glVertex3fv(a[7]);
glEnd();
}
void init()
{
glClearColor(1.0,1.0,1.0,1.0); //set backgrond color to white
glOrtho(-454.0,454.0,-250.0,250.0,-250.0,250.0);
// Set the no. of Co-ordinates along X & Y axes and their gappings
glEnable(GL_DEPTH_TEST);
// To Render the surfaces Properly according to their depths
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
Axes();
glColor3f(1.0,0.0,0.0);
draw(input);
setIdentityM(theMatrix);
switch(choice)
{
case 1:
translate(tx,ty,tz);
break;
case 2:
scale(sx,sy,sz);
multiplyM();
break;
case 3:
switch (choiceRot) {
case 1:
RotateX(angle);
break;
case 2: RotateY(angle);
break;
case 3:
RotateZ(angle);
break;
default:
break;
}
multiplyM();
break;
}
draw(output);
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(1362,750);
glutInitWindowPosition(0,0);
glutCreateWindow("3D TRANSFORMATIONS");
init();
cout<<"Enter your choice number:\n1.Translation\n2.Scaling\n3.Rotation\n=>";
cin>>choice;
switch (choice) {
case 1:
cout<<"\nEnter Tx,Ty &Tz: \n";
cin>>tx>>ty>>tz;
break;
case 2:
cout<<"\nEnter Sx,Sy & Sz: \n";
cin>>sx>>sy>>sz;
break;
case 3:
cout<<"Enter your choice for Rotation about axis:\n1.parallel to X-axis."
<<"(y& z)\n2.parallel to Y-axis.(x& z)\n3.parallel to Z-axis."
<<"(x& y)\n =>";
cin>>choiceRot;
switch (choiceRot) {
case 1:
cout<<"\nENter Rotation angle: ";
cin>>angle;
break;
case 2:
cout<<"\nENter Rotation angle: ";
cin>>angle;
break;
case 3:
cout<<"\nENter Rotation angle: ";
cin>>angle;
break;
default:
break;
}
break;
default:
break;
}
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

Comments

Popular posts from this blog

DSL A1

Hello Friends lets See the first practical of Data Structure Laboratory(DSL) of SPPU Second Year. Some of the students are afraid of coding as it is new to all of us. So we are here to conquer your fear. You will find it easy as it explained by students only. So lets Get into it The first practical of part A is : In second year computer engineering class, group A student’s play cricket, group B students play badminton and group C students play football. Write a Python program using functions to compute following: -  a) List of students who play both cricket and badminton  b) List of students who play either cricket or badminton but not both  c) Number of students who play neither cricket nor badminton  d) Number of students who play cricket and football but not badminton.  (Note- While realizing the group, duplicate entries should be avoided, Do not use SET built-in functions)  So most of us know the SET theory in mathematics. The above problem is very easy...

OOP 2

Object Oriented Programming Group A - Practical : 2 Problem Statement :  Develop a program in C++ to create a database of student’s information system containing the following information: Name, Roll number, Class, Division, Date of Birth, Blood group, Contact address, Telephone number, Driving license no. and other. Construct the database with suitable member functions. Make use of constructor, default constructor, copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete as well as exception handling. Check Out Code Here  👇 Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

Bouncing Ball

Group B   Practical 4 a   Problem Statement :   Write a C++ program to implement bouncing ball using sine wave form. Apply the concept of polymorphism. Check Out Code Here  👇 Outputs :  Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊