| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Lighting functions. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <bitset> |
| 28 |
#include <numeric> |
| 29 |
#include <stdio.h> |
| 30 |
#include <string.h> // for memset() |
| 31 |
|
| 32 |
#include "render_state.h" |
| 33 |
#include "maths.h" |
| 34 |
#include "vecmat.h" |
| 35 |
#include "gr.h" |
| 36 |
#include "inferno.h" |
| 37 |
#include "segment.h" |
| 38 |
#include "dxxerror.h" |
| 39 |
#include "game.h" |
| 40 |
#include "vclip.h" |
| 41 |
#include "lighting.h" |
| 42 |
#include "3d.h" |
| 43 |
#include "interp.h" |
| 44 |
#include "gameseg.h" |
| 45 |
#include "laser.h" |
| 46 |
#include "timer.h" |
| 47 |
#include "player.h" |
| 48 |
#include "playsave.h" |
| 49 |
#include "weapon.h" |
| 50 |
#include "powerup.h" |
| 51 |
#include "fvi.h" |
| 52 |
#include "object.h" |
| 53 |
#include "robot.h" |
| 54 |
#include "multi.h" |
| 55 |
#include "palette.h" |
| 56 |
#include "bm.h" |
| 57 |
#include "wall.h" |
| 58 |
|
| 59 |
#include "compiler-range_for.h" |
| 60 |
#include "d_bitset.h" |
| 61 |
#include "d_enumerate.h" |
| 62 |
#include "d_levelstate.h" |
| 63 |
#include "partial_range.h" |
| 64 |
#include "d_range.h" |
| 65 |
|
| 66 |
using std::min; |
| 67 |
|
| 68 |
#define HEADLIGHT_CONE_DOT (F1_0*9/10) |
| 69 |
#define HEADLIGHT_SCALE (F1_0*10) |
| 70 |
|
| 71 |
namespace dcx { |
| 72 |
namespace { |
| 73 |
|
| 74 |
static int use_fcd_lighting; |
| 75 |
|
| 76 |
static void add_light_div(g3s_lrgb &d, const g3s_lrgb &light, const fix &scale) |
| 77 |
{ |
| 78 |
d.r += fixdiv(light.r, scale); |
| 79 |
d.g += fixdiv(light.g, scale); |
| 80 |
d.b += fixdiv(light.b, scale); |
| 81 |
} |
| 82 |
|
| 83 |
static void add_light_dot_square(g3s_lrgb &d, const g3s_lrgb &light, const fix &dot) |
| 84 |
{ |
| 85 |
auto square = fixmul(dot, dot); |
| 86 |
d.r += fixmul(square, light.r)/8; |
| 87 |
d.g += fixmul(square, light.g)/8; |
| 88 |
d.b += fixmul(square, light.b)/8; |
| 89 |
} |
| 90 |
|
| 91 |
static fix compute_player_light_emission_intensity(const object_base &objp) |
| 92 |
{ |
| 93 |
auto &phys_info = objp.mtype.phys_info; |
| 94 |
const auto drag = phys_info.drag; |
| 95 |
const fix k = fixmuldiv(phys_info.mass, drag, (F1_0 - drag)); |
| 96 |
// smooth thrust value like set_thrust_from_velocity() |
| 97 |
const auto sthrust{vm_vec_copy_scale(phys_info.velocity, k)}; |
| 98 |
return std::max(static_cast<fix>(vm_vec_mag_quick(sthrust) / 4), F2_0) + F0_5; |
| 99 |
} |
| 100 |
|
| 101 |
static fix compute_fireball_light_emission_intensity(const d_vclip_array &Vclip, const object_base &objp) |
| 102 |
{ |
| 103 |
const auto oid = get_fireball_id(objp); |
| 104 |
if (!Vclip.valid_index(oid)) |
| 105 |
return 0; |
| 106 |
auto &v = Vclip[oid]; |
| 107 |
const auto light_intensity = v.light_value; |
| 108 |
if (objp.lifeleft < F1_0*4) |
| 109 |
return fixmul(fixdiv(objp.lifeleft, v.play_time), light_intensity); |
| 110 |
return light_intensity; |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// ---------------------------------------------------------------------------------------------- |
| 117 |
namespace dsx { |
| 118 |
namespace { |
| 119 |
|
| 120 |
static void apply_light(fvmsegptridx &vmsegptridx, const g3s_lrgb obj_light_emission, const vcsegptridx_t obj_seg, const vms_vector &obj_pos, const unsigned n_render_vertices, std::array<vertnum_t, MAX_VERTICES> &render_vertices, const std::array<segnum_t, MAX_VERTICES> &vert_segnum_list, const icobjptridx_t objnum) |
| 121 |
{ |
| 122 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 123 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 124 |
if (((obj_light_emission.r+obj_light_emission.g+obj_light_emission.b)/3) > 0) |
| 125 |
{ |
| 126 |
fix obji_64 = ((obj_light_emission.r+obj_light_emission.g+obj_light_emission.b)/3)*64; |
| 127 |
sbyte is_marker{0}; |
| 128 |
#if DXX_BUILD_DESCENT == 2 |
| 129 |
if (objnum && objnum->type == OBJ_MARKER) |
| 130 |
is_marker = 1; |
| 131 |
#endif |
| 132 |
|
| 133 |
auto &Dynamic_light = LevelUniqueLightState.Dynamic_light; |
| 134 |
auto &vcvertptr = Vertices.vcptr; |
| 135 |
// for pretty dim sources, only process vertices in object's own segment. |
| 136 |
// 12/04/95, MK, markers only cast light in own segment. |
| 137 |
if ((abs(obji_64) <= F1_0*8) || is_marker) { |
| 138 |
auto &vp = obj_seg->verts; |
| 139 |
|
| 140 |
range_for (const auto vertnum, vp) |
| 141 |
{ |
| 142 |
fix dist; |
| 143 |
auto &vertpos = *vcvertptr(vertnum); |
| 144 |
dist = vm_vec_dist_quick(obj_pos, vertpos); |
| 145 |
dist = fixmul(dist/4, dist/4); |
| 146 |
if (dist < abs(obji_64)) { |
| 147 |
if (dist < MIN_LIGHT_DIST) |
| 148 |
dist = MIN_LIGHT_DIST; |
| 149 |
|
| 150 |
add_light_div(Dynamic_light[vertnum], obj_light_emission, dist); |
| 151 |
} |
| 152 |
} |
| 153 |
} else { |
| 154 |
int headlight_shift{0}; |
| 155 |
fix max_headlight_dist = F1_0*200; |
| 156 |
|
| 157 |
#if DXX_BUILD_DESCENT == 2 |
| 158 |
if (objnum) |
| 159 |
{ |
| 160 |
const object &obj = *objnum; |
| 161 |
if (obj.type == OBJ_PLAYER) |
| 162 |
if (obj.ctype.player_info.powerup_flags & PLAYER_FLAGS_HEADLIGHT_ON) { |
| 163 |
headlight_shift = 3; |
| 164 |
if (get_player_id(obj) != Player_num) |
| 165 |
{ |
| 166 |
fvi_info hit_data; |
| 167 |
|
| 168 |
const auto tvec{vm_vec_scale_add(obj.pos, obj.orient.fvec, F1_0 * 200)}; |
| 169 |
const auto fate = find_vector_intersection(fvi_query{ |
| 170 |
obj.pos, |
| 171 |
tvec, |
| 172 |
fvi_query::unused_ignore_obj_list, |
| 173 |
fvi_query::unused_LevelUniqueObjectState, |
| 174 |
fvi_query::unused_Robot_info, |
| 175 |
FQ_TRANSWALL, |
| 176 |
objnum, |
| 177 |
}, obj_seg, 0, hit_data); |
| 178 |
if (fate != fvi_hit_type::None) |
| 179 |
max_headlight_dist = vm_vec_mag_quick(vm_vec_build_sub(hit_data.hit_pnt, obj.pos)) + F1_0*4; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
#endif |
| 184 |
range_for (const unsigned vv, xrange(n_render_vertices)) |
| 185 |
{ |
| 186 |
fix dist; |
| 187 |
int apply_light{0}; |
| 188 |
|
| 189 |
const auto vertnum = render_vertices[vv]; |
| 190 |
auto vsegnum = vert_segnum_list[vv]; |
| 191 |
auto &vertpos = *vcvertptr(vertnum); |
| 192 |
|
| 193 |
if (use_fcd_lighting && abs(obji_64) > F1_0*32) |
| 194 |
{ |
| 195 |
dist = find_connected_distance(obj_pos, obj_seg, vertpos, vmsegptridx(vsegnum), n_render_vertices, wall_is_doorway_mask::fly_rendpast); |
| 196 |
if (dist >= 0) |
| 197 |
apply_light = 1; |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
dist = vm_vec_dist_quick(obj_pos, vertpos); |
| 202 |
apply_light = 1; |
| 203 |
} |
| 204 |
|
| 205 |
if (apply_light && ((dist >> headlight_shift) < abs(obji_64))) { |
| 206 |
|
| 207 |
if (dist < MIN_LIGHT_DIST) |
| 208 |
dist = MIN_LIGHT_DIST; |
| 209 |
|
| 210 |
if (headlight_shift && objnum) |
| 211 |
{ |
| 212 |
fix dot; |
| 213 |
// MK, Optimization note: You compute distance about 15 lines up, this is partially redundant |
| 214 |
const auto vec_to_point = vm_vec_normalized_quick(vm_vec_build_sub(vertpos, obj_pos)); |
| 215 |
dot = vm_vec_build_dot(vec_to_point, objnum->orient.fvec); |
| 216 |
if (dot < F1_0/2) |
| 217 |
{ |
| 218 |
// Do the normal thing, but darken around headlight. |
| 219 |
add_light_div(Dynamic_light[vertnum], obj_light_emission, fixmul(HEADLIGHT_SCALE, dist)); |
| 220 |
} |
| 221 |
else |
| 222 |
{ |
| 223 |
if (!(Game_mode & GM_MULTI) || dist < max_headlight_dist) |
| 224 |
{ |
| 225 |
add_light_dot_square(Dynamic_light[vertnum], obj_light_emission, dot); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
add_light_div(Dynamic_light[vertnum], obj_light_emission, dist); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#define FLASH_LEN_FIXED_SECONDS (F1_0/3) |
| 242 |
#define FLASH_SCALE (3*F1_0/FLASH_LEN_FIXED_SECONDS) |
| 243 |
namespace { |
| 244 |
|
| 245 |
// ---------------------------------------------------------------------------------------------- |
| 246 |
static void cast_muzzle_flash_light(fvmsegptridx &vmsegptridx, int n_render_vertices, std::array<vertnum_t, MAX_VERTICES> &render_vertices, const std::array<segnum_t, MAX_VERTICES> &vert_segnum_list) |
| 247 |
{ |
| 248 |
fix64 current_time; |
| 249 |
short time_since_flash; |
| 250 |
|
| 251 |
current_time = timer_query(); |
| 252 |
|
| 253 |
range_for (auto &i, Muzzle_data) |
| 254 |
{ |
| 255 |
if (i.create_time) |
| 256 |
{ |
| 257 |
time_since_flash = current_time - i.create_time; |
| 258 |
if (time_since_flash < FLASH_LEN_FIXED_SECONDS) |
| 259 |
{ |
| 260 |
g3s_lrgb ml; |
| 261 |
ml.r = ml.g = ml.b = ((FLASH_LEN_FIXED_SECONDS - time_since_flash) * FLASH_SCALE); |
| 262 |
apply_light(vmsegptridx, ml, vmsegptridx(i.segnum), i.pos, n_render_vertices, render_vertices, vert_segnum_list, object_none); |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
i.create_time = 0; // turn off this muzzle flash |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
// Translation table to make flares flicker at different rates |
| 273 |
const std::array<fix, 16> Obj_light_xlate{{0x1234, 0x3321, 0x2468, 0x1735, |
| 274 |
0x0123, 0x19af, 0x3f03, 0x232a, |
| 275 |
0x2123, 0x39af, 0x0f03, 0x132a, |
| 276 |
0x3123, 0x29af, 0x1f03, 0x032a |
| 277 |
}}; |
| 278 |
#if DXX_BUILD_DESCENT == 1 |
| 279 |
#define compute_player_light_emission_intensity(LevelUniqueHeadlightState, obj) compute_player_light_emission_intensity(obj) |
| 280 |
#define compute_light_emission(Robot_info, LevelUniqueHeadlightState, Vclip, obj) compute_light_emission(Vclip, obj) |
| 281 |
#elif DXX_BUILD_DESCENT == 2 |
| 282 |
#undef compute_player_light_emission_intensity |
| 283 |
#undef compute_light_emission |
| 284 |
#endif |
| 285 |
} |
| 286 |
|
| 287 |
// --------------------------------------------------------- |
| 288 |
namespace dsx { |
| 289 |
namespace { |
| 290 |
|
| 291 |
#if DXX_BUILD_DESCENT == 2 |
| 292 |
static fix compute_player_light_emission_intensity(d_level_unique_headlight_state &LevelUniqueHeadlightState, const object &objp) |
| 293 |
{ |
| 294 |
if (objp.ctype.player_info.powerup_flags & PLAYER_FLAGS_HEADLIGHT_ON) |
| 295 |
{ |
| 296 |
auto &Headlights = LevelUniqueHeadlightState.Headlights; |
| 297 |
auto &Num_headlights = LevelUniqueHeadlightState.Num_headlights; |
| 298 |
if (Num_headlights < Headlights.size()) |
| 299 |
Headlights[Num_headlights++] = &objp; |
| 300 |
return HEADLIGHT_SCALE; |
| 301 |
} |
| 302 |
// If hoard game and player, add extra light based on how many orbs you have Pulse as well. |
| 303 |
if (game_mode_hoard(Game_mode)) |
| 304 |
{ |
| 305 |
if (const auto hoard_orbs{objp.ctype.player_info.hoard.orbs}) |
| 306 |
{ |
| 307 |
const fix hoardlight = 1 + (i2f(hoard_orbs) / 2); |
| 308 |
const auto s = fix_sin(static_cast<fix>(GameTime64 >> 1) & 0xFFFF); // probably a bad way to do it |
| 309 |
return fixmul((s + F1_0) >> 1, hoardlight); |
| 310 |
} |
| 311 |
} |
| 312 |
return ::dcx::compute_player_light_emission_intensity(objp); |
| 313 |
} |
| 314 |
#endif |
| 315 |
|
| 316 |
static g3s_lrgb build_object_color_from_bitmap(GameBitmaps_array &GameBitmaps, const bitmap_index i, grs_bitmap &bm) |
| 317 |
{ |
| 318 |
if (bm.get_flag_mask(BM_FLAG_PAGED_OUT)) |
| 319 |
piggy_bitmap_page_in(GameBitmaps, i); |
| 320 |
return g3s_lrgb{ |
| 321 |
.r = bm.avg_color_rgb[0], |
| 322 |
.g = bm.avg_color_rgb[1], |
| 323 |
.b = bm.avg_color_rgb[2], |
| 324 |
}; |
| 325 |
} |
| 326 |
|
| 327 |
static g3s_lrgb build_object_color_from_range(GameBitmaps_array &GameBitmaps, const bitmap_index index_begin, const bitmap_index index_end) |
| 328 |
{ |
| 329 |
g3s_lrgb obj_color{}; |
| 330 |
for (auto &&[i, bm] : enumerate(partial_range(GameBitmaps, underlying_value(index_begin), underlying_value(index_end)))) |
| 331 |
{ |
| 332 |
const auto r = build_object_color_from_bitmap(GameBitmaps, i, bm); |
| 333 |
obj_color.r += r.r; |
| 334 |
obj_color.g += r.g; |
| 335 |
obj_color.b += r.b; |
| 336 |
} |
| 337 |
return obj_color; |
| 338 |
} |
| 339 |
|
| 340 |
static g3s_lrgb build_object_color_from_vclip(GameBitmaps_array &GameBitmaps, const d_vclip_array &Vclip, const vclip_index id) |
| 341 |
{ |
| 342 |
auto &v = Vclip[id]; |
| 343 |
auto &f = v.frames; |
| 344 |
const auto t_idx_s = f[0]; |
| 345 |
const auto t_idx_e = f[v.num_frames - 1]; |
| 346 |
return build_object_color_from_range(GameBitmaps, t_idx_s, t_idx_e); |
| 347 |
} |
| 348 |
|
| 349 |
static g3s_lrgb build_object_color(GameBitmaps_array &GameBitmaps, const object_base &objp) |
| 350 |
{ |
| 351 |
switch (objp.render_type) |
| 352 |
{ |
| 353 |
case render_type::RT_POLYOBJ: |
| 354 |
{ |
| 355 |
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models; |
| 356 |
const polymodel *const po = &Polygon_models[objp.rtype.pobj_info.model_num]; |
| 357 |
if (const auto n_textures = po->n_textures; n_textures > 0) |
| 358 |
{ |
| 359 |
const bitmap_index t_idx_s = ObjBitmaps[ObjBitmapPtrs[po->first_texture]]; |
| 360 |
const bitmap_index t_idx_e{static_cast<uint16_t>(underlying_value(t_idx_s) + n_textures)}; |
| 361 |
return build_object_color_from_range(GameBitmaps, t_idx_s, t_idx_e); |
| 362 |
} |
| 363 |
/* If no texture, try to get a general polygon color */ |
| 364 |
if (const auto color = g3_poly_get_color(po->model_data.get())) |
| 365 |
{ |
| 366 |
auto &rgb = gr_current_pal[color]; |
| 367 |
return {rgb.r, rgb.g, rgb.b}; |
| 368 |
} |
| 369 |
/* If no color either, fall through and use a generic value */ |
| 370 |
} |
| 371 |
[[fallthrough]]; |
| 372 |
case render_type::RT_NONE: |
| 373 |
// no object - no light |
| 374 |
return {255, 255, 255}; |
| 375 |
case render_type::RT_LASER: |
| 376 |
{ |
| 377 |
const bitmap_index t_idx_s = Weapon_info[get_weapon_id(objp)].bitmap; |
| 378 |
return build_object_color_from_bitmap(GameBitmaps, t_idx_s, GameBitmaps[t_idx_s]); |
| 379 |
} |
| 380 |
case render_type::RT_POWERUP: |
| 381 |
return build_object_color_from_vclip(GameBitmaps, Vclip, objp.rtype.vclip_info.vclip_num); |
| 382 |
case render_type::RT_WEAPON_VCLIP: |
| 383 |
return build_object_color_from_vclip(GameBitmaps, Vclip, Weapon_info[get_weapon_id(objp)].weapon_vclip); |
| 384 |
default: |
| 385 |
return build_object_color_from_vclip(GameBitmaps, Vclip, vclip_index{objp.id}); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
static g3s_lrgb compute_light_emission(const d_robot_info_array &Robot_info, d_level_unique_headlight_state &LevelUniqueHeadlightState, const d_vclip_array &Vclip, const vcobjptridx_t obj) |
| 390 |
{ |
| 391 |
int compute_color{0}; |
| 392 |
fix light_intensity{0}; |
| 393 |
const object &objp = obj; |
| 394 |
switch (objp.type) |
| 395 |
{ |
| 396 |
case OBJ_PLAYER: |
| 397 |
light_intensity = compute_player_light_emission_intensity(LevelUniqueHeadlightState, objp); |
| 398 |
break; |
| 399 |
case OBJ_FIREBALL: |
| 400 |
light_intensity = compute_fireball_light_emission_intensity(Vclip, objp); |
| 401 |
break; |
| 402 |
case OBJ_ROBOT: |
| 403 |
#if DXX_BUILD_DESCENT == 1 |
| 404 |
light_intensity = F1_0/2; // F1_0*Robot_info[obj->id].lightcast; |
| 405 |
#elif DXX_BUILD_DESCENT == 2 |
| 406 |
light_intensity = F1_0*Robot_info[get_robot_id(objp)].lightcast; |
| 407 |
#endif |
| 408 |
break; |
| 409 |
case OBJ_WEAPON: |
| 410 |
{ |
| 411 |
const auto wid = get_weapon_id(objp); |
| 412 |
const fix tval = Weapon_info[wid].light; |
| 413 |
if (wid == weapon_id_type::FLARE_ID) |
| 414 |
light_intensity = 2 * (min(tval, objp.lifeleft) + ((static_cast<fix>(GameTime64) ^ Obj_light_xlate[obj.get_unchecked_index() % Obj_light_xlate.size()]) & 0x3fff)); |
| 415 |
else |
| 416 |
light_intensity = tval; |
| 417 |
break; |
| 418 |
} |
| 419 |
#if DXX_BUILD_DESCENT == 2 |
| 420 |
case OBJ_MARKER: |
| 421 |
{ |
| 422 |
fix lightval = objp.lifeleft; |
| 423 |
|
| 424 |
lightval &= 0xffff; |
| 425 |
lightval = 8 * abs(F1_0/2 - lightval); |
| 426 |
|
| 427 |
light_intensity = lightval; |
| 428 |
break; |
| 429 |
} |
| 430 |
#endif |
| 431 |
case OBJ_POWERUP: |
| 432 |
light_intensity = Powerup_info[get_powerup_id(objp)].light; |
| 433 |
break; |
| 434 |
case OBJ_DEBRIS: |
| 435 |
light_intensity = F1_0/4; |
| 436 |
break; |
| 437 |
case OBJ_LIGHT: |
| 438 |
light_intensity = objp.ctype.light_info.intensity; |
| 439 |
break; |
| 440 |
default: |
| 441 |
light_intensity = 0; |
| 442 |
break; |
| 443 |
} |
| 444 |
|
| 445 |
const auto &&white_light = [light_intensity] { |
| 446 |
return g3s_lrgb{light_intensity, light_intensity, light_intensity}; |
| 447 |
}; |
| 448 |
|
| 449 |
if (!PlayerCfg.DynLightColor) // colored lights not desired so use intensity only OR no intensity (== no light == no color) at all |
| 450 |
return white_light(); |
| 451 |
|
| 452 |
switch (objp.type) // find out if given object should cast colored light and compute if so |
| 453 |
{ |
| 454 |
default: |
| 455 |
break; |
| 456 |
case OBJ_FIREBALL: |
| 457 |
case OBJ_WEAPON: |
| 458 |
#if DXX_BUILD_DESCENT == 2 |
| 459 |
case OBJ_MARKER: |
| 460 |
#endif |
| 461 |
compute_color = 1; |
| 462 |
break; |
| 463 |
case OBJ_POWERUP: |
| 464 |
{ |
| 465 |
switch (get_powerup_id(objp)) |
| 466 |
{ |
| 467 |
case powerup_type_t::POW_EXTRA_LIFE: |
| 468 |
case powerup_type_t::POW_ENERGY: |
| 469 |
case powerup_type_t::POW_SHIELD_BOOST: |
| 470 |
case powerup_type_t::POW_KEY_BLUE: |
| 471 |
case powerup_type_t::POW_KEY_RED: |
| 472 |
case powerup_type_t::POW_KEY_GOLD: |
| 473 |
case powerup_type_t::POW_CLOAK: |
| 474 |
case powerup_type_t::POW_INVULNERABILITY: |
| 475 |
#if DXX_BUILD_DESCENT == 2 |
| 476 |
case powerup_type_t::POW_HOARD_ORB: |
| 477 |
#endif |
| 478 |
compute_color = 1; |
| 479 |
break; |
| 480 |
default: |
| 481 |
break; |
| 482 |
} |
| 483 |
break; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
if (compute_color) |
| 488 |
{ |
| 489 |
if (light_intensity < F1_0) // for every effect we want color, increase light_intensity so the effect becomes barely visible |
| 490 |
light_intensity = F1_0; |
| 491 |
|
| 492 |
const auto &&obj_color = build_object_color(GameBitmaps, objp); |
| 493 |
const fix rgbsum = obj_color.r + obj_color.g + obj_color.b; |
| 494 |
// obviously this object did not give us any usable color. so let's do our own but with blackjack and hookers! |
| 495 |
if (rgbsum <= 0) |
| 496 |
return white_light(); |
| 497 |
// scale color to light intensity |
| 498 |
const float cscale = static_cast<float>(light_intensity * 3) / rgbsum; |
| 499 |
return g3s_lrgb{ |
| 500 |
static_cast<fix>(obj_color.r * cscale), |
| 501 |
static_cast<fix>(obj_color.g * cscale), |
| 502 |
static_cast<fix>(obj_color.b * cscale) |
| 503 |
}; |
| 504 |
} |
| 505 |
|
| 506 |
return white_light(); |
| 507 |
} |
| 508 |
|
| 509 |
} |
| 510 |
|
| 511 |
// ---------------------------------------------------------------------------------------------- |
| 512 |
void set_dynamic_light(const d_robot_info_array &Robot_info, render_state_t &rstate) |
| 513 |
{ |
| 514 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 515 |
auto &vcobjptridx = Objects.vcptridx; |
| 516 |
std::array<vertnum_t, MAX_VERTICES> render_vertices; |
| 517 |
std::array<segnum_t, MAX_VERTICES> vert_segnum_list; |
| 518 |
static fix light_time; |
| 519 |
|
| 520 |
#if DXX_BUILD_DESCENT == 2 |
| 521 |
LevelUniqueLightState.Num_headlights = 0; |
| 522 |
#endif |
| 523 |
|
| 524 |
light_time += FrameTime; |
| 525 |
if (light_time < (F1_0/60)) // it's enough to stress the CPU 60 times per second |
| 526 |
return; |
| 527 |
light_time = light_time - (F1_0/60); |
| 528 |
|
| 529 |
enumerated_bitset<MAX_VERTICES, vertnum_t> render_vertex_flags; |
| 530 |
|
| 531 |
// Create list of vertices that need to be looked at for setting of ambient light. |
| 532 |
auto &Dynamic_light = LevelUniqueLightState.Dynamic_light; |
| 533 |
uint_fast32_t n_render_vertices{0}; |
| 534 |
range_for (const auto segnum, partial_const_range(rstate.Render_list, rstate.N_render_segs)) |
| 535 |
{ |
| 536 |
if (segnum != segment_none) { |
| 537 |
auto &vp = Segments[segnum].verts; |
| 538 |
range_for (const auto vnum, vp) |
| 539 |
{ |
| 540 |
auto &&b = render_vertex_flags[vnum]; |
| 541 |
if (!b) |
| 542 |
{ |
| 543 |
b = true; |
| 544 |
render_vertices[n_render_vertices] = vnum; |
| 545 |
vert_segnum_list[n_render_vertices] = segnum; |
| 546 |
n_render_vertices++; |
| 547 |
Dynamic_light[vnum] = {}; |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
cast_muzzle_flash_light(vmsegptridx, n_render_vertices, render_vertices, vert_segnum_list); |
| 554 |
|
| 555 |
range_for (const auto &&obj, vcobjptridx) |
| 556 |
{ |
| 557 |
const object &objp = obj; |
| 558 |
if (objp.type == OBJ_NONE) |
| 559 |
continue; |
| 560 |
const auto &&obj_light_emission = compute_light_emission(Robot_info, LevelUniqueLightState, Vclip, obj); |
| 561 |
|
| 562 |
if (((obj_light_emission.r+obj_light_emission.g+obj_light_emission.b)/3) > 0) |
| 563 |
apply_light(vmsegptridx, obj_light_emission, vcsegptridx(objp.segnum), objp.pos, n_render_vertices, render_vertices, vert_segnum_list, obj); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
// --------------------------------------------------------- |
| 568 |
|
| 569 |
#if DXX_BUILD_DESCENT == 2 |
| 570 |
|
| 571 |
void toggle_headlight_active(object &player) |
| 572 |
{ |
| 573 |
auto &player_info = player.ctype.player_info; |
| 574 |
if (player_info.powerup_flags & PLAYER_FLAGS_HEADLIGHT) { |
| 575 |
player_info.powerup_flags ^= PLAYER_FLAGS_HEADLIGHT_ON; |
| 576 |
if (Game_mode & GM_MULTI) |
| 577 |
multi_send_flags(player.id); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
namespace { |
| 582 |
|
| 583 |
static fix compute_headlight_light_on_object(const d_level_unique_headlight_state &LevelUniqueHeadlightState, const object_base &objp) |
| 584 |
{ |
| 585 |
fix light; |
| 586 |
|
| 587 |
// Let's just illuminate players and robots for speed reasons, ok? |
| 588 |
if (objp.type != OBJ_ROBOT && objp.type != OBJ_PLAYER) |
| 589 |
return 0; |
| 590 |
|
| 591 |
light = 0; |
| 592 |
|
| 593 |
range_for (const object_base *const light_objp, partial_const_range(LevelUniqueHeadlightState.Headlights, LevelUniqueHeadlightState.Num_headlights)) |
| 594 |
{ |
| 595 |
const auto &&[dist, vec_to_obj] = vm_vec_normalize_quick_with_magnitude(vm_vec_build_sub(objp.pos, light_objp->pos)); |
| 596 |
if (dist > 0) { |
| 597 |
const fix dot = vm_vec_build_dot(light_objp->orient.fvec, vec_to_obj); |
| 598 |
|
| 599 |
if (dot < F1_0/2) |
| 600 |
light += fixdiv(HEADLIGHT_SCALE, fixmul(HEADLIGHT_SCALE, dist)); // Do the normal thing, but darken around headlight. |
| 601 |
else |
| 602 |
light += fixmul(fixmul(dot, dot), HEADLIGHT_SCALE)/8; |
| 603 |
} |
| 604 |
} |
| 605 |
return light; |
| 606 |
} |
| 607 |
|
| 608 |
} |
| 609 |
#endif |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
namespace { |
| 614 |
|
| 615 |
//compute the average dynamic light in a segment. Takes the segment number |
| 616 |
static g3s_lrgb compute_seg_dynamic_light(const enumerated_array<g3s_lrgb, MAX_VERTICES, vertnum_t> &Dynamic_light, const shared_segment &seg) |
| 617 |
{ |
| 618 |
const auto &&op = [&Dynamic_light](g3s_lrgb r, const vertnum_t v) { |
| 619 |
r.r += Dynamic_light[v].r; |
| 620 |
r.g += Dynamic_light[v].g; |
| 621 |
r.b += Dynamic_light[v].b; |
| 622 |
return r; |
| 623 |
}; |
| 624 |
g3s_lrgb sum = std::accumulate(begin(seg.verts), end(seg.verts), g3s_lrgb{0, 0, 0}, op); |
| 625 |
sum.r >>= 3; |
| 626 |
sum.g >>= 3; |
| 627 |
sum.b >>= 3; |
| 628 |
return sum; |
| 629 |
} |
| 630 |
|
| 631 |
static std::array<g3s_lrgb, MAX_OBJECTS> object_light; |
| 632 |
static std::array<object_signature_t, MAX_OBJECTS> object_sig; |
| 633 |
static int reset_lighting_hack; |
| 634 |
} |
| 635 |
const object *old_viewer; |
| 636 |
#define LIGHT_RATE i2f(4) //how fast the light ramps up |
| 637 |
|
| 638 |
void start_lighting_frame(const object &viewer) |
| 639 |
{ |
| 640 |
reset_lighting_hack = (&viewer != old_viewer); |
| 641 |
old_viewer = &viewer; |
| 642 |
} |
| 643 |
|
| 644 |
namespace dsx { |
| 645 |
|
| 646 |
//compute the lighting for an object. Takes a pointer to the object, |
| 647 |
//and possibly a rotated 3d point. If the point isn't specified, the |
| 648 |
//object's center point is rotated. |
| 649 |
g3s_lrgb compute_object_light(const d_level_unique_light_state &LevelUniqueLightState, const vcobjptridx_t obj) |
| 650 |
{ |
| 651 |
g3s_lrgb light; |
| 652 |
const vcobjidx_t objnum = obj; |
| 653 |
|
| 654 |
//First, get static (mono) light for this segment |
| 655 |
const cscusegment objsegp = vcsegptr(obj->segnum); |
| 656 |
light.r = light.g = light.b = objsegp.u.static_light; |
| 657 |
|
| 658 |
auto &os = object_sig[objnum]; |
| 659 |
auto &ol = object_light[objnum]; |
| 660 |
//Now, maybe return different value to smooth transitions |
| 661 |
if (!reset_lighting_hack && os == obj->signature) |
| 662 |
{ |
| 663 |
fix frame_delta; |
| 664 |
g3s_lrgb delta_light; |
| 665 |
|
| 666 |
delta_light.r = light.r - ol.r; |
| 667 |
delta_light.g = light.g - ol.g; |
| 668 |
delta_light.b = light.b - ol.b; |
| 669 |
|
| 670 |
frame_delta = fixmul(LIGHT_RATE,FrameTime); |
| 671 |
|
| 672 |
if (abs(((delta_light.r+delta_light.g+delta_light.b)/3)) <= frame_delta) |
| 673 |
{ |
| 674 |
ol = light; //we've hit the goal |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
if (((delta_light.r+delta_light.g+delta_light.b)/3) < 0) |
| 679 |
frame_delta = -frame_delta; |
| 680 |
ol.r += frame_delta; |
| 681 |
ol.g += frame_delta; |
| 682 |
ol.b += frame_delta; |
| 683 |
light = ol; |
| 684 |
} |
| 685 |
|
| 686 |
} |
| 687 |
else //new object, initialize |
| 688 |
{ |
| 689 |
os = obj->signature; |
| 690 |
ol = light; |
| 691 |
} |
| 692 |
|
| 693 |
//Finally, add in dynamic light for this segment |
| 694 |
auto &Dynamic_light = LevelUniqueLightState.Dynamic_light; |
| 695 |
const auto &&seg_dl = compute_seg_dynamic_light(Dynamic_light, objsegp); |
| 696 |
#if DXX_BUILD_DESCENT == 2 |
| 697 |
//Next, add in (NOTE: WHITE) headlight on this object |
| 698 |
const fix mlight = compute_headlight_light_on_object(LevelUniqueLightState, obj); |
| 699 |
light.r += mlight; |
| 700 |
light.g += mlight; |
| 701 |
light.b += mlight; |
| 702 |
#endif |
| 703 |
|
| 704 |
light.r += seg_dl.r; |
| 705 |
light.g += seg_dl.g; |
| 706 |
light.b += seg_dl.b; |
| 707 |
|
| 708 |
return light; |
| 709 |
} |
| 710 |
|
| 711 |
} |