some meshgen and map rendering updates
[voxelands-alt.git] / src / graphics / lighting.c
blob7bfaabe46f3762665d32290edbb273be2b22a665
1 /************************************************************************
2 * lighting.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
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 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "graphics.h"
22 #include "array.h"
23 #include "list.h"
25 static struct {
26 int ids;
27 array_t lights;
28 } light_data = {
32 /* comparison functions:
33 * return 0 to insert
34 * return 1 to insert later
35 * return -1 to not insert at all
37 static int light_insert_cmp(void *e1, void *e2)
39 light_t *l1;
40 light_t *l2;
42 l1 = e1;
43 l2 = e2;
45 if (l1->d < l2->d)
46 return 0;
47 return 1;
50 int light_add(colour_t *colour, v3_t *pos, v3_t *att)
52 light_t *l;
53 if (light_data.ids < 0)
54 array_init(&light_data.lights,ARRAY_TYPE_PTR);
56 l = malloc(sizeof(light_t));
57 if (!l)
58 return -1;
60 l->pos.x = pos->x;
61 l->pos.y = pos->y;
62 l->pos.z = pos->z;
64 l->colour.r = colour->r;
65 l->colour.g = colour->g;
66 l->colour.b = colour->b;
67 l->colour.a = colour->a;
69 if (att) {
70 l->att.x = att->x;
71 l->att.y = att->y;
72 l->att.z = att->z;
73 }else{
74 l->att.x = 1.0;
75 l->att.y = 0.0;
76 l->att.z = 0.0;
79 l->id = ++light_data.ids;
81 array_push_ptr(&light_data.lights,l);
83 return -1;
86 void light_remove(int id)
90 void light_bind_near(shader_t *s, v3_t *pos)
92 int i;
93 light_t *l;
94 light_t *nearby = NULL;
96 if (light_data.ids < 0)
97 return;
99 for (i=0; i<light_data.lights.length; i++) {
100 l = array_get_ptr(&light_data.lights,i);
101 if (!l)
102 continue;
103 l->d = math_distance(pos,&l->pos);
104 nearby = list_insert_cmp(&nearby,l,light_insert_cmp);
107 l = nearby;
108 for (i=0; l; i++) {
109 shader_uniform_light(s,i,l);
110 l = l->next;
112 for (; i<8; i++) {
113 shader_uniform_light(s,i,NULL);