-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight.c
More file actions
331 lines (244 loc) · 7.24 KB
/
light.c
File metadata and controls
331 lines (244 loc) · 7.24 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Final Program, CMPS 160, Fall 1999
* Ben Gertzfield <ben@cse.ucsc.edu>
*
* This program is released under the GNU GPL, version 2 or later.
*/
/* Light routines */
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "prog.h"
static pthread_mutex_t light_mutex = PTHREAD_MUTEX_INITIALIZER;
struct light *gkos_first_light = NULL; /* Linked list of lights */
struct light *gkos_cur_light = NULL; /* Currently-selected light */
static int num_lights = 0; /* Number of lights */
static GLenum lights[] = { GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4,
GL_LIGHT5, GL_LIGHT6, GL_LIGHT7 };
static void idle_light(struct light *light);
static void free_light(struct light *light);
static void free_light(struct light *light)
{
gkos_disable_light(light);
free(light);
}
void gkos_init_lights(void)
{
int i;
pthread_mutex_init(&light_mutex, NULL);
for (i = 0; i < 8; i++)
gkos_new_light();
gkos_reset_all_lights();
}
void gkos_reset_all_lights(void)
{
int i;
struct light *light = gkos_first_light;
pthread_mutex_lock(&light_mutex);
for (i = 0; i < gkos_config->num_lights; i++) {
if (light == NULL)
continue;
light->enabled = 1;
light = light->next;
}
for (i = gkos_config->num_lights; i < 8; i++) {
if (light == NULL)
continue;
light->enabled = 0;
gkos_disable_light(light);
light = light->next;
}
pthread_mutex_unlock(&light_mutex);
}
void gkos_pop_light(void)
{
struct light *first, *next;
pthread_mutex_lock(&light_mutex);
if (gkos_first_light == NULL) {
pthread_mutex_unlock(&light_mutex);
return;
}
first = gkos_first_light;
next = gkos_first_light->next;
if (first->free != NULL)
(*first->free)(first);
num_lights--;
gkos_first_light = next;
pthread_mutex_unlock(&light_mutex);
}
/* Create a new light and push it onto the light stack. */
struct light *gkos_new_light(void)
{
struct light *light, *temp;
GLfloat white_light[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat gray_light[4] = { 0.3, 0.3, 0.3, 1.0 };
int i;
if (num_lights > 7) { /* only 8 lights in opengl :/ */
fprintf(stderr, "Max number of lights reached\n");
return NULL;
}
light = (struct light *) malloc(sizeof(struct light));
if (light == NULL) { /* malloc failed */
fprintf(stderr, "Cannot allocate memory for light %i, exiting\n",
num_lights + 1);
exit(1);
}
for (i = 0; i < 3; i++)
light->direction[i] = 5.0 + (5.0 * rand() / (RAND_MAX + 5.0));
light->direction[3] = rand() % 2;
for (i = 0; i < 3; i++)
light->color_delta[i] = (0.2 * rand() / (RAND_MAX - 0.1)) - 0.1;
light->color_delta[3] = 0.0;
light->id = lights[num_lights];
light->num = num_lights;
num_lights++;
memcpy(light->amb_color, gray_light, sizeof(gray_light));
memcpy(light->dif_color, white_light, sizeof(white_light));
memcpy(light->spec_color, white_light, sizeof(white_light));
/* give it a tilt so we can rotate nicely */
light->rot_angle.x = light->rot_angle.y = light->rot_angle.z = 50.0;
light->delta_angle.x = (4 * rand() / (RAND_MAX - 2.0)) - 2.0;
light->delta_angle.y = (4 * rand() / (RAND_MAX - 2.0)) - 2.0;
light->delta_angle.z = (4 * rand() / (RAND_MAX - 2.0)) - 2.0;
light->enabled = 1; /* turn the light on */
light->idle = idle_light;
light->free = free_light;
light->next = NULL;
gkos_cur_light = light;
if (gkos_first_light == NULL) { /* push light onto end of light list */
gkos_first_light = light;
return light;
}
temp = gkos_first_light;
while (temp->next != NULL)
temp = temp->next;
temp->next = light;
return light;
}
void gkos_enable_all_lights(void)
{
struct light *light = gkos_first_light;
pthread_mutex_lock(&light_mutex);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
while (light != NULL) {
if (light->enabled)
gkos_enable_light(light);
light = light->next;
}
pthread_mutex_unlock(&light_mutex);
}
/* Enable the given light and tell GL it exists. */
void gkos_enable_light(struct light *light)
{
if (light == NULL) {
fprintf(stderr, "Null light passed to enable_light()\n");
return;
}
glEnable(light->id);
glPushMatrix();
{
glRotatef(light->rot_angle.x, 1.0, 0.0, 0.0);
glRotatef(light->rot_angle.y, 0.0, 1.0, 0.0);
glRotatef(light->rot_angle.z, 0.0, 0.0, 1.0);
glLightfv(light->id, GL_POSITION, light->direction);
glLightfv(light->id, GL_AMBIENT, light->amb_color);
glLightfv(light->id, GL_DIFFUSE, light->dif_color);
glLightfv(light->id, GL_SPECULAR, light->spec_color);
}
glPopMatrix();
}
/* Disable the given light and tell GL it's off. */
void gkos_disable_light(struct light *light)
{
if (light == NULL) {
fprintf(stderr, "Null light passed to disable_light()\n");
return;
}
if (glIsEnabled(light->id))
glDisable(light->id);
}
/* Just go through the global light linked list and pick #pickme */
void gkos_select_light(int pickme)
{
struct light *light = gkos_first_light;
int i;
if (light == NULL) {
fprintf(stderr, "Cannot select light #%i, does not exist\n", pickme);
gkos_cur_light = NULL;
return;
}
for (i = 1; i < pickme; i++) {
light = light->next;
if (light == NULL) {
fprintf(stderr, "Cannot select light #%i, does not exist\n", pickme);
gkos_cur_light = NULL;
return;
}
}
gkos_cur_light = light; /* good, we got a light */
}
static void idle_light(struct light *light)
{
int i;
pthread_mutex_lock(&light_mutex);
if (light == NULL || !light->enabled) {
pthread_mutex_unlock(&light_mutex);
return;
}
if (gkos_config->lights_move) {
light->rot_angle.x = (GLfloat)((int) (light->rot_angle.x +
light->delta_angle.x) % 360);
light->rot_angle.y = (GLfloat)((int) (light->rot_angle.y +
light->delta_angle.y) % 360);
light->rot_angle.z = (GLfloat)((int) (light->rot_angle.z +
light->delta_angle.z) % 360);
}
if (gkos_config->lights_change_color) {
for (i = 0; i < 3; i++) {
if ((light->dif_color[i] + light->color_delta[i] > 1.0) ||
(light->dif_color[i] + light->color_delta[i] < 0.0))
light->color_delta[i] *= -1;
light->dif_color[i] += light->color_delta[i];
light->spec_color[i] += light->color_delta[i];
}
}
pthread_mutex_unlock(&light_mutex);
}
/* Increase a light's rotation angle by dx, dy, and dz */
void gkos_rotate_light(struct light *light, int x0, int y0, int z0,
int x, int y, int z)
{
int dx, dy, dz;
dx = x0 - x;
dy = y0 - y;
dz = z0 - z;
light->rot_angle.x -= dx;
light->rot_angle.y += dy;
light->rot_angle.z -= dz;
if (light->rot_angle.x > 360.0)
light->rot_angle.x -= 360.0;
if (light->rot_angle.x < 0.0)
light->rot_angle.x += 360.0;
if (light->rot_angle.y > 360.0)
light->rot_angle.y -= 360.0;
if (light->rot_angle.y < 0.0)
light->rot_angle.y += 360.0;
if (light->rot_angle.z > 360.0)
light->rot_angle.z -= 360.0;
if (light->rot_angle.z < 0.0)
light->rot_angle.z += 360.0;
}
// Local variables:
// c-file-style:"cc-mode"
// End: