-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspacedef.c
318 lines (235 loc) · 8.14 KB
/
spacedef.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#ifndef SPACE_DEF_GUARD_H
#ifdef _WIN32
#include <windows.h>
#endif
#include"spacedef.h"
#include"mathHelp.h"
#include"texture.h"
#define SPACE_NUM_STARS 800000
/**
* Coordenada mínima de aparição das estrelas em função da origem.
*/
#define STAR_MIN_RANGE -10.5
/**
* Coordenada mínima de aparição das estrelas em função da origem.
*/
#define STAR_MAX_RANGE 60.5
/**
* Velocidade mínima das estrelas
*/
#define STAR_MIN_SPEED .01
/**
* Velocidade máxima das estrelas
*/
#define STAR_MAX_SPEED .02
typedef struct star_s{
coord_t coord;
color_t color;
GLdouble speed;
}star_t;
#endif
static star_t G_stars[SPACE_NUM_STARS];
static GLdouble G_daysPerFrame = SPACE_DAYS_PER_FRAME;
static GLdouble G_sizeScale = SPACE_SIZE_SCALE;
static GLdouble G_distanceScale = SPACE_DISTANCE_SCALE;
/**
* Desenha uma esfera. Esta função não colore, rotaciona ou translada a esfera.
* @param radius Raio da esfera
*/
static void spaceDrawSphere(GLdouble radius){
GLUquadricObj *q = gluNewQuadric();
assert(q != NULL);
gluQuadricDrawStyle(q, GLU_FILL);
gluQuadricNormals(q, GLU_SMOOTH);
gluQuadricTexture(q, GL_TRUE);
gluSphere(q, radius, SPACE_SPHERE_DETAIL, SPACE_SPHERE_DETAIL);
gluDeleteQuadric(q);
}
/**
* Realiza as transformações no objeto, como translação, rotação, desenhar na tela, etc. As cores
* @param object Objeto a ser movido. Não pode ser NULL.
*/
void spaceTransformObject(object_t *object){
assert(object != NULL);
object -> currentSelfRotation += object -> selfRotationAngularSpeed * G_daysPerFrame;
BOUND_GLDOUBLE_ANGLE(object -> currentSelfRotation);
object -> currentRotation += object -> angularSpeed * G_daysPerFrame;
BOUND_GLDOUBLE_ANGLE(object -> currentRotation);
glPushMatrix();
glTranslated(0., 0., 0.);
if(object -> centerOfRotation != NULL){
/*
GLdouble difX = object -> centerOfRotation -> x - object -> coord.x;
GLdouble difY = object -> centerOfRotation -> y - object -> coord.y;
GLdouble euclidianDistance = sqrt(difX * difX + difY * difY);
object -> coord.x = euclidianDistance * sin(object -> currentRotation * M_PI / 180.);
object -> coord.y = euclidianDistance * cos(object -> currentRotation * M_PI / 180.);
*/
glTranslated(object -> centerOfRotation -> x * G_distanceScale,
object -> centerOfRotation -> y * G_distanceScale,
object -> centerOfRotation -> z * G_distanceScale);
glRotated(object -> currentRotation, .0, .0, 1.0);
glTranslated(-object -> centerOfRotation -> x * G_distanceScale,
-object -> centerOfRotation -> y * G_distanceScale,
-object -> centerOfRotation -> z * G_distanceScale);
}
glRotated(object -> currentSelfRotation, 0.0, 0.0, 1.0); // rotaciona o objeto em torno de si mesmo
glTranslated(object -> coord.x * G_distanceScale,
object -> coord.y * G_distanceScale,
object -> coord.z * G_distanceScale);
if(object -> texture.hasTexture){
glEnable(GL_TEXTURE_2D);
glBindTexture(object -> texture.target, object -> texture.textureId);
}
else
glDisable(GL_TEXTURE_2D);
glColor4ub(object -> color.r, object -> color.g, object -> color.b, object -> color.a);
spaceDrawSphere(object -> radius * G_sizeScale);
glPopMatrix();
}
/**
* Atribui as cores ao objeto;
* @param object a receber as cores.
* @param r Cor vermelha de 0 a 255.
* @param g Cor verde de 0 a 255.
* @param b Cor azul de 0 a 255.
* @param a Transparência de 0 a 255.
*/
void spaceObjectSetColor(object_t *object, GLubyte r, GLubyte g, GLubyte b, GLubyte a){
assert(object != NULL);
object -> color.r = r;
object -> color.g = g;
object -> color.b = b;
object -> color.a = a;
}
/**
* Libera da memória todos os recursos deste objeto.
* @param object Objeto a ser liberado. Não pode ser NULL.
* @return Sempre NULL.
*/
object_t *spaceFreeObject(object_t *object){
assert(object != NULL);
free(object);
return NULL;
}
/**
* Atribui uma cor para este objeto emitir.
* @param object Objecto a receber o atributo. Não pode ser NULL.
* @param isLightSource {@code 0} se não for um emissor de luz, {@code 1} caso contrário.
* @param r Tonalidade vermelha de emissão.
* @param g Tonalidade verde de emissão.
* @param b Tonalidade azul de emissão.
* @param radius Raio de alcance da luz.
*/
void spaceObjectSetLightSource(object_t *object, GLbyte isLightSource, GLubyte r, GLubyte g, GLubyte b, GLdouble radius){
assert(object != NULL);
object -> light.isLightSource = isLightSource;
object -> light.lightColor.r = r;
object -> light.lightColor.g = g;
object -> light.lightColor.b = b;
object -> light.radius = radius;
}
/**
* @param object Objeto a ser posicionado.
* @param x Posição X deste objeto.
* @param y Posição Y deste objeto.
* @param z Posição Z deste objeto.
*/
void spaceObjectSetCoord(object_t *object, GLdouble x, GLdouble y, GLdouble z){
assert(object != NULL);
object -> coord.x = x;
object -> coord.y = y;
object -> coord.z = z;
}
/**
* Inicializa uma estrela.
* @param i estrela a ser inicializada
*/
static void initStar(size_t i){
G_stars[i].coord.x = STAR_MIN_RANGE + (rand() % (GLint) (STAR_MAX_RANGE * 100.)) / 100.;
G_stars[i].coord.y = STAR_MIN_RANGE + (rand() % (GLint) (STAR_MAX_RANGE * 100.)) / 100.;
G_stars[i].coord.z = STAR_MIN_RANGE + (rand() % (GLint) (STAR_MAX_RANGE * 100.)) / 100.;
G_stars[i].color.r = 200 + rand() % 56;
G_stars[i].color.g = 200 + rand() % 56;
G_stars[i].color.b = 200 + rand() % 56;
G_stars[i].color.a = 200 + rand() % 56;
G_stars[i].speed = STAR_MIN_SPEED + rand() % (GLint)(STAR_MAX_SPEED * 100.) / 100.;
}
/**
* Inicializa todas as estrelas
*/
void spaceInitStars(){
size_t i;
for(i = 0; i < SPACE_NUM_STARS; i++)
initStar(i);
}
/**
* Desenha estrelas próximo a câmera do espectador
*/
void spaceDrawStars(){
size_t i;
glPushMatrix();
glPointSize(1.);
glBegin(GL_POINTS);
for(i = 0; i < SPACE_NUM_STARS; i++){
if(G_stars[i].coord.x < STAR_MIN_RANGE || G_stars[i].coord.y < STAR_MIN_RANGE || G_stars[i].coord.z < STAR_MIN_RANGE ||
G_stars[i].coord.x > STAR_MAX_RANGE || G_stars[i].coord.y > STAR_MAX_RANGE || G_stars[i].coord.z > STAR_MAX_RANGE )
initStar(i);
G_stars[i].coord.y += G_stars[i].speed / 10.;
glColor4ub(G_stars[i].color.r, G_stars[i].color.g, G_stars[i].color.b, G_stars[i].color.a);
glVertex3d(G_stars[i].coord.x, G_stars[i].coord.y, G_stars[i].coord.z);
}
glEnd();
glPopMatrix();
}
/**
* Atribui uma textura para o objeto especificado. O que de fato acontecerá é a chamada da função
* {@code glBindTexture()}, portanto a textura já deve ter sido preparada antes de chamar a função
* {@link #spaceTransformObject(object_t *)}
* @param object Objeto que vai receber a textura. Não pode ser NULL.
* @para target Primeiro argumento da função {@code glBindTexture()}.
* @param textureId Segundo argumento da função {@code glBindTexture()}.
*/
void spaceObjectSetTexture(object_t *object, GLenum target, GLuint textureId){
assert(object != NULL);
object -> texture.target = target;
object -> texture.textureId = textureId;
object -> texture.hasTexture = 1;
}
/**
* Configura o objeto para que tenha a textura ignorada, tendo ou não uma textura.
* @param object Objeto que não terá textura. Não pode ser NULL.
*/
void spaceObjectNoTexture(object_t *object){
assert(object != NULL);
object -> texture.hasTexture = 0;
}
/**
* Altera a escala de distância entre os planetas
* @param sizeScale Escala que multiplicará a distância dos objetos.
*/
void spaceSetDistanceScale(GLdouble distanceScale){
G_distanceScale = distanceScale;
}
/**
* Altera a escala de tamanho entre os planetas
* @param sizeScale Escala que multiplicará o tamanho dos objetos.
*/
void spaceSetSizeScale(GLdouble sizeScale){
G_sizeScale = sizeScale;
}
/**
* Quantos dias deve passar para cada frame renderizado. Valores maiores representará movimentos maiores.
* @param daysPerFrame Escala que multiplicará o tamanho dos objetos.
*/
void spaceSetDaysPerFrame(GLdouble daysPerFrame){
G_daysPerFrame = daysPerFrame;
}
/**
* Retorna a escala de distância atual.
* @return Escala multiplicadora das distâncias dos objetos.
* @see #spaceSetDistanceScale(GLdouble)
*/
GLdouble spaceGetDistanceScale(){
return G_distanceScale;
}