-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
255 lines (213 loc) · 6.96 KB
/
main.cpp
File metadata and controls
255 lines (213 loc) · 6.96 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Alunos:
// Leonardo Ferreira da Cunha
// Lucas de Jesus Santos Moura
#include <glut.h>
#include <string>
#include <vector>
const float PI = 3.14159265359;
float anguloRotacaoSol = 0.0;
float anguloOrbitaMercurio = 0.0;
float anguloOrbitaVenus = 0.0;
float anguloOrbitaTerra = 0.0;
float anguloOrbitaLua = 0.0;
float anguloOrbitaMarte = 0.0;
float anguloOrbitaJupiter = 0.0;
float anguloOrbitaSaturno = 0.0;
float anguloOrbitaUrano = 0.0;
float anguloOrbitaNetuno = 0.0;
struct PontoDeVista {
float eyeX, eyeY, eyeZ;
float centerX, centerY, centerZ;
float upX, upY, upZ;
std::string label;
};
std::vector<PontoDeVista> cameras = {
{0.0f, 250.0f, 300.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, "Vista Padrao"},
{0.0f, 600.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, "Vista Superior"},
{0.0f, 50.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, "Vista Proxima"},
};
int camIndex = 0;
void init(void);
void redimensionar(int w, int h);
void configurarCamera();
void desenharOrbita(float raio);
void desenharPlaneta(float raio, float distancia, float angulo, float r, float g, float b);
void desenharTexto(float x, float y, std::string texto);
void desenharCena(void);
void anima(int value);
void teclado(unsigned char key, int x, int y);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Sistema Solar");
init();
glutDisplayFunc(desenharCena);
glutReshapeFunc(redimensionar);
glutKeyboardFunc(teclado);
glutTimerFunc(16, anima, 0);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
}
void redimensionar(int w, int h)
{
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Raz�o de aspecto (aspect ratio) da janela
float fAspect = (float)w / (float)h;
gluPerspective(65.0, fAspect, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void configurarCamera() {
PontoDeVista cam = cameras[camIndex];
gluLookAt(cam.eyeX, cam.eyeY, cam.eyeZ,
cam.centerX, cam.centerY, cam.centerZ,
cam.upX, cam.upY, cam.upZ);
}
void desenharOrbita(float raio) {
glColor3f(0.3f, 0.3f, 0.3f);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 100; i++) {
float theta = 2.0f * PI * float(i) / float(100);
float x = raio * cosf(theta);
float z = raio * sinf(theta);
glVertex3f(x, 0, z);
}
glEnd();
}
void desenharPlaneta(float raio, float distancia, float angulo, float r, float g, float b) {
desenharOrbita(distancia);
glPushMatrix();
glRotatef(angulo, 0.0f, 1.0f, 0.0f);
glTranslatef(distancia, 0.0f, 0.0f);
glColor3f(r, g, b);
glutWireSphere(raio, 10, 10);
glPopMatrix();
}
void desenharTexto(float x, float y, std::string texto) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 800, 0, 600);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(x, y);
for (char c : texto) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void desenharCena(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
configurarCamera();
// SOL
glColor3f(1.0, 1.0, 0.0);
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
glRotatef(anguloRotacaoSol, 0.0, 0.0, 1.0);
glutWireSphere(20.0, 20, 20);
glPopMatrix();
// MERC�RIO
desenharPlaneta(5.0, 30.0, anguloOrbitaMercurio, 0.6, 0.6, 0.6);
// V�NUS
desenharPlaneta(5.5, 50.0, anguloOrbitaVenus, 1.0, 0.5, 0.0);
// TERRA
desenharOrbita(70.0);
glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
glRotatef(anguloOrbitaTerra, 0.0, 1.0, 0.0);
glTranslatef(70.0, 0.0, 0.0);
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
glutWireSphere(6.0, 10, 10);
glPopMatrix();
// LUA
glColor3f(0.8, 0.8, 0.8);
glPushMatrix();
glRotatef(anguloOrbitaLua, 0.0, 1.0, 0.0);
glTranslatef(5.0, 0.0, 0.0);
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
glutWireSphere(2.5, 8, 8);
glPopMatrix();
glPopMatrix();
glPopMatrix();
// MARTE
desenharPlaneta(5.5, 90.0, anguloOrbitaMarte, 1.0, 0.0, 0.0);
// JUPITER
desenharPlaneta(7.0, 120.0, anguloOrbitaJupiter, 0.8, 0.5, 0.2);
// SATURNO
desenharOrbita(150.0);
glColor3f(0.9, 0.7, 0.3);
glPushMatrix();
glRotatef(anguloOrbitaSaturno, 0.0, 1.0, 0.0);
glTranslatef(150.0, 0.0, 0.0);
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
glutWireSphere(6.0, 12, 12);
glPopMatrix();
// ANEL
glColor3f(0.6, 0.6, 0.6);
glPushMatrix();
glRotatef(90.0, 1.0, 0.0, 0.0);
glutWireTorus(1.5, 7.0, 10, 20);
glPopMatrix();
glPopMatrix();
// URANO
desenharPlaneta(5.5, 170.0, anguloOrbitaUrano, 0.5, 0.8, 0.8);
// NETUNO
desenharPlaneta(5.0, 200.0, anguloOrbitaNetuno, 0.0, 0.8, 0.8);
desenharTexto(10, 35, "Camera: " + cameras[camIndex].label);
desenharTexto(10, 10, "Aperte SPACE para alterar a camera");
glutSwapBuffers();
}
void anima(int value)
{
anguloRotacaoSol += 0.01;
anguloOrbitaMercurio += 2.0;
anguloOrbitaVenus += 1.5;
anguloOrbitaTerra += 1.0;
anguloOrbitaLua += 3.0;
anguloOrbitaMarte += 0.8;
anguloOrbitaJupiter += 0.5;
anguloOrbitaSaturno += 0.3;
anguloOrbitaUrano += 0.2;
anguloOrbitaNetuno += 0.15;
if (anguloOrbitaMercurio > 360.0f) anguloOrbitaMercurio -= 360.0f;
if (anguloOrbitaVenus > 360.0f) anguloOrbitaVenus -= 360.0f;
if (anguloOrbitaTerra > 360.0f) anguloOrbitaTerra -= 360.0f;
if (anguloOrbitaLua > 360.0f) anguloOrbitaLua -= 360.0f;
if (anguloOrbitaMarte > 360.0f) anguloOrbitaMarte -= 360.0f;
if (anguloOrbitaJupiter > 360.0f) anguloOrbitaJupiter -= 360.0f;
if (anguloOrbitaSaturno > 360.0f) anguloOrbitaSaturno -= 360.0f;
if (anguloOrbitaUrano > 360.0f) anguloOrbitaUrano -= 360.0f;
if (anguloOrbitaNetuno > 360.0f) anguloOrbitaNetuno -= 360.0f;
glutPostRedisplay();
glutTimerFunc(16, anima, 0);
}
void teclado(unsigned char key, int x, int y) {
if (key == 27) exit(0);
if (key == ' ') {
camIndex++;
if (camIndex >= cameras.size()) camIndex = 0;
glutPostRedisplay();
}
}