/
r_light.c
122 lines (101 loc) · 1.93 KB
/
r_light.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
/* This file is part of punani-strike
* Copyright (c) 2011 Gianni Tedesco
* Released under the terms of GPLv3
*/
#include <punani/punani.h>
#include <punani/renderer.h>
#include <punani/light.h>
#include <punani/asset.h>
#include <punani/punani_gl.h>
#include <math.h>
#define RENDER_LIGHTS 1
#include "render-internal.h"
struct _light {
renderer_t owner;
float pos[4];
float color[4];
uint8_t flags;
uint8_t num;
uint8_t enabled;
};
light_t light_new(renderer_t r, unsigned int flags)
{
struct _light *l = NULL;
int num;
num = renderer_get_free_light(r);
if ( num < 0 )
goto out;
l = calloc(1, sizeof(*l));
if ( NULL == l )
goto out;
l->owner = r;
l->num = num;
l->pos[0] = -25.0;
l->pos[1] = 20.0;
l->pos[2] = 25.0;
l->pos[3] = 0.0;
l->color[0] = 1.0;
l->color[1] = 1.0;
l->color[2] = 1.0;
l->color[3] = 1.0;
l->enabled = 1;
l->flags = flags;
renderer_set_light(r, num, l);
out:
return l;
}
void light_set_pos(light_t l, const vec3_t pos)
{
l->pos[0] = pos[0];
l->pos[1] = pos[1];
l->pos[2] = pos[2];
if ( l->flags & LIGHT_CAST_SHADOWS ) {
assets_recalc_shadow_vols(l);
}
}
void light_get_pos(light_t l, vec3_t pos)
{
pos[0] = l->pos[0];
pos[1] = l->pos[1];
pos[2] = l->pos[2];
}
void light_disable(light_t l)
{
l->enabled = 0;
}
void light_enable(light_t l)
{
l->enabled = 1;
}
int light_enabled(light_t l)
{
return l->enabled;
}
void light_render(light_t l)
{
GLint num;
num = GL_LIGHT0 + l->num;
if ( l->enabled ) {
glEnable(num);
glLightf(num, GL_CONSTANT_ATTENUATION, 1.0);
glLightf(num, GL_LINEAR_ATTENUATION, 0.0);
glLightf(num, GL_QUADRATIC_ATTENUATION, 0.0);
glLightfv(num, GL_DIFFUSE, l->color);
glLightfv(num, GL_POSITION, l->pos);
}else{
glDisable(num);
}
}
void light_set_color(light_t l, float r, float b, float g)
{
l->color[0] = r;
l->color[1] = b;
l->color[2] = g;
}
void light_free(light_t l)
{
if ( l ) {
renderer_nuke_light(l->owner, l->num);
free(l);
}
}