-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light.cpp
176 lines (132 loc) · 4.48 KB
/
Light.cpp
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
/*-----------------------------------------------------------------------------
Light.cpp
2006 Shamus Young
-------------------------------------------------------------------------------
This tracks and renders the light sources. (Note that they do not really
CAST light in the OpenGL sense of the world, these are just simple panels.)
These are NOT subclassed to entities because these are dynamic. Some lights
blink, and thus they can't go into the fixed render lists managed by
Entity.cpp.
-----------------------------------------------------------------------------*/
#define MAX_SIZE 5
#include <windows.h>
#include <math.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include "glTypes.h"
#include "camera.h"
#include "entity.h"
#include "light.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "visible.h"
static GLvector2 angles[5][360];
static CLight* head;
static bool angles_done;
static int count;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void LightClear ()
{
CLight* l;
while (head) {
l = head;
head = l->_next;
delete l;
}
count = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int LightCount ()
{
return count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void LightRender ()
{
CLight* l;
if (!EntityReady ())
return;
if (!angles_done) {
for (int size = 0; size < MAX_SIZE; size++) {
for (int i = 0 ;i < 360; i++) {
angles[size][i].x = cosf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
angles[size][i].y = sinf ((float)i * DEGREES_TO_RADIANS) * ((float)size + 0.5f);
}
}
}
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
glBlendFunc (GL_ONE, GL_ONE);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_LIGHT));
glDisable (GL_CULL_FACE);
glBegin (GL_QUADS);
for (l = head; l; l = l->_next)
l->Render ();
glEnd ();
glDepthMask (true);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CLight::CLight (GLvector pos, GLrgba color, int size)
{
_position = pos;
_color = color;
_size = CLAMP (size, 0, (MAX_SIZE - 1));
_vert_size = (float)_size + 0.5f;
_flat_size = _vert_size + 0.5f;
_blink = false;
_cell_x = WORLD_TO_GRID(pos.x);
_cell_z = WORLD_TO_GRID(pos.z);
_next = head;
head = this;
count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CLight::Blink ()
{
_blink = true;
//we don't want blinkers to be in sync, so have them blink at
//slightly different rates. (Milliseconds)
_blink_interval = 1500 + RandomVal (500);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CLight::Render ()
{
int angle;
GLvector pos;
GLvector camera;
GLvector camera_position;
GLvector2 offset;
if (!Visible (_cell_x, _cell_z))
return;
camera = CameraAngle ();
camera_position = CameraPosition ();
if (fabs (camera_position.x - _position.x) > RenderFogDistance ())
return;
if (fabs (camera_position.z - _position.z) > RenderFogDistance ())
return;
if (_blink && (GetTickCount () % _blink_interval) > 200)
return;
angle = (int)MathAngle (camera.y);
offset = angles[_size][angle];
pos = _position;
glColor4fv (&_color.red);
glTexCoord2f (0, 0);
glVertex3f (pos.x + offset.x, pos.y - _vert_size, pos.z + offset.y);
glTexCoord2f (0, 1);
glVertex3f (pos.x - offset.x, pos.y - _vert_size, pos.z - offset.y);
glTexCoord2f (1, 1);
glVertex3f (pos.x - offset.x, pos.y + _vert_size, pos.z - offset.y);
glTexCoord2f (1, 0);
glVertex3f (pos.x + offset.x, pos.y + _vert_size, pos.z + offset.y);
}