-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeD.cpp
More file actions
118 lines (102 loc) · 3.28 KB
/
ThreeD.cpp
File metadata and controls
118 lines (102 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<GL/glut.h>
#include<string.h>
#include<stdio.h>
#include<iostream>
float OrignalCube [8][4]={{-50,50,50,1},{50,50,50,1},{50,50,-50,1},{-50,50,-50,1},{-50,-50,50,1},{50,-50,50,1},{50,-50,-50,1},{-50,-50,-50}};
static float t_x=0, t_y=0, t_z=0;
void drawCube(float Cube[8][4])
{
glClear(GL_COLOR_BUFFER_BIT);//clearing Screen
glClearColor(0.4f, 0.4f, 0.4f, 0.8f);
glBegin(GL_QUADS);//drawing the cube
glColor4f(0.0,0.0,0.4,0.3);
glVertex3f(Cube[3][0],Cube[3][1],Cube[3][2]);//back surface
glVertex3f(Cube[2][0],Cube[2][1],Cube[2][2]);
glVertex3f(Cube[6][0],Cube[6][1],Cube[6][2]);
glVertex3f(Cube[7][0],Cube[7][1],Cube[7][2]);
glColor4f(0.4,0.0,0.0,0.3);
glVertex3f(Cube[4][0],Cube[4][1],Cube[4][2]);//bottom surface
glVertex3f(Cube[5][0],Cube[5][1],Cube[5][2]);
glVertex3f(Cube[6][0],Cube[6][1],Cube[6][2]);
glVertex3f(Cube[7][0],Cube[7][1],Cube[7][2]);
glColor4f(0.0,0.4,0.0,0.3);
glVertex3f(Cube[0][0],Cube[0][1],Cube[0][2]);//left surface
glVertex3f(Cube[4][0],Cube[4][1],Cube[4][2]);
glVertex3f(Cube[7][0],Cube[7][1],Cube[7][2]);
glVertex3f(Cube[3][0],Cube[3][1],Cube[3][2]);
glColor4f(0.0,0.0,0.4,0.3);
glVertex3f(Cube[0][0],Cube[0][1],Cube[0][2]);//front surface
glVertex3f(Cube[1][0],Cube[1][1],Cube[1][2]);
glVertex3f(Cube[5][0],Cube[5][1],Cube[5][2]);
glVertex3f(Cube[4][0],Cube[4][1],Cube[4][2]);
glColor4f(0.4,0.0,0.0,0.3);
glVertex3f(Cube[0][0],Cube[0][1],Cube[0][2]);//top surface
glVertex3f(Cube[1][0],Cube[1][1],Cube[1][2]);
glVertex3f(Cube[2][0],Cube[2][1],Cube[2][2]);
glVertex3f(Cube[3][0],Cube[3][1],Cube[3][2]);
glColor4f(0.0,0.4,0.0,0.3);
glVertex3f(Cube[1][0],Cube[1][1],Cube[1][2]);//right surface
glVertex3f(Cube[5][0],Cube[5][1],Cube[5][2]);
glVertex3f(Cube[6][0],Cube[6][1],Cube[6][2]);
glVertex3f(Cube[2][0],Cube[2][1],Cube[2][2]);
glEnd();
glFinish();
}
void translate(float l, float m, float n)
{
float TranslateMatrix [4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{l,m,n,1}}, ReturnMatrix[8][4];
for (int i=0;i<4;i++)
{
for(int j=0;j<8;j++)
{
float sum=0;
for (int k=0;k<4;k++)
{
sum=sum+(TranslateMatrix[i][k]*OrignalCube[k][j]);
}
ReturnMatrix[i][j]=sum;
}
}
drawCube(ReturnMatrix[8][4])
}
void myKey(unsigned char key, int x, int y)
{
if (key==97)//move left with 'a'
translate(--t_x, t_y,t_z);
else if (key==119)//move up with 'w'
translate(t_x,--t_y,t_z);
else if (key==100)//move right with 'd'
translate(++t_x,t_y,t_z);
else if (key==115)//move down with 's'
translate(t_x,++t_y,t_z);
else if (key=113)
translate(t_x,t_y,++t_z);
else if(key=101)
translate(t_x,t_y,--t_z);
}
void display()
{
const char *str2="Shubhankar Nath R134214062";
int j = strlen( str2 );
glColor3f( 0.9, 0.5, 1.0 );
glRasterPos2f(-280, 280 );
for( int i = 0; i < j; i++ )
{
glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24, str2[i] );
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(80, 80);
glutInitWindowSize(600, 600);
glutCreateWindow("3D");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-300,300,-300,300,300,-300);
gluLookAt(3.0, 4.0, 5.0,0.0, 0.0, 0.0,0.0, 1.0, 0.);
glutDisplayFunc(display);
glutKeyboardFunc(myKey);
glutMainLoop();
}