2006-03-20 17:12:09 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
|
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Functions for sizing segments
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "inferno.h"
|
|
|
|
#include "editor.h"
|
2013-03-16 03:10:55 +00:00
|
|
|
#include "editor/esegment.h"
|
2012-07-07 18:35:06 +00:00
|
|
|
#include "dxxerror.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "gameseg.h"
|
2012-11-17 06:14:09 +00:00
|
|
|
#include "kdefs.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-08-12 03:09:32 +00:00
|
|
|
#include "compiler-range_for.h"
|
2014-10-12 23:10:05 +00:00
|
|
|
#include "highest_valid.h"
|
2014-08-12 03:09:32 +00:00
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
#define XDIM 0
|
|
|
|
#define YDIM 1
|
|
|
|
#define ZDIM 2
|
|
|
|
|
|
|
|
#define MAX_MODIFIED_VERTICES 32
|
|
|
|
int Modified_vertices[MAX_MODIFIED_VERTICES];
|
|
|
|
int Modified_vertex_index = 0;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
2013-10-27 22:00:14 +00:00
|
|
|
static void validate_modified_segments(void)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-09-26 02:42:11 +00:00
|
|
|
int v0;
|
2014-08-12 03:09:32 +00:00
|
|
|
visited_segment_bitarray_t modified_segments;
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int v=0; v<Modified_vertex_index; v++) {
|
2006-03-20 17:12:09 +00:00
|
|
|
v0 = Modified_vertices[v];
|
|
|
|
|
2014-10-12 23:10:05 +00:00
|
|
|
range_for (auto seg, highest_valid(Segments))
|
|
|
|
{
|
2013-12-26 22:21:16 +00:00
|
|
|
if (Segments[seg].segnum != segment_none)
|
2014-08-12 03:09:32 +00:00
|
|
|
{
|
|
|
|
if (modified_segments[seg])
|
|
|
|
continue;
|
|
|
|
range_for (auto w, Segments[seg].verts)
|
|
|
|
if (w == v0)
|
|
|
|
{
|
|
|
|
modified_segments[seg] = true;
|
|
|
|
validate_segment(&Segments[seg]);
|
|
|
|
for (unsigned s=0; s<MAX_SIDES_PER_SEGMENT; s++) {
|
|
|
|
Num_tilings = 1;
|
|
|
|
assign_default_uvs_to_side(&Segments[seg], s);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-12 03:09:32 +00:00
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Scale vertex *vertp by vector *vp, scaled by scale factor scale_factor
|
2013-10-27 22:00:14 +00:00
|
|
|
static void scale_vert_aux(int vertex_ind, vms_vector *vp, fix scale_factor)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
vms_vector *vertp = &Vertices[vertex_ind];
|
|
|
|
|
|
|
|
vertp->x += fixmul(vp->x,scale_factor)/2;
|
|
|
|
vertp->y += fixmul(vp->y,scale_factor)/2;
|
|
|
|
vertp->z += fixmul(vp->z,scale_factor)/2;
|
|
|
|
|
|
|
|
Assert(Modified_vertex_index < MAX_MODIFIED_VERTICES);
|
|
|
|
Modified_vertices[Modified_vertex_index++] = vertex_ind;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
2014-10-02 03:02:34 +00:00
|
|
|
static void scale_vert(const vsegptr_t sp, int vertex_ind, vms_vector *vp, fix scale_factor)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
switch (SegSizeMode) {
|
|
|
|
case SEGSIZEMODE_FREE:
|
|
|
|
if (is_free_vertex(vertex_ind))
|
|
|
|
scale_vert_aux(vertex_ind, vp, scale_factor);
|
|
|
|
break;
|
|
|
|
case SEGSIZEMODE_ALL:
|
|
|
|
scale_vert_aux(vertex_ind, vp, scale_factor);
|
|
|
|
break;
|
|
|
|
case SEGSIZEMODE_CURSIDE: {
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int v=0; v<4; v++)
|
2006-03-20 17:12:09 +00:00
|
|
|
if (sp->verts[Side_to_verts[Curside][v]] == vertex_ind)
|
|
|
|
scale_vert_aux(vertex_ind, vp, scale_factor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SEGSIZEMODE_EDGE: {
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int v=0; v<2; v++)
|
2006-03-20 17:12:09 +00:00
|
|
|
if (sp->verts[Side_to_verts[Curside][(Curedge+v)%4]] == vertex_ind)
|
|
|
|
scale_vert_aux(vertex_ind, vp, scale_factor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SEGSIZEMODE_VERTEX:
|
|
|
|
if (sp->verts[Side_to_verts[Curside][Curvert]] == vertex_ind)
|
|
|
|
scale_vert_aux(vertex_ind, vp, scale_factor);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Error("Unsupported SegSizeMode in ksegsize.c/scale_vert = %i\n", SegSizeMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
2014-10-02 03:02:34 +00:00
|
|
|
static void scale_free_verts(const vsegptr_t sp, vms_vector *vp, int side, fix scale_factor)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2013-07-14 21:47:36 +00:00
|
|
|
const sbyte *verts;
|
2006-03-20 17:12:09 +00:00
|
|
|
int vertex_ind;
|
|
|
|
|
|
|
|
verts = Side_to_verts[side];
|
|
|
|
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int v=0; v<4; v++) {
|
2006-03-20 17:12:09 +00:00
|
|
|
vertex_ind = sp->verts[(int) verts[v]];
|
|
|
|
if (SegSizeMode || is_free_vertex(vertex_ind))
|
|
|
|
scale_vert(sp, vertex_ind, vp, scale_factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Make segment *sp bigger in dimension dimension by amount amount.
|
2014-10-02 03:02:34 +00:00
|
|
|
static void med_scale_segment_new(const vsegptr_t sp, int dimension, fix amount)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
vms_matrix mat;
|
|
|
|
|
|
|
|
Modified_vertex_index = 0;
|
|
|
|
|
|
|
|
med_extract_matrix_from_segment(sp, &mat);
|
|
|
|
|
|
|
|
switch (dimension) {
|
|
|
|
case XDIM:
|
|
|
|
scale_free_verts(sp, &mat.rvec, WLEFT, -amount);
|
|
|
|
scale_free_verts(sp, &mat.rvec, WRIGHT, +amount);
|
|
|
|
break;
|
|
|
|
case YDIM:
|
|
|
|
scale_free_verts(sp, &mat.uvec, WBOTTOM, -amount);
|
|
|
|
scale_free_verts(sp, &mat.uvec, WTOP, +amount);
|
|
|
|
break;
|
|
|
|
case ZDIM:
|
|
|
|
scale_free_verts(sp, &mat.fvec, WFRONT, -amount);
|
|
|
|
scale_free_verts(sp, &mat.fvec, WBACK, +amount);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
validate_modified_segments();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Extract a vector from a segment. The vector goes from the start face to the end face.
|
|
|
|
// The point on each face is the average of the four points forming the face.
|
2014-10-02 03:02:34 +00:00
|
|
|
static void extract_vector_from_segment_side(const vsegptr_t sp, int side, vms_vector &vp, int vla, int vlb, int vra, int vrb)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
vms_vector v1, v2;
|
|
|
|
|
2014-09-28 21:11:45 +00:00
|
|
|
vm_vec_sub(v1,Vertices[sp->verts[Side_to_verts[side][vra]]],Vertices[sp->verts[Side_to_verts[side][vla]]]);
|
|
|
|
vm_vec_sub(v2,Vertices[sp->verts[Side_to_verts[side][vrb]]],Vertices[sp->verts[Side_to_verts[side][vlb]]]);
|
2014-10-02 03:02:37 +00:00
|
|
|
vm_vec_add(vp, v1, v2);
|
|
|
|
vm_vec_scale(vp, F1_0/2);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Extract the right vector from segment *sp, return in *vp.
|
|
|
|
// The forward vector is defined to be the vector from the the center of the left face of the segment
|
|
|
|
// to the center of the right face of the segment.
|
2014-10-02 03:02:34 +00:00
|
|
|
void med_extract_right_vector_from_segment_side(const vsegptr_t sp, int sidenum, vms_vector &vp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
extract_vector_from_segment_side(sp, sidenum, vp, 3, 2, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Extract the up vector from segment *sp, return in *vp.
|
|
|
|
// The forward vector is defined to be the vector from the the center of the bottom face of the segment
|
|
|
|
// to the center of the top face of the segment.
|
2014-10-02 03:02:34 +00:00
|
|
|
void med_extract_up_vector_from_segment_side(const vsegptr_t sp, int sidenum, vms_vector &vp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
extract_vector_from_segment_side(sp, sidenum, vp, 1, 2, 0, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Increase the size of Cursegp in dimension dimension by amount
|
2013-10-27 22:00:14 +00:00
|
|
|
static int segsize_common(int dimension, fix amount)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
int propagated[MAX_SIDES_PER_SEGMENT];
|
|
|
|
vms_vector uvec, rvec, fvec, scalevec;
|
|
|
|
|
|
|
|
Degenerate_segment_found = 0;
|
|
|
|
|
|
|
|
med_scale_segment_new(Cursegp, dimension, amount);
|
|
|
|
|
2014-10-02 03:02:37 +00:00
|
|
|
med_extract_up_vector_from_segment_side(Cursegp, Curside, uvec);
|
|
|
|
med_extract_right_vector_from_segment_side(Cursegp, Curside, rvec);
|
2014-10-26 21:37:06 +00:00
|
|
|
extract_forward_vector_from_segment(Cursegp, fvec);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-09-28 21:11:03 +00:00
|
|
|
scalevec.x = vm_vec_mag(rvec);
|
|
|
|
scalevec.y = vm_vec_mag(uvec);
|
|
|
|
scalevec.z = vm_vec_mag(fvec);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (Degenerate_segment_found) {
|
|
|
|
Degenerate_segment_found = 0;
|
|
|
|
editor_status("Applying scale would create degenerate segments. Aborting scale.");
|
|
|
|
med_scale_segment_new(Cursegp, dimension, -amount);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-26 22:01:13 +00:00
|
|
|
med_create_new_segment(scalevec);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
// For all segments to which Cursegp is connected, propagate tmap (uv coordinates) from the connected
|
|
|
|
// segment back to Cursegp. This will meaningfully propagate uv coordinates to all sides which havve
|
|
|
|
// an incident edge. It will also do some sides more than once. And it is probably just not what you want.
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int i=0; i<MAX_SIDES_PER_SEGMENT; i++)
|
2006-03-20 17:12:09 +00:00
|
|
|
propagated[i] = 0;
|
|
|
|
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int i=0; i<MAX_SIDES_PER_SEGMENT; i++)
|
2006-03-20 17:12:09 +00:00
|
|
|
if (IS_CHILD(Cursegp->children[i])) {
|
|
|
|
int s;
|
|
|
|
for (s=0; s<MAX_SIDES_PER_SEGMENT; s++)
|
|
|
|
propagated[s]++;
|
|
|
|
propagated[(int) Side_opposite[i]]--;
|
|
|
|
med_propagate_tmaps_to_segments(&Segments[Cursegp->children[i]],Cursegp,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, for all sides that were not adjacent to another side, and therefore did not get tmaps
|
|
|
|
// propagated to them, treat as a back side.
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int i=0; i<MAX_SIDES_PER_SEGMENT; i++)
|
2006-03-20 17:12:09 +00:00
|
|
|
if (!propagated[i]) {
|
|
|
|
med_propagate_tmaps_to_back_side(Cursegp, i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// New stuff, assign default texture to all affected sides.
|
|
|
|
|
|
|
|
Update_flags |= UF_WORLD_CHANGED;
|
|
|
|
mine_changed = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// ---------- segment size control ----------
|
|
|
|
|
|
|
|
int IncreaseSegLength()
|
|
|
|
{
|
|
|
|
return segsize_common(ZDIM,+F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegLength()
|
|
|
|
{
|
|
|
|
return segsize_common(ZDIM,-F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegWidth()
|
|
|
|
{
|
|
|
|
return segsize_common(XDIM,-F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int IncreaseSegWidth()
|
|
|
|
{
|
|
|
|
return segsize_common(XDIM,+F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int IncreaseSegHeight()
|
|
|
|
{
|
|
|
|
return segsize_common(YDIM,+F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegHeight()
|
|
|
|
{
|
|
|
|
return segsize_common(YDIM,-F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int IncreaseSegLengthBig()
|
|
|
|
{
|
|
|
|
return segsize_common(ZDIM,+5 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegLengthBig()
|
|
|
|
{
|
|
|
|
return segsize_common(ZDIM,-5 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegWidthBig()
|
|
|
|
{
|
|
|
|
return segsize_common(XDIM,-5 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int IncreaseSegWidthBig()
|
|
|
|
{
|
|
|
|
return segsize_common(XDIM,+5 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int IncreaseSegHeightBig()
|
|
|
|
{
|
|
|
|
return segsize_common(YDIM,+5 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegHeightBig()
|
|
|
|
{
|
|
|
|
return segsize_common(YDIM,-5 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int IncreaseSegLengthDefault()
|
|
|
|
{
|
|
|
|
return segsize_common(ZDIM,+40 *F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegLengthDefault()
|
|
|
|
{
|
|
|
|
return segsize_common(ZDIM,-40*F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int IncreaseSegWidthDefault()
|
|
|
|
{
|
|
|
|
return segsize_common(XDIM,+40*F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegWidthDefault()
|
|
|
|
{
|
|
|
|
return segsize_common(XDIM,-40*F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int IncreaseSegHeightDefault()
|
|
|
|
{
|
|
|
|
return segsize_common(YDIM,+40 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DecreaseSegHeightDefault()
|
|
|
|
{
|
|
|
|
return segsize_common(YDIM,-40 * F1_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
int ToggleSegSizeMode(void)
|
|
|
|
{
|
|
|
|
SegSizeMode++;
|
|
|
|
if (SegSizeMode > SEGSIZEMODE_MAX)
|
|
|
|
SegSizeMode = SEGSIZEMODE_MIN;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2013-10-27 22:00:14 +00:00
|
|
|
static int PerturbCursideCommon(fix amount)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
int saveSegSizeMode = SegSizeMode;
|
|
|
|
vms_vector fvec, rvec, uvec;
|
|
|
|
fix fmag, rmag, umag;
|
|
|
|
SegSizeMode = SEGSIZEMODE_CURSIDE;
|
|
|
|
|
|
|
|
Modified_vertex_index = 0;
|
|
|
|
|
2014-10-26 21:37:06 +00:00
|
|
|
extract_forward_vector_from_segment(Cursegp, fvec);
|
|
|
|
extract_right_vector_from_segment(Cursegp, rvec);
|
|
|
|
extract_up_vector_from_segment(Cursegp, uvec);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-09-28 21:11:03 +00:00
|
|
|
fmag = vm_vec_mag(fvec);
|
|
|
|
rmag = vm_vec_mag(rvec);
|
|
|
|
umag = vm_vec_mag(uvec);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-09-26 02:42:11 +00:00
|
|
|
for (int v=0; v<4; v++) {
|
2006-03-20 17:12:09 +00:00
|
|
|
vms_vector perturb_vec;
|
|
|
|
|
|
|
|
perturb_vec.x = fixmul(rmag, d_rand()*2 - 32767);
|
|
|
|
perturb_vec.y = fixmul(umag, d_rand()*2 - 32767);
|
|
|
|
perturb_vec.z = fixmul(fmag, d_rand()*2 - 32767);
|
|
|
|
|
|
|
|
scale_vert(Cursegp, Cursegp->verts[Side_to_verts[Curside][v]], &perturb_vec, amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate_segment(Cursegp);
|
|
|
|
// if (SegSizeMode) {
|
|
|
|
// for (i=0; i<MAX_SIDES_PER_SEGMENT; i++)
|
|
|
|
// if (Cursegp->children[i] != -1)
|
|
|
|
// validate_segment(&Segments[Cursegp->children[i]]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
validate_modified_segments();
|
|
|
|
SegSizeMode = saveSegSizeMode;
|
|
|
|
|
|
|
|
Update_flags |= UF_WORLD_CHANGED;
|
|
|
|
mine_changed = 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
int PerturbCurside(void)
|
|
|
|
{
|
|
|
|
PerturbCursideCommon(F1_0/10);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
int PerturbCursideBig(void)
|
|
|
|
{
|
|
|
|
PerturbCursideCommon(F1_0/2);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|