1 #include <stdlib.h>
2 #include <math.h>
3 #include "lights.h"
4 #include "bbox_tree.h"
5 #include "map.h"
6 #include "multiplayer.h"
7 #include "shadows.h"
8 #include "weather.h"
9 #ifdef CLUSTER_INSIDES
10 #include "cluster.h"
11 #endif
12 #ifdef EXTRA_DEBUG
13 #include "errors.h"
14 #endif
15 #include "eye_candy_wrapper.h"
16 #ifdef OPENGL_TRACE
17 #include "gl_init.h"
18 #endif
19 #include "elconfig.h"
20 #include "sky.h"
21 #include "draw_scene.h"
22 
23 
24 #ifdef DEBUG_TIME
25 const float debug_time_accel = 120.0f;
26 #endif
27 
28 #ifdef DEBUG_TIME
29 Uint64 old_time = 0;
30 #endif
31 
32 typedef struct
33 {
34 	float x;
35 	float y;
36 	float z;
37 	float w;
38 }sun;
39 
40 GLfloat global_diffuse_light[GLOBAL_LIGHTS_NO][4];
41 
42 GLfloat sky_lights_c1[GLOBAL_LIGHTS_NO*2][4];
43 GLfloat sky_lights_c2[GLOBAL_LIGHTS_NO*2][4];
44 GLfloat sky_lights_c3[GLOBAL_LIGHTS_NO*2][4];
45 GLfloat sky_lights_c4[GLOBAL_LIGHTS_NO*2][4];
46 
47 int	show_lights;
48 int	num_lights;	// the highest light number loaded
49 light *lights_list[MAX_LIGHTS];
50 unsigned char light_level=58;
51 sun sun_pos[360];
52 sun sun_show[181];
53 
54 short game_minute = 0;
55 short game_second = 0;
56 unsigned char freeze_time = 0;
57 
test_point_visible(float x,float y,float z)58 int test_point_visible(float x,float y,float z)
59 {
60 	double MV[16];
61 	double PROJ[16];
62 	int viewp[4];
63 	double winx,winy,winz;
64 	float z_value;
65 
66 	glGetDoublev(GL_MODELVIEW_MATRIX,&MV[0]);
67 	glGetDoublev(GL_PROJECTION_MATRIX,&PROJ[0]);
68 	glGetIntegerv(GL_VIEWPORT,&viewp[0]);
69 
70 	gluProject(x,y,z,&MV[0],&PROJ[0],&viewp[0],&winx,&winy,&winz);
71 	glReadPixels(winx,winy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z_value);
72 #ifdef OPENGL_TRACE
73 CHECK_GL_ERRORS();
74 #endif //OPENGL_TRACE
75 
76 	if (winz<z_value)
77 		return 1;
78 	else 	return 0;
79 }
80 
81 int	max_enabled;
disable_local_lights()82 void disable_local_lights()
83 {
84 	max_enabled= -1;
85 
86     glDisable(GL_LIGHT0);
87     glDisable(GL_LIGHT1);
88     glDisable(GL_LIGHT2);
89     glDisable(GL_LIGHT3);
90 #ifdef OPENGL_TRACE
91 CHECK_GL_ERRORS();
92 #endif //OPENGL_TRACE
93 }
94 
enable_local_lights()95 void enable_local_lights()
96 {
97 	if(show_lights < 0) return;
98 	if (show_lights != max_enabled)	max_enabled= show_lights;
99     glEnable(GL_LIGHT0);
100     if(show_lights >= 1)	glEnable(GL_LIGHT1);
101     if(show_lights >= 2)	glEnable(GL_LIGHT2);
102     if(show_lights >= 3)	glEnable(GL_LIGHT3);
103 #ifdef OPENGL_TRACE
104 CHECK_GL_ERRORS();
105 #endif //OPENGL_TRACE
106 }
107 
draw_lights()108 void draw_lights()
109 {
110 	unsigned int i, j, l, start, stop;
111 	VECTOR4 vec4;
112 #ifdef CLUSTER_INSIDES_OLD
113 	short cluster = get_actor_cluster ();
114 #endif
115 
116 	if(show_lights <0){
117 		if(max_enabled >= 0){
118 			disable_local_lights();
119 		}
120 		return;
121 	} else if(max_enabled != show_lights){
122         enable_local_lights();
123 	}
124 	if(max_enabled >= 0 && show_lights != max_enabled)	enable_local_lights();
125 
126 	j= 0;
127 
128 	get_intersect_start_stop(main_bbox_tree, TYPE_LIGHT, &start, &stop);
129 	for(i=start; i<stop; i++)
130 	{
131 		l= get_intersect_item_ID(main_bbox_tree, i);
132 		// and make sure it's a valid light
133 		if ((l >= MAX_LIGHTS) || !lights_list[l]
134 #ifdef CLUSTER_INSIDES_OLD
135 		   || (lights_list[l]->cluster && lights_list[l]->cluster != cluster)
136 #endif
137 		)
138 		{
139 #ifdef EXTRA_DEBUG
140 			ERR();
141 #endif
142 			continue;
143 		}
144 		vec4[0] = lights_list[l]->pos_x;
145 		vec4[1] = lights_list[l]->pos_y;
146 		vec4[2] = lights_list[l]->pos_z;
147 		vec4[3] = 1.0f;
148 		glLightfv(GL_LIGHT0+j, GL_POSITION, vec4);
149 		vec4[0] = lights_list[l]->r;
150 		vec4[1] = lights_list[l]->g;
151 		vec4[2] = lights_list[l]->b;
152 		vec4[3] = 1.0f;
153 		glLightfv(GL_LIGHT0+j, GL_DIFFUSE, vec4);
154 		if (j >= 4) break;
155 		else j++;
156 	}
157 
158 #ifdef OPENGL_TRACE
159 CHECK_GL_ERRORS();
160 #endif //OPENGL_TRACE
161 }
162 
destroy_light(int i)163 void destroy_light(int i)
164 {
165 	if((i < 0) || (i >= MAX_LIGHTS)) return;
166 	if(lights_list[i] == NULL) return;
167 	delete_light_from_abt(main_bbox_tree, i);
168 	free(lights_list[i]);
169 	lights_list[i]= NULL;
170 }
171 
172 #if defined (MAP_EDITOR2) || defined (MAP_EDITOR)
add_light(GLfloat x,GLfloat y,GLfloat z,GLfloat r,GLfloat g,GLfloat b,GLfloat intensity,int locked,unsigned int dynamic)173 int add_light(GLfloat x, GLfloat y, GLfloat z, GLfloat r, GLfloat g, GLfloat b, GLfloat intensity, int locked, unsigned int dynamic)
174 #else
175 int add_light(GLfloat x, GLfloat y, GLfloat z, GLfloat r, GLfloat g, GLfloat b, GLfloat intensity, unsigned int dynamic)
176 #endif
177 {
178 	int i;
179 	light *new_light;
180 	AABBOX bbox;
181 
182 	//find a free spot, in the lights list
183 	for(i=0; i<MAX_LIGHTS; i++)
184 	{
185 		if(lights_list[i] == NULL)
186 			break;
187 	}
188 
189 	if(i >= MAX_LIGHTS)
190 		// oops no way to store the new light
191 		return i;
192 
193 	new_light = calloc(1, sizeof(light));
194 
195 	new_light->pos_x= x;
196 	new_light->pos_y= y;
197 	new_light->pos_z= z;
198 
199 	new_light->r= r*intensity;
200 	new_light->g= g*intensity;
201 	new_light->b= b*intensity;
202 
203 #ifdef MAP_EDITOR2
204 	new_light->locked=locked;
205 #endif
206 
207 #ifdef CLUSTER_INSIDES
208 	new_light->cluster = get_cluster ((int)(x/0.5f), (int)(y/0.5f));
209 	current_cluster = new_light->cluster;
210 #endif
211 
212 	lights_list[i] = new_light;
213 	if (i >= num_lights) num_lights = i+1;
214 	calc_light_aabb(&bbox, x, y, z, r*intensity, g*intensity, b*intensity, 1.41f, 1.0f, 0.004f); // 0.004 ~ 1/256
215 	if ((main_bbox_tree_items != NULL) && (dynamic == 0)) add_light_to_list(main_bbox_tree_items, i, bbox);
216 	else add_light_to_abt(main_bbox_tree, i, bbox, dynamic);
217 
218 	return i;
219 }
220 
cleanup_lights(void)221 void cleanup_lights(void)
222 {
223 	int i;
224 
225 	for(i = 0; i < MAX_LIGHTS; i++) {
226 		if(lights_list[i] != NULL) {
227 			free(lights_list[i]);
228 			lights_list[i]= NULL;
229 		}
230 	}
231 }
232 
233 //get the lights visible in the scene
234 //should be called only when we change the camera pos
update_scene_lights()235 void update_scene_lights()
236 {
237 	unsigned int start, stop;
238 
239 	get_intersect_start_stop(main_bbox_tree, TYPE_LIGHT, &start, &stop);
240 	show_lights = min2i(6, stop - start -1);
241 }
242 
init_lights()243 void init_lights()
244 {
245 	GLfloat light_diffuse[] = { 0.0, 0.0, 0.0, 0.0 };
246 	GLfloat no_light[] = { 0.0, 0.0, 0.0, 0.0 };
247 	float linear_att=1.41f;
248 	float cut_off=180;
249 	//most of the things in here are redundant, since we kind of set the light sources
250 	//to their default values. However, better safe than sorry.
251 
252 	glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, cut_off);
253 	glLightfv(GL_LIGHT0,GL_SPECULAR,no_light);
254 	glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
255 	glLightfv(GL_LIGHT0,GL_AMBIENT,no_light);
256 	glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,linear_att);
257     glEnable(GL_LIGHT0);
258 
259 	glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, cut_off);
260 	glLightfv(GL_LIGHT1,GL_SPECULAR,no_light);
261 	glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse);
262 	glLightfv(GL_LIGHT1,GL_AMBIENT,no_light);
263 	glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,linear_att);
264     glEnable(GL_LIGHT1);
265 
266 	glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, cut_off);
267 	glLightfv(GL_LIGHT2,GL_SPECULAR,no_light);
268 	glLightfv(GL_LIGHT2,GL_DIFFUSE,light_diffuse);
269 	glLightfv(GL_LIGHT2,GL_AMBIENT,no_light);
270 	glLightf(GL_LIGHT2,GL_LINEAR_ATTENUATION,linear_att);
271     glEnable(GL_LIGHT2);
272 
273 	glLightf(GL_LIGHT3, GL_SPOT_CUTOFF, cut_off);
274 	glLightfv(GL_LIGHT3,GL_SPECULAR,no_light);
275 	glLightfv(GL_LIGHT3,GL_DIFFUSE,light_diffuse);
276 	glLightfv(GL_LIGHT3,GL_AMBIENT,no_light);
277 	glLightf(GL_LIGHT3,GL_LINEAR_ATTENUATION,linear_att);
278     glEnable(GL_LIGHT3);
279 
280 	glLightfv(GL_LIGHT7,GL_AMBIENT,no_light);
281 	glLightfv(GL_LIGHT7,GL_SPECULAR,no_light);
282 	glLightfv(GL_LIGHT7,GL_DIFFUSE,no_light);
283 	glLightf(GL_LIGHT7,GL_CONSTANT_ATTENUATION,0);
284 	glEnable(GL_LIGHT7);
285 
286 	ec_add_light(GL_LIGHT4);
287 	ec_add_light(GL_LIGHT5);
288 	ec_add_light(GL_LIGHT6);
289 
290 	glEnable(GL_LIGHTING);
291 
292 	glNormal3f(0.0f,0.0f,1.0f);
293 #ifdef OPENGL_TRACE
294 CHECK_GL_ERRORS();
295 #endif //OPENGL_TRACE
296 }
297 
298 
reset_material()299 void reset_material()
300 {
301 	GLfloat mat_emission[]={ 0.0, 0.0, 0.0, 1.0 };
302 	GLfloat mat_specular[]={ 1.0, 1.0, 1.0, 1.0 };
303 		GLfloat mat_ambient[]={ 1.0, 1.0, 1.0, 1.0 };
304 		glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_ambient);
305 
306 	glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
307 	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
308 #ifdef OPENGL_TRACE
309 CHECK_GL_ERRORS();
310 #endif //OPENGL_TRACE
311 }
312 
313 
set_material(float r,float g,float b)314 void set_material(float r, float g, float b)
315 {
316 	GLfloat mat_emission[]={ r, g, b, 1.0 };
317 
318 	glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
319 	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_emission);
320 	glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_emission);
321 #ifdef OPENGL_TRACE
322 CHECK_GL_ERRORS();
323 #endif //OPENGL_TRACE
324 }
325 
326 int sun_use_static_position=0;
327 GLfloat ambient_light[] = { 0.0, 0.0, 0.0, 1.0 };
328 GLfloat diffuse_light[] = { 0.0, 0.0, 0.0, 0.0 };
draw_global_light()329 void draw_global_light()
330 {
331 	int i;
332 	GLfloat global_light_position[] = { 400.0, 400.0, 500.0, 0.0 };
333 
334 	//add the thunder light to the ambient/diffuse light
335 #ifndef MAP_EDITOR2
336 	memcpy(ambient_light, skybox_light_ambient_color, 3*sizeof(float));
337 	memcpy(diffuse_light, skybox_light_diffuse_color, 3*sizeof(float));
338 	// the thunder is handled elsewhere for the new weather
339 #else // MAP_EDITOR2
340 	diffuse_light[0]=global_diffuse_light[i][0];
341 	diffuse_light[1]=global_diffuse_light[i][1];
342 	diffuse_light[2]=global_diffuse_light[i][2];
343 #endif // MAP_EDITOR2
344 
345 	for (i = 0; i < 3; i++)
346 	{
347 		if (diffuse_light[i] < 0.0f)
348 		{
349 			diffuse_light[i] = 0.0f;
350 		}
351 		if (ambient_light[i] < 0.0f)
352 		{
353 			ambient_light[i] = 0.0f;
354 		}
355 	}
356 
357 
358 	glLightfv(GL_LIGHT7, GL_AMBIENT, ambient_light);
359 	glLightfv(GL_LIGHT7, GL_DIFFUSE, diffuse_light);
360 
361 
362 	if (sun_use_static_position)
363 	{
364 		glLightfv(GL_LIGHT7, GL_POSITION, global_light_position);
365 	}
366 	else
367 	{
368 		if ((sun_position[0] == 0.0f) && (sun_position[1] == 0.0f) &&
369 			(sun_position[2] == 0.0f) && (sun_position[3] == 0.0f))
370 		{
371 			glLightfv(GL_LIGHT7, GL_POSITION, global_light_position);
372 		}
373 		else
374 		{
375 			glLightfv(GL_LIGHT7, GL_POSITION, sun_position);
376 		}
377 	}
378 #ifdef OPENGL_TRACE
379 CHECK_GL_ERRORS();
380 #endif //OPENGL_TRACE
381 }
382 
draw_dungeon_light()383 void draw_dungeon_light()
384 {
385 	GLfloat global_light_position[] = { 400.0, 400.0, 500.0, 0.0 };
386 	GLfloat diffuse_light[] = { 0.0, 0.0, 0.0, 0.0 };
387 	GLfloat ambient_light[4];
388 	int i;
389 
390 	//the ambient light should be half of the diffuse light
391 		ambient_light[0]=ambient_r;
392 		ambient_light[1]=ambient_g;
393 		ambient_light[2]=ambient_b;
394 		ambient_light[3]=1.0f;
395 	for (i = 0; i < 3; i++)
396 	{
397 		if (diffuse_light[i] < 0.0f)
398 		{
399 			diffuse_light[i] = 0.0f;
400 		}
401 		if (ambient_light[i] < 0.0f)
402 		{
403 			ambient_light[i] = 0.0f;
404 		}
405 	}
406 
407 	glLightfv(GL_LIGHT7,GL_AMBIENT,ambient_light);
408 	glLightfv(GL_LIGHT7, GL_POSITION, global_light_position);
409 	glLightfv(GL_LIGHT7,GL_DIFFUSE,diffuse_light);
410 #ifdef OPENGL_TRACE
411 CHECK_GL_ERRORS();
412 #endif //OPENGL_TRACE
413 }
414 
415 
make_gradient_light(int start,int steps,float * light_table,float r_start,float g_start,float b_start,float r_end,float g_end,float b_end)416 void make_gradient_light(int start,int steps,float *light_table, float r_start,
417 						 float g_start, float b_start, float r_end, float g_end, float b_end)
418 {
419 	int i,j;
420 	float r_slope,g_slope,b_slope;
421 
422 	r_slope=(r_end-r_start)/steps;
423 	g_slope=(g_end-g_start)/steps;
424 	b_slope=(b_end-b_start)/steps;
425 
426 	j=0;
427 	for(i=start;i<start+steps;i++)
428 		{
429 			light_table[i*4+0]=r_start+r_slope*(float)j;
430 			light_table[i*4+1]=g_start+g_slope*(float)j;
431 			light_table[i*4+2]=b_start+b_slope*(float)j;
432 			light_table[i*4+3]=1.0f;
433 			j++;
434 
435 		}
436 }
437 
438 
439 //build the light table for smooth transition between night and day
build_global_light_table()440 void build_global_light_table()
441 {
442 	//the sun light
443   	make_gradient_light(0,30,(float *)global_diffuse_light,0.85f,0.85f,0.85f,0.32f,0.25f,0.25f);
444 	make_gradient_light(30,30,(float *)global_diffuse_light,0.318f,0.248f,0.248f,0.05f,0.05f,0.08f);
445 	//lake light
446 	make_gradient_light(0,30,(float *)sky_lights_c1,
447 						0.3f, 0.7f, 0.9f,
448 						0.6f, 0.0f, 0.0f);
449 	make_gradient_light(30,30,(float *)sky_lights_c1,
450 						0.6f, 0.0f, 0.0f,
451 						0.0f, 0.05f, 0.1f);
452 	make_gradient_light(60,30,(float *)sky_lights_c1,
453 						0.0f, 0.05f, 0.1f,
454 						0.6f, 0.0f, 0.2f);
455 	make_gradient_light(90,30,(float *)sky_lights_c1,
456 						0.6f, 0.0f, 0.2f,
457 						0.3f, 0.7f, 0.9f);
458 
459 	make_gradient_light(0,30,(float *)sky_lights_c2,
460 						0.2f, 0.5f, 0.8f,
461 						0.6f, 0.3f, 0.0f);
462 	make_gradient_light(30,30,(float *)sky_lights_c2,
463 						0.6f, 0.3f, 0.0f,
464 						0.0f, 0.025f, 0.05f);
465 	make_gradient_light(60,30,(float *)sky_lights_c2,
466 						0.0f, 0.025f, 0.05f,
467 						0.6f, 0.3f, 0.0f);
468 	make_gradient_light(90,30,(float *)sky_lights_c2,
469 						0.6f, 0.3f, 0.0f,
470 						0.2f, 0.5f, 0.8f);
471 
472 	make_gradient_light(0,30,(float *)sky_lights_c3,
473 						0.1f, 0.3f, 0.7f,
474 						0.2f, 0.2f, 0.3f);
475 	make_gradient_light(30,30,(float *)sky_lights_c3,
476 						0.2f, 0.2f, 0.3f,
477 						0.0f, 0.01f, 0.02f);
478 	make_gradient_light(60,30,(float *)sky_lights_c3,
479 						0.0f, 0.01f, 0.02f,
480 						0.2f, 0.2f, 0.3f);
481 	make_gradient_light(90,30,(float *)sky_lights_c3,
482 						0.2f, 0.2f, 0.3f,
483 						0.1f, 0.3f, 0.7f);
484 
485 	make_gradient_light(0,30,(float *)sky_lights_c4,
486 						0.05f, 0.2f, 0.6f,
487 						0.0f, 0.1f, 0.4f);
488 	make_gradient_light(30,30,(float *)sky_lights_c4,
489 						0.0f, 0.1f, 0.4f,
490 						0.01f, 0.01f, 0.01f);
491 	make_gradient_light(60,30,(float *)sky_lights_c4,
492 						0.01f, 0.01f, 0.01f,
493 						0.0f, 0.1f, 0.4f);
494 	make_gradient_light(90,30,(float *)sky_lights_c4,
495 						0.0f, 0.1f, 0.4f,
496 						0.05f, 0.2f, 0.6f);
497 
498 }
499 
build_sun_pos_table()500 void build_sun_pos_table()
501 {
502 	int i;
503 		float x,y,z;
504 		float start, step;
505 
506 		// position of the displayed sun
507 		step  = 189.0/180.0;
508 		start = -4.5;
509 		for(i = 0; i <= 180; i++)
510 		{
511 			z = sinf((start+i*step)*M_PI/180.0);
512 			x = cosf((start+i*step)*M_PI/180.0);
513 			y = 0;
514 
515 			sun_show[i].x=x;
516 			sun_show[i].y=y;
517 			sun_show[i].z=z;
518 			sun_show[i].w=0.0;
519 		}
520 
521 		// position of the light during the day
522 		step  = 162.0/180.0;
523 		start = 9.0;
524 		for(i = 0; i < 180; i++)
525 		{
526 			z = sinf((start+i*step)*M_PI/180.0);
527 			x = cosf((start+i*step)*M_PI/180.0);
528 			y = 0;
529 
530 			sun_pos[i].x=x;
531 			sun_pos[i].y=y;
532 			sun_pos[i].z=z;
533 			sun_pos[i].w=0.0;
534 		}
535 
536 		// position of the light during the night (used to light the moons)
537 		step  = 198.0/180.0;
538 		start = -27.0;
539 		for(i = 180; i < 360; i++)
540 		{
541 			z = sinf((start+i*step)*M_PI/180.0);
542 			x = cosf((start+i*step)*M_PI/180.0);
543 			y = 0;
544 
545 			sun_pos[i].x=x;
546 			sun_pos[i].y=y;
547 			sun_pos[i].z=z;
548 			sun_pos[i].w=0.0;
549 		}
550 }
551 
new_minute()552 void new_minute()
553 {
554 #ifdef EXTRA_DEBUG
555 	ERR();
556 #endif
557 	if (!freeze_time) game_minute = real_game_minute;
558 	if (!freeze_time) game_second = real_game_second;
559 
560 	//morning starts at 0
561 	//game_minute=90;
562 	//is it morning?
563 	if(game_minute<60)light_level=game_minute+60;
564 	//check to see if it is full day
565 	if(game_minute>=60 && game_minute<60*3)light_level=0;
566 	//is it evening?
567 	if(game_minute>=60*3 && game_minute<60*4)light_level=game_minute-60*3;
568 	//full night?
569 	if(game_minute>=60*4)light_level=59;
570 
571 	//is it day?
572 		if(game_minute >= 30 && game_minute < 210 && !dungeon)
573 		{
574 			skybox_sun_position[0] = sun_show[game_minute-30].x;
575 			skybox_sun_position[1] = sun_show[game_minute-30].y;
576 			skybox_sun_position[2] = sun_show[game_minute-30].z;
577 			skybox_sun_position[3] = sun_show[game_minute-30].w;
578 			disable_local_lights();
579 			is_day=1;
580 			sun_position[0]=sun_pos[game_minute-30].x;
581 			sun_position[1]=sun_pos[game_minute-30].y;
582 			sun_position[2]=sun_pos[game_minute-30].z;
583 			sun_position[3]=sun_pos[game_minute-30].w;
584 			calc_shadow_matrix();
585 		}
586 		else//it's too dark, or we are in a dungeon
587 		{
588 			int shift_time = (game_minute+330)%360;
589 			sun_position[0] = sun_pos[shift_time].x;
590 			sun_position[1] = sun_pos[shift_time].y;
591 			sun_position[2] = sun_pos[shift_time].z;
592 			sun_position[3] = sun_pos[shift_time].w;
593 			skybox_sun_position[0] = skybox_sun_position[1] = skybox_sun_position[2] = skybox_sun_position[3] = 0.0;
594 			is_day=0;
595 			enable_local_lights();
596 		}
597 
598 	skybox_update_positions();
599 	if (skybox_update_delay > 0)
600 		skybox_update_colors();
601 }
602 
new_second()603 void new_second()
604 {
605 	if (!freeze_time) game_second = real_game_second;
606 
607 	if (skybox_update_delay < 1 || real_game_second % skybox_update_delay == 0)
608 	{
609 		int cur_min = (game_minute+330)%360;
610 		int next_min = (game_minute+331)%360;
611 		float ratio2 = (float)game_second/60.0;
612 		float ratio1 = 1.0 - ratio2;
613 
614 		sun_position[0] = sun_pos[cur_min].x * ratio1 + sun_pos[next_min].x * ratio2;
615 		sun_position[1] = sun_pos[cur_min].y * ratio1 + sun_pos[next_min].y * ratio2;
616 		sun_position[2] = sun_pos[cur_min].z * ratio1 + sun_pos[next_min].z * ratio2;
617 		sun_position[3] = sun_pos[cur_min].w * ratio1 + sun_pos[next_min].w * ratio2;
618 
619 		if (is_day)
620 		{
621 			skybox_sun_position[0] = sun_show[cur_min].x * ratio1 + sun_show[next_min].x * ratio2;
622 			skybox_sun_position[1] = sun_show[cur_min].y * ratio1 + sun_show[next_min].y * ratio2;
623 			skybox_sun_position[2] = sun_show[cur_min].z * ratio1 + sun_show[next_min].z * ratio2;
624 			skybox_sun_position[3] = sun_show[cur_min].w * ratio1 + sun_show[next_min].w * ratio2;
625 			calc_shadow_matrix();
626 		}
627 
628 		skybox_update_positions();
629 		if (skybox_update_delay > 0)
630 			skybox_update_colors();
631 	}
632 	else if (skybox_update_delay > 1 && weather_get_intensity() > 0.0)
633 	{
634 		skybox_update_colors();
635 	}
636 }
637 
638 #ifdef DEBUG_TIME
639 
640 #ifndef WINDOWS
641 #include <sys/time.h>
642 #endif
643 
light_idle(void)644 void light_idle(void)
645 {
646 	Uint64 new_time;
647 #ifdef WINDOWS
648 	FILETIME ft;
649 	GetSystemTimeAsFileTime(&ft);
650 	new_time = ft.dwHighDateTime;
651 	new_time <<= 32;
652 	new_time |= ft.dwLowDateTime;
653 	new_time /= 10;
654 #else
655 	struct timeval t;
656 	gettimeofday(&t, NULL);
657 	new_time = ((Uint64)t.tv_sec)*1000000ul + (Uint64)t.tv_usec;
658 #endif
659 	if (new_time / (Uint64)(60000000 / debug_time_accel) - old_time / (Uint64)(60000000 / debug_time_accel)){
660 		game_minute++;
661 		if (game_minute >= 360)
662 		      game_minute -= 360;
663 		new_minute();
664 	}
665 
666 	old_time = new_time;
667 }
668 #endif // DEBUG_TIME
669