2006-03-20 16:43:15 +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 16:43:15 +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
|
2010-12-24 04:26:24 +00:00
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
2006-03-20 16:43:15 +00:00
|
|
|
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
2008-04-06 20:23:28 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Interrogation functions for segment data structure.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "key.h"
|
|
|
|
#include "gr.h"
|
|
|
|
#include "inferno.h"
|
|
|
|
#include "segment.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 16:43:15 +00:00
|
|
|
#include "object.h"
|
|
|
|
#include "gameseg.h"
|
|
|
|
#include "render.h"
|
|
|
|
#include "game.h"
|
|
|
|
#include "wall.h"
|
|
|
|
#include "switch.h"
|
|
|
|
#include "fuelcen.h"
|
2010-12-24 04:26:24 +00:00
|
|
|
#include "cntrlcen.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
#include "seguvs.h"
|
|
|
|
#include "gameseq.h"
|
2012-11-17 06:14:09 +00:00
|
|
|
#include "kdefs.h"
|
2010-12-24 04:26:24 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
#include "medwall.h"
|
|
|
|
#include "hostage.h"
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
#include "compiler-poison.h"
|
2014-04-27 23:12:34 +00:00
|
|
|
#include "compiler-range_for.h"
|
2020-08-10 03:45:14 +00:00
|
|
|
#include "d_levelstate.h"
|
2019-05-04 18:27:36 +00:00
|
|
|
#include "d_range.h"
|
2019-05-04 18:27:36 +00:00
|
|
|
#include "d_enumerate.h"
|
|
|
|
#include "d_zip.h"
|
2014-08-02 16:49:59 +00:00
|
|
|
#include "segiter.h"
|
2014-04-27 23:12:34 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
int Do_duplicate_vertex_check = 0; // Gets set to 1 in med_create_duplicate_vertex, means to check for duplicate vertices in compress_mine
|
|
|
|
|
|
|
|
// Remap all vertices in polygons in a segment through translation table xlate_verts.
|
|
|
|
int ToggleBottom(void)
|
|
|
|
{
|
|
|
|
Render_only_bottom = !Render_only_bottom;
|
|
|
|
Update_flags = UF_WORLD_CHANGED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
namespace dcx {
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// This function can be used to determine whether a vertex is used exactly once in
|
|
|
|
// all segments, in which case it can be freely moved because it is not connected
|
|
|
|
// to any other segment.
|
2020-12-26 21:17:29 +00:00
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
int is_free_vertex(const fvcsegptr &vcsegptr, const vertnum_t vi)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-12-26 21:17:29 +00:00
|
|
|
unsigned count = 0;
|
|
|
|
for (const shared_segment &s : vcsegptr)
|
2015-01-12 00:26:02 +00:00
|
|
|
{
|
|
|
|
auto sp = &s;
|
2013-12-26 22:21:16 +00:00
|
|
|
if (sp->segnum != segment_none)
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &v, s.verts)
|
|
|
|
if (v == vi)
|
2020-12-26 21:17:29 +00:00
|
|
|
{
|
|
|
|
if (++ count > 1)
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Return true if one fixed point number is very close to another, else return false.
|
2013-10-27 22:00:14 +00:00
|
|
|
static int fnear(fix f1, fix f2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
return (abs(f1 - f2) <= FIX_EPSILON);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
2014-10-30 03:19:11 +00:00
|
|
|
static int vnear(const vms_vector &vp1, const vms_vector &vp2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2014-10-30 03:19:11 +00:00
|
|
|
return fnear(vp1.x, vp2.x) && fnear(vp1.y, vp2.y) && fnear(vp1.z, vp2.z);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Add the vertex *vp to the global list of vertices, return its index.
|
|
|
|
// Search until a matching vertex is found (has nearly the same coordinates) or until Num_vertices
|
|
|
|
// vertices have been looked at without a match. If no match, add a new vertex.
|
2020-12-26 21:17:29 +00:00
|
|
|
vertnum_t med_add_vertex(const vertex &vp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2006-03-20 16:43:15 +00:00
|
|
|
int count; // number of used vertices found, for loops exits when count == Num_vertices
|
|
|
|
|
|
|
|
// set_vertex_counts();
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
const auto Num_vertices = LevelSharedVertexState.Num_vertices;
|
2006-03-20 16:43:15 +00:00
|
|
|
Assert(Num_vertices < MAX_SEGMENT_VERTICES);
|
|
|
|
|
|
|
|
count = 0;
|
2020-12-26 21:17:29 +00:00
|
|
|
constexpr vertnum_t free_index_sentinel{UINT32_MAX};
|
|
|
|
vertnum_t free_index = free_index_sentinel;
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2020-12-26 21:17:29 +00:00
|
|
|
for (const unsigned vi : xrange(MAX_SEGMENT_VERTICES))
|
|
|
|
{
|
|
|
|
const vertnum_t v{vi};
|
2006-03-20 16:43:15 +00:00
|
|
|
if (Vertex_active[v]) {
|
2018-12-30 00:43:57 +00:00
|
|
|
if (vnear(vp, Vertices.vcptr(v))) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return v;
|
|
|
|
}
|
2020-12-26 21:17:29 +00:00
|
|
|
if (++ count >= Num_vertices)
|
|
|
|
break;
|
|
|
|
} else if (free_index == free_index_sentinel)
|
|
|
|
/* No break here. There might be a later vertex which
|
|
|
|
* matches the vnear test, and if there is, it should be
|
|
|
|
* used instead of adding a new vertex at the first free
|
|
|
|
* slot.
|
|
|
|
*/
|
2006-03-20 16:43:15 +00:00
|
|
|
free_index = v; // we want free_index to be the first free slot to add a vertex
|
2020-12-26 21:17:29 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
if (free_index == free_index_sentinel)
|
2006-03-20 16:43:15 +00:00
|
|
|
free_index = Num_vertices;
|
|
|
|
|
|
|
|
while (Vertex_active[free_index] && (free_index < MAX_VERTICES))
|
|
|
|
free_index++;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
*Vertices.vmptr(free_index) = vp;
|
2006-03-20 16:43:15 +00:00
|
|
|
Vertex_active[free_index] = 1;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
++LevelSharedVertexState.Num_vertices;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
if (Vertices.get_count() - 1 < free_index)
|
2017-08-11 23:43:54 +00:00
|
|
|
Vertices.set_count(free_index + 1);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
return free_index;
|
|
|
|
}
|
|
|
|
|
2015-12-22 04:18:50 +00:00
|
|
|
namespace dsx {
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Returns the index of a free segment.
|
|
|
|
// Scans the Segments array.
|
2018-09-19 02:13:29 +00:00
|
|
|
segnum_t get_free_segment_number(segment_array &Segments)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2013-12-29 04:28:07 +00:00
|
|
|
for (segnum_t segnum=0; segnum<MAX_SEGMENTS; segnum++)
|
2013-12-26 22:21:16 +00:00
|
|
|
if (Segments[segnum].segnum == segment_none) {
|
2018-09-19 02:13:29 +00:00
|
|
|
++ LevelSharedSegmentState.Num_segments;
|
2006-03-20 16:43:15 +00:00
|
|
|
if (segnum > Highest_segment_index)
|
2016-01-09 16:38:12 +00:00
|
|
|
Segments.set_count(segnum + 1);
|
2006-03-20 16:43:15 +00:00
|
|
|
return segnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert(0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Create a new segment, duplicating exactly, including vertex ids and children, the passed segment.
|
2018-09-19 02:13:29 +00:00
|
|
|
segnum_t med_create_duplicate_segment(segment_array &Segments, const segment &sp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2018-09-19 02:13:29 +00:00
|
|
|
const auto segnum = get_free_segment_number(Segments);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-09-19 02:13:29 +00:00
|
|
|
auto &nsp = *Segments.vmptr(segnum);
|
|
|
|
nsp = sp;
|
2020-08-10 03:45:13 +00:00
|
|
|
unique_segment &unsp = nsp;
|
|
|
|
unsp.objects = object_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
return segnum;
|
|
|
|
}
|
|
|
|
|
2015-12-22 04:18:50 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Add the vertex *vp to the global list of vertices, return its index.
|
|
|
|
// This is the same as med_add_vertex, except that it does not search for the presence of the vertex.
|
2014-10-02 03:02:34 +00:00
|
|
|
int med_create_duplicate_vertex(const vertex &vp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2018-12-30 00:43:57 +00:00
|
|
|
const auto Num_vertices = LevelSharedVertexState.Num_vertices;
|
2006-03-20 16:43:15 +00:00
|
|
|
Assert(Num_vertices < MAX_SEGMENT_VERTICES);
|
|
|
|
|
|
|
|
Do_duplicate_vertex_check = 1;
|
|
|
|
|
2017-08-11 23:43:54 +00:00
|
|
|
unsigned free_index = Num_vertices;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2006-03-20 16:43:15 +00:00
|
|
|
while (Vertex_active[free_index] && (free_index < MAX_VERTICES))
|
|
|
|
free_index++;
|
|
|
|
|
|
|
|
Assert(free_index < MAX_VERTICES);
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
|
|
|
*Vertices.vmptr(free_index) = vp;
|
2006-03-20 16:43:15 +00:00
|
|
|
Vertex_active[free_index] = 1;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
++LevelSharedVertexState.Num_vertices;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
if (Vertices.get_count() - 1 < free_index)
|
2017-08-11 23:43:54 +00:00
|
|
|
Vertices.set_count(free_index + 1);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
return free_index;
|
|
|
|
}
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
namespace {
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Set the vertex *vp at index vnum in the global list of vertices, return its index (just for compatibility).
|
2020-12-26 21:17:29 +00:00
|
|
|
vertnum_t med_set_vertex(d_level_shared_vertex_state &LevelSharedVertexState, const vertnum_t vnum, const vertex &vp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
|
|
|
*Vertices.vmptr(vnum) = vp;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Just in case this vertex wasn't active, mark it as active.
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2020-12-26 21:17:29 +00:00
|
|
|
if (auto &va = Vertex_active[vnum]; !va) {
|
|
|
|
va = 1;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return vnum;
|
|
|
|
}
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 04:18:50 +00:00
|
|
|
namespace dsx {
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
// -------------------------------------------------------------------------------
|
2020-09-11 03:08:02 +00:00
|
|
|
void create_removable_wall(fvcvertptr &vcvertptr, const vmsegptridx_t sp, const unsigned sidenum, const texture1_value tmap_num)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2018-09-19 02:13:29 +00:00
|
|
|
create_walls_on_side(vcvertptr, sp, sidenum);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-13 02:31:38 +00:00
|
|
|
sp->unique_segment::sides[sidenum].tmap_num = tmap_num;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
assign_default_uvs_to_side(sp, sidenum);
|
|
|
|
assign_light_to_side(sp, sidenum);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
|
|
// Orthogonalize matrix smat, returning result in rmat.
|
|
|
|
// Does not modify smat.
|
|
|
|
// Uses Gram-Schmidt process.
|
|
|
|
// See page 172 of Strang, Gilbert, Linear Algebra and its Applications
|
|
|
|
// Matt -- This routine should be moved to the vector matrix library.
|
|
|
|
// It IS legal for smat == rmat.
|
|
|
|
// We should also have the functions:
|
|
|
|
// mat_a = mat_b * scalar; // we now have mat_a = mat_a * scalar;
|
|
|
|
// mat_a = mat_b + mat_c * scalar; // or maybe not, maybe this is not primitive
|
|
|
|
void make_orthogonal(vms_matrix *rmat,vms_matrix *smat)
|
|
|
|
{
|
|
|
|
vms_matrix tmat;
|
|
|
|
vms_vector tvec1,tvec2;
|
|
|
|
float dot;
|
|
|
|
|
|
|
|
// Copy source matrix to work area.
|
|
|
|
tmat = *smat;
|
|
|
|
|
|
|
|
// Normalize the three rows of the matrix tmat.
|
|
|
|
vm_vec_normalize(&tmat.xrow);
|
|
|
|
vm_vec_normalize(&tmat.yrow);
|
|
|
|
vm_vec_normalize(&tmat.zrow);
|
|
|
|
|
|
|
|
// Now, compute the first vector.
|
|
|
|
// This is very easy -- just copy the (normalized) source vector.
|
|
|
|
rmat->zrow = tmat.zrow;
|
|
|
|
|
|
|
|
// Now, compute the second vector.
|
|
|
|
// From page 172 of Strang, we use the equation:
|
|
|
|
// b' = b - [transpose(q1) * b] * q1
|
|
|
|
// where: b = the second row of tmat
|
|
|
|
// q1 = the first row of rmat
|
|
|
|
// b' = the second row of rmat
|
|
|
|
|
|
|
|
// Compute: transpose(q1) * b
|
2014-09-28 21:41:59 +00:00
|
|
|
dot = vm_vec_dot(&rmat->zrow,&tmat.yrow);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Compute: b - dot * q1
|
|
|
|
rmat->yrow.x = tmat.yrow.x - fixmul(dot,rmat->zrow.x);
|
|
|
|
rmat->yrow.y = tmat.yrow.y - fixmul(dot,rmat->zrow.y);
|
|
|
|
rmat->yrow.z = tmat.yrow.z - fixmul(dot,rmat->zrow.z);
|
|
|
|
|
|
|
|
// Now, compute the third vector.
|
|
|
|
// From page 173 of Strang, we use the equation:
|
|
|
|
// c' = c - (q1*c)*q1 - (q2*c)*q2
|
|
|
|
// where: c = the third row of tmat
|
|
|
|
// q1 = the first row of rmat
|
|
|
|
// q2 = the second row of rmat
|
|
|
|
// c' = the third row of rmat
|
|
|
|
|
|
|
|
// Compute: q1*c
|
2014-09-28 21:41:59 +00:00
|
|
|
dot = vm_vec_dot(&rmat->zrow,&tmat.xrow);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
tvec1.x = fixmul(dot,rmat->zrow.x);
|
|
|
|
tvec1.y = fixmul(dot,rmat->zrow.y);
|
|
|
|
tvec1.z = fixmul(dot,rmat->zrow.z);
|
|
|
|
|
|
|
|
// Compute: q2*c
|
2014-09-28 21:41:59 +00:00
|
|
|
dot = vm_vec_dot(&rmat->yrow,&tmat.xrow);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
tvec2.x = fixmul(dot,rmat->yrow.x);
|
|
|
|
tvec2.y = fixmul(dot,rmat->yrow.y);
|
|
|
|
tvec2.z = fixmul(dot,rmat->yrow.z);
|
|
|
|
|
|
|
|
vm_vec_sub(&rmat->xrow,vm_vec_sub(&rmat->xrow,&tmat.xrow,&tvec1),&tvec2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Given a segment, extract the rotation matrix which defines it.
|
|
|
|
// Do this by extracting the forward, right, up vectors and then making them orthogonal.
|
|
|
|
// In the process of making the vectors orthogonal, favor them in the order forward, up, right.
|
|
|
|
// This means that the forward vector will remain unchanged.
|
2018-06-24 05:06:15 +00:00
|
|
|
void med_extract_matrix_from_segment(const shared_segment &sp, vms_matrix &rotmat)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2006-03-20 16:43:15 +00:00
|
|
|
vms_vector forwardvec,upvec;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2018-09-19 02:13:29 +00:00
|
|
|
extract_forward_vector_from_segment(vcvertptr, sp, forwardvec);
|
|
|
|
extract_up_vector_from_segment(vcvertptr, sp, upvec);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (((forwardvec.x == 0) && (forwardvec.y == 0) && (forwardvec.z == 0)) || ((upvec.x == 0) && (upvec.y == 0) && (upvec.z == 0))) {
|
2018-06-24 05:06:15 +00:00
|
|
|
rotmat = vmd_identity_matrix;
|
2006-03-20 16:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-24 05:06:15 +00:00
|
|
|
vm_vector_2_matrix(rotmat, forwardvec, &upvec, nullptr);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
vms_matrix rm;
|
|
|
|
|
|
|
|
extract_forward_vector_from_segment(sp,&rm.zrow);
|
|
|
|
extract_right_vector_from_segment(sp,&rm.xrow);
|
|
|
|
extract_up_vector_from_segment(sp,&rm.yrow);
|
|
|
|
|
|
|
|
vm_vec_normalize(&rm.xrow);
|
|
|
|
vm_vec_normalize(&rm.yrow);
|
|
|
|
vm_vec_normalize(&rm.zrow);
|
|
|
|
|
|
|
|
make_orthogonal(rotmat,&rm);
|
|
|
|
|
|
|
|
vm_vec_normalize(&rotmat->xrow);
|
|
|
|
vm_vec_normalize(&rotmat->yrow);
|
|
|
|
vm_vec_normalize(&rotmat->zrow);
|
|
|
|
|
|
|
|
// *rotmat = rm; // include this line (and remove the call to make_orthogonal) if you don't want the matrix orthogonalized
|
|
|
|
#endif
|
2015-12-22 04:18:50 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Given a rotation matrix *rotmat which describes the orientation of a segment
|
|
|
|
// and a side destside, return the rotation matrix which describes the orientation for the side.
|
2014-10-26 22:01:18 +00:00
|
|
|
void update_matrix_based_on_side(vms_matrix &rotmat,int destside)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2014-07-13 03:12:26 +00:00
|
|
|
vms_angvec rotvec;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
switch (destside) {
|
|
|
|
case WLEFT:
|
2014-07-13 03:12:26 +00:00
|
|
|
vm_angvec_make(&rotvec,0,0,-16384);
|
2015-02-05 03:03:51 +00:00
|
|
|
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix(rotvec));
|
2006-03-20 16:43:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WTOP:
|
2014-07-13 03:12:26 +00:00
|
|
|
vm_angvec_make(&rotvec,-16384,0,0);
|
2015-02-05 03:03:51 +00:00
|
|
|
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix(rotvec));
|
2006-03-20 16:43:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WRIGHT:
|
2014-07-13 03:12:26 +00:00
|
|
|
vm_angvec_make(&rotvec,0,0,16384);
|
2015-02-05 03:03:51 +00:00
|
|
|
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix(rotvec));
|
2006-03-20 16:43:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WBOTTOM:
|
2014-07-13 03:12:26 +00:00
|
|
|
vm_angvec_make(&rotvec,+16384,-32768,0); // bank was -32768, but I think that was an erroneous compensation
|
2015-02-05 03:03:51 +00:00
|
|
|
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix(rotvec));
|
2006-03-20 16:43:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WFRONT:
|
2014-07-13 03:12:26 +00:00
|
|
|
vm_angvec_make(&rotvec,0,0,-32768);
|
2015-02-05 03:03:51 +00:00
|
|
|
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix(rotvec));
|
2006-03-20 16:43:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WBACK:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------
|
2020-12-26 21:17:29 +00:00
|
|
|
static void change_vertex_occurrences(fvmsegptr &vmsegptr, const vertnum_t dest, const vertnum_t src)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
// Fix vertices in groups
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &g, partial_range(GroupList, num_groups))
|
|
|
|
g.vertices.replace(src, dest);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// now scan all segments, changing occurrences of src to dest
|
2020-12-26 21:17:29 +00:00
|
|
|
for (shared_segment &segp : vmsegptr)
|
|
|
|
if (segp.segnum != segment_none)
|
|
|
|
{
|
|
|
|
auto &verts = segp.verts;
|
|
|
|
std::replace(verts.begin(), verts.end(), src, dest);
|
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------
|
2013-10-27 22:00:14 +00:00
|
|
|
static void compress_vertices(void)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2018-12-30 00:43:57 +00:00
|
|
|
const auto Num_vertices = LevelSharedVertexState.Num_vertices;
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2018-12-30 00:43:57 +00:00
|
|
|
if (Vertices.get_count() == Num_vertices)
|
2006-03-20 16:43:15 +00:00
|
|
|
return;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
unsigned vert = Vertices.get_count() - 1; //MAX_SEGMENT_VERTICES-1;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vmvertptr = Vertices.vmptr;
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2017-02-19 19:33:38 +00:00
|
|
|
for (unsigned hole = 0; hole < vert; ++hole)
|
2020-12-26 21:17:29 +00:00
|
|
|
{
|
|
|
|
const vertnum_t vhole{hole};
|
|
|
|
if (auto &active_hole = Vertex_active[vhole]; !active_hole)
|
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
// found an unused vertex which is a hole if a used vertex follows (not necessarily immediately) it.
|
2020-12-26 21:17:29 +00:00
|
|
|
for (; hole < vert; --vert)
|
|
|
|
{
|
|
|
|
auto &active_vert = Vertex_active[vert];
|
|
|
|
if (!active_vert)
|
|
|
|
continue;
|
|
|
|
active_hole = std::exchange(active_vert, 0);
|
2006-03-20 16:43:15 +00:00
|
|
|
// Ok, hole is the index of a hole, vert is the index of a vertex which follows it.
|
|
|
|
// Copy vert into hole, update pointers to it.
|
2020-12-26 21:17:29 +00:00
|
|
|
auto &vp_vert = *vmvertptr(vert);
|
|
|
|
*vmvertptr(vhole) = vp_vert;
|
|
|
|
vp_vert = {};
|
|
|
|
DXX_MAKE_VAR_UNDEFINED(vp_vert);
|
|
|
|
change_vertex_occurrences(vmsegptr, vhole, vert);
|
2006-03-20 16:43:15 +00:00
|
|
|
vert--;
|
2020-12-26 21:17:29 +00:00
|
|
|
break;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-26 21:17:29 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2017-08-11 23:43:54 +00:00
|
|
|
Vertices.set_count(Num_vertices);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------------
|
2013-10-27 22:00:14 +00:00
|
|
|
static void compress_segments(void)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2019-03-03 00:31:08 +00:00
|
|
|
auto &Objects = LevelUniqueObjectState.Objects;
|
|
|
|
auto &vmobjptridx = Objects.vmptridx;
|
2018-09-19 02:13:29 +00:00
|
|
|
if (Highest_segment_index == LevelSharedSegmentState.Num_segments - 1)
|
2006-03-20 16:43:15 +00:00
|
|
|
return;
|
|
|
|
|
2013-12-29 04:28:07 +00:00
|
|
|
segnum_t hole,seg;
|
2006-03-20 16:43:15 +00:00
|
|
|
seg = Highest_segment_index;
|
|
|
|
|
2019-02-02 18:36:39 +00:00
|
|
|
auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
|
2018-12-30 00:43:58 +00:00
|
|
|
auto &Walls = LevelUniqueWallSubsystemState.Walls;
|
|
|
|
auto &vmwallptr = Walls.vmptr;
|
2006-03-20 16:43:15 +00:00
|
|
|
for (hole=0; hole < seg; hole++)
|
2013-12-26 22:21:16 +00:00
|
|
|
if (Segments[hole].segnum == segment_none) {
|
2006-03-20 16:43:15 +00:00
|
|
|
// found an unused segment which is a hole if a used segment follows (not necessarily immediately) it.
|
2013-12-26 22:21:16 +00:00
|
|
|
for ( ; (seg>hole) && (Segments[seg].segnum == segment_none); seg--)
|
2006-03-20 16:43:15 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
if (seg > hole) {
|
|
|
|
// Ok, hole is the index of a hole, seg is the index of a segment which follows it.
|
|
|
|
// Copy seg into hole, update pointers to it, update Cursegp, Markedsegp if necessary.
|
|
|
|
Segments[hole] = Segments[seg];
|
2013-12-26 22:21:16 +00:00
|
|
|
Segments[seg].segnum = segment_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (Cursegp == &Segments[seg])
|
2017-06-10 03:31:02 +00:00
|
|
|
Cursegp = imsegptridx(hole);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (Markedsegp == &Segments[seg])
|
2017-06-10 03:31:02 +00:00
|
|
|
Markedsegp = imsegptridx(hole);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Fix segments in groups
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &g, partial_range(GroupList, num_groups))
|
|
|
|
g.segments.replace(seg, hole);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Fix walls
|
2017-06-10 03:31:02 +00:00
|
|
|
range_for (const auto &&w, vmwallptr)
|
2016-02-12 04:02:28 +00:00
|
|
|
if (w->segnum == seg)
|
|
|
|
w->segnum = hole;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Fix fuelcenters, robotcens, and triggers... added 2/1/95 -Yuan
|
2019-02-02 18:36:39 +00:00
|
|
|
range_for (auto &f, partial_range(LevelUniqueFuelcenterState.Station, LevelUniqueFuelcenterState.Num_fuelcenters))
|
2015-01-12 00:26:02 +00:00
|
|
|
if (f.segnum == seg)
|
|
|
|
f.segnum = hole;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2019-03-12 04:01:07 +00:00
|
|
|
range_for (auto &f, partial_range(RobotCenters, LevelSharedRobotcenterState.Num_robot_centers))
|
2015-01-12 00:26:02 +00:00
|
|
|
if (f.segnum == seg)
|
|
|
|
f.segnum = hole;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:58 +00:00
|
|
|
auto &Triggers = LevelUniqueWallSubsystemState.Triggers;
|
|
|
|
auto &vmtrgptr = Triggers.vmptr;
|
2017-06-10 03:31:02 +00:00
|
|
|
range_for (const auto vt, vmtrgptr)
|
2016-10-02 00:34:39 +00:00
|
|
|
{
|
|
|
|
auto &t = *vt;
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &l, partial_range(t.seg, t.num_links))
|
|
|
|
if (l == seg)
|
|
|
|
l = hole;
|
2016-10-02 00:34:39 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2017-06-10 03:31:02 +00:00
|
|
|
auto &sp = *vmsegptr(hole);
|
2016-09-03 17:30:18 +00:00
|
|
|
range_for (auto &s, sp.children)
|
2015-01-12 00:26:02 +00:00
|
|
|
{
|
|
|
|
if (IS_CHILD(s)) {
|
2006-03-20 16:43:15 +00:00
|
|
|
// Find out on what side the segment connection to the former seg is on in *csegp.
|
2017-06-10 03:31:02 +00:00
|
|
|
range_for (auto &t, vmsegptr(s)->children)
|
2015-01-12 00:26:02 +00:00
|
|
|
{
|
|
|
|
if (t == seg) {
|
|
|
|
t = hole; // It used to be connected to seg, so make it connected to hole
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
} // end for t
|
|
|
|
} // end if
|
|
|
|
} // end for s
|
|
|
|
|
|
|
|
//Update object segment pointers
|
2017-08-13 20:38:31 +00:00
|
|
|
range_for (const auto objp, objects_in(sp, vmobjptridx, vmsegptr))
|
2014-08-02 16:49:59 +00:00
|
|
|
{
|
|
|
|
Assert(objp->segnum == seg);
|
|
|
|
objp->segnum = hole;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seg--;
|
|
|
|
|
|
|
|
} // end if (seg > hole)
|
|
|
|
} // end if
|
|
|
|
|
2018-09-19 02:13:29 +00:00
|
|
|
Segments.set_count(LevelSharedSegmentState.Num_segments);
|
2006-03-20 16:43:15 +00:00
|
|
|
med_create_new_segment_from_cursegp();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Combine duplicate vertices.
|
|
|
|
// If two vertices have the same coordinates, within some small tolerance, then assign
|
|
|
|
// the same vertex number to the two vertices, freeing up one of the vertices.
|
2020-05-02 21:18:42 +00:00
|
|
|
void med_combine_duplicate_vertices(std::array<uint8_t, MAX_VERTICES> &vlp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
|
|
|
auto &vcvertptridx = Vertices.vcptridx;
|
2017-08-11 23:43:54 +00:00
|
|
|
const auto &&range = make_range(vcvertptridx);
|
|
|
|
// Note: ok to do to <, rather than <= because w for loop starts at v+1
|
|
|
|
if (range.m_begin == range.m_end)
|
|
|
|
return;
|
|
|
|
for (auto i = range.m_begin;;)
|
|
|
|
{
|
|
|
|
const auto &&v = *i;
|
|
|
|
if (++i == range.m_end)
|
|
|
|
return;
|
2006-03-20 16:43:15 +00:00
|
|
|
if (vlp[v]) {
|
2017-08-11 23:43:54 +00:00
|
|
|
auto &vvp = *v;
|
|
|
|
auto subrange = range;
|
|
|
|
subrange.m_begin = i;
|
|
|
|
range_for (auto &&w, subrange)
|
2006-03-20 16:43:15 +00:00
|
|
|
if (vlp[w]) { // used to be Vertex_active[w]
|
2017-08-11 23:43:54 +00:00
|
|
|
if (vnear(vvp, *w)) {
|
2020-12-26 21:17:29 +00:00
|
|
|
change_vertex_occurrences(vmsegptr, v, w);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-11 23:43:54 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
// Compress mine at Segments and Vertices by squeezing out all holes.
|
|
|
|
// If no holes (ie, an unused segment followed by a used segment), then no action.
|
|
|
|
// If Cursegp or Markedsegp is a segment which gets moved to fill in a hole, then
|
|
|
|
// they are properly updated.
|
|
|
|
void med_compress_mine(void)
|
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2006-03-20 16:43:15 +00:00
|
|
|
if (Do_duplicate_vertex_check) {
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2006-03-20 16:43:15 +00:00
|
|
|
med_combine_duplicate_vertices(Vertex_active);
|
|
|
|
Do_duplicate_vertex_check = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
compress_segments();
|
|
|
|
compress_vertices();
|
|
|
|
set_vertex_counts();
|
|
|
|
|
|
|
|
//--repair-- create_local_segment_data();
|
|
|
|
|
|
|
|
// This is necessary becuase a segment search (due to click in 3d window) uses the previous frame's
|
|
|
|
// segment information, which could get changed by this.
|
|
|
|
Update_flags = UF_WORLD_CHANGED;
|
|
|
|
}
|
|
|
|
|
2015-12-22 04:18:50 +00:00
|
|
|
namespace dsx {
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Copy texture map ids for each face in sseg to dseg.
|
2018-12-13 02:31:38 +00:00
|
|
|
static void copy_tmap_ids(unique_segment &dseg, const unique_segment &sseg)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-08-24 01:31:28 +00:00
|
|
|
for (auto &&[ss, ds] : zip(sseg.sides, dseg.sides))
|
2019-05-04 18:27:36 +00:00
|
|
|
{
|
2020-08-24 01:31:28 +00:00
|
|
|
ds.tmap_num = ss.tmap_num;
|
2020-08-24 01:31:28 +00:00
|
|
|
ds.tmap_num2 = texture2_value::None;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Attach a segment with a rotated orientation.
|
|
|
|
// Return value:
|
|
|
|
// 0 = successful attach
|
|
|
|
// 1 = No room in Segments[].
|
|
|
|
// 2 = No room in Vertices[].
|
|
|
|
// 3 = newside != WFRONT -- for now, the new segment must be attached at its (own) front side
|
|
|
|
// 4 = already a face attached on destseg:destside
|
2020-08-24 01:31:28 +00:00
|
|
|
static int med_attach_segment_rotated(const vmsegptridx_t destseg, const csmusegment newseg, const unsigned destside, const unsigned newside, const vms_matrix &attmat)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2014-11-04 01:27:55 +00:00
|
|
|
vms_matrix rotmat,rotmat2,rotmat3;
|
2006-03-20 16:43:15 +00:00
|
|
|
vms_vector forvec,upvec;
|
|
|
|
|
|
|
|
// Return if already a face attached on this side.
|
|
|
|
if (IS_CHILD(destseg->children[destside]))
|
|
|
|
return 4;
|
|
|
|
|
2018-09-19 02:13:29 +00:00
|
|
|
const auto segnum = get_free_segment_number(Segments);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2014-11-02 03:45:01 +00:00
|
|
|
forvec = attmat.fvec;
|
|
|
|
upvec = attmat.uvec;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// We are pretty confident we can add the segment.
|
2015-12-22 04:18:51 +00:00
|
|
|
const auto &&nsp = destseg.absolute_sibling(segnum);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
nsp->segnum = segnum;
|
2020-08-10 03:45:13 +00:00
|
|
|
static_cast<unique_segment &>(nsp).objects = object_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
nsp->matcen_num = -1;
|
|
|
|
|
|
|
|
// Copy group value.
|
|
|
|
nsp->group = destseg->group;
|
|
|
|
|
|
|
|
// Add segment to proper group list.
|
|
|
|
if (nsp->group > -1)
|
2015-07-13 01:09:37 +00:00
|
|
|
add_segment_to_group(nsp, nsp->group);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Copy the texture map ids.
|
|
|
|
copy_tmap_ids(nsp,newseg);
|
|
|
|
|
|
|
|
// clear all connections
|
2017-02-19 19:33:38 +00:00
|
|
|
for (unsigned side = 0; side < MAX_SIDES_PER_SEGMENT; ++side)
|
|
|
|
{
|
2013-12-26 22:21:16 +00:00
|
|
|
nsp->children[side] = segment_none;
|
2018-12-13 02:31:38 +00:00
|
|
|
nsp->shared_segment::sides[side].wall_num = wall_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Form the connection
|
|
|
|
destseg->children[destside] = segnum;
|
|
|
|
// destseg->sides[destside].render_flag = 0;
|
2014-10-02 03:02:34 +00:00
|
|
|
nsp->children[newside] = destseg;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Copy vertex indices of the four vertices forming the joint
|
2015-02-05 03:03:50 +00:00
|
|
|
auto &dvp = Side_to_verts[destside];
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Set the vertex indices for the four vertices forming the front of the new side
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const unsigned v, xrange(4u))
|
2016-07-06 01:54:24 +00:00
|
|
|
nsp->verts[v] = destseg->verts[static_cast<int>(dvp[v])];
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// The other 4 vertices must be created.
|
|
|
|
// Their coordinates are determined by the 4 welded vertices and the vector from front
|
|
|
|
// to back of the original *newseg.
|
|
|
|
|
|
|
|
// Do lots of hideous matrix stuff, about 3/4 of which could probably be simplified out.
|
2018-06-24 05:06:15 +00:00
|
|
|
med_extract_matrix_from_segment(destseg, rotmat); // get orientation matrix for destseg (orthogonal rotation matrix)
|
2014-10-26 22:01:18 +00:00
|
|
|
update_matrix_based_on_side(rotmat,destside);
|
2014-11-04 01:27:55 +00:00
|
|
|
const auto rotmat1 = vm_vector_2_matrix(forvec,&upvec,nullptr);
|
2014-10-01 02:28:42 +00:00
|
|
|
const auto rotmat4 = vm_matrix_x_matrix(rotmat,rotmat1); // this is the desired orientation of the new segment
|
2018-06-24 05:06:15 +00:00
|
|
|
med_extract_matrix_from_segment(newseg, rotmat3); // this is the current orientation of the new segment
|
2014-09-28 21:11:04 +00:00
|
|
|
vm_transpose_matrix(rotmat3); // get the inverse of the current orientation matrix
|
2014-10-01 02:28:42 +00:00
|
|
|
vm_matrix_x_matrix(rotmat2,rotmat4,rotmat3); // now rotmat2 takes the current segment to the desired orientation
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Warning -- look at this line!
|
2014-09-28 21:11:04 +00:00
|
|
|
vm_transpose_matrix(rotmat2); // added 12:33 pm, 10/01/93
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Compute and rotate the center point of the attaching face.
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2018-03-12 03:43:47 +00:00
|
|
|
const auto &&vc0 = compute_center_point_on_side(vcvertptr, newseg, newside);
|
2014-11-04 01:31:22 +00:00
|
|
|
const auto vr = vm_vec_rotate(vc0,rotmat2);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Now rotate the free vertices in the segment
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<vertex, 4> tvs;
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const unsigned v, xrange(4u))
|
2020-08-24 01:31:28 +00:00
|
|
|
vm_vec_rotate(tvs[v], vcvertptr(newseg.s.verts[v + 4]), rotmat2);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Now translate the new segment so that the center point of the attaching faces are the same.
|
2018-03-12 03:43:47 +00:00
|
|
|
const auto &&vc1 = compute_center_point_on_side(vcvertptr, destseg, destside);
|
2014-10-30 03:32:27 +00:00
|
|
|
const auto xlate_vec = vm_vec_sub(vc1,vr);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Create and add the 4 new vertices.
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const unsigned v, xrange(4u))
|
2017-02-19 19:33:38 +00:00
|
|
|
{
|
2014-09-28 21:43:00 +00:00
|
|
|
vm_vec_add2(tvs[v],xlate_vec);
|
2014-10-02 03:02:34 +00:00
|
|
|
nsp->verts[v+4] = med_add_vertex(tvs[v]);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_vertex_counts();
|
|
|
|
|
|
|
|
// Now all the vertices are in place. Create the faces.
|
2018-09-19 02:13:30 +00:00
|
|
|
validate_segment(vcvertptr, nsp);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Say to not render at the joint.
|
|
|
|
// destseg->sides[destside].render_flag = 0;
|
|
|
|
// nsp->sides[newside].render_flag = 0;
|
|
|
|
|
|
|
|
Cursegp = nsp;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Attach side newside of newseg to side destside of destseg.
|
|
|
|
// Copies *newseg into global array Segments, increments Num_segments.
|
|
|
|
// Forms a weld between the two segments by making the new segment fit to the old segment.
|
|
|
|
// Updates number of faces per side if necessitated by new vertex coordinates.
|
|
|
|
// Updates Cursegp.
|
|
|
|
// Return value:
|
|
|
|
// 0 = successful attach
|
|
|
|
// 1 = No room in Segments[].
|
|
|
|
// 2 = No room in Vertices[].
|
|
|
|
// 3 = newside != WFRONT -- for now, the new segment must be attached at its (own) front side
|
|
|
|
// 4 = already a face attached on side newside
|
2020-08-24 01:31:28 +00:00
|
|
|
int med_attach_segment(const vmsegptridx_t destseg, const csmusegment newseg, const unsigned destside, const unsigned newside)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
int rval;
|
2015-07-13 01:09:37 +00:00
|
|
|
const auto ocursegp = Cursegp;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
vms_angvec tang = {0,0,0};
|
2015-02-05 03:03:51 +00:00
|
|
|
const auto &&rotmat = vm_angles_2_matrix(tang);
|
2014-11-02 03:45:01 +00:00
|
|
|
rval = med_attach_segment_rotated(destseg,newseg,destside,newside,rotmat);
|
2006-03-20 16:43:15 +00:00
|
|
|
med_propagate_tmaps_to_segments(ocursegp,Cursegp,0);
|
|
|
|
med_propagate_tmaps_to_back_side(Cursegp, Side_opposite[newside],0);
|
2017-06-10 03:31:02 +00:00
|
|
|
copy_uvs_seg_to_seg(vmsegptr(&New_segment), Cursegp);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
|
2015-12-22 04:18:50 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Delete a vertex, sort of.
|
|
|
|
// Decrement the vertex count. If the count goes to 0, then the vertex is free (has been deleted).
|
2017-02-19 19:33:38 +00:00
|
|
|
static void delete_vertex(const unsigned v)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
Assert(v < MAX_VERTICES); // abort if vertex is not in array Vertices
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2006-03-20 16:43:15 +00:00
|
|
|
Assert(Vertex_active[v] >= 1); // abort if trying to delete a non-existent vertex
|
|
|
|
|
|
|
|
Vertex_active[v]--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Update Num_vertices.
|
|
|
|
// This routine should be called by anyone who calls delete_vertex. It could be called in delete_vertex,
|
|
|
|
// but then it would be called much more often than necessary, and it is a slow routine.
|
2013-10-27 22:00:14 +00:00
|
|
|
static void update_num_vertices(void)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2006-03-20 16:43:15 +00:00
|
|
|
// Now count the number of vertices.
|
2017-08-11 23:43:54 +00:00
|
|
|
unsigned n = 0;
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2018-12-30 00:43:57 +00:00
|
|
|
range_for (const auto v, partial_range(Vertex_active, Vertices.get_count()))
|
2017-08-11 23:43:54 +00:00
|
|
|
if (v)
|
|
|
|
++n;
|
2018-12-30 00:43:57 +00:00
|
|
|
LevelSharedVertexState.Num_vertices = n;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 04:18:50 +00:00
|
|
|
namespace dsx {
|
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Set Vertex_active to number of occurrences of each vertex.
|
|
|
|
// Set Num_vertices.
|
|
|
|
void set_vertex_counts(void)
|
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2018-12-30 00:43:57 +00:00
|
|
|
unsigned Num_vertices = 0;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2016-05-21 17:24:51 +00:00
|
|
|
Vertex_active = {};
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Count number of occurrences of each vertex.
|
2017-06-10 03:31:02 +00:00
|
|
|
range_for (const auto &&segp, vmsegptr)
|
2015-06-13 22:42:19 +00:00
|
|
|
{
|
|
|
|
if (segp->segnum != segment_none)
|
|
|
|
range_for (auto &v, segp->verts)
|
2015-01-12 00:26:02 +00:00
|
|
|
{
|
|
|
|
if (!Vertex_active[v])
|
2006-03-20 16:43:15 +00:00
|
|
|
Num_vertices++;
|
2015-01-12 00:26:02 +00:00
|
|
|
++ Vertex_active[v];
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
2015-06-13 22:42:19 +00:00
|
|
|
}
|
2018-12-30 00:43:57 +00:00
|
|
|
LevelSharedVertexState.Num_vertices = Num_vertices;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Delete all vertices in segment *sp from the vertex list if they are not contained in another segment.
|
|
|
|
// This is kind of a dangerous routine. It modifies the global array Vertex_active, using the field as
|
|
|
|
// a count.
|
2018-12-30 00:43:57 +00:00
|
|
|
static void delete_vertices_in_segment(const shared_segment &sp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
// init_vertices();
|
|
|
|
set_vertex_counts();
|
|
|
|
// Subtract one count for each appearance of vertex in deleted segment
|
2018-12-30 00:43:57 +00:00
|
|
|
range_for (auto &v, sp.verts)
|
2015-01-12 00:26:02 +00:00
|
|
|
delete_vertex(v);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
update_num_vertices();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Delete segment *sp in Segments array.
|
|
|
|
// Return value:
|
|
|
|
// 0 successfully deleted.
|
|
|
|
// 1 unable to delete.
|
2017-06-10 03:31:02 +00:00
|
|
|
int med_delete_segment(const vmsegptridx_t sp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2019-03-03 00:31:08 +00:00
|
|
|
auto &Objects = LevelUniqueObjectState.Objects;
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2019-03-03 00:31:08 +00:00
|
|
|
auto &vmobjptr = Objects.vmptr;
|
|
|
|
auto &vmobjptridx = Objects.vmptridx;
|
2015-01-12 00:26:02 +00:00
|
|
|
segnum_t segnum = sp;
|
2006-03-20 16:43:15 +00:00
|
|
|
// Cannot delete segment if only segment.
|
2018-09-19 02:13:29 +00:00
|
|
|
if (LevelSharedSegmentState.Num_segments == 1)
|
2006-03-20 16:43:15 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Don't try to delete if segment doesn't exist.
|
2013-12-26 22:21:16 +00:00
|
|
|
if (sp->segnum == segment_none) {
|
2006-03-20 16:43:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete its refueling center if it has one
|
|
|
|
fuelcen_delete(sp);
|
|
|
|
|
|
|
|
delete_vertices_in_segment(sp);
|
|
|
|
|
2018-09-19 02:13:29 +00:00
|
|
|
-- LevelSharedSegmentState.Num_segments;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// If deleted segment has walls on any side, wipe out the wall.
|
2016-05-21 17:24:51 +00:00
|
|
|
for (unsigned side = 0; side < MAX_SIDES_PER_SEGMENT; ++side)
|
2018-12-13 02:31:38 +00:00
|
|
|
if (sp->shared_segment::sides[side].wall_num != wall_none)
|
2006-03-20 16:43:15 +00:00
|
|
|
wall_remove_side(sp, side);
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2006-03-20 16:43:15 +00:00
|
|
|
// Find out what this segment was connected to and break those connections at the other end.
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &side, sp->children)
|
|
|
|
if (IS_CHILD(side)) {
|
2015-12-22 04:18:51 +00:00
|
|
|
const auto &&csp = sp.absolute_sibling(side);
|
2013-12-29 04:28:07 +00:00
|
|
|
for (int s=0; s<MAX_SIDES_PER_SEGMENT; s++)
|
2006-03-20 16:43:15 +00:00
|
|
|
if (csp->children[s] == segnum) {
|
2013-12-26 22:21:16 +00:00
|
|
|
csp->children[s] = segment_none; // this is the side of connection, break it
|
2018-09-19 02:13:29 +00:00
|
|
|
validate_segment_side(vcvertptr, csp, s); // we have converted a connection to a side so validate the segment
|
2006-03-20 16:43:15 +00:00
|
|
|
med_propagate_tmaps_to_back_side(csp,s,0);
|
|
|
|
}
|
|
|
|
Cursegp = csp;
|
|
|
|
med_create_new_segment_from_cursegp();
|
2017-06-10 03:31:02 +00:00
|
|
|
copy_uvs_seg_to_seg(vmsegptr(&New_segment), Cursegp);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 22:21:16 +00:00
|
|
|
sp->segnum = segment_none; // Mark segment as inactive.
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// If deleted segment = marked segment, then say there is no marked segment
|
|
|
|
if (sp == Markedsegp)
|
2015-07-12 01:04:18 +00:00
|
|
|
Markedsegp = segment_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// If deleted segment = a Group segment ptr, then wipe it out.
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &s, partial_range(Groupsegp, num_groups))
|
|
|
|
if (s == sp)
|
|
|
|
s = nullptr;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// If deleted segment = group segment, wipe it off the group list.
|
|
|
|
if (sp->group > -1)
|
2014-10-02 03:02:34 +00:00
|
|
|
delete_segment_from_group(sp, sp->group);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// If we deleted something which was not connected to anything, must now select a new current segment.
|
|
|
|
if (Cursegp == sp)
|
2013-12-29 04:28:07 +00:00
|
|
|
for (segnum_t s=0; s<MAX_SEGMENTS; s++)
|
2013-12-26 22:21:16 +00:00
|
|
|
if ((Segments[s].segnum != segment_none) && (s!=segnum) ) {
|
2017-06-10 03:31:02 +00:00
|
|
|
Cursegp = imsegptridx(s);
|
2006-03-20 16:43:15 +00:00
|
|
|
med_create_new_segment_from_cursegp();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If deleted segment contains objects, wipe out all objects
|
2017-08-13 20:38:31 +00:00
|
|
|
range_for (const auto objnum, objects_in(*sp, vmobjptridx, vmsegptr))
|
2014-08-02 16:49:59 +00:00
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
//if an object is in the seg, delete it
|
|
|
|
//if the object is the player, move to new curseg
|
2014-08-02 16:49:59 +00:00
|
|
|
if (objnum == ConsoleObject) {
|
2018-03-12 03:43:47 +00:00
|
|
|
compute_segment_center(vcvertptr, ConsoleObject->pos,Cursegp);
|
2018-03-12 03:43:46 +00:00
|
|
|
obj_relink(vmobjptr, vmsegptr, objnum, Cursegp);
|
2006-03-20 16:43:15 +00:00
|
|
|
} else
|
2018-12-30 00:43:57 +00:00
|
|
|
obj_delete(LevelUniqueObjectState, Segments, objnum);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are leaving many holes in Segments or Vertices, then compress mine, because it is inefficient to be that way
|
|
|
|
// if ((Highest_segment_index > Num_segments+4) || (Highest_vertex_index > Num_vertices+4*8))
|
|
|
|
// med_compress_mine();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Copy texture maps from sseg to dseg
|
2019-05-04 18:27:36 +00:00
|
|
|
static void copy_tmaps_to_segment(segment &dstseg, const segment &srcseg)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2019-05-04 18:27:36 +00:00
|
|
|
shared_segment &shared_dst_seg = dstseg;
|
|
|
|
unique_segment &unique_dst_seg = dstseg;
|
|
|
|
const shared_segment &shared_src_seg = srcseg;
|
|
|
|
const unique_segment &unique_src_seg = srcseg;
|
|
|
|
range_for (const auto &&z, zip(shared_src_seg.sides, shared_dst_seg.sides, unique_src_seg.sides, unique_dst_seg.sides))
|
|
|
|
{
|
|
|
|
auto &shared_src_side = std::get<0>(z);
|
|
|
|
auto &shared_dst_side = std::get<1>(z);
|
|
|
|
auto &unique_src_side = std::get<2>(z);
|
|
|
|
auto &unique_dst_side = std::get<3>(z);
|
|
|
|
shared_dst_side.set_type(shared_src_side.get_type());
|
|
|
|
unique_dst_side.tmap_num = unique_src_side.tmap_num;
|
|
|
|
unique_dst_side.tmap_num2 = unique_src_side.tmap_num2;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Rotate the segment *seg by the pitch, bank, heading defined by *rot, destructively
|
|
|
|
// modifying its four free vertices in the global array Vertices.
|
|
|
|
// It is illegal to rotate a segment which has connectivity != 1.
|
|
|
|
// Pitch, bank, heading are about the point which is the average of the four points
|
|
|
|
// forming the side of connection.
|
|
|
|
// Return value:
|
|
|
|
// 0 = successful rotation
|
|
|
|
// 1 = Connectivity makes rotation illegal (connected to 0 or 2+ segments)
|
|
|
|
// 2 = Rotation causes degeneracy, such as self-intersecting segment.
|
|
|
|
// 3 = Unable to rotate because not connected to exactly 1 segment.
|
2017-06-10 03:31:02 +00:00
|
|
|
int med_rotate_segment(const vmsegptridx_t seg, const vms_matrix &rotmat)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2019-05-04 18:27:36 +00:00
|
|
|
int newside=0,destside;
|
2006-03-20 16:43:15 +00:00
|
|
|
int count;
|
|
|
|
|
|
|
|
// Find side of attachment
|
|
|
|
count = 0;
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const auto &&es, enumerate(seg->children))
|
|
|
|
if (IS_CHILD(es.value))
|
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
count++;
|
2019-05-04 18:27:36 +00:00
|
|
|
newside = es.idx;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return if passed in segment is connected to other than 1 segment.
|
|
|
|
if (count != 1)
|
|
|
|
return 3;
|
|
|
|
|
2015-12-22 04:18:51 +00:00
|
|
|
const auto &&destseg = seg.absolute_sibling(seg->children[newside]);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
destside = 0;
|
2014-10-02 03:02:34 +00:00
|
|
|
while (destside < MAX_SIDES_PER_SEGMENT && destseg->children[destside] != seg)
|
2006-03-20 16:43:15 +00:00
|
|
|
destside++;
|
|
|
|
|
|
|
|
// Before deleting the segment, copy its texture maps to New_segment
|
2017-06-10 03:31:02 +00:00
|
|
|
copy_tmaps_to_segment(vmsegptr(&New_segment), seg);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (Curside == WFRONT)
|
|
|
|
Curside = WBACK;
|
|
|
|
|
2017-06-10 03:31:02 +00:00
|
|
|
med_attach_segment_rotated(destseg, vmsegptr(&New_segment), destside, AttachSide, rotmat);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Save tmap_num on each side to restore after call to med_propagate_tmaps_to_segments and _back_side
|
|
|
|
// which will change the tmap nums.
|
2020-09-11 03:08:02 +00:00
|
|
|
std::array<texture1_value, MAX_SIDES_PER_SEGMENT> side_tmaps;
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const auto &&z, zip(side_tmaps, seg->unique_segment::sides))
|
|
|
|
{
|
|
|
|
const unique_side &us = std::get<1>(z);
|
|
|
|
std::get<0>(z) = us.tmap_num;
|
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2014-09-06 04:06:18 +00:00
|
|
|
auto back_side = Side_opposite[find_connect_side(destseg, seg)];
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
med_propagate_tmaps_to_segments(destseg, seg,0);
|
|
|
|
med_propagate_tmaps_to_back_side(seg, back_side,0);
|
|
|
|
|
2020-05-02 21:18:43 +00:00
|
|
|
for (const auto &&[idx, side_tmap, us] : enumerate(zip(side_tmaps, seg->unique_segment::sides)))
|
|
|
|
if (idx != back_side)
|
2019-05-04 18:27:36 +00:00
|
|
|
{
|
2020-05-02 21:18:43 +00:00
|
|
|
us.tmap_num = side_tmap;
|
2019-05-04 18:27:36 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Compute the sum of the distances between the four pairs of points.
|
|
|
|
// The connections are:
|
|
|
|
// firstv1 : 0 (firstv1+1)%4 : 1 (firstv1+2)%4 : 2 (firstv1+3)%4 : 3
|
2020-08-24 01:31:28 +00:00
|
|
|
static fix seg_seg_vertex_distsum(const shared_segment &seg1, const unsigned side1, const shared_segment &seg2, const unsigned side2, const unsigned firstv1)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2006-03-20 16:43:15 +00:00
|
|
|
fix distsum;
|
|
|
|
|
|
|
|
distsum = 0;
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const unsigned secondv, xrange(4u))
|
2017-02-19 19:33:38 +00:00
|
|
|
{
|
|
|
|
const unsigned firstv = (4 - secondv + (3 - firstv1)) % 4;
|
2020-08-24 01:31:28 +00:00
|
|
|
distsum += vm_vec_dist(vcvertptr(seg1.verts[Side_to_verts[side1][firstv]]),vcvertptr(seg2.verts[Side_to_verts[side2][secondv]]));
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return distsum;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Determine how to connect two segments together with the least amount of twisting.
|
|
|
|
// Returns vertex index in 0..3 on first segment. Assumed ordering of vertices
|
|
|
|
// on second segment is 0,1,2,3.
|
|
|
|
// So, if return value is 2, connect 2:0 3:1 0:2 1:3.
|
|
|
|
// Theory:
|
|
|
|
// We select an ordering of vertices for connection. For the first pair of vertices to be connected,
|
|
|
|
// compute the vector. For the three remaining pairs of vertices, compute the vectors from one vertex
|
|
|
|
// to the other. Compute the dot products of these vectors with the original vector. Add them up.
|
|
|
|
// The close we are to 3, the better fit we have. Reason: The largest value for the dot product is
|
|
|
|
// 1.0, and this occurs for a parallel set of vectors.
|
2020-08-24 01:31:28 +00:00
|
|
|
static int get_index_of_best_fit(const shared_segment &seg1, const unsigned side1, const shared_segment &seg2, const unsigned side2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
int firstv;
|
|
|
|
fix min_distance;
|
|
|
|
int best_index=0;
|
|
|
|
|
|
|
|
min_distance = F1_0*30000;
|
|
|
|
|
|
|
|
for (firstv=0; firstv<4; firstv++) {
|
|
|
|
fix t;
|
|
|
|
t = seg_seg_vertex_distsum(seg1, side1, seg2, side2, firstv);
|
|
|
|
if (t <= min_distance) {
|
|
|
|
min_distance = t;
|
|
|
|
best_index = firstv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_index;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_VALIDATIONS 50
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Modify seg2 to share side2 with seg1:side1. This forms a connection between
|
|
|
|
// two segments without creating a new segment. It modifies seg2 by sharing
|
|
|
|
// vertices from seg1. seg1 is not modified. Four vertices from seg2 are
|
|
|
|
// deleted.
|
|
|
|
// Return code:
|
|
|
|
// 0 joint formed
|
|
|
|
// 1 -- no, this is legal! -- unable to form joint because one or more vertices of side2 is not free
|
|
|
|
// 2 unable to form joint because side1 is already used
|
2017-06-10 03:31:02 +00:00
|
|
|
int med_form_joint(const vmsegptridx_t seg1, int side1, const vmsegptridx_t seg2, int side2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2015-01-12 00:26:02 +00:00
|
|
|
int bfi,v,s1;
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<int, 4> lost_vertices, remap_vertices;
|
|
|
|
std::array<segnum_t, MAX_VALIDATIONS> validation_list;
|
2015-01-12 00:26:02 +00:00
|
|
|
uint_fast32_t nv;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Make sure that neither side is connected.
|
|
|
|
if (IS_CHILD(seg1->children[side1]) || IS_CHILD(seg2->children[side2]))
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
// Make sure there is no wall there
|
2018-12-13 02:31:38 +00:00
|
|
|
if ((seg1->shared_segment::sides[side1].wall_num != wall_none) || (seg2->shared_segment::sides[side2].wall_num != wall_none))
|
2006-03-20 16:43:15 +00:00
|
|
|
return 2;
|
|
|
|
|
|
|
|
// We can form the joint. Find the best orientation of vertices.
|
|
|
|
bfi = get_index_of_best_fit(seg1, side1, seg2, side2);
|
|
|
|
|
2015-02-05 03:03:50 +00:00
|
|
|
auto &vp1 = Side_to_verts[side1];
|
|
|
|
auto &vp2 = Side_to_verts[side2];
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Make a copy of the list of vertices in seg2 which will be deleted and set the
|
|
|
|
// associated vertex number, so that all occurrences of the vertices can be replaced.
|
|
|
|
for (v=0; v<4; v++)
|
2016-07-06 01:54:24 +00:00
|
|
|
lost_vertices[v] = seg2->verts[static_cast<int>(vp2[v])];
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Now, for each vertex in lost_vertices, determine which vertex it maps to.
|
|
|
|
for (v=0; v<4; v++)
|
2016-07-06 01:54:24 +00:00
|
|
|
remap_vertices[3 - ((v + bfi) % 4)] = seg1->verts[static_cast<int>(vp1[v])];
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Now, in all segments, replace all occurrences of vertices in lost_vertices with remap_vertices
|
|
|
|
|
|
|
|
// Put the one segment we know are being modified into the validation list.
|
|
|
|
// Note: seg1 does not require a full validation, only a validation of the affected side. Its vertices do not move.
|
|
|
|
nv = 1;
|
2014-10-02 03:02:34 +00:00
|
|
|
validation_list[0] = seg2;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
for (v=0; v<4; v++)
|
2017-06-10 03:31:02 +00:00
|
|
|
range_for (const auto &&segp, vmsegptridx)
|
2015-06-13 22:42:19 +00:00
|
|
|
{
|
|
|
|
if (segp->segnum != segment_none)
|
|
|
|
range_for (auto &sv, segp->verts)
|
2015-01-12 00:26:02 +00:00
|
|
|
if (sv == lost_vertices[v]) {
|
|
|
|
sv = remap_vertices[v];
|
2006-03-20 16:43:15 +00:00
|
|
|
// Add segment to list of segments to be validated.
|
|
|
|
for (s1=0; s1<nv; s1++)
|
2015-12-22 04:18:51 +00:00
|
|
|
if (validation_list[s1] == segp)
|
2006-03-20 16:43:15 +00:00
|
|
|
break;
|
|
|
|
if (s1 == nv)
|
2015-12-22 04:18:51 +00:00
|
|
|
validation_list[nv++] = segp;
|
2006-03-20 16:43:15 +00:00
|
|
|
Assert(nv < MAX_VALIDATIONS);
|
|
|
|
}
|
2015-06-13 22:42:19 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Form new connections.
|
2014-10-02 03:02:34 +00:00
|
|
|
seg1->children[side1] = seg2;
|
|
|
|
seg2->children[side2] = seg1;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// validate all segments
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2018-09-19 02:13:29 +00:00
|
|
|
validate_segment_side(vcvertptr, seg1, side1);
|
2016-02-12 04:02:28 +00:00
|
|
|
range_for (auto &s, partial_const_range(validation_list, nv))
|
2015-01-12 00:26:02 +00:00
|
|
|
{
|
2015-12-22 04:18:51 +00:00
|
|
|
const auto &&segp = seg1.absolute_sibling(s);
|
2018-09-19 02:13:30 +00:00
|
|
|
validate_segment(vcvertptr, segp);
|
2015-07-13 01:09:37 +00:00
|
|
|
warn_if_concave_segment(segp);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_vertex_counts();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Create a new segment and use it to form a bridge between two existing segments.
|
|
|
|
// Specify two segment:side pairs. If either segment:side is not open (ie, segment->children[side] != -1)
|
|
|
|
// then it is not legal to form the brider.
|
|
|
|
// Return:
|
|
|
|
// 0 bridge segment formed
|
|
|
|
// 1 unable to form bridge because one (or both) of the sides is not open.
|
|
|
|
// Note that no new vertices are created by this process.
|
2017-06-10 03:31:02 +00:00
|
|
|
int med_form_bridge_segment(const vmsegptridx_t seg1, int side1, const vmsegptridx_t seg2, int side2)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2019-05-04 18:27:36 +00:00
|
|
|
int v,bfi;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (IS_CHILD(seg1->children[side1]) || IS_CHILD(seg2->children[side2]))
|
|
|
|
return 1;
|
|
|
|
|
2018-09-19 02:13:29 +00:00
|
|
|
const auto &&bs = seg1.absolute_sibling(get_free_segment_number(Segments));
|
2019-05-04 18:27:36 +00:00
|
|
|
shared_segment &sbs = *bs;
|
2020-08-10 03:45:13 +00:00
|
|
|
unique_segment &ubs = *bs;
|
2019-05-04 18:27:36 +00:00
|
|
|
sbs.segnum = bs;
|
2020-08-10 03:45:13 +00:00
|
|
|
ubs.objects = object_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Copy vertices from seg2 into last 4 vertices of bridge segment.
|
2015-02-05 03:03:50 +00:00
|
|
|
{
|
|
|
|
auto &sv = Side_to_verts[side2];
|
2006-03-20 16:43:15 +00:00
|
|
|
for (v=0; v<4; v++)
|
2019-05-04 18:27:36 +00:00
|
|
|
sbs.verts[(3-v)+4] = seg2->verts[static_cast<int>(sv[v])];
|
2015-02-05 03:03:50 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Copy vertices from seg1 into first 4 vertices of bridge segment.
|
|
|
|
bfi = get_index_of_best_fit(seg1, side1, seg2, side2);
|
|
|
|
|
2015-02-05 03:03:50 +00:00
|
|
|
{
|
|
|
|
auto &sv = Side_to_verts[side1];
|
2006-03-20 16:43:15 +00:00
|
|
|
for (v=0; v<4; v++)
|
2016-07-06 01:54:24 +00:00
|
|
|
bs->verts[(v + bfi) % 4] = seg1->verts[static_cast<int>(sv[v])];
|
2015-02-05 03:03:50 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Form connections to children, first initialize all to unconnected.
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const auto &&z, zip(sbs.children, sbs.sides))
|
|
|
|
{
|
|
|
|
std::get<0>(z) = segment_none;
|
|
|
|
std::get<1>(z).wall_num = wall_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now form connections between segments.
|
|
|
|
|
2014-10-02 03:02:34 +00:00
|
|
|
bs->children[AttachSide] = seg1;
|
|
|
|
bs->children[Side_opposite[AttachSide]] = seg2;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2014-10-02 03:02:34 +00:00
|
|
|
seg1->children[side1] = bs; //seg2 - Segments;
|
|
|
|
seg2->children[side2] = bs; //seg1 - Segments;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Validate bridge segment, and if degenerate, clean up mess.
|
|
|
|
Degenerate_segment_found = 0;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2018-09-19 02:13:30 +00:00
|
|
|
validate_segment(vcvertptr, bs);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (Degenerate_segment_found) {
|
2013-12-26 22:21:16 +00:00
|
|
|
seg1->children[side1] = segment_none;
|
|
|
|
seg2->children[side2] = segment_none;
|
|
|
|
bs->children[AttachSide] = segment_none;
|
2016-07-06 01:54:24 +00:00
|
|
|
bs->children[static_cast<int>(Side_opposite[AttachSide])] = segment_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
if (med_delete_segment(bs)) {
|
|
|
|
Int3();
|
|
|
|
}
|
|
|
|
editor_status("Bridge segment would be degenerate, not created.\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
2018-09-19 02:13:30 +00:00
|
|
|
validate_segment(vcvertptr, seg1); // used to only validate side, but segment does more error checking: ,side1);
|
|
|
|
validate_segment(vcvertptr, seg2); // ,side2);
|
2006-03-20 16:43:15 +00:00
|
|
|
med_propagate_tmaps_to_segments(seg1,bs,0);
|
|
|
|
|
|
|
|
editor_status("Bridge segment formed.");
|
|
|
|
warn_if_concave_segment(bs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Create a segment given center, dimensions, rotation matrix.
|
|
|
|
// Note that the created segment will always have planar sides and rectangular cross sections.
|
|
|
|
// It will be created with walls on all sides, ie not connected to anything.
|
2017-06-10 03:31:02 +00:00
|
|
|
void med_create_segment(const vmsegptridx_t sp,fix cx, fix cy, fix cz, fix length, fix width, fix height, const vms_matrix &mp)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-08-10 03:45:13 +00:00
|
|
|
unique_segment &usp = sp;
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2016-05-22 17:49:30 +00:00
|
|
|
int f;
|
2018-09-19 02:13:29 +00:00
|
|
|
++ LevelSharedSegmentState.Num_segments;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
sp->segnum = 1; // What to put here? I don't know.
|
|
|
|
|
|
|
|
// Form connections to children, of which it has none.
|
2016-05-22 17:49:30 +00:00
|
|
|
for (unsigned i = 0; i < MAX_SIDES_PER_SEGMENT; ++i)
|
|
|
|
{
|
2013-12-26 22:21:16 +00:00
|
|
|
sp->children[i] = segment_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
// sp->sides[i].render_flag = 0;
|
2018-12-13 02:31:38 +00:00
|
|
|
sp->shared_segment::sides[i].wall_num = wall_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sp->group = -1;
|
|
|
|
sp->matcen_num = -1;
|
|
|
|
|
|
|
|
// Create relative-to-center vertices, which are the rotated points on the box defined by length, width, height
|
2015-01-12 00:26:02 +00:00
|
|
|
sp->verts[0] = med_add_vertex(vertex{vm_vec_rotate({+width/2, +height/2, -length/2}, mp)});
|
|
|
|
sp->verts[1] = med_add_vertex(vertex{vm_vec_rotate({+width/2, -height/2, -length/2}, mp)});
|
|
|
|
sp->verts[2] = med_add_vertex(vertex{vm_vec_rotate({-width/2, -height/2, -length/2}, mp)});
|
|
|
|
sp->verts[3] = med_add_vertex(vertex{vm_vec_rotate({-width/2, +height/2, -length/2}, mp)});
|
|
|
|
sp->verts[4] = med_add_vertex(vertex{vm_vec_rotate({+width/2, +height/2, +length/2}, mp)});
|
|
|
|
sp->verts[5] = med_add_vertex(vertex{vm_vec_rotate({+width/2, -height/2, +length/2}, mp)});
|
|
|
|
sp->verts[6] = med_add_vertex(vertex{vm_vec_rotate({-width/2, -height/2, +length/2}, mp)});
|
|
|
|
sp->verts[7] = med_add_vertex(vertex{vm_vec_rotate({-width/2, +height/2, +length/2}, mp)});
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Now create the vector which is the center of the segment and add that to all vertices.
|
2015-01-12 00:26:02 +00:00
|
|
|
const vms_vector cv{cx, cy, cz};
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Now, add the center to all vertices, placing the segment in 3 space.
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vmvertptr = Vertices.vmptr;
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &i, sp->verts)
|
2017-08-11 23:43:54 +00:00
|
|
|
vm_vec_add2(vmvertptr(i), cv);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Set scale vector.
|
|
|
|
// sp->scale.x = width;
|
|
|
|
// sp->scale.y = height;
|
|
|
|
// sp->scale.z = length;
|
|
|
|
|
|
|
|
// Add faces to all sides.
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2006-03-20 16:43:15 +00:00
|
|
|
for (f=0; f<MAX_SIDES_PER_SEGMENT; f++)
|
2018-09-19 02:13:29 +00:00
|
|
|
create_walls_on_side(vcvertptr, sp, f);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-08-10 03:45:13 +00:00
|
|
|
usp.objects = object_none; //no objects in this segment
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Assume nothing special about this segment
|
|
|
|
sp->special = 0;
|
2017-07-08 18:17:49 +00:00
|
|
|
sp->station_idx = station_none;
|
2020-08-10 03:45:13 +00:00
|
|
|
usp.static_light = 0;
|
2006-03-20 16:43:15 +00:00
|
|
|
sp->matcen_num = -1;
|
|
|
|
|
2016-01-09 16:38:13 +00:00
|
|
|
copy_tmaps_to_segment(sp, vcsegptr(&New_segment));
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
assign_default_uvs_to_segment(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------
|
|
|
|
// Create New_segment using a specified scale factor.
|
2014-10-26 22:01:13 +00:00
|
|
|
void med_create_new_segment(const vms_vector &scale)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2017-06-10 03:31:02 +00:00
|
|
|
const auto &&sp = vmsegptridx(&New_segment);
|
2020-08-10 03:45:13 +00:00
|
|
|
unique_segment &usp = sp;
|
2006-03-20 16:43:15 +00:00
|
|
|
fix length,width,height;
|
|
|
|
|
2014-10-26 22:01:13 +00:00
|
|
|
length = scale.z;
|
|
|
|
width = scale.x;
|
|
|
|
height = scale.y;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
sp->segnum = 1; // What to put here? I don't know.
|
|
|
|
|
|
|
|
// Create relative-to-center vertices, which are the points on the box defined by length, width, height
|
2020-12-26 21:17:29 +00:00
|
|
|
auto &verts = sp->verts;
|
|
|
|
verts[0] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 0}, {+width / 2, +height / 2, -length / 2});
|
|
|
|
verts[1] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 1}, {+width / 2, -height / 2, -length / 2});
|
|
|
|
verts[2] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 2}, {-width / 2, -height / 2, -length / 2});
|
|
|
|
verts[3] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 3}, {-width / 2, +height / 2, -length / 2});
|
|
|
|
verts[4] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 4}, {+width / 2, +height / 2, +length / 2});
|
|
|
|
verts[5] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 5}, {+width / 2, -height / 2, +length / 2});
|
|
|
|
verts[6] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 6}, {-width / 2, -height / 2, +length / 2});
|
|
|
|
verts[7] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 7}, {-width / 2, +height / 2, +length / 2});
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// sp->scale = *scale;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2006-03-20 16:43:15 +00:00
|
|
|
// Form connections to children, of which it has none, init faces and tmaps.
|
2020-05-02 21:18:43 +00:00
|
|
|
for (const auto &&[s, child, ss, us] : enumerate(zip(sp->children, sp->shared_segment::sides, sp->unique_segment::sides)))
|
2019-05-04 18:27:36 +00:00
|
|
|
{
|
2020-05-02 21:18:43 +00:00
|
|
|
child = segment_none;
|
2019-05-04 18:27:36 +00:00
|
|
|
ss.wall_num = wall_none;
|
2018-09-19 02:13:29 +00:00
|
|
|
create_walls_on_side(vcvertptr, sp, s);
|
2020-09-11 03:08:02 +00:00
|
|
|
us.tmap_num = build_texture1_value(s + 1); // assign some stupid old tmap to this side.
|
2020-08-24 01:31:28 +00:00
|
|
|
us.tmap_num2 = texture2_value::None;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 03:08:04 +00:00
|
|
|
Seg_orientation = {};
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-08-10 03:45:13 +00:00
|
|
|
usp.objects = object_none; //no objects in this segment
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
assign_default_uvs_to_segment(sp);
|
|
|
|
|
|
|
|
// Assume nothing special about this segment
|
|
|
|
sp->special = 0;
|
2017-07-08 18:17:49 +00:00
|
|
|
sp->station_idx = station_none;
|
2020-08-10 03:45:13 +00:00
|
|
|
usp.static_light = 0;
|
2006-03-20 16:43:15 +00:00
|
|
|
sp->matcen_num = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
void med_create_new_segment_from_cursegp(void)
|
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2006-03-20 16:43:15 +00:00
|
|
|
vms_vector scalevec;
|
|
|
|
vms_vector uvec, rvec, fvec;
|
|
|
|
|
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);
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2018-09-19 02:13:29 +00:00
|
|
|
extract_forward_vector_from_segment(vcvertptr, Cursegp, fvec);
|
2006-03-20 16:43:15 +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);
|
2014-10-26 22:01:13 +00:00
|
|
|
med_create_new_segment(scalevec);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Initialize all vertices to inactive status.
|
|
|
|
void init_all_vertices(void)
|
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
|
2016-03-05 17:26:24 +00:00
|
|
|
Vertex_active = {};
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &s, Segments)
|
|
|
|
s.segnum = segment_none;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Create coordinate axes in orientation of specified segment, stores vertices at *vp.
|
2020-08-24 01:31:28 +00:00
|
|
|
void create_coordinate_axes_from_segment(const shared_segment &sp, std::array<unsigned, 16> &vertnums)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2006-03-20 16:43:15 +00:00
|
|
|
vms_matrix rotmat;
|
|
|
|
vms_vector t;
|
|
|
|
|
2018-06-24 05:06:15 +00:00
|
|
|
med_extract_matrix_from_segment(sp, rotmat);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
|
|
|
auto &vmvertptr = Vertices.vmptr;
|
2017-08-11 23:43:54 +00:00
|
|
|
const auto &&v0 = vmvertptr(vertnums[0]);
|
2018-03-12 03:43:47 +00:00
|
|
|
compute_segment_center(vcvertptr, v0, sp);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
t = rotmat.rvec;
|
2014-09-28 21:11:05 +00:00
|
|
|
vm_vec_scale(t,i2f(32));
|
2017-08-11 23:43:54 +00:00
|
|
|
vm_vec_add(vmvertptr(vertnums[1]), v0, t);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
t = rotmat.uvec;
|
2014-09-28 21:11:05 +00:00
|
|
|
vm_vec_scale(t,i2f(32));
|
2017-08-11 23:43:54 +00:00
|
|
|
vm_vec_add(vmvertptr(vertnums[2]), v0, t);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
t = rotmat.fvec;
|
2014-09-28 21:11:05 +00:00
|
|
|
vm_vec_scale(t,i2f(32));
|
2017-08-11 23:43:54 +00:00
|
|
|
vm_vec_add(vmvertptr(vertnums[3]), v0, t);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Determine if a segment is concave. Returns true if concave
|
2020-08-24 01:31:28 +00:00
|
|
|
static int check_seg_concavity(const shared_segment &s)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2014-11-01 03:17:48 +00:00
|
|
|
vms_vector n0;
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &sn, Side_to_verts)
|
2017-02-19 19:33:38 +00:00
|
|
|
for (unsigned vn = 0; vn <= 4; ++vn)
|
|
|
|
{
|
2014-11-01 03:17:48 +00:00
|
|
|
const auto n1 = vm_vec_normal(
|
2020-08-24 01:31:28 +00:00
|
|
|
vcvertptr(s.verts[sn[vn % 4]]),
|
|
|
|
vcvertptr(s.verts[sn[(vn + 1) % 4]]),
|
|
|
|
vcvertptr(s.verts[sn[(vn + 2) % 4]]));
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
//vm_vec_normalize(&n1);
|
|
|
|
|
2014-09-28 21:41:59 +00:00
|
|
|
if (vn>0) if (vm_vec_dot(n0,n1) < f0_5) return 1;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
n0 = n1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Find all concave segments and add to list
|
|
|
|
void find_concave_segs()
|
|
|
|
{
|
2013-12-11 23:59:36 +00:00
|
|
|
Warning_segs.clear();
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2016-02-12 04:02:28 +00:00
|
|
|
range_for (const auto &&s, vcsegptridx)
|
2013-12-26 22:21:16 +00:00
|
|
|
if (s->segnum != segment_none)
|
2016-01-09 16:38:13 +00:00
|
|
|
if (check_seg_concavity(s))
|
|
|
|
Warning_segs.emplace_back(s);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
void warn_if_concave_segments(void)
|
|
|
|
{
|
|
|
|
find_concave_segs();
|
|
|
|
|
2017-06-10 03:31:03 +00:00
|
|
|
if (!Warning_segs.empty())
|
|
|
|
{
|
|
|
|
editor_status_fmt("*** WARNING *** %d concave segments in mine! *** WARNING ***", Warning_segs.size());
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Check segment s, if concave, warn
|
2017-06-10 03:31:02 +00:00
|
|
|
void warn_if_concave_segment(const vmsegptridx_t s)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = check_seg_concavity(s);
|
|
|
|
|
|
|
|
if (result) {
|
2014-10-02 03:02:34 +00:00
|
|
|
Warning_segs.emplace_back(s);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
editor_status("*** WARNING *** New segment is concave! *** WARNING ***");
|
|
|
|
} //else
|
|
|
|
//editor_status("");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Find segment adjacent to sp:side.
|
|
|
|
// Adjacent means a segment which shares all four vertices.
|
|
|
|
// Return true if segment found and fill in segment in adj_sp and side in adj_side.
|
|
|
|
// Return false if unable to find, in which case adj_sp and adj_side are undefined.
|
2017-06-10 03:31:02 +00:00
|
|
|
int med_find_adjacent_segment_side(const vmsegptridx_t sp, int side, imsegptridx_t &adj_sp, int *adj_side)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<int, 4> abs_verts;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
// Stuff abs_verts[4] array with absolute vertex indices
|
2019-05-04 18:27:36 +00:00
|
|
|
range_for (const unsigned v, xrange(4u))
|
2006-03-20 16:43:15 +00:00
|
|
|
abs_verts[v] = sp->verts[Side_to_verts[side][v]];
|
|
|
|
|
|
|
|
// Scan all segments, looking for a segment which contains the four abs_verts
|
2017-06-10 03:31:02 +00:00
|
|
|
range_for (const auto &&segp, vmsegptridx)
|
2014-10-12 23:10:05 +00:00
|
|
|
{
|
2015-12-22 04:18:51 +00:00
|
|
|
if (segp != sp)
|
2015-06-13 22:42:19 +00:00
|
|
|
{
|
2015-01-12 00:26:02 +00:00
|
|
|
range_for (auto &v, abs_verts)
|
|
|
|
{ // do for each vertex in abs_verts
|
2015-06-13 22:42:19 +00:00
|
|
|
range_for (auto &vv, segp->verts) // do for each vertex in segment
|
2015-01-12 00:26:02 +00:00
|
|
|
if (v == vv)
|
2006-03-20 16:43:15 +00:00
|
|
|
goto fass_found1; // Current vertex (indexed by v) is present in segment, try next
|
|
|
|
goto fass_next_seg; // This segment doesn't contain the vertex indexed by v
|
|
|
|
fass_found1: ;
|
|
|
|
} // end for v
|
|
|
|
|
|
|
|
// All four vertices in sp:side are present in segment seg.
|
|
|
|
// Determine side and return
|
2019-05-04 18:27:37 +00:00
|
|
|
range_for (const auto &&es, enumerate(Side_to_verts))
|
|
|
|
{
|
|
|
|
range_for (const auto v, es.value)
|
2015-01-12 00:26:02 +00:00
|
|
|
{
|
|
|
|
range_for (auto &vv, abs_verts)
|
|
|
|
{
|
2015-06-13 22:42:19 +00:00
|
|
|
if (segp->verts[v] == vv)
|
2006-03-20 16:43:15 +00:00
|
|
|
goto fass_found2;
|
|
|
|
}
|
|
|
|
goto fass_next_side; // Couldn't find vertex v in current side, so try next side.
|
|
|
|
fass_found2: ;
|
|
|
|
}
|
|
|
|
// Found all four vertices in current side. We are done!
|
2015-06-13 22:42:19 +00:00
|
|
|
adj_sp = segp;
|
2019-05-04 18:27:37 +00:00
|
|
|
*adj_side = es.idx;
|
2006-03-20 16:43:15 +00:00
|
|
|
return 1;
|
|
|
|
fass_next_side: ;
|
|
|
|
}
|
|
|
|
Assert(0); // Impossible -- we identified this segment as containing all 4 vertices of side "side", but we couldn't find them.
|
|
|
|
return 0;
|
|
|
|
fass_next_seg: ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define JOINT_THRESHOLD 10000*F1_0 // (Huge threshold)
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------
|
|
|
|
// Find segment closest to sp:side.
|
|
|
|
// Return true if segment found and fill in segment in adj_sp and side in adj_side.
|
|
|
|
// Return false if unable to find, in which case adj_sp and adj_side are undefined.
|
2017-06-10 03:31:02 +00:00
|
|
|
int med_find_closest_threshold_segment_side(const vmsegptridx_t sp, int side, imsegptridx_t &adj_sp, int *adj_side, fix threshold)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-17 23:35:25 +00:00
|
|
|
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
|
|
|
|
auto &Vertices = LevelSharedVertexState.get_vertices();
|
2006-03-20 16:43:15 +00:00
|
|
|
fix current_dist, closest_seg_dist;
|
|
|
|
|
|
|
|
if (IS_CHILD(sp->children[side]))
|
|
|
|
return 0;
|
|
|
|
|
2018-12-30 00:43:57 +00:00
|
|
|
auto &vcvertptr = Vertices.vcptr;
|
2018-03-12 03:43:47 +00:00
|
|
|
const auto &&vsc = compute_center_point_on_side(vcvertptr, sp, side);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
closest_seg_dist = JOINT_THRESHOLD;
|
|
|
|
|
|
|
|
// Scan all segments, looking for a segment which contains the four abs_verts
|
2017-06-10 03:31:02 +00:00
|
|
|
range_for (const auto &&segp, vmsegptridx)
|
2015-06-13 22:42:19 +00:00
|
|
|
{
|
2015-12-22 04:18:51 +00:00
|
|
|
if (segp != sp)
|
2019-05-04 18:27:37 +00:00
|
|
|
range_for (const auto &&es, enumerate(segp->children))
|
|
|
|
{
|
|
|
|
if (!IS_CHILD(es.value))
|
|
|
|
{
|
|
|
|
const auto &&vtc = compute_center_point_on_side(vcvertptr, segp, es.idx);
|
2014-10-01 02:28:41 +00:00
|
|
|
current_dist = vm_vec_dist( vsc, vtc );
|
2006-03-20 16:43:15 +00:00
|
|
|
if (current_dist < closest_seg_dist) {
|
2015-06-13 22:42:19 +00:00
|
|
|
adj_sp = segp;
|
2019-05-04 18:27:37 +00:00
|
|
|
*adj_side = es.idx;
|
2006-03-20 16:43:15 +00:00
|
|
|
closest_seg_dist = current_dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 22:42:19 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (closest_seg_dist < threshold)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-22 04:18:50 +00:00
|
|
|
|
|
|
|
}
|