| 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-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Editor lighting functions. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include "inferno.h" |
| 28 |
#include "segment.h" |
| 29 |
#include "editor.h" |
| 30 |
#include "editor/esegment.h" |
| 31 |
#include "seguvs.h" |
| 32 |
#include "wall.h" |
| 33 |
#include "textures.h" |
| 34 |
#include "maths.h" |
| 35 |
#include "dxxerror.h" |
| 36 |
#include "kdefs.h" |
| 37 |
#include "gameseg.h" |
| 38 |
#include "texmap.h" |
| 39 |
|
| 40 |
#include "compiler-range_for.h" |
| 41 |
#include "d_range.h" |
| 42 |
|
| 43 |
// ----------------------------------------------------------------------------- |
| 44 |
// Return light intensity at an instance of a vertex on a side in a segment. |
| 45 |
static fix get_light_intensity(const unique_side &s, const side_relative_vertnum vert) |
| 46 |
{ |
| 47 |
return s.uvls[vert].l; |
| 48 |
} |
| 49 |
|
| 50 |
static fix get_light_intensity(const unique_segment &segp, const sidenum_t sidenum, const side_relative_vertnum vert) |
| 51 |
{ |
| 52 |
return get_light_intensity(segp.sides[sidenum], vert); |
| 53 |
} |
| 54 |
|
| 55 |
static fix clamp_light_intensity(const fix intensity) |
| 56 |
{ |
| 57 |
if (intensity < MIN_LIGHTING_VALUE) |
| 58 |
return MIN_LIGHTING_VALUE; |
| 59 |
if (intensity > MAX_LIGHTING_VALUE) |
| 60 |
return MAX_LIGHTING_VALUE; |
| 61 |
return intensity; |
| 62 |
} |
| 63 |
|
| 64 |
// ----------------------------------------------------------------------------- |
| 65 |
// Set light intensity at a vertex, saturating in .5 to 15.5 |
| 66 |
static void set_light_intensity(unique_side &s, const side_relative_vertnum vert, const fix intensity) |
| 67 |
{ |
| 68 |
s.uvls[vert].l = clamp_light_intensity(intensity); |
| 69 |
Update_flags |= UF_WORLD_CHANGED; |
| 70 |
} |
| 71 |
|
| 72 |
static void set_light_intensity(unique_segment &segp, const sidenum_t sidenum, const side_relative_vertnum vert, const fix intensity) |
| 73 |
{ |
| 74 |
set_light_intensity(segp.sides[sidenum], vert, intensity); |
| 75 |
} |
| 76 |
|
| 77 |
// ----------------------------------------------------------------------------- |
| 78 |
// Add light intensity to a vertex, saturating in .5 to 15.5 |
| 79 |
static void add_light_intensity_all_verts(unique_side &s, const fix intensity) |
| 80 |
{ |
| 81 |
range_for (auto &u, s.uvls) |
| 82 |
u.l = clamp_light_intensity(u.l + intensity); |
| 83 |
Update_flags |= UF_WORLD_CHANGED; |
| 84 |
} |
| 85 |
|
| 86 |
// ----------------------------------------------------------------------------- |
| 87 |
// Recursively apply light to segments. |
| 88 |
// If current side is a wall, apply light there. |
| 89 |
// If not a wall, apply light to child through that wall. |
| 90 |
// Notes: |
| 91 |
// It is possible to enter a segment twice by taking different paths. It is easy |
| 92 |
// to prevent this by maintaining a list of visited segments, but it is important |
| 93 |
// to reach segments with the greatest light intensity. This can be done by doing |
| 94 |
// a breadth-first-search, or by storing the applied intensity with a visited segment, |
| 95 |
// and if the current intensity is brighter, then apply the difference between it and |
| 96 |
// the previous intensity. |
| 97 |
// Note that it is also possible to visit the original light-casting segment, for example |
| 98 |
// going from segment 0 to 2, then from 2 to 0. This is peculiar and probably not |
| 99 |
// desired, but not entirely invalid. 2 reflects some light back to 0. |
| 100 |
static void apply_light_intensity(const csmusegment segp, const sidenum_t sidenum, fix intensity, const unsigned depth) |
| 101 |
{ |
| 102 |
if (intensity == 0) |
| 103 |
return; |
| 104 |
|
| 105 |
auto &Walls = LevelUniqueWallSubsystemState.Walls; |
| 106 |
auto &vcwallptr = Walls.vcptr; |
| 107 |
const auto wid_result = WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, segp, sidenum); |
| 108 |
if (!(wid_result & WALL_IS_DOORWAY_FLAG::rendpast)) { |
| 109 |
add_light_intensity_all_verts(segp.u.sides[sidenum], intensity); |
| 110 |
return; // we return because there is a wall here, and light does not shine through walls |
| 111 |
} |
| 112 |
|
| 113 |
// No wall here, so apply light recursively |
| 114 |
if (depth < 3) { |
| 115 |
intensity /= 3; |
| 116 |
if (!intensity) |
| 117 |
return; |
| 118 |
const csmusegment &&csegp = vmsegptr(segp.s.children[sidenum]); |
| 119 |
for (const auto s : MAX_SIDES_PER_SEGMENT) |
| 120 |
apply_light_intensity(csegp, s, intensity, depth+1); |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
// ----------------------------------------------------------------------------- |
| 126 |
// Top level recursive function for applying light. |
| 127 |
// Calls apply_light_intensity. |
| 128 |
// Uses light value on segp:sidenum (tmap_num2 defines light value) and applies |
| 129 |
// the associated intensity to segp. It calls apply_light_intensity to apply intensity/3 |
| 130 |
// to all neighbors. apply_light_intensity recursively calls itself to apply light to |
| 131 |
// subsequent neighbors (and forming loops, see above). |
| 132 |
static void propagate_light_intensity(const csmusegment segp, const sidenum_t sidenum) |
| 133 |
{ |
| 134 |
fix intensity; |
| 135 |
|
| 136 |
intensity = 0; |
| 137 |
auto &us = segp.u.sides[sidenum]; |
| 138 |
auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo; |
| 139 |
const auto texmap = get_texture_index(us.tmap_num); |
| 140 |
intensity += TmapInfo[texmap].lighting; |
| 141 |
intensity += TmapInfo[get_texture_index(us.tmap_num2)].lighting; |
| 142 |
|
| 143 |
if (intensity > 0) { |
| 144 |
add_light_intensity_all_verts(us, intensity); |
| 145 |
|
| 146 |
// Now, for all sides which are not the same as sidenum (the side casting the light), |
| 147 |
// add a light value to them (if they have no children, ie, they have a wall there). |
| 148 |
for (const auto s : MAX_SIDES_PER_SEGMENT) |
| 149 |
if (s != sidenum) |
| 150 |
apply_light_intensity(segp, s, intensity/2, 1); |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
// ----------------------------------------------------------------------------- |
| 157 |
// Highest level function, bound to a key. Apply ambient light to all segments based |
| 158 |
// on user-defined light sources. |
| 159 |
int LightAmbientLighting() |
| 160 |
{ |
| 161 |
for (auto &seg : vmsegptr) |
| 162 |
{ |
| 163 |
for (const auto side : MAX_SIDES_PER_SEGMENT) |
| 164 |
propagate_light_intensity(seg, side); |
| 165 |
} |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
// ----------------------------------------------------------------------------- |
| 171 |
int LightSelectNextVertex(void) |
| 172 |
{ |
| 173 |
Curvert = next_side_vertex(Curvert); |
| 174 |
Update_flags |= UF_WORLD_CHANGED; |
| 175 |
return 1; |
| 176 |
} |
| 177 |
|
| 178 |
// ----------------------------------------------------------------------------- |
| 179 |
int LightSelectNextEdge(void) |
| 180 |
{ |
| 181 |
Curedge = next_side_vertex(Curedge); |
| 182 |
Update_flags |= UF_WORLD_CHANGED; |
| 183 |
return 1; |
| 184 |
} |
| 185 |
|
| 186 |
// ----------------------------------------------------------------------------- |
| 187 |
// Copy intensity from current vertex to all other vertices on side. |
| 188 |
int LightCopyIntensity(void) |
| 189 |
{ |
| 190 |
int intensity; |
| 191 |
|
| 192 |
unique_segment &segp = Cursegp; |
| 193 |
intensity = get_light_intensity(segp, Curside, Curvert); |
| 194 |
|
| 195 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 196 |
if (v != Curvert) |
| 197 |
set_light_intensity(segp, Curside, v, intensity); |
| 198 |
|
| 199 |
return 1; |
| 200 |
} |
| 201 |
|
| 202 |
// ----------------------------------------------------------------------------- |
| 203 |
// Copy intensity from current vertex to all other vertices on side. |
| 204 |
int LightCopyIntensitySegment(void) |
| 205 |
{ |
| 206 |
int intensity; |
| 207 |
|
| 208 |
unique_segment &segp = Cursegp; |
| 209 |
intensity = get_light_intensity(segp, Curside, Curvert); |
| 210 |
|
| 211 |
for (const auto s : MAX_SIDES_PER_SEGMENT) |
| 212 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 213 |
if ((s != Curside) || (v != Curvert)) |
| 214 |
set_light_intensity(segp, s, v, intensity); |
| 215 |
|
| 216 |
return 1; |
| 217 |
} |
| 218 |
|
| 219 |
// ----------------------------------------------------------------------------- |
| 220 |
int LightDecreaseLightVertex(void) |
| 221 |
{ |
| 222 |
unique_segment &segp = Cursegp; |
| 223 |
set_light_intensity(segp, Curside, Curvert, get_light_intensity(segp, Curside, Curvert) - F1_0 / NUM_LIGHTING_LEVELS); |
| 224 |
|
| 225 |
return 1; |
| 226 |
} |
| 227 |
|
| 228 |
// ----------------------------------------------------------------------------- |
| 229 |
int LightIncreaseLightVertex(void) |
| 230 |
{ |
| 231 |
unique_segment &segp = Cursegp; |
| 232 |
set_light_intensity(segp, Curside, Curvert, get_light_intensity(segp, Curside, Curvert) + F1_0 / NUM_LIGHTING_LEVELS); |
| 233 |
|
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
// ----------------------------------------------------------------------------- |
| 238 |
int LightDecreaseLightSide(void) |
| 239 |
{ |
| 240 |
unique_segment &segp = Cursegp; |
| 241 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 242 |
set_light_intensity(segp, Curside, v, get_light_intensity(segp, Curside, v) - F1_0 / NUM_LIGHTING_LEVELS); |
| 243 |
|
| 244 |
return 1; |
| 245 |
} |
| 246 |
|
| 247 |
// ----------------------------------------------------------------------------- |
| 248 |
int LightIncreaseLightSide(void) |
| 249 |
{ |
| 250 |
unique_segment &segp = Cursegp; |
| 251 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 252 |
set_light_intensity(segp, Curside, v, get_light_intensity(segp, Curside, v) + F1_0 / NUM_LIGHTING_LEVELS); |
| 253 |
|
| 254 |
return 1; |
| 255 |
} |
| 256 |
|
| 257 |
// ----------------------------------------------------------------------------- |
| 258 |
int LightDecreaseLightSegment(void) |
| 259 |
{ |
| 260 |
unique_segment &segp = Cursegp; |
| 261 |
for (const auto s : MAX_SIDES_PER_SEGMENT) |
| 262 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 263 |
set_light_intensity(segp, s, v, get_light_intensity(segp, s, v) - F1_0 / NUM_LIGHTING_LEVELS); |
| 264 |
|
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
// ----------------------------------------------------------------------------- |
| 269 |
int LightIncreaseLightSegment(void) |
| 270 |
{ |
| 271 |
unique_segment &segp = Cursegp; |
| 272 |
for (const auto s : MAX_SIDES_PER_SEGMENT) |
| 273 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 274 |
set_light_intensity(segp, s, v, get_light_intensity(segp, s, v) + F1_0 / NUM_LIGHTING_LEVELS); |
| 275 |
|
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
// ----------------------------------------------------------------------------- |
| 280 |
int LightSetDefault(void) |
| 281 |
{ |
| 282 |
unique_segment &segp = Cursegp; |
| 283 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 284 |
set_light_intensity(segp, Curside, v, DEFAULT_LIGHTING); |
| 285 |
|
| 286 |
return 1; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
// ----------------------------------------------------------------------------- |
| 291 |
int LightSetMaximum(void) |
| 292 |
{ |
| 293 |
unique_segment &segp = Cursegp; |
| 294 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 295 |
set_light_intensity(segp, Curside, v, (NUM_LIGHTING_LEVELS - 1) * F1_0 / NUM_LIGHTING_LEVELS); |
| 296 |
|
| 297 |
return 1; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
// ----------------------------------------------------------------------------- |
| 302 |
int LightSetDefaultAll(void) |
| 303 |
{ |
| 304 |
|
| 305 |
assign_default_lighting_all(); |
| 306 |
|
| 307 |
Update_flags |= UF_WORLD_CHANGED; |
| 308 |
|
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|