| 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 |
* Functions for sizing segments |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdlib.h> |
| 27 |
#include "editor.h" |
| 28 |
#include "editor/esegment.h" |
| 29 |
#include "dxxerror.h" |
| 30 |
#include "gameseg.h" |
| 31 |
#include "kdefs.h" |
| 32 |
|
| 33 |
#include "compiler-range_for.h" |
| 34 |
#include "d_range.h" |
| 35 |
|
| 36 |
#define XDIM 0 |
| 37 |
#define YDIM 1 |
| 38 |
#define ZDIM 2 |
| 39 |
|
| 40 |
#define MAX_MODIFIED_VERTICES 32 |
| 41 |
namespace { |
| 42 |
static std::array<vertnum_t, MAX_MODIFIED_VERTICES> Modified_vertices; |
| 43 |
int Modified_vertex_index{0}; |
| 44 |
} |
| 45 |
|
| 46 |
namespace dcx { |
| 47 |
|
| 48 |
namespace { |
| 49 |
|
| 50 |
// ------------------------------------------------------------------------------------------ |
| 51 |
// Extract a vector from a segment. The vector goes from the start face to the end face. |
| 52 |
// The point on each face is the average of the four points forming the face. |
| 53 |
[[nodiscard]] |
| 54 |
static vms_vector extract_vector_from_segment_side(const shared_segment &sp, const sidenum_t side, const side_relative_vertnum vla, const side_relative_vertnum vlb, const side_relative_vertnum vra, const side_relative_vertnum vrb) |
| 55 |
{ |
| 56 |
vms_vector vp; |
| 57 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 58 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 59 |
auto &sv = Side_to_verts[side]; |
| 60 |
auto &verts = sp.verts; |
| 61 |
auto &vcvertptr = Vertices.vcptr; |
| 62 |
vm_vec_add(vp, |
| 63 |
vm_vec_build_sub(vcvertptr(verts[sv[vra]]), vcvertptr(verts[sv[vla]])), |
| 64 |
vm_vec_build_sub(vcvertptr(verts[sv[vrb]]), vcvertptr(verts[sv[vlb]])) |
| 65 |
); |
| 66 |
vm_vec_scale(vp, F1_0/2); |
| 67 |
return vp; |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
// ------------------------------------------------------------------------------------------ |
| 73 |
// Extract the right vector from segment *sp, return in *vp. |
| 74 |
// The forward vector is defined to be the vector from the the center of the left face of the segment |
| 75 |
// to the center of the right face of the segment. |
| 76 |
vms_vector med_extract_right_vector_from_segment_side(const shared_segment &sp, const sidenum_t sidenum) |
| 77 |
{ |
| 78 |
return extract_vector_from_segment_side(sp, sidenum, side_relative_vertnum::_3, side_relative_vertnum::_2, side_relative_vertnum::_0, side_relative_vertnum::_1); |
| 79 |
} |
| 80 |
|
| 81 |
// ------------------------------------------------------------------------------------------ |
| 82 |
// Extract the up vector from segment *sp, return in *vp. |
| 83 |
// The forward vector is defined to be the vector from the the center of the bottom face of the segment |
| 84 |
// to the center of the top face of the segment. |
| 85 |
vms_vector med_extract_up_vector_from_segment_side(const shared_segment &sp, const sidenum_t sidenum) |
| 86 |
{ |
| 87 |
return extract_vector_from_segment_side(sp, sidenum, side_relative_vertnum::_1, side_relative_vertnum::_2, side_relative_vertnum::_0, side_relative_vertnum::_3); |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
namespace dsx { |
| 93 |
|
| 94 |
namespace { |
| 95 |
|
| 96 |
// ------------------------------------------------------------------------------------------ |
| 97 |
static void validate_modified_segments(void) |
| 98 |
{ |
| 99 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 100 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 101 |
visited_segment_bitarray_t modified_segments; |
| 102 |
auto &vcvertptr = Vertices.vcptr; |
| 103 |
for (int v=0; v<Modified_vertex_index; v++) { |
| 104 |
const auto v0 = Modified_vertices[v]; |
| 105 |
|
| 106 |
range_for (const auto &&segp, vmsegptridx) |
| 107 |
{ |
| 108 |
if (segp->segnum != segment_none) |
| 109 |
{ |
| 110 |
if (modified_segments[segp]) |
| 111 |
continue; |
| 112 |
range_for (const auto w, segp->verts) |
| 113 |
if (w == v0) |
| 114 |
{ |
| 115 |
modified_segments[segp] = true; |
| 116 |
validate_segment(vcvertptr, segp); |
| 117 |
for (const auto s : MAX_SIDES_PER_SEGMENT) |
| 118 |
{ |
| 119 |
Num_tilings = 1; |
| 120 |
assign_default_uvs_to_side(segp, s); |
| 121 |
} |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// ------------------------------------------------------------------------------------------ |
| 130 |
// Scale vertex *vertp by vector *vp, scaled by scale factor scale_factor |
| 131 |
static void scale_vert_aux(const vertnum_t vertex_ind, const vms_vector &vp, const fix scale_factor) |
| 132 |
{ |
| 133 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 134 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 135 |
auto &vmvertptr = Vertices.vmptr; |
| 136 |
auto &vertp = *vmvertptr(vertex_ind); |
| 137 |
|
| 138 |
vertp.x += fixmul(vp.x,scale_factor)/2; |
| 139 |
vertp.y += fixmul(vp.y,scale_factor)/2; |
| 140 |
vertp.z += fixmul(vp.z,scale_factor)/2; |
| 141 |
|
| 142 |
Assert(Modified_vertex_index < MAX_MODIFIED_VERTICES); |
| 143 |
Modified_vertices[Modified_vertex_index++] = vertex_ind; |
| 144 |
} |
| 145 |
|
| 146 |
// ------------------------------------------------------------------------------------------ |
| 147 |
static void scale_vert(const shared_segment &sp, const vertnum_t vertex_ind, const vms_vector &vp, const fix scale_factor) |
| 148 |
{ |
| 149 |
auto &verts = sp.verts; |
| 150 |
switch (SegSizeMode) { |
| 151 |
case SEGSIZEMODE_FREE: |
| 152 |
if (is_free_vertex(vcsegptr, vertex_ind)) |
| 153 |
scale_vert_aux(vertex_ind, vp, scale_factor); |
| 154 |
break; |
| 155 |
case SEGSIZEMODE_ALL: |
| 156 |
scale_vert_aux(vertex_ind, vp, scale_factor); |
| 157 |
break; |
| 158 |
case SEGSIZEMODE_CURSIDE: { |
| 159 |
range_for (const auto v, Side_to_verts[Curside]) |
| 160 |
if (verts[v] == vertex_ind) |
| 161 |
scale_vert_aux(vertex_ind, vp, scale_factor); |
| 162 |
break; |
| 163 |
} |
| 164 |
case SEGSIZEMODE_EDGE: { |
| 165 |
auto &sv = Side_to_verts[Curside]; |
| 166 |
const auto edge{Curedge}; |
| 167 |
range_for (const int v, xrange(2u)) |
| 168 |
if (verts[sv[next_side_vertex(edge, v)]] == vertex_ind) |
| 169 |
scale_vert_aux(vertex_ind, vp, scale_factor); |
| 170 |
break; |
| 171 |
} |
| 172 |
case SEGSIZEMODE_VERTEX: |
| 173 |
if (verts[Side_to_verts[Curside][Curvert]] == vertex_ind) |
| 174 |
scale_vert_aux(vertex_ind, vp, scale_factor); |
| 175 |
break; |
| 176 |
default: |
| 177 |
Error("Unsupported SegSizeMode in ksegsize.c/scale_vert = %i\n", SegSizeMode); |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
// ------------------------------------------------------------------------------------------ |
| 183 |
static void scale_free_verts(const shared_segment &sp, const vms_vector &vp, const sidenum_t side, fix scale_factor) |
| 184 |
{ |
| 185 |
range_for (auto &v, Side_to_verts[side]) |
| 186 |
{ |
| 187 |
const auto vertex_ind = sp.verts[v]; |
| 188 |
if (SegSizeMode || is_free_vertex(vcsegptr, vertex_ind)) |
| 189 |
scale_vert(sp, vertex_ind, vp, scale_factor); |
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
// ----------------------------------------------------------------------------- |
| 196 |
// Make segment *sp bigger in dimension dimension by amount amount. |
| 197 |
static void med_scale_segment_new(const shared_segment &sp, const int dimension, const fix amount) |
| 198 |
{ |
| 199 |
Modified_vertex_index = 0; |
| 200 |
|
| 201 |
auto mat{med_extract_matrix_from_segment(sp)}; |
| 202 |
|
| 203 |
const vms_vector *vec; |
| 204 |
sidenum_t side0, side1; |
| 205 |
switch (dimension) { |
| 206 |
case XDIM: |
| 207 |
side0 = sidenum_t::WLEFT; |
| 208 |
side1 = sidenum_t::WRIGHT; |
| 209 |
vec = &mat.rvec; |
| 210 |
break; |
| 211 |
case YDIM: |
| 212 |
side0 = sidenum_t::WBOTTOM; |
| 213 |
side1 = sidenum_t::WTOP; |
| 214 |
vec = &mat.uvec; |
| 215 |
break; |
| 216 |
case ZDIM: |
| 217 |
side0 = sidenum_t::WFRONT; |
| 218 |
side1 = sidenum_t::WBACK; |
| 219 |
vec = &mat.fvec; |
| 220 |
break; |
| 221 |
default: |
| 222 |
return; |
| 223 |
} |
| 224 |
scale_free_verts(sp, *vec, side0, -amount); |
| 225 |
scale_free_verts(sp, *vec, side1, +amount); |
| 226 |
|
| 227 |
validate_modified_segments(); |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
namespace { |
| 233 |
|
| 234 |
// ----------------------------------------------------------------------------- |
| 235 |
// Increase the size of Cursegp in dimension dimension by amount |
| 236 |
static int segsize_common(int dimension, fix amount) |
| 237 |
{ |
| 238 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 239 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 240 |
vms_vector scalevec; |
| 241 |
|
| 242 |
Degenerate_segment_found = 0; |
| 243 |
|
| 244 |
med_scale_segment_new(Cursegp, dimension, amount); |
| 245 |
|
| 246 |
const auto uvec{med_extract_up_vector_from_segment_side(Cursegp, Curside)}; |
| 247 |
const auto rvec{med_extract_right_vector_from_segment_side(Cursegp, Curside)}; |
| 248 |
auto &vcvertptr = Vertices.vcptr; |
| 249 |
const auto fvec{extract_forward_vector_from_segment(vcvertptr, Cursegp)}; |
| 250 |
|
| 251 |
scalevec.x = vm_vec_mag(rvec); |
| 252 |
scalevec.y = vm_vec_mag(uvec); |
| 253 |
scalevec.z = vm_vec_mag(fvec); |
| 254 |
|
| 255 |
if (Degenerate_segment_found) { |
| 256 |
Degenerate_segment_found = 0; |
| 257 |
editor_status("Applying scale would create degenerate segments. Aborting scale."); |
| 258 |
med_scale_segment_new(Cursegp, dimension, -amount); |
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
med_create_new_segment(scalevec); |
| 263 |
|
| 264 |
// For all segments to which Cursegp is connected, propagate tmap (uv coordinates) from the connected |
| 265 |
// segment back to Cursegp. This will meaningfully propagate uv coordinates to all sides which havve |
| 266 |
// an incident edge. It will also do some sides more than once. And it is probably just not what you want. |
| 267 |
per_side_array<int> propagated{}; |
| 268 |
for (const auto i : MAX_SIDES_PER_SEGMENT) |
| 269 |
{ |
| 270 |
const auto c = Cursegp->children[i]; |
| 271 |
if (IS_CHILD(c)) |
| 272 |
{ |
| 273 |
range_for (auto &s, propagated) |
| 274 |
++s; |
| 275 |
-- propagated[Side_opposite[i]]; |
| 276 |
med_propagate_tmaps_to_segments(vmsegptridx(c), Cursegp, 1); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// Now, for all sides that were not adjacent to another side, and therefore did not get tmaps |
| 281 |
// propagated to them, treat as a back side. |
| 282 |
for (const auto i : MAX_SIDES_PER_SEGMENT) |
| 283 |
if (!propagated[i]) { |
| 284 |
med_propagate_tmaps_to_back_side(Cursegp, i, 1); |
| 285 |
} |
| 286 |
|
| 287 |
// New stuff, assign default texture to all affected sides. |
| 288 |
|
| 289 |
Update_flags |= UF_WORLD_CHANGED; |
| 290 |
mine_changed = 1; |
| 291 |
return 1; |
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
// ----------------------------------------------------------------------------- |
| 297 |
// ---------- segment size control ---------- |
| 298 |
|
| 299 |
int IncreaseSegLength() |
| 300 |
{ |
| 301 |
return segsize_common(ZDIM,+F1_0); |
| 302 |
} |
| 303 |
|
| 304 |
int DecreaseSegLength() |
| 305 |
{ |
| 306 |
return segsize_common(ZDIM,-F1_0); |
| 307 |
} |
| 308 |
|
| 309 |
int DecreaseSegWidth() |
| 310 |
{ |
| 311 |
return segsize_common(XDIM,-F1_0); |
| 312 |
} |
| 313 |
|
| 314 |
int IncreaseSegWidth() |
| 315 |
{ |
| 316 |
return segsize_common(XDIM,+F1_0); |
| 317 |
} |
| 318 |
|
| 319 |
int IncreaseSegHeight() |
| 320 |
{ |
| 321 |
return segsize_common(YDIM,+F1_0); |
| 322 |
} |
| 323 |
|
| 324 |
int DecreaseSegHeight() |
| 325 |
{ |
| 326 |
return segsize_common(YDIM,-F1_0); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
int IncreaseSegLengthBig() |
| 331 |
{ |
| 332 |
return segsize_common(ZDIM,+5 * F1_0); |
| 333 |
} |
| 334 |
|
| 335 |
int DecreaseSegLengthBig() |
| 336 |
{ |
| 337 |
return segsize_common(ZDIM,-5 * F1_0); |
| 338 |
} |
| 339 |
|
| 340 |
int DecreaseSegWidthBig() |
| 341 |
{ |
| 342 |
return segsize_common(XDIM,-5 * F1_0); |
| 343 |
} |
| 344 |
|
| 345 |
int IncreaseSegWidthBig() |
| 346 |
{ |
| 347 |
return segsize_common(XDIM,+5 * F1_0); |
| 348 |
} |
| 349 |
|
| 350 |
int IncreaseSegHeightBig() |
| 351 |
{ |
| 352 |
return segsize_common(YDIM,+5 * F1_0); |
| 353 |
} |
| 354 |
|
| 355 |
int DecreaseSegHeightBig() |
| 356 |
{ |
| 357 |
return segsize_common(YDIM,-5 * F1_0); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
int IncreaseSegLengthDefault() |
| 362 |
{ |
| 363 |
return segsize_common(ZDIM,+40 *F1_0); |
| 364 |
} |
| 365 |
|
| 366 |
int DecreaseSegLengthDefault() |
| 367 |
{ |
| 368 |
return segsize_common(ZDIM,-40*F1_0); |
| 369 |
} |
| 370 |
|
| 371 |
int IncreaseSegWidthDefault() |
| 372 |
{ |
| 373 |
return segsize_common(XDIM,+40*F1_0); |
| 374 |
} |
| 375 |
|
| 376 |
int DecreaseSegWidthDefault() |
| 377 |
{ |
| 378 |
return segsize_common(XDIM,-40*F1_0); |
| 379 |
} |
| 380 |
|
| 381 |
int IncreaseSegHeightDefault() |
| 382 |
{ |
| 383 |
return segsize_common(YDIM,+40 * F1_0); |
| 384 |
} |
| 385 |
|
| 386 |
int DecreaseSegHeightDefault() |
| 387 |
{ |
| 388 |
return segsize_common(YDIM,-40 * F1_0); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
// --------------------------------------------------------------------------- |
| 393 |
int ToggleSegSizeMode(void) |
| 394 |
{ |
| 395 |
SegSizeMode++; |
| 396 |
if (SegSizeMode > SEGSIZEMODE_MAX) |
| 397 |
SegSizeMode = SEGSIZEMODE_MIN; |
| 398 |
|
| 399 |
return 1; |
| 400 |
} |
| 401 |
|
| 402 |
namespace { |
| 403 |
|
| 404 |
// --------------------------------------------------------------------------- |
| 405 |
static int PerturbCursideCommon(fix amount) |
| 406 |
{ |
| 407 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 408 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 409 |
int saveSegSizeMode = SegSizeMode; |
| 410 |
SegSizeMode = SEGSIZEMODE_CURSIDE; |
| 411 |
|
| 412 |
Modified_vertex_index = 0; |
| 413 |
|
| 414 |
auto &vcvertptr = Vertices.vcptr; |
| 415 |
const auto fvec{extract_forward_vector_from_segment(vcvertptr, Cursegp)}; |
| 416 |
const auto rvec{extract_right_vector_from_segment(vcvertptr, Cursegp)}; |
| 417 |
const auto uvec{extract_up_vector_from_segment(vcvertptr, Cursegp)}; |
| 418 |
|
| 419 |
const auto fmag = vm_vec_mag(fvec); |
| 420 |
const auto rmag = vm_vec_mag(rvec); |
| 421 |
const auto umag = vm_vec_mag(uvec); |
| 422 |
|
| 423 |
range_for (const auto v, Side_to_verts[Curside]) |
| 424 |
{ |
| 425 |
vms_vector perturb_vec; |
| 426 |
perturb_vec.x = fixmul(rmag, d_rand()*2 - 32767); |
| 427 |
perturb_vec.y = fixmul(umag, d_rand()*2 - 32767); |
| 428 |
perturb_vec.z = fixmul(fmag, d_rand()*2 - 32767); |
| 429 |
scale_vert(Cursegp, Cursegp->verts[v], perturb_vec, amount); |
| 430 |
} |
| 431 |
|
| 432 |
// validate_segment(Cursegp); |
| 433 |
// if (SegSizeMode) { |
| 434 |
// for (i=0; i<MAX_SIDES_PER_SEGMENT; i++) |
| 435 |
// if (Cursegp->children[i] != -1) |
| 436 |
// validate_segment(&Segments[Cursegp->children[i]]); |
| 437 |
// } |
| 438 |
|
| 439 |
validate_modified_segments(); |
| 440 |
SegSizeMode = saveSegSizeMode; |
| 441 |
|
| 442 |
Update_flags |= UF_WORLD_CHANGED; |
| 443 |
mine_changed = 1; |
| 444 |
|
| 445 |
return 1; |
| 446 |
} |
| 447 |
|
| 448 |
} |
| 449 |
|
| 450 |
// --------------------------------------------------------------------------- |
| 451 |
int PerturbCurside(void) |
| 452 |
{ |
| 453 |
PerturbCursideCommon(F1_0/10); |
| 454 |
|
| 455 |
return 1; |
| 456 |
} |
| 457 |
|
| 458 |
// --------------------------------------------------------------------------- |
| 459 |
int PerturbCursideBig(void) |
| 460 |
{ |
| 461 |
PerturbCursideCommon(F1_0/2); |
| 462 |
|
| 463 |
return 1; |
| 464 |
} |
| 465 |
|
| 466 |
} |