1 /* Asteroids3D - a first person game of blowing up asteroids
2 * Copyright (C) 2000 Stuart Mark Pomerantz <smp [at] psc edu>
3 * Copyright © Jan Engelhardt <jengelh [at] gmx de>, 2003 - 2006
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Stuart Pomerantz
21 * 3935 Stonecliffe drive
22 * Monroeville, PA 15146
23 */
24
25 #include <GL/glut.h>
26 #include "asteroids3D.h"
27
28 /* light0 (the sun) position
29 * light1 (headlight) position & direction
30 * light2 torpedo tracer
31 */
32 float l0_pos[4], l1_pos[4], l1_dir[4];
33
34 //-----------------------------------------------------------------------------
gl_init_light(void)35 void gl_init_light(void)
36 {
37 static const float global_ambient[] = {0.1, 0.1, 0.1, 1.0}; /* original */
38 //static const float global_ambient[] = {0.5, 0.5, 0.5, 1.0}; /* for debugging */
39
40 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
41 static const float diffuse[] = {1.0, 1.0, 0.8, 1.0}; /* slightly yellow sun */
42 static const float specular[] = {0.0, 0.0, 0.0, 1.0};
43
44 /* w = 0 implies a directional source (light coming parallel to this vector)*/
45 l0_pos[0] = 70;
46 l0_pos[1] = 0;
47 l0_pos[2] = 0;
48 l0_pos[3] = 0;
49
50 glEnable(GL_LIGHTING);
51 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
52 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
53
54 glEnable(GL_LIGHT0);
55 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
56 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
57 glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
58 glLightfv(GL_LIGHT0, GL_POSITION, l0_pos);
59
60 return;
61 }
62
toggle_headlight(void)63 void toggle_headlight(void)
64 {
65 static int headlight_activated = 0;
66
67 if (!headlight_activated)
68 glEnable(GL_LIGHT1);
69 else
70 glDisable(GL_LIGHT1);
71
72 headlight_activated = !headlight_activated;
73 return;
74 }
75
gl_init_headlight(void)76 void gl_init_headlight(void)
77 {
78 static const float ambient[] = {0, 0, 0, 1};
79 static const float diffuse[] = {1, 1, 1, 1}; /* white headlight */
80 static const float specular[] = {0, 0, 0, 1};
81
82 /* set the initial headlight position to be at the viewer's position */
83 l1_pos[0] = pos.x;
84 l1_pos[1] = pos.y;
85 l1_pos[2] = pos.z;
86 l1_pos[3] = 1;
87
88 /* set the initial headlight direction to be down the positive z-axis,
89 * which is where the view was set to be in reset_view()
90 */
91 l1_dir[0] = pos.x + zaxis.x;
92 l1_dir[1] = pos.y + zaxis.y;
93 l1_dir[2] = pos.z + zaxis.z;
94 l1_dir[3] = 1;
95
96 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
97 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
98 glLightfv(GL_LIGHT1, GL_SPECULAR, specular);
99 glLightfv(GL_LIGHT1, GL_POSITION, l1_pos);
100 glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, l1_dir);
101 glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 20);
102 /*
103 glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.04);
104 glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.04);
105 */
106 return;
107 }
108