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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
| #include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
typedef struct{float x;float y;}Point2D;
typedef struct{float x;float y;float z;}Point3D;
int d = 600;
GLfloat vlist[8][4];
void init(void)
{
vlist[0][0] = 140;
vlist[0][1] = 270;
vlist[0][2] = 100;
vlist[1][0] = 220;
vlist[1][1] = 270;
vlist[1][2] = 100;
vlist[2][0] = 260;
vlist[2][1] = 200;
vlist[2][2] = 100;
vlist[3][0] = 220;
vlist[3][1] = 130;
vlist[3][2] = 100;
vlist[4][0] = 140;
vlist[4][1] = 130;
vlist[4][2] = 100;
vlist[5][0] = 100;
vlist[5][1] = 200;
vlist[5][2] = 100;
vlist[6][0] = 140;
vlist[6][1] = 270;
vlist[6][2] = 300;
vlist[7][0] = 220;
vlist[7][1] = 270;
vlist[7][2] = 300;
vlist[8][0] = 260;
vlist[8][1] = 200;
vlist[8][2] = 300;
vlist[9][0] = 220;
vlist[9][1] = 130;
vlist[9][2] = 300;
vlist[10][0] = 140;
vlist[10][1] = 130;
vlist[10][2] = 300;
vlist[11][0] = 100;
vlist[11][1] = 200;
vlist[11][2] = 300;
}
void rotateObj_x(float deg_x){ // rotate the Object about a line which is parallel to X axis
float ang = deg_x * 3.14159265359 / 180.0; //angle in radians
for(int i=0; i<12; i++){
float tmpx = vlist[i][0];
float tmpy = vlist[i][1];
float tmpz = vlist[i][2];
vlist[i][0] = tmpx;
vlist[i][2] = tmpz*cos(ang) - tmpy*sin(ang);
vlist[i][1] = tmpz*sin(ang) + tmpy*cos(ang);
}
glutPostRedisplay(); //this will call the function "myDisplay()" again
}
void rotateObj_y(float deg_y){ // rotate the Object about a line which is parallel to Y axis
float ang = deg_y * 3.14159265359 / 180.0;
for(int i=0; i<12; i++){
float tmpx = vlist[i][0];
float tmpy = vlist[i][1];
float tmpz = vlist[i][2];
vlist[i][1] = tmpy;
vlist[i][0] = tmpx*cos(ang) - tmpz*sin(ang);
vlist[i][2] = tmpx*sin(ang) + tmpz*cos(ang);
}
glutPostRedisplay(); //this will call the function "myDisplay()" again
}
Point2D ProjectXY(float x, float y, float z){ //Perspective projection on XY plane with COP is at (180,200,-d)
Point2D p0;
d= abs(d);
p0.x = (x*d)/(d-z);
p0.y = (y*d)/(d-z);
return p0;
}
void drawObj(Point2D A,Point2D B,Point2D C,Point2D D, Point2D E,Point2D F, Point2D G,Point2D H, Point2D I,Point2D J,Point2D K, Point2D L)
{
glPointSize(1.0);
glLineWidth(2.0);
//Draw front
glBegin(GL_LINE_LOOP);
glVertex2f(A.x,A.y);glVertex2f(B.x,B.y);glVertex2f(C.x,C.y);glVertex2f(D.x,D.y);glVertex2f(E.x,E.y);glVertex2f(F.x,F.y);
glEnd();
//Draw BACK
glBegin(GL_LINE_LOOP);
glVertex2f(G.x,G.y),glVertex2f(H.x,H.y);glVertex2f(I.x,I.y);glVertex2f(J.x,J.y);glVertex2f(K.x,K.y);glVertex2f(L.x,L.y);
glEnd();
//Connect Lines
glBegin(GL_LINES);
glVertex2f(A.x,A.y);glVertex2f(G.x,G.y);
glVertex2f(B.x,B.y);glVertex2f(H.x,H.y);
glVertex2f(C.x,C.y);glVertex2f(I.x,I.y);
glVertex2f(D.x,D.y);glVertex2f(J.x,J.y);
glVertex2f(E.x,E.y);glVertex2f(K.x,K.y);
glVertex2f(F.x,F.y);glVertex2f(L.x,L.y);
glEnd();
glFlush();
}
void myDisplay()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
Point2D pro_A = ProjectXY(vlist[0][0],vlist[0][1],vlist[0][2]);
Point2D pro_B = ProjectXY(vlist[1][0],vlist[1][1],vlist[1][2]);
Point2D pro_C = ProjectXY(vlist[2][0],vlist[2][1],vlist[2][2]);
Point2D pro_D = ProjectXY(vlist[3][0],vlist[3][1],vlist[3][2]);
Point2D pro_E = ProjectXY(vlist[4][0],vlist[4][1],vlist[4][2]);
Point2D pro_F = ProjectXY(vlist[5][0],vlist[5][1],vlist[5][2]);
Point2D pro_G = ProjectXY(vlist[6][0],vlist[6][1],vlist[6][2]);
Point2D pro_H = ProjectXY(vlist[7][0],vlist[7][1],vlist[7][2]);
Point2D pro_I = ProjectXY(vlist[8][0],vlist[8][1],vlist[8][2]);
Point2D pro_J = ProjectXY(vlist[9][0],vlist[9][1],vlist[9][2]);
Point2D pro_K = ProjectXY(vlist[10][0],vlist[10][1],vlist[10][2]);
Point2D pro_L = ProjectXY(vlist[11][0],vlist[11][1],vlist[11][2]);
drawObj(pro_A,pro_B,pro_C,pro_D,pro_E,pro_F,pro_G,pro_H,pro_I,pro_J,pro_K,pro_L);
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q')
exit(EXIT_SUCCESS);
if (key == '+')
{
if(d > 0 && d < 2000){
d++;
glutPostRedisplay();
}
}
if (key == '-')
{
if(d > 0 && d < 2000){
d--;
glutPostRedisplay();
}
}
}
void special(int key, int, int) {
switch (key) {
case GLUT_KEY_LEFT: rotateObj_y(-1); break; // decreasing the angle by 1 degree
case GLUT_KEY_RIGHT: rotateObj_y(1);break; // increasing the angle by 1 degree
case GLUT_KEY_UP: rotateObj_x(-1);break; // decreasing the angle by 1 degree
case GLUT_KEY_DOWN: rotateObj_x(1);break; // increasing the angle by 1 degree
default: return;
}
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "Hexagonal Prism " );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
init();
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutDisplayFunc( myDisplay );
glutMainLoop();
return( 0 );
}
|
Comments
Post a Comment