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
|
2013-02-24 02:39:48 +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
|
|
|
/*
|
|
|
|
*
|
2008-04-06 20:23:28 +00:00
|
|
|
* Morphing code
|
2006-03-20 16:43:15 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-11-11 22:12:51 +00:00
|
|
|
#include <algorithm>
|
2006-03-20 16:43:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-07-07 18:35:06 +00:00
|
|
|
#include "dxxerror.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
#include "inferno.h"
|
|
|
|
#include "polyobj.h"
|
|
|
|
#include "game.h"
|
|
|
|
#include "lighting.h"
|
|
|
|
#include "newdemo.h"
|
|
|
|
#include "piggy.h"
|
|
|
|
#include "bm.h"
|
2013-03-03 01:03:33 +00:00
|
|
|
#include "interp.h"
|
2019-04-13 18:00:07 +00:00
|
|
|
#include "render.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-11 04:48:14 +00:00
|
|
|
#include "compiler-poison.h"
|
2015-02-14 22:48:27 +00:00
|
|
|
#include "compiler-range_for.h"
|
2020-02-01 22:33:31 +00:00
|
|
|
#include "d_enumerate.h"
|
2020-08-10 03:45:14 +00:00
|
|
|
#include "d_levelstate.h"
|
2019-05-04 18:27:37 +00:00
|
|
|
#include "d_range.h"
|
2020-02-01 22:33:31 +00:00
|
|
|
#include "d_zip.h"
|
2015-02-14 22:48:27 +00:00
|
|
|
#include "partial_range.h"
|
|
|
|
|
2012-11-11 22:12:51 +00:00
|
|
|
using std::max;
|
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
namespace dcx {
|
2018-12-30 00:43:58 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-08-24 01:31:28 +00:00
|
|
|
constexpr vms_vector morph_rotvel{0x4000,0x2000,0x1000};
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
class invalid_morph_model_type : public std::runtime_error
|
|
|
|
{
|
|
|
|
__attribute_cold
|
|
|
|
static std::string prepare_message(const unsigned type)
|
|
|
|
{
|
|
|
|
char buf[32 + sizeof("4294967295")];
|
|
|
|
const auto len = std::snprintf(buf, sizeof buf, "invalid morph model type: %u", type);
|
|
|
|
return std::string(buf, len);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
invalid_morph_model_type(const unsigned type) :
|
|
|
|
runtime_error(prepare_message(type))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
class invalid_morph_model_vertex_count : public std::runtime_error
|
|
|
|
{
|
|
|
|
__attribute_cold
|
|
|
|
static std::string prepare_message(const unsigned count, const morph_data::polymodel_idx idx, const unsigned submodel_num)
|
|
|
|
{
|
|
|
|
char buf[68 + 3 * sizeof("4294967295")];
|
|
|
|
const unsigned uidx = idx.idx;
|
|
|
|
const auto len = std::snprintf(buf, sizeof buf, "too many vertices in morph model: found %u in model %u, submodel %u", count, uidx, submodel_num);
|
|
|
|
return std::string(buf, len);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
invalid_morph_model_vertex_count(const unsigned count, const morph_data::polymodel_idx idx, const unsigned submodel_num) :
|
|
|
|
runtime_error(prepare_message(count, idx, submodel_num))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
struct submodel_data
|
|
|
|
{
|
|
|
|
const uint16_t *body;
|
|
|
|
const unsigned type;
|
|
|
|
const unsigned nverts;
|
|
|
|
const unsigned startpoint;
|
|
|
|
};
|
|
|
|
|
|
|
|
submodel_data parse_model_data_header(const polymodel &pm, const unsigned submodel_num)
|
|
|
|
{
|
|
|
|
auto data = reinterpret_cast<const uint16_t *>(&pm.model_data[pm.submodel_ptrs[submodel_num]]);
|
|
|
|
const auto ptype = data++;
|
|
|
|
|
|
|
|
const uint16_t type = *ptype;
|
|
|
|
const auto pnverts = data++;
|
|
|
|
|
|
|
|
const uint16_t startpoint = (type == 7)
|
2020-05-02 21:18:42 +00:00
|
|
|
? *std::exchange(data, data + 2) //get start point number, skip pad
|
2020-02-26 05:07:34 +00:00
|
|
|
: (type == 1)
|
|
|
|
? 0 //start at zero
|
|
|
|
: throw invalid_morph_model_type(type);
|
|
|
|
const uint16_t nverts = *pnverts;
|
|
|
|
return {data, type, nverts, startpoint};
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
std::size_t count_submodel_points(const polymodel &pm, const morph_data::polymodel_idx model_idx, const unsigned submodel_num)
|
|
|
|
{
|
|
|
|
/* Return the minimum array size that will not cause this submodel
|
|
|
|
* to index past the end of the array.
|
|
|
|
*/
|
|
|
|
const auto &&sd = parse_model_data_header(pm, submodel_num);
|
|
|
|
const std::size_t count = sd.startpoint + sd.nverts;
|
|
|
|
if (count > morph_data::MAX_VECS)
|
|
|
|
throw invalid_morph_model_vertex_count(count, model_idx, submodel_num);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t count_model_points(const polymodel &pm, const morph_data::polymodel_idx model_idx)
|
|
|
|
{
|
|
|
|
/* Return the minimum array size that will not cause any used
|
|
|
|
* submodel of this model to index past the end of the array.
|
|
|
|
*
|
|
|
|
* Unused submodels are not considered. A submodel is used if:
|
|
|
|
* - its index is not above pm.n_models
|
|
|
|
* - its parent index is valid
|
|
|
|
* - its parent index is a used submodel
|
|
|
|
*
|
|
|
|
* Submodel 0 is always used.
|
|
|
|
*/
|
|
|
|
auto count = count_submodel_points(pm, model_idx, 0);
|
|
|
|
unsigned visited_submodels = 1;
|
|
|
|
const unsigned mask_all_enabled_models = (1 << pm.n_models) - 1;
|
|
|
|
const auto &&submodel_parents = enumerate(partial_range(pm.submodel_parents, pm.n_models));
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (visited_submodels == mask_all_enabled_models)
|
|
|
|
/* Every submodel has been checked, so the next pass through
|
|
|
|
* the loop will ignore every element. Break out early to
|
|
|
|
* avoid the extra iteration.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
const auto previous_visited_submodels = visited_submodels;
|
2021-06-28 03:37:50 +00:00
|
|
|
for (const auto &&[idx, value] : submodel_parents)
|
2020-02-26 05:07:34 +00:00
|
|
|
{
|
2021-06-28 03:37:50 +00:00
|
|
|
const unsigned mask_this_submodel = 1 << idx;
|
2020-02-26 05:07:34 +00:00
|
|
|
if (mask_this_submodel & visited_submodels)
|
|
|
|
/* Already tested on a prior iteration */
|
|
|
|
continue;
|
2021-06-28 03:37:50 +00:00
|
|
|
if (value >= pm.submodel_parents.size())
|
2020-02-26 05:07:34 +00:00
|
|
|
/* Ignore submodels with out-of-range parents. This
|
|
|
|
* avoids undefined behavior in the shift, since some
|
|
|
|
* submodels have a parent of 0xff.
|
|
|
|
*/
|
|
|
|
continue;
|
2021-06-28 03:37:50 +00:00
|
|
|
const unsigned mask_parent_submodel = 1 << value;
|
2020-02-26 05:07:34 +00:00
|
|
|
if (mask_parent_submodel & visited_submodels)
|
|
|
|
{
|
|
|
|
visited_submodels |= mask_this_submodel;
|
|
|
|
/* Parent is in use, so this submodel is also in use. */
|
2021-06-28 03:37:50 +00:00
|
|
|
const auto subcount = count_submodel_points(pm, model_idx, idx);
|
2020-02-26 05:07:34 +00:00
|
|
|
count = std::max(count, subcount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (previous_visited_submodels == visited_submodels)
|
|
|
|
/* No changes on the most recent pass, so no changes will
|
|
|
|
* occur on any subsequent pass. Break out.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
void *morph_data::operator new(std::size_t, const max_vectors max_vecs)
|
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
return ::operator new(sizeof(morph_data) + (max_vecs.count * (sizeof(fix) + sizeof(vms_vector) + sizeof(vms_vector))));
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
morph_data::ptr morph_data::create(object_base &o, const polymodel &pm, const polymodel_idx model_idx)
|
2020-02-26 05:07:34 +00:00
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
const max_vectors m{count_model_points(pm, model_idx)};
|
|
|
|
/* This is an unusual form of `new` overload. Although arguments to
|
|
|
|
* `new` are typically considered a use of `placement new`, this is
|
|
|
|
* not used to place the object. Instead, it is used to pass extra
|
|
|
|
* information to `operator new` so that the count of allocated
|
|
|
|
* bytes can be adjusted.
|
|
|
|
*/
|
|
|
|
return ptr(new(m) morph_data(o, m));
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
morph_data::morph_data(object_base &o, const max_vectors m) :
|
|
|
|
obj(&o), Morph_sig(o.signature), max_vecs(m)
|
2020-02-01 22:33:31 +00:00
|
|
|
{
|
2020-02-06 03:22:45 +00:00
|
|
|
DXX_POISON_VAR(submodel_active, 0xcc);
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_times = get_morph_times();
|
|
|
|
DXX_POISON_MEMORY(morph_times.begin(), morph_times.end(), 0xcc);
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_vecs = get_morph_times();
|
|
|
|
DXX_POISON_MEMORY(morph_vecs.begin(), morph_vecs.end(), 0xcc);
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_deltas = get_morph_times();
|
|
|
|
DXX_POISON_MEMORY(morph_deltas.begin(), morph_deltas.end(), 0xcc);
|
2020-02-06 03:22:45 +00:00
|
|
|
DXX_POISON_VAR(n_morphing_points, 0xcc);
|
|
|
|
DXX_POISON_VAR(submodel_startpoints, 0xcc);
|
2020-02-01 22:33:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
span<fix> morph_data::get_morph_times()
|
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
return {reinterpret_cast<fix *>(this + 1), max_vecs.count};
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
span<vms_vector> morph_data::get_morph_vecs()
|
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
return {reinterpret_cast<vms_vector *>(get_morph_times().end()), max_vecs.count};
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
span<vms_vector> morph_data::get_morph_deltas()
|
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
return {get_morph_vecs().end(), max_vecs.count};
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
d_level_unique_morph_object_state::~d_level_unique_morph_object_state() = default;
|
2018-12-30 00:43:58 +00:00
|
|
|
|
2006-03-20 16:43:15 +00:00
|
|
|
//returns ptr to data for this object, or NULL if none
|
2020-02-26 05:07:34 +00:00
|
|
|
morph_data::ptr *find_morph_data(d_level_unique_morph_object_state &LevelUniqueMorphObjectState, object_base &obj)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2018-12-30 00:43:58 +00:00
|
|
|
auto &morph_objects = LevelUniqueMorphObjectState.morph_objects;
|
2006-03-20 16:43:15 +00:00
|
|
|
if (Newdemo_state == ND_STATE_PLAYBACK) {
|
2020-02-06 03:22:45 +00:00
|
|
|
return nullptr;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-02-14 22:48:27 +00:00
|
|
|
range_for (auto &i, morph_objects)
|
2020-02-01 22:33:31 +00:00
|
|
|
if (i && i->obj == &obj)
|
2015-02-14 22:48:27 +00:00
|
|
|
return &i;
|
2018-06-24 05:06:15 +00:00
|
|
|
return nullptr;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
namespace {
|
2020-02-01 22:33:31 +00:00
|
|
|
|
2014-11-02 03:42:54 +00:00
|
|
|
static void assign_max(fix &a, const fix &b)
|
|
|
|
{
|
|
|
|
a = std::max(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void assign_min(fix &a, const fix &b)
|
|
|
|
{
|
|
|
|
a = std::min(a, b);
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
static void update_bounds(vms_vector &minv, vms_vector &maxv, const vms_vector &v, fix vms_vector::*const p)
|
2014-11-02 03:42:54 +00:00
|
|
|
{
|
2014-11-04 03:21:23 +00:00
|
|
|
auto &mx = maxv.*p;
|
2020-02-26 05:07:34 +00:00
|
|
|
assign_max(mx, v.*p);
|
2014-11-04 03:21:23 +00:00
|
|
|
auto &mn = minv.*p;
|
2020-02-26 05:07:34 +00:00
|
|
|
assign_min(mn, v.*p);
|
2014-11-02 03:42:54 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
//takes pm, fills in min & max
|
2020-02-26 05:07:34 +00:00
|
|
|
static void find_min_max(const polymodel &pm, const unsigned submodel_num, vms_vector &minv, vms_vector &maxv)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto &&sd = parse_model_data_header(pm, submodel_num);
|
|
|
|
const unsigned nverts = sd.nverts;
|
|
|
|
if (!nverts)
|
|
|
|
{
|
|
|
|
minv = maxv = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto vp = reinterpret_cast<const vms_vector *>(sd.body);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
minv = maxv = *vp;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
range_for (auto &v, unchecked_partial_range(vp + 1, nverts - 1))
|
|
|
|
{
|
|
|
|
update_bounds(minv, maxv, v, &vms_vector::x);
|
|
|
|
update_bounds(minv, maxv, v, &vms_vector::y);
|
|
|
|
update_bounds(minv, maxv, v, &vms_vector::z);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MORPH_RATE (f1_0*3)
|
|
|
|
|
2017-11-05 20:49:09 +00:00
|
|
|
constexpr fix morph_rate = MORPH_RATE;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
static fix update_bounding_box_extent(const vms_vector &vp, const vms_vector &box_size, fix vms_vector::*const p, const fix entry_extent)
|
|
|
|
{
|
|
|
|
if (!(vp.*p))
|
|
|
|
return entry_extent;
|
|
|
|
const auto box_size_p = box_size.*p;
|
|
|
|
const auto abs_vp_p = abs(vp.*p);
|
|
|
|
if (f2i(box_size_p) >= abs_vp_p / 2)
|
|
|
|
return entry_extent;
|
|
|
|
const fix t = fixdiv(box_size_p, abs_vp_p);
|
|
|
|
return std::min(entry_extent, t);
|
|
|
|
}
|
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
static fix compute_bounding_box_extents(const vms_vector &vp, const vms_vector &box_size)
|
|
|
|
{
|
|
|
|
fix k = INT32_MAX;
|
|
|
|
|
|
|
|
k = update_bounding_box_extent(vp, box_size, &vms_vector::x, k);
|
|
|
|
k = update_bounding_box_extent(vp, box_size, &vms_vector::y, k);
|
|
|
|
k = update_bounding_box_extent(vp, box_size, &vms_vector::z, k);
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
static void init_points(const polymodel &pm, const vms_vector *const box_size, const unsigned submodel_num, morph_data *const md)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto &&sd = parse_model_data_header(pm, submodel_num);
|
|
|
|
const unsigned startpoint = sd.startpoint;
|
|
|
|
const unsigned endpoint = sd.startpoint + sd.nverts;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
md->submodel_active[submodel_num] = morph_data::submodel_state::animating;
|
2006-03-20 16:43:15 +00:00
|
|
|
md->n_morphing_points[submodel_num] = 0;
|
2020-02-01 22:33:31 +00:00
|
|
|
md->submodel_startpoints[submodel_num] = startpoint;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_times = md->get_morph_times();
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_vecs = md->get_morph_vecs();
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_deltas = md->get_morph_deltas();
|
2020-02-01 22:33:31 +00:00
|
|
|
auto &&zr = zip(
|
2020-02-26 05:07:34 +00:00
|
|
|
unchecked_partial_range(reinterpret_cast<const vms_vector *>(sd.body), sd.nverts),
|
2020-02-26 05:07:34 +00:00
|
|
|
partial_range(morph_vecs, startpoint, endpoint),
|
2020-02-26 05:07:34 +00:00
|
|
|
partial_range(morph_deltas, startpoint, endpoint),
|
2020-02-26 05:07:34 +00:00
|
|
|
partial_range(morph_times, startpoint, endpoint)
|
2020-02-01 22:33:31 +00:00
|
|
|
);
|
|
|
|
range_for (auto &&z, zr)
|
2020-02-01 22:33:31 +00:00
|
|
|
{
|
2020-02-01 22:33:31 +00:00
|
|
|
const auto vp = &std::get<0>(z);
|
|
|
|
auto &morph_vec = std::get<1>(z);
|
|
|
|
auto &morph_delta = std::get<2>(z);
|
|
|
|
auto &morph_time = std::get<3>(z);
|
|
|
|
fix k;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
if (box_size && (k = compute_bounding_box_extents(*vp, *box_size) != INT32_MAX))
|
|
|
|
vm_vec_copy_scale(morph_vec, *vp, k);
|
2006-03-20 16:43:15 +00:00
|
|
|
else
|
2020-02-01 22:33:31 +00:00
|
|
|
morph_vec = {};
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2021-09-19 10:53:48 +00:00
|
|
|
const auto dist = vm_vec_normalized_dir_quick(morph_delta, *vp, morph_vec);
|
2020-02-01 22:33:31 +00:00
|
|
|
morph_time = fixdiv(dist, morph_rate);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
if (morph_time != 0)
|
2006-03-20 16:43:15 +00:00
|
|
|
md->n_morphing_points[submodel_num]++;
|
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
vm_vec_scale(morph_delta, morph_rate);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
static void update_points(const polymodel &pm, const unsigned submodel_num, morph_data *const md)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto &&sd = parse_model_data_header(pm, submodel_num);
|
|
|
|
const unsigned startpoint = sd.startpoint;
|
2020-02-26 05:07:34 +00:00
|
|
|
const unsigned endpoint = startpoint + sd.nverts;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_times = md->get_morph_times();
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_vecs = md->get_morph_vecs();
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_deltas = md->get_morph_deltas();
|
2020-02-26 05:07:34 +00:00
|
|
|
auto &&zr = zip(
|
|
|
|
unchecked_partial_range(reinterpret_cast<const vms_vector *>(sd.body), sd.nverts),
|
2020-02-26 05:07:34 +00:00
|
|
|
partial_range(morph_vecs, startpoint, endpoint),
|
2020-02-26 05:07:34 +00:00
|
|
|
partial_range(morph_deltas, startpoint, endpoint),
|
2020-02-26 05:07:34 +00:00
|
|
|
partial_range(morph_times, startpoint, endpoint)
|
2020-02-26 05:07:34 +00:00
|
|
|
);
|
|
|
|
range_for (auto &&z, zr)
|
2020-02-01 22:33:31 +00:00
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto vp = &std::get<0>(z);
|
|
|
|
auto &morph_vec = std::get<1>(z);
|
|
|
|
auto &morph_delta = std::get<2>(z);
|
|
|
|
auto &morph_time = std::get<3>(z);
|
|
|
|
if (morph_time) //not done yet
|
2013-02-21 00:20:26 +00:00
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
if ((morph_time -= FrameTime) <= 0) {
|
|
|
|
morph_vec = *vp;
|
|
|
|
morph_time = 0;
|
2006-03-20 16:43:15 +00:00
|
|
|
md->n_morphing_points[submodel_num]--;
|
|
|
|
}
|
|
|
|
else
|
2020-02-26 05:07:34 +00:00
|
|
|
vm_vec_scale_add2(morph_vec, morph_delta, FrameTime);
|
2013-02-21 00:20:26 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 01:31:28 +00:00
|
|
|
namespace dsx {
|
2006-03-20 16:43:15 +00:00
|
|
|
//process the morphing object for one frame
|
2018-06-24 05:06:15 +00:00
|
|
|
void do_morph_frame(object &obj)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-02-26 05:07:34 +00:00
|
|
|
auto &LevelUniqueMorphObjectState = LevelUniqueObjectState.MorphObjectState;
|
|
|
|
const auto umd = find_morph_data(LevelUniqueMorphObjectState, obj);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-01 22:33:31 +00:00
|
|
|
if (!umd) { //maybe loaded half-morphed from disk
|
2018-06-24 05:06:15 +00:00
|
|
|
obj.flags |= OF_SHOULD_BE_DEAD; //..so kill it
|
2006-03-20 16:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-02-01 22:33:31 +00:00
|
|
|
const auto md = umd->get();
|
2018-06-24 05:06:15 +00:00
|
|
|
assert(md->obj == &obj);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:59 +00:00
|
|
|
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
|
2020-02-26 05:07:34 +00:00
|
|
|
const polymodel &pm = Polygon_models[obj.rtype.pobj_info.model_num];
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto n_models = pm.n_models;
|
|
|
|
range_for (const auto &&zi, zip(xrange(n_models), md->submodel_active, md->n_morphing_points))
|
|
|
|
{
|
|
|
|
const unsigned i = std::get<0>(zi);
|
|
|
|
auto &submodel_active = std::get<1>(zi);
|
|
|
|
if (submodel_active == morph_data::submodel_state::animating)
|
2020-02-01 22:33:31 +00:00
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
update_points(pm,i,md);
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto &n_morphing_points = std::get<2>(zi);
|
|
|
|
if (n_morphing_points == 0) { //maybe start submodel
|
|
|
|
submodel_active = morph_data::submodel_state::visible; //not animating, just visible
|
2006-03-20 16:43:15 +00:00
|
|
|
md->n_submodels_active--; //this one done animating
|
2020-02-26 05:07:34 +00:00
|
|
|
range_for (const auto &&zt, zip(xrange(n_models), pm.submodel_parents))
|
|
|
|
{
|
|
|
|
auto &submodel_parents = std::get<1>(zt);
|
|
|
|
if (submodel_parents == i)
|
|
|
|
{ //start this one
|
|
|
|
const auto t = std::get<0>(zt);
|
2014-11-02 03:42:54 +00:00
|
|
|
init_points(pm,nullptr,t,md);
|
2006-03-20 16:43:15 +00:00
|
|
|
md->n_submodels_active++;
|
|
|
|
}
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 05:07:34 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (!md->n_submodels_active) { //done morphing!
|
|
|
|
|
2020-08-10 03:45:13 +00:00
|
|
|
obj.control_source = md->morph_save_control_type;
|
2020-08-10 03:45:13 +00:00
|
|
|
obj.movement_source = md->morph_save_movement_type;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-06-24 05:06:15 +00:00
|
|
|
obj.render_type = RT_POLYOBJ;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-06-24 05:06:15 +00:00
|
|
|
obj.mtype.phys_info = md->morph_save_phys_info;
|
2020-02-01 22:33:31 +00:00
|
|
|
umd->reset();
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_morphs()
|
|
|
|
{
|
2020-02-01 22:33:31 +00:00
|
|
|
auto &LevelUniqueMorphObjectState = LevelUniqueObjectState.MorphObjectState;
|
2018-12-30 00:43:58 +00:00
|
|
|
auto &morph_objects = LevelUniqueMorphObjectState.morph_objects;
|
2020-02-01 22:33:31 +00:00
|
|
|
morph_objects = {};
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//make the object morph
|
2020-02-26 05:07:34 +00:00
|
|
|
void morph_start(d_level_unique_morph_object_state &LevelUniqueMorphObjectState, d_level_shared_polygon_model_state &LevelSharedPolygonModelState, object_base &obj)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
|
|
|
vms_vector pmmin,pmmax;
|
|
|
|
vms_vector box_size;
|
|
|
|
|
2018-12-30 00:43:58 +00:00
|
|
|
auto &morph_objects = LevelUniqueMorphObjectState.morph_objects;
|
2018-06-18 04:11:40 +00:00
|
|
|
const auto mob = morph_objects.begin();
|
|
|
|
const auto moe = morph_objects.end();
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto mop = [](const morph_data::ptr &pmo) {
|
2020-02-01 22:33:31 +00:00
|
|
|
if (!pmo)
|
|
|
|
return true;
|
|
|
|
auto &mo = *pmo.get();
|
|
|
|
return mo.obj->type == OBJ_NONE || mo.obj->signature != mo.Morph_sig;
|
2018-06-18 04:11:40 +00:00
|
|
|
};
|
|
|
|
const auto moi = std::find_if(mob, moe, mop);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-06-18 04:11:40 +00:00
|
|
|
if (moi == moe) //no free slots
|
2006-03-20 16:43:15 +00:00
|
|
|
return;
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
|
|
|
|
const morph_data::polymodel_idx pmi(obj.rtype.pobj_info.model_num);
|
|
|
|
auto &pm = Polygon_models[pmi.idx];
|
|
|
|
|
|
|
|
*moi = morph_data::create(obj, pm, pmi);
|
2020-02-01 22:33:31 +00:00
|
|
|
morph_data *const md = moi->get();
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
assert(obj.render_type == RT_POLYOBJ);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-08-10 03:45:13 +00:00
|
|
|
md->morph_save_control_type = obj.control_source;
|
2020-08-10 03:45:13 +00:00
|
|
|
md->morph_save_movement_type = obj.movement_source;
|
2020-02-26 05:07:34 +00:00
|
|
|
md->morph_save_phys_info = obj.mtype.phys_info;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-08-10 03:45:13 +00:00
|
|
|
assert(obj.control_source == object::control_type::ai); //morph objects are also AI objects
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-08-10 03:45:13 +00:00
|
|
|
obj.control_source = object::control_type::morph;
|
2020-02-26 05:07:34 +00:00
|
|
|
obj.render_type = RT_MORPH;
|
2020-08-10 03:45:13 +00:00
|
|
|
obj.movement_source = object::movement_type::physics; //RT_NONE;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
obj.mtype.phys_info.rotvel = morph_rotvel;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2014-11-02 03:42:54 +00:00
|
|
|
find_min_max(pm,0,pmmin,pmmax);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
box_size.x = max(-pmmin.x,pmmax.x) / 2;
|
|
|
|
box_size.y = max(-pmmin.y,pmmax.y) / 2;
|
|
|
|
box_size.z = max(-pmmin.z,pmmax.z) / 2;
|
|
|
|
|
2016-02-12 04:02:28 +00:00
|
|
|
//clear all points
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_times = md->get_morph_times();
|
|
|
|
std::fill(morph_times.begin(), morph_times.end(), fix());
|
2016-02-12 04:02:28 +00:00
|
|
|
//clear all parts
|
|
|
|
md->submodel_active = {};
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
md->n_submodels_active = 1;
|
|
|
|
|
|
|
|
//now, project points onto surface of box
|
|
|
|
|
|
|
|
init_points(pm,&box_size,0,md);
|
|
|
|
}
|
2020-08-24 01:31:28 +00:00
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-06-18 04:11:40 +00:00
|
|
|
static void draw_model(grs_canvas &canvas, polygon_model_points &robot_points, polymodel *const pm, const unsigned submodel_num, const submodel_angles anim_angles, g3s_lrgb light, morph_data *const md)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<unsigned, MAX_SUBMODELS> sort_list;
|
2016-05-22 17:49:30 +00:00
|
|
|
unsigned sort_n;
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
//first, sort the submodels
|
|
|
|
|
|
|
|
sort_list[0] = submodel_num;
|
|
|
|
sort_n = 1;
|
|
|
|
|
2017-09-30 18:00:15 +00:00
|
|
|
const uint_fast32_t n_models = pm->n_models;
|
2019-05-04 18:27:37 +00:00
|
|
|
range_for (const uint_fast32_t i, xrange(n_models))
|
2020-02-01 22:33:31 +00:00
|
|
|
if (md->submodel_active[i] != morph_data::submodel_state::invisible && pm->submodel_parents[i] == submodel_num)
|
|
|
|
{
|
2017-09-30 18:00:15 +00:00
|
|
|
const auto facing = g3_check_normal_facing(pm->submodel_pnts[i],pm->submodel_norms[i]);
|
2006-03-20 16:43:15 +00:00
|
|
|
if (!facing)
|
2017-09-30 18:00:15 +00:00
|
|
|
sort_list[sort_n] = i;
|
2006-03-20 16:43:15 +00:00
|
|
|
else { //put at start
|
2017-09-30 18:00:15 +00:00
|
|
|
const auto b = sort_list.begin();
|
|
|
|
const auto e = std::next(b, sort_n);
|
|
|
|
std::move_backward(b, e, std::next(e));
|
2006-03-20 16:43:15 +00:00
|
|
|
sort_list[0] = i;
|
|
|
|
}
|
2017-09-30 18:00:15 +00:00
|
|
|
++sort_n;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//now draw everything
|
|
|
|
|
2016-05-22 17:49:30 +00:00
|
|
|
range_for (const auto mn, partial_const_range(sort_list, sort_n))
|
|
|
|
{
|
2006-03-20 16:43:15 +00:00
|
|
|
if (mn == submodel_num) {
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<bitmap_index, MAX_POLYOBJ_TEXTURES> texture_list_index;
|
2020-05-22 02:40:26 +00:00
|
|
|
std::array<grs_bitmap *, MAX_POLYOBJ_TEXTURES> texture_list;
|
2016-02-12 04:02:28 +00:00
|
|
|
for (unsigned i = 0; i < pm->n_textures; ++i)
|
|
|
|
{
|
2016-05-22 17:49:30 +00:00
|
|
|
const auto ptr = ObjBitmapPtrs[pm->first_texture + i];
|
|
|
|
const auto &bmp = ObjBitmaps[ptr];
|
|
|
|
texture_list_index[i] = bmp;
|
|
|
|
texture_list[i] = &GameBitmaps[bmp.index];
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the textures for this object are paged in...
|
2016-02-12 04:02:28 +00:00
|
|
|
range_for (auto &j, partial_const_range(texture_list_index, pm->n_textures))
|
|
|
|
PIGGY_PAGE_IN(j);
|
2006-03-20 16:43:15 +00:00
|
|
|
// Hmmm... cache got flushed in the middle of paging all these in,
|
|
|
|
// so we need to reread them all in.
|
|
|
|
// Make sure that they can all fit in memory.
|
2020-02-26 05:07:34 +00:00
|
|
|
const auto morph_vecs = md->get_morph_vecs();
|
|
|
|
g3_draw_morphing_model(canvas, &pm->model_data[pm->submodel_ptrs[submodel_num]], &texture_list[0], anim_angles, light, &morph_vecs[md->submodel_startpoints[submodel_num]], robot_points);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-02-05 03:03:51 +00:00
|
|
|
const auto &&orient = vm_angles_2_matrix(anim_angles[mn]);
|
2021-09-04 12:17:14 +00:00
|
|
|
auto &&ctx = g3_start_instance_matrix(pm->submodel_offsets[mn], orient);
|
2017-03-11 19:56:27 +00:00
|
|
|
draw_model(canvas, robot_points,pm,mn,anim_angles,light,md);
|
2021-09-04 12:17:14 +00:00
|
|
|
g3_done_instance(ctx);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-26 21:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 18:00:07 +00:00
|
|
|
namespace dsx {
|
|
|
|
|
|
|
|
void draw_morph_object(grs_canvas &canvas, const d_level_unique_light_state &LevelUniqueLightState, const vmobjptridx_t obj)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2020-02-06 03:22:45 +00:00
|
|
|
if (Newdemo_state == ND_STATE_PLAYBACK)
|
|
|
|
return;
|
2006-03-20 16:43:15 +00:00
|
|
|
polymodel *po;
|
|
|
|
|
2020-02-26 05:07:34 +00:00
|
|
|
auto &LevelUniqueMorphObjectState = LevelUniqueObjectState.MorphObjectState;
|
|
|
|
const auto umd = find_morph_data(LevelUniqueMorphObjectState, obj);
|
2020-02-01 22:33:31 +00:00
|
|
|
if (!umd)
|
|
|
|
throw std::runtime_error("missing morph data");
|
|
|
|
const auto md = umd->get();
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2018-12-30 00:43:59 +00:00
|
|
|
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
|
2006-03-20 16:43:15 +00:00
|
|
|
po=&Polygon_models[obj->rtype.pobj_info.model_num];
|
|
|
|
|
2019-04-13 18:00:07 +00:00
|
|
|
const auto light = compute_object_light(LevelUniqueLightState, obj);
|
2006-03-20 16:43:15 +00:00
|
|
|
|
2021-09-04 12:17:14 +00:00
|
|
|
{
|
|
|
|
auto &&ctx = g3_start_instance_matrix(obj->pos, obj->orient);
|
2015-02-05 03:03:50 +00:00
|
|
|
polygon_model_points robot_points;
|
2017-03-11 19:56:27 +00:00
|
|
|
draw_model(canvas, robot_points, po, 0, obj->rtype.pobj_info.anim_angles, light, md);
|
2021-09-04 12:17:14 +00:00
|
|
|
g3_done_instance(ctx);
|
|
|
|
}
|
2006-03-20 16:43:15 +00:00
|
|
|
|
|
|
|
if (Newdemo_state == ND_STATE_RECORDING)
|
2019-07-07 22:00:02 +00:00
|
|
|
newdemo_record_morph_frame(obj);
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
2019-04-13 18:00:07 +00:00
|
|
|
|
|
|
|
}
|