-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAnswer7.cpp
144 lines (115 loc) · 3.55 KB
/
Answer7.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
int flag = 0;
int dir = 1;
int thres_angle = 30;
float speed = 1.0;
void changeSize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(60.0f, ratio, 0.1f, 100.0f);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
float angle = 0.0f;
void renderScene(void)
{
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(0.0f, 15.0f, 30.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glRotatef(30, 0.0f, 1.0f, 0.0f);
glLineWidth(5.0f);
glColor3f(1.0, 0.7, 0.0);
glBegin(GL_LINES);
glVertex3f(-8.0f, 0.0f, 3.0f);
glVertex3f(-8.0f, 10.0f, 0.0f);
glVertex3f(-8.0f, 0.0f, -3.0f);
glVertex3f(-8.0f, 10.0f, 0.0f);
glVertex3f(8.0f, 0.0f, 3.0f);
glVertex3f(8.0f, 10.0f, 0.0f);
glVertex3f(8.0f, 0.0f, -3.0f);
glVertex3f(8.0f, 10.0f, 0.0f);
glVertex3f(-8.0f, 10.0f, 0.0f);
glVertex3f(8.0f, 10.0f, 0.0f);
glEnd();
glColor3f(0.0, 0.7, .8);
glTranslatef(0.0f, 10.0f, 0.0f);
glRotatef(angle, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, -10.0f, 0.0f);
glLineWidth(5.0f);
glBegin(GL_LINES);
glVertex3f(-1.3f, 10.0f, 0.0f);
glVertex3f(-1.3f, 4.0f, 0.0f);
glVertex3f(1.3f, 10.0f, 0.0f);
glVertex3f(1.3f, 4.0f, 0.0f);
glVertex3f(-1.3f, 4.0f, 0.0f);
glVertex3f(-1.3f, 2.0f, -0.8f);
glVertex3f(-1.3f, 4.0f, 0.0f);
glVertex3f(-1.3f, 2.0f, 0.8f);
glVertex3f(1.3f, 4.0f, 0.0f);
glVertex3f(1.3f, 2.0f, -0.8f);
glVertex3f(1.3f, 4.0f, 0.0f);
glVertex3f(1.3f, 2.0f, 0.8f);
glColor3f(0.9, 0.9, 0.9);
glVertex3f(-1.5f, 2.0f, -1.0f);
glVertex3f(1.5f, 2.0f, -1.0f);
glVertex3f(1.5f, 2.0f, 1.0f);
glVertex3f(-1.5f, 2.0f, 1.0f);
glVertex3f(-1.5f, 2.0f, 1.0f);
glVertex3f(-1.5f, 2.0f, -1.0f);
glVertex3f(1.5f, 2.0f, 1.0f);
glVertex3f(1.5f, 2.0f, -1.0f);
glEnd();
if(flag == 1) {
if (thres_angle == 0) flag = 0;
if (angle >= thres_angle && dir == 1) { thres_angle--; dir = -1; speed *= 0.95;}
if(angle <= -thres_angle && dir == -1) { thres_angle--; dir = 1; speed *= 0.95;}
angle = angle + ((float) dir * speed);
printf("%f %f\n", angle, speed);
}
glutSwapBuffers();
}
void Timer(int value) {
glutTimerFunc(1000, Timer, 0);
glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y) {
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
flag = 1;
}
}
int main(int argc, char **argv)
{
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(1920, 1080);
glutCreateWindow("Swing Animation");
// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutMouseFunc(mouse);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}