aboutsummaryrefslogtreecommitdiff
path: root/engines/bladerunner/light.cpp
blob: 0590630dbbd0895cb094fb29c08cbed6e3d4dca3 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "bladerunner/light.h"

#include "common/util.h"

namespace BladeRunner {

Light::Light() {
	_frameCount         = 0;
	_animated           = 0;
	_animationData      = nullptr;
	_animatedParameters = 0;
	_falloffStart       = 0.0f;
	_falloffEnd         = 0.0f;
	_angleStart         = 0.0f;
	_angleEnd           = 0.0f;
	_m11ptr             = nullptr;
	_m12ptr             = nullptr;
	_m13ptr             = nullptr;
	_m14ptr             = nullptr;
	_m21ptr             = nullptr;
	_m22ptr             = nullptr;
	_m23ptr             = nullptr;
	_m24ptr             = nullptr;
	_m31ptr             = nullptr;
	_m32ptr             = nullptr;
	_m33ptr             = nullptr;
	_m34ptr             = nullptr;
	_colorRPtr          = nullptr;
	_colorGPtr          = nullptr;
	_colorBPtr          = nullptr;
	_falloffStartPtr    = nullptr;
	_falloffEndPtr      = nullptr;
	_angleStartPtr      = nullptr;
	_angleEndPtr        = nullptr;
}

Light::~Light() {
	if (_animationData != nullptr) {
		delete[] _animationData;
	}
}

void Light::read(Common::ReadStream *stream, int frameCount, int frame, int animated) {
	_frameCount = frameCount;
	_animated = animated;

	int size = stream->readUint32LE();
	size = size - 32;

	char buf[20];
	stream->read(buf, sizeof(buf));
	_name = buf;

	_animatedParameters = stream->readUint32LE();

	if (_animationData != nullptr) {
		delete[] _animationData;
	}
	int floatCount = size / 4;
	_animationData = new float[floatCount];
	for (int i = 0; i < floatCount; i++) {
		_animationData[i] = stream->readFloatLE();
	}

	_m11ptr          = _animationData;
	_m12ptr          = _m11ptr          + ((_animatedParameters &     0x1) ? frameCount : 1);
	_m13ptr          = _m12ptr          + ((_animatedParameters &     0x2) ? frameCount : 1);
	_m14ptr          = _m13ptr          + ((_animatedParameters &     0x4) ? frameCount : 1);
	_m21ptr          = _m14ptr          + ((_animatedParameters &     0x8) ? frameCount : 1);
	_m22ptr          = _m21ptr          + ((_animatedParameters &    0x10) ? frameCount : 1);
	_m23ptr          = _m22ptr          + ((_animatedParameters &    0x20) ? frameCount : 1);
	_m24ptr          = _m23ptr          + ((_animatedParameters &    0x40) ? frameCount : 1);
	_m31ptr          = _m24ptr          + ((_animatedParameters &    0x80) ? frameCount : 1);
	_m32ptr          = _m31ptr          + ((_animatedParameters &   0x100) ? frameCount : 1);
	_m33ptr          = _m32ptr          + ((_animatedParameters &   0x200) ? frameCount : 1);
	_m34ptr          = _m33ptr          + ((_animatedParameters &   0x400) ? frameCount : 1);
	_colorRPtr       = _m34ptr          + ((_animatedParameters &   0x800) ? frameCount : 1);
	_colorGPtr       = _colorRPtr       + ((_animatedParameters &  0x1000) ? frameCount : 1);
	_colorBPtr       = _colorGPtr       + ((_animatedParameters &  0x2000) ? frameCount : 1);
	_falloffStartPtr = _colorBPtr       + ((_animatedParameters &  0x4000) ? frameCount : 1);
	_falloffEndPtr   = _falloffStartPtr + ((_animatedParameters &  0x8000) ? frameCount : 1);
	_angleStartPtr   = _falloffEndPtr   + ((_animatedParameters & 0x10000) ? frameCount : 1);
	_angleEndPtr     = _angleStartPtr   + ((_animatedParameters & 0x20000) ? frameCount : 1);

	setupFrame(frame);
}

void Light::readVqa(Common::ReadStream *stream, int frameCount, int frame, int animated) {
	_frameCount = frameCount;
	_animated = animated;

	_animatedParameters = stream->readUint32LE();

	int size = stream->readUint32LE();

	if(_animationData != nullptr) {
		delete[] _animationData;
	}

	int floatCount = size / 4;
	_animationData = new float[floatCount];
	for (int i = 0; i < floatCount; i++) {
		_animationData[i] = stream->readFloatLE();
	}

	_m11ptr          = _animationData;
	_m12ptr          = _m11ptr          + ((_animatedParameters &     0x1) ? frameCount : 1);
	_m13ptr          = _m12ptr          + ((_animatedParameters &     0x2) ? frameCount : 1);
	_m14ptr          = _m13ptr          + ((_animatedParameters &     0x4) ? frameCount : 1);
	_m21ptr          = _m14ptr          + ((_animatedParameters &     0x8) ? frameCount : 1);
	_m22ptr          = _m21ptr          + ((_animatedParameters &    0x10) ? frameCount : 1);
	_m23ptr          = _m22ptr          + ((_animatedParameters &    0x20) ? frameCount : 1);
	_m24ptr          = _m23ptr          + ((_animatedParameters &    0x40) ? frameCount : 1);
	_m31ptr          = _m24ptr          + ((_animatedParameters &    0x80) ? frameCount : 1);
	_m32ptr          = _m31ptr          + ((_animatedParameters &   0x100) ? frameCount : 1);
	_m33ptr          = _m32ptr          + ((_animatedParameters &   0x200) ? frameCount : 1);
	_m34ptr          = _m33ptr          + ((_animatedParameters &   0x400) ? frameCount : 1);
	_colorRPtr       = _m34ptr          + ((_animatedParameters &   0x800) ? frameCount : 1);
	_colorGPtr       = _colorRPtr       + ((_animatedParameters &  0x1000) ? frameCount : 1);
	_colorBPtr       = _colorGPtr       + ((_animatedParameters &  0x2000) ? frameCount : 1);
	_falloffStartPtr = _colorBPtr       + ((_animatedParameters &  0x4000) ? frameCount : 1);
	_falloffEndPtr   = _falloffStartPtr + ((_animatedParameters &  0x8000) ? frameCount : 1);
	_angleStartPtr   = _falloffEndPtr   + ((_animatedParameters & 0x10000) ? frameCount : 1);
	_angleEndPtr     = _angleStartPtr   + ((_animatedParameters & 0x20000) ? frameCount : 1);

	setupFrame(frame);
}

void Light::setupFrame(int frame) {
	int offset = frame % _frameCount;
	_matrix._m[0][0] = ((_animatedParameters &     0x1) ? _m11ptr[offset]          : *_m11ptr);
	_matrix._m[0][1] = ((_animatedParameters &     0x2) ? _m12ptr[offset]          : *_m12ptr);
	_matrix._m[0][2] = ((_animatedParameters &     0x4) ? _m13ptr[offset]          : *_m13ptr);
	_matrix._m[0][3] = ((_animatedParameters &     0x8) ? _m14ptr[offset]          : *_m14ptr);
	_matrix._m[1][0] = ((_animatedParameters &    0x10) ? _m21ptr[offset]          : *_m21ptr);
	_matrix._m[1][1] = ((_animatedParameters &    0x20) ? _m22ptr[offset]          : *_m22ptr);
	_matrix._m[1][2] = ((_animatedParameters &    0x40) ? _m23ptr[offset]          : *_m23ptr);
	_matrix._m[1][3] = ((_animatedParameters &    0x80) ? _m24ptr[offset]          : *_m24ptr);
	_matrix._m[2][0] = ((_animatedParameters &   0x100) ? _m31ptr[offset]          : *_m31ptr);
	_matrix._m[2][1] = ((_animatedParameters &   0x200) ? _m32ptr[offset]          : *_m32ptr);
	_matrix._m[2][2] = ((_animatedParameters &   0x400) ? _m33ptr[offset]          : *_m33ptr);
	_matrix._m[2][3] = ((_animatedParameters &   0x800) ? _m34ptr[offset]          : *_m34ptr);
	_color.r         = ((_animatedParameters &  0x1000) ? _colorRPtr[offset]       : *_colorRPtr);
	_color.g         = ((_animatedParameters &  0x2000) ? _colorGPtr[offset]       : *_colorGPtr);
	_color.b         = ((_animatedParameters &  0x4000) ? _colorBPtr[offset]       : *_colorBPtr);
	_falloffStart    = ((_animatedParameters &  0x8000) ? _falloffStartPtr[offset] : *_falloffStartPtr);
	_falloffEnd      = ((_animatedParameters & 0x10000) ? _falloffEndPtr[offset]   : *_falloffEndPtr);
	_angleStart      = ((_animatedParameters & 0x20000) ? _angleStartPtr[offset]   : *_angleStartPtr);
	_angleEnd        = ((_animatedParameters & 0x40000) ? _angleEndPtr[offset]     : *_angleEndPtr);
}

float Light::calculate(Vector3 start, Vector3 end) const {
	return calculateFalloutCoefficient(_matrix * start, _matrix * end, _falloffStart, _falloffEnd);
}

void Light::calculateColor(Color *outColor, Vector3 position) const {
	Vector3 positionT = _matrix * position;
	float att = attenuation(_falloffStart, _falloffEnd, positionT.length());
	outColor->r = _color.r * att;
	outColor->g = _color.g * att;
	outColor->b = _color.b * att;
}

float Light::calculateFalloutCoefficient(Vector3 start, Vector3 end, float falloffStart, float falloffEnd) const {
	if (falloffEnd == 0.0f) {
		return 1.0e30f;
	}

	if (falloffStart * falloffStart >= start.length() && falloffStart * falloffStart >= end.length()) {
		return 1.0e30f;
	}

	float diff = (end - start).length();
	float v31 = 0.0f;
	if (diff != 0.0f) {
		Vector3 v27 = Vector3::cross(start, (end - start));
		v31 = v27.length() / diff;
	}

	if (v31 < falloffEnd) {
		return 1.0f / (1.0f - (v31 / falloffEnd));
	}
	return 1.0e30f;
}

float Light::attenuation(float min, float max, float distance) const {
	if (max == 0.0f) {
		return 1.0f;
	}
	if (min < max) {
		distance = CLIP(distance, min, max);
		float x = (max - distance) / (max - min);
		return x * x * (3.0f - 2.0f * x);
	}
	if (distance < min) {
		return 1.0f;
	}
	return 0.0f;
}

float Light1::calculate(Vector3 start, Vector3 end) const {
	start = _matrix * start;
	end = _matrix * end;

	float v40 = 0.0f;
	if (_falloffEnd != 0.0f) {
		v40 = calculateFalloutCoefficient(start, end, _falloffStart, _falloffEnd);
	}

	float v41 = atan2(sqrt(start.x * start.x + start.y * start.y), -start.z);
	float v42 = atan2(sqrt(end.x * end.x + end.y * end.y), -end.z);

	float v43;
	if ((_angleStart >= v41 && _angleStart >= v42) || (_angleEnd <= v41 && _angleEnd <= v42)) {
		v43 = 1.0e30f;
	} else {
		v43 = 2.0;
	}
	if (v43 < v40) {
		return v40;
	} else {
		return v43;
	}
}

void Light1::calculateColor(Color *outColor, Vector3 position) const {
	Vector3 positionT = _matrix * position;

	outColor->r = 0.0f;
	outColor->g = 0.0f;
	outColor->b = 0.0f;

	if (positionT.z < 0.0f) {
		float v12 = attenuation(_angleStart, _angleEnd, atan2(sqrt(positionT.x * positionT.x + positionT.y * positionT.y), -positionT.z));
		float v13 = attenuation(_falloffStart, _falloffEnd, positionT.length());

		outColor->r = v12 * v13 * _color.r;
		outColor->g = v12 * v13 * _color.g;
		outColor->b = v12 * v13 * _color.b;
	}
}

float Light2::calculate(Vector3 start, Vector3 end) const {
	start = _matrix * start;
	end = _matrix * end;

	float v54 = 0.0f;
	if (_falloffEnd != 0.0f) {
		v54 = calculateFalloutCoefficient(start, end, _falloffStart, _falloffEnd);
	}

	float v55 = atan2(fabs(start.x), -start.z);
	float v58 = atan2(fabs(start.y), -start.z);
	float v57 = atan2(fabs(end.x), -end.z);
	float v56 = atan2(fabs(end.y), -end.z);

	float v59;
	if ((_angleStart >= v55 && _angleStart >= v57 && _angleStart >= v58 && _angleStart >= v56) || (_angleEnd <= v55 && _angleEnd <= v57 && _angleEnd <= v58 && _angleEnd <= v56)) {
		v59 = 1.0e30f;
	} else {
		v59 = 2.0f;
	}
	if (v59 < v54) {
		return v54;
	} else {
		return v59;
	}
}

void Light2::calculateColor(Color *outColor, Vector3 position) const {
	Vector3 positionT = _matrix * position;

	outColor->r = 0.0f;
	outColor->g = 0.0f;
	outColor->b = 0.0f;

	if (positionT.z < 0.0f) {
		float v11 = attenuation(_angleStart, _angleEnd, atan2(fabs(positionT.y), -positionT.z));
		float v12 = attenuation(_angleStart, _angleEnd, atan2(fabs(positionT.x), -positionT.z));
		float v13 = attenuation(_falloffStart, _falloffEnd, positionT.length());

		outColor->r = v11 * v12 * v13 * _color.r;
		outColor->g = v11 * v12 * v13 * _color.g;
		outColor->b = v11 * v12 * v13 * _color.b;
	}
}

void Light3::calculateColor(Color *outColor, Vector3 position) const {
	Vector3 positionT = _matrix * position;

	outColor->r = 0.0f;
	outColor->g = 0.0f;
	outColor->b = 0.0f;

	if (positionT.z < 0.0f) {
		float v12 = attenuation(_angleStart, _angleEnd, sqrt(positionT.x * positionT.x + positionT.y * positionT.y));
		float v13 = attenuation(_falloffStart, _falloffEnd, positionT.length());

		outColor->r = v12 * v13 * _color.r;
		outColor->g = v12 * v13 * _color.g;
		outColor->b = v12 * v13 * _color.b;
	}
}

void Light4::calculateColor(Color *outColor, Vector3 position) const {
	Vector3 positionT = _matrix * position;

	outColor->r = 0.0f;
	outColor->g = 0.0f;
	outColor->b = 0.0f;

	if (positionT.z < 0.0f) {
		float v11 = attenuation(_angleStart, _angleEnd, fabs(positionT.y));
		float v12 = attenuation(_angleStart, _angleEnd, fabs(positionT.x));
		float v13 = attenuation(_falloffStart, _falloffEnd, positionT.length());

		outColor->r = v11 * v12 * v13 * _color.r;
		outColor->g = v11 * v12 * v13 * _color.g;
		outColor->b = v11 * v12 * v13 * _color.b;
	}
}

float LightAmbient::calculate(Vector3 start, Vector3 end) const {
	return 1.0e30f;
}

void LightAmbient::calculateColor(Color *outColor, Vector3 position) const {
	outColor->r = _color.r;
	outColor->g = _color.g;
	outColor->b = _color.b;
}

} // End of namespace BladeRunner