| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Hacked-in polygon objects |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
|
| 27 |
#include <span> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "inferno.h" |
| 33 |
#include "robot.h" |
| 34 |
#include "vecmat.h" |
| 35 |
#include "cntrlcen.h" |
| 36 |
#include "interp.h" |
| 37 |
#include "dxxerror.h" |
| 38 |
#include "u_mem.h" |
| 39 |
#include "physfs-serial.h" |
| 40 |
#include "physfsx.h" |
| 41 |
#ifndef DRIVE |
| 42 |
#include "bm.h" |
| 43 |
#include "textures.h" |
| 44 |
#include "object.h" |
| 45 |
#include "lighting.h" |
| 46 |
#include "piggy.h" |
| 47 |
#endif |
| 48 |
#include "render.h" |
| 49 |
#if DXX_USE_OGL |
| 50 |
#include "ogl_init.h" |
| 51 |
#else |
| 52 |
#include "texmap.h" |
| 53 |
#endif |
| 54 |
#include "bm.h" |
| 55 |
|
| 56 |
#include "d_construct.h" |
| 57 |
#include "d_zip.h" |
| 58 |
#include "partial_range.h" |
| 59 |
#include <memory> |
| 60 |
|
| 61 |
#define PM_COMPATIBLE_VERSION 6 |
| 62 |
#define PM_OBJFILE_VERSION 8 |
| 63 |
|
| 64 |
namespace { |
| 65 |
|
| 66 |
#define MODEL_BUF_SIZE 32768 |
| 67 |
|
| 68 |
static void _pof_cfseek(int len, int type, std::size_t &Pof_addr) |
| 69 |
{ |
| 70 |
switch (type) { |
| 71 |
case SEEK_SET: Pof_addr = len; break; |
| 72 |
case SEEK_CUR: Pof_addr += len; break; |
| 73 |
} |
| 74 |
|
| 75 |
if (Pof_addr > MODEL_BUF_SIZE) |
| 76 |
Int3(); |
| 77 |
} |
| 78 |
|
| 79 |
#define pof_cfseek(_buf,_len,_type) _pof_cfseek((_len), (_type), Pof_addr) |
| 80 |
|
| 81 |
static int32_t pof_read_int(const std::span<const uint8_t> bufp, std::size_t &Pof_addr) |
| 82 |
{ |
| 83 |
const auto s = bufp.subspan(Pof_addr, 4); |
| 84 |
Pof_addr += 4; |
| 85 |
return {GET_INTEL_INT<int32_t>(s.data())}; |
| 86 |
} |
| 87 |
|
| 88 |
static std::size_t pof_cfread(void *const dst, const size_t elsize, const std::span<const uint8_t> bufp, std::size_t &Pof_addr) |
| 89 |
{ |
| 90 |
if (Pof_addr + elsize > bufp.size()) |
| 91 |
return 0; |
| 92 |
|
| 93 |
memcpy(dst, &bufp[Pof_addr], elsize); |
| 94 |
|
| 95 |
Pof_addr += elsize; |
| 96 |
|
| 97 |
if (Pof_addr > MODEL_BUF_SIZE) |
| 98 |
Int3(); |
| 99 |
return elsize; |
| 100 |
} |
| 101 |
|
| 102 |
#define new_pof_read_int(i,f) pof_cfread(&(i), sizeof(i), (f), Pof_addr) |
| 103 |
|
| 104 |
static int16_t pof_read_short(const std::span<const uint8_t> bufp, std::size_t &Pof_addr) |
| 105 |
{ |
| 106 |
const auto s = bufp.subspan(Pof_addr, 2); |
| 107 |
Pof_addr += 2; |
| 108 |
return {GET_INTEL_SHORT<int16_t>(s.data())}; |
| 109 |
} |
| 110 |
|
| 111 |
static void pof_skip_string(int max_char, const std::span<const uint8_t> bufp, std::size_t &Pof_addr) |
| 112 |
{ |
| 113 |
for (int i=0; i<max_char; i++) { |
| 114 |
if (bufp[Pof_addr++] == 0) |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
[[nodiscard]] |
| 120 |
static vms_vector pof_read_vec(const std::span<const uint8_t> bufp, std::size_t &Pof_addr) |
| 121 |
{ |
| 122 |
vms_vector vec; |
| 123 |
vec.x = pof_read_int(bufp, Pof_addr); |
| 124 |
vec.y = pof_read_int(bufp, Pof_addr); |
| 125 |
vec.z = pof_read_int(bufp, Pof_addr); |
| 126 |
if (Pof_addr > MODEL_BUF_SIZE) |
| 127 |
Int3(); |
| 128 |
return vec; |
| 129 |
} |
| 130 |
|
| 131 |
[[nodiscard]] |
| 132 |
static vms_angvec pof_read_ang(const std::span<const uint8_t> bufp, std::size_t &Pof_addr) |
| 133 |
{ |
| 134 |
vms_angvec ang; |
| 135 |
ang.p = pof_read_short(bufp, Pof_addr); |
| 136 |
ang.b = pof_read_short(bufp, Pof_addr); |
| 137 |
ang.h = pof_read_short(bufp, Pof_addr); |
| 138 |
if (Pof_addr > MODEL_BUF_SIZE) |
| 139 |
Int3(); |
| 140 |
return ang; |
| 141 |
} |
| 142 |
|
| 143 |
#define ID_OHDR 0x5244484f // 'RDHO' //Object header |
| 144 |
#define ID_SOBJ 0x4a424f53 // 'JBOS' //Subobject header |
| 145 |
#define ID_GUNS 0x534e5547 // 'SNUG' //List of guns on this object |
| 146 |
#define ID_ANIM 0x4d494e41 // 'MINA' //Animation data |
| 147 |
#define ID_IDTA 0x41544449 // 'ATDI' //Interpreter data |
| 148 |
#define ID_TXTR 0x52545854 // 'RTXT' //Texture filename list |
| 149 |
|
| 150 |
//set the animation angles for this robot. Gun fields of robot info must |
| 151 |
//be filled in. |
| 152 |
|
| 153 |
//reads a binary file containing a 3d model |
| 154 |
static void read_model_file(polymodel &pm, const char *const filename, robot_info *const r) |
| 155 |
{ |
| 156 |
short version; |
| 157 |
int len, next_chunk; |
| 158 |
|
| 159 |
auto &&[ifile, physfserr] = PHYSFSX_openReadBuffered(filename); |
| 160 |
if (!ifile) |
| 161 |
Error("Failed to open file <%s>: %s", filename, PHYSFS_getErrorByCode(physfserr)); |
| 162 |
|
| 163 |
std::size_t Pof_addr = 0; |
| 164 |
std::array<uint8_t, MODEL_BUF_SIZE> model_storage; |
| 165 |
const std::size_t Pof_file_end = PHYSFSX_readBytes(ifile, model_storage.data(), model_storage.size()); |
| 166 |
const std::span model_buf{model_storage.data(), Pof_file_end}; |
| 167 |
ifile.reset(); |
| 168 |
const int model_id = pof_read_int(model_buf, Pof_addr); |
| 169 |
|
| 170 |
if (model_id != 0x4f505350) /* 'OPSP' */ |
| 171 |
Error("Bad ID in model file <%s>",filename); |
| 172 |
|
| 173 |
version = pof_read_short(model_buf, Pof_addr); |
| 174 |
|
| 175 |
if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION) |
| 176 |
Error("Bad version (%d) in model file <%s>",version,filename); |
| 177 |
|
| 178 |
int pof_id; |
| 179 |
while (new_pof_read_int(pof_id, model_buf)) |
| 180 |
{ |
| 181 |
pof_id = INTEL_INT(pof_id); |
| 182 |
//id = pof_read_int(model_buf); |
| 183 |
len = pof_read_int(model_buf, Pof_addr); |
| 184 |
next_chunk = Pof_addr + len; |
| 185 |
|
| 186 |
switch (pof_id) |
| 187 |
{ |
| 188 |
case ID_OHDR: { //Object header |
| 189 |
pm.n_models = pof_read_int(model_buf, Pof_addr); |
| 190 |
pm.rad = pof_read_int(model_buf, Pof_addr); |
| 191 |
|
| 192 |
assert(pm.n_models <= MAX_SUBMODELS); |
| 193 |
|
| 194 |
{ |
| 195 |
const auto pmmin{pof_read_vec(model_buf, Pof_addr)}; |
| 196 |
static_cast<void>(pmmin); |
| 197 |
} |
| 198 |
{ |
| 199 |
const auto pmmax{pof_read_vec(model_buf, Pof_addr)}; |
| 200 |
static_cast<void>(pmmax); |
| 201 |
} |
| 202 |
break; |
| 203 |
} |
| 204 |
|
| 205 |
case ID_SOBJ: { //Subobject header |
| 206 |
const auto n{pof_read_short(model_buf, Pof_addr)}; |
| 207 |
const auto on{pm.submodel_rads.valid_index(n)}; |
| 208 |
if (!on) |
| 209 |
/* This leaves the position pointer in an incorrect |
| 210 |
* location, but it only happens on ill-formed input. |
| 211 |
* Older versions of the game would have undefined behavior |
| 212 |
* on this input, so defining the result to be an incorrect |
| 213 |
* position is fine. |
| 214 |
*/ |
| 215 |
break; |
| 216 |
const submodel_index sn{*on}; |
| 217 |
|
| 218 |
pm.submodel_parents[n] = pof_read_short(model_buf, Pof_addr); |
| 219 |
|
| 220 |
reconstruct_at(pm.submodel_norms[n], pof_read_vec, model_buf, Pof_addr); |
| 221 |
reconstruct_at(pm.submodel_pnts[n], pof_read_vec, model_buf, Pof_addr); |
| 222 |
reconstruct_at(pm.submodel_offsets[n], pof_read_vec, model_buf, Pof_addr); |
| 223 |
|
| 224 |
pm.submodel_rads[sn] = pof_read_int(model_buf, Pof_addr); //radius |
| 225 |
pm.submodel_ptrs[n] = pof_read_int(model_buf, Pof_addr); //offset |
| 226 |
|
| 227 |
break; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
#ifndef DRIVE |
| 232 |
case ID_GUNS: { //List of guns on this object |
| 233 |
|
| 234 |
if (r) { |
| 235 |
const uint8_t n_guns = pof_read_int(model_buf, Pof_addr); |
| 236 |
r->n_guns = std::min<uint8_t>(n_guns, MAX_GUNS); |
| 237 |
for (const auto i : xrange(n_guns)) |
| 238 |
{ |
| 239 |
(void)i; |
| 240 |
const uint_fast32_t gun_id = pof_read_short(model_buf, Pof_addr); |
| 241 |
/* |
| 242 |
* D1 v1.0 boss02.pof has id=4 and r->n_guns==4. This |
| 243 |
* is wrong, but it will not cause memory corruption. |
| 244 |
*/ |
| 245 |
const auto submodel = pof_read_short(model_buf, Pof_addr); |
| 246 |
const auto v{pof_read_vec(model_buf, Pof_addr)}; |
| 247 |
Assert(submodel != 0xff); |
| 248 |
if (gun_id < std::size(r->gun_submodels)) |
| 249 |
{ |
| 250 |
/* Cast to robot_gun_number is well defined because |
| 251 |
* the if test ensures the index is in range for |
| 252 |
* the array. |
| 253 |
*/ |
| 254 |
const auto g = static_cast<robot_gun_number>(gun_id); |
| 255 |
r->gun_submodels[g] = submodel; |
| 256 |
r->gun_points[g] = v; |
| 257 |
} |
| 258 |
|
| 259 |
if (version >= 7) |
| 260 |
{ |
| 261 |
const auto gun_dir{pof_read_vec(model_buf, Pof_addr)}; |
| 262 |
static_cast<void>(gun_dir); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
else |
| 267 |
pof_cfseek(model_buf,len,SEEK_CUR); |
| 268 |
|
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
case ID_ANIM: //Animation data |
| 273 |
if (r) { |
| 274 |
enumerated_array<std::array<vms_angvec, MAX_SUBMODELS>, N_ANIM_STATES, robot_animation_state> anim_angs{}; |
| 275 |
unsigned n_frames; |
| 276 |
|
| 277 |
n_frames = pof_read_short(model_buf, Pof_addr); |
| 278 |
|
| 279 |
Assert(n_frames == N_ANIM_STATES); |
| 280 |
|
| 281 |
for (int m = 0; m < pm.n_models; ++m) |
| 282 |
range_for (auto &f, partial_range(anim_angs, n_frames)) |
| 283 |
reconstruct_at(f[m], pof_read_ang, model_buf, Pof_addr); |
| 284 |
|
| 285 |
robot_set_angles(*r, pm, anim_angs); |
| 286 |
} |
| 287 |
else |
| 288 |
pof_cfseek(model_buf,len,SEEK_CUR); |
| 289 |
|
| 290 |
break; |
| 291 |
#endif |
| 292 |
|
| 293 |
case ID_TXTR: { //Texture filename list |
| 294 |
int n; |
| 295 |
|
| 296 |
n = pof_read_short(model_buf, Pof_addr); |
| 297 |
while (n--) { |
| 298 |
pof_skip_string(128, model_buf, Pof_addr); |
| 299 |
} |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
case ID_IDTA: //Interpreter data |
| 304 |
pm.model_data_size = len; |
| 305 |
pm.model_data = std::make_unique<uint8_t[]>(pm.model_data_size); |
| 306 |
|
| 307 |
pof_cfread(pm.model_data.get(), len, model_buf, Pof_addr); |
| 308 |
|
| 309 |
break; |
| 310 |
|
| 311 |
default: |
| 312 |
pof_cfseek(model_buf,len,SEEK_CUR); |
| 313 |
break; |
| 314 |
|
| 315 |
} |
| 316 |
if ( version >= 8 ) // Version 8 needs 4-byte alignment!!! |
| 317 |
pof_cfseek(model_buf,next_chunk,SEEK_SET); |
| 318 |
} |
| 319 |
|
| 320 |
if constexpr (DXX_WORDS_NEED_ALIGNMENT) |
| 321 |
align_polygon_model_data(&pm); |
| 322 |
if constexpr (words_bigendian) |
| 323 |
swap_polygon_model_data(pm.model_data.get()); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
namespace dsx { |
| 328 |
|
| 329 |
//reads the gun information for a model |
| 330 |
//fills in arrays gun_points & gun_dirs, returns the number of guns read |
| 331 |
reactor read_model_guns(const char *filename) |
| 332 |
{ |
| 333 |
reactor r; |
| 334 |
short version; |
| 335 |
int len; |
| 336 |
int n_guns{0}; |
| 337 |
|
| 338 |
auto &&[ifile, physfserr] = PHYSFSX_openReadBuffered(filename); |
| 339 |
if (!ifile) |
| 340 |
Error("Failed to open file <%s>: %s", filename, PHYSFS_getErrorByCode(physfserr)); |
| 341 |
|
| 342 |
std::size_t Pof_addr = 0; |
| 343 |
std::array<uint8_t, MODEL_BUF_SIZE> model_storage; |
| 344 |
const std::size_t Pof_file_end = PHYSFSX_readBytes(ifile, model_storage.data(), model_storage.size()); |
| 345 |
const std::span model_buf{model_storage.data(), Pof_file_end}; |
| 346 |
ifile.reset(); |
| 347 |
|
| 348 |
const int model_id = pof_read_int(model_buf, Pof_addr); |
| 349 |
|
| 350 |
if (model_id != 0x4f505350) /* 'OPSP' */ |
| 351 |
Error("Bad ID in model file <%s>",filename); |
| 352 |
|
| 353 |
version = pof_read_short(model_buf, Pof_addr); |
| 354 |
|
| 355 |
Assert(version >= 7); //must be 7 or higher for this data |
| 356 |
|
| 357 |
if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION) |
| 358 |
Error("Bad version (%d) in model file <%s>",version,filename); |
| 359 |
|
| 360 |
int pof_id; |
| 361 |
while (new_pof_read_int(pof_id,model_buf)) |
| 362 |
{ |
| 363 |
pof_id = INTEL_INT(pof_id); |
| 364 |
//id = pof_read_int(model_buf); |
| 365 |
len = pof_read_int(model_buf, Pof_addr); |
| 366 |
|
| 367 |
if (pof_id == ID_GUNS) |
| 368 |
{ //List of guns on this object |
| 369 |
n_guns = pof_read_int(model_buf, Pof_addr); |
| 370 |
|
| 371 |
for (int i=0;i<n_guns;i++) { |
| 372 |
int sm; |
| 373 |
|
| 374 |
const int gun_id = pof_read_short(model_buf, Pof_addr); |
| 375 |
sm = pof_read_short(model_buf, Pof_addr); |
| 376 |
if (sm!=0) |
| 377 |
Error("Invalid gun submodel in file <%s>",filename); |
| 378 |
reconstruct_at(r.gun_points[gun_id], pof_read_vec, model_buf, Pof_addr); |
| 379 |
reconstruct_at(r.gun_dirs[gun_id], pof_read_vec, model_buf, Pof_addr); |
| 380 |
} |
| 381 |
|
| 382 |
} |
| 383 |
else |
| 384 |
pof_cfseek(model_buf,len,SEEK_CUR); |
| 385 |
|
| 386 |
} |
| 387 |
r.n_guns = n_guns; |
| 388 |
return r; |
| 389 |
} |
| 390 |
|
| 391 |
} |
| 392 |
|
| 393 |
namespace dcx { |
| 394 |
|
| 395 |
//free up a model, getting rid of all its memory |
| 396 |
#if DXX_BUILD_DESCENT == 1 |
| 397 |
static |
| 398 |
#endif |
| 399 |
void free_model(polymodel &po) |
| 400 |
{ |
| 401 |
po.model_data.reset(); |
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
//draw a polygon model |
| 407 |
|
| 408 |
namespace dsx { |
| 409 |
|
| 410 |
void draw_polygon_model(const enumerated_array<polymodel, MAX_POLYGON_MODELS, polygon_model_index> &Polygon_models, grs_canvas &canvas, const tmap_drawer_type tmap_drawer_ptr, const vms_vector &pos, const vms_matrix &orient, const submodel_angles anim_angles, const polygon_model_index model_num, const unsigned flags, const g3s_lrgb light, const glow_values_t *const glow_values, const alternate_textures alt_textures) |
| 411 |
{ |
| 412 |
draw_polygon_model(canvas, tmap_drawer_ptr, pos, orient, anim_angles, Polygon_models[model_num], flags, light, glow_values, alt_textures); |
| 413 |
} |
| 414 |
|
| 415 |
static polygon_model_index build_polygon_model_index_from_polygon_simpler_model_index(const polygon_simpler_model_index i) |
| 416 |
{ |
| 417 |
return static_cast<polygon_model_index>(underlying_value(i) - 1); |
| 418 |
} |
| 419 |
|
| 420 |
void draw_polygon_model(grs_canvas &canvas, const tmap_drawer_type tmap_drawer_ptr, const vms_vector &pos, const vms_matrix &orient, const submodel_angles anim_angles, const polymodel &pm, unsigned flags, const g3s_lrgb light, const glow_values_t *const glow_values, const alternate_textures alt_textures) |
| 421 |
{ |
| 422 |
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models; |
| 423 |
const polymodel *po = ± |
| 424 |
|
| 425 |
//check if should use simple model |
| 426 |
if (po->simpler_model != polygon_simpler_model_index::None) //must have a simpler model |
| 427 |
if (flags==0) //can't switch if this is debris |
| 428 |
//alt textures might not match, but in the one case we're using this |
| 429 |
//for on 11/14/94, they do match. So we leave it in. |
| 430 |
{ |
| 431 |
int cnt{1}; |
| 432 |
const auto depth = g3_calc_point_depth(pos); //gets 3d depth |
| 433 |
while (po->simpler_model != polygon_simpler_model_index::None && depth > cnt++ * Simple_model_threshhold_scale * po->rad) |
| 434 |
po = &Polygon_models[build_polygon_model_index_from_polygon_simpler_model_index(po->simpler_model)]; |
| 435 |
} |
| 436 |
|
| 437 |
std::array<grs_bitmap *, MAX_POLYOBJ_TEXTURES> texture_list; |
| 438 |
{ |
| 439 |
const unsigned n_textures = po->n_textures; |
| 440 |
std::array<bitmap_index, MAX_POLYOBJ_TEXTURES> texture_list_index; |
| 441 |
auto &&tlir = partial_range(texture_list_index, n_textures); |
| 442 |
if (const std::span<const bitmap_index> a{alt_textures}; !a.empty()) |
| 443 |
{ |
| 444 |
for (auto &&[at, tli] : zip(a.first(n_textures), tlir)) |
| 445 |
tli = at; |
| 446 |
} |
| 447 |
else |
| 448 |
{ |
| 449 |
const unsigned first_texture = po->first_texture; |
| 450 |
for (auto &&[obp, tli] : zip(partial_range(ObjBitmapPtrs, first_texture, first_texture + n_textures), tlir)) |
| 451 |
tli = ObjBitmaps[obp]; |
| 452 |
} |
| 453 |
|
| 454 |
// Make sure the textures for this object are paged in... |
| 455 |
for (auto &&[tli, tl] : zip(tlir, partial_range(texture_list, n_textures))) |
| 456 |
{ |
| 457 |
tl = &GameBitmaps[tli]; |
| 458 |
PIGGY_PAGE_IN(tli); |
| 459 |
} |
| 460 |
} |
| 461 |
// Hmmm... cache got flushed in the middle of paging all these in, |
| 462 |
// so we need to reread them all in. |
| 463 |
// Make sure that they can all fit in memory. |
| 464 |
|
| 465 |
auto &&ctx = g3_start_instance_matrix(pos, orient); |
| 466 |
|
| 467 |
polygon_model_points robot_points; |
| 468 |
|
| 469 |
if (flags == 0) //draw entire object |
| 470 |
g3_draw_polygon_model(texture_list.data(), robot_points, canvas, tmap_drawer_ptr, anim_angles, light, glow_values, po->model_data.get()); |
| 471 |
|
| 472 |
else { |
| 473 |
for (int i=0;flags;flags>>=1,i++) |
| 474 |
if (flags & 1) { |
| 475 |
Assert(i < po->n_models); |
| 476 |
|
| 477 |
//if submodel, rotate around its center point, not pivot point |
| 478 |
auto &&subctx = g3_start_instance_matrix(); |
| 479 |
g3_draw_polygon_model(texture_list.data(), robot_points, canvas, tmap_drawer_ptr, anim_angles, light, glow_values, &po->model_data[po->submodel_ptrs[i]]); |
| 480 |
g3_done_instance(subctx); |
| 481 |
} |
| 482 |
} |
| 483 |
g3_done_instance(ctx); |
| 484 |
} |
| 485 |
|
| 486 |
void free_polygon_models(d_level_shared_polygon_model_state &LevelSharedPolygonModelState) |
| 487 |
{ |
| 488 |
for (auto &i : partial_range(LevelSharedPolygonModelState.Polygon_models, LevelSharedPolygonModelState.N_polygon_models)) |
| 489 |
free_model(i); |
| 490 |
#if DXX_BUILD_DESCENT == 2 |
| 491 |
LevelSharedPolygonModelState.Exit_models_loaded = false; |
| 492 |
#endif |
| 493 |
} |
| 494 |
|
| 495 |
} |
| 496 |
|
| 497 |
namespace dcx { |
| 498 |
namespace { |
| 499 |
|
| 500 |
static void assign_max(fix &a, const fix &b) |
| 501 |
{ |
| 502 |
a = std::max(a, b); |
| 503 |
} |
| 504 |
|
| 505 |
static void assign_min(fix &a, const fix &b) |
| 506 |
{ |
| 507 |
a = std::min(a, b); |
| 508 |
} |
| 509 |
|
| 510 |
template <fix vms_vector::*p> |
| 511 |
static void update_bounds(vms_vector &minv, vms_vector &maxv, const vms_vector &vp) |
| 512 |
{ |
| 513 |
auto &mx = maxv.*p; |
| 514 |
assign_max(mx, vp.*p); |
| 515 |
auto &mn = minv.*p; |
| 516 |
assign_min(mn, vp.*p); |
| 517 |
} |
| 518 |
|
| 519 |
static void assign_minmax(vms_vector &minv, vms_vector &maxv, const vms_vector &v) |
| 520 |
{ |
| 521 |
update_bounds<&vms_vector::x>(minv, maxv, v); |
| 522 |
update_bounds<&vms_vector::y>(minv, maxv, v); |
| 523 |
update_bounds<&vms_vector::z>(minv, maxv, v); |
| 524 |
} |
| 525 |
|
| 526 |
static void polyobj_find_min_max(polymodel &pm) |
| 527 |
{ |
| 528 |
auto &big_mn = pm.mins; |
| 529 |
auto &big_mx = pm.maxs; |
| 530 |
for (int m = 0; m < pm.n_models; ++m) |
| 531 |
{ |
| 532 |
auto &mn = pm.submodel_mins[m]; |
| 533 |
auto &mx = pm.submodel_maxs[m]; |
| 534 |
const auto &ofs = pm.submodel_offsets[m]; |
| 535 |
|
| 536 |
auto data = reinterpret_cast<const uint16_t *>(&pm.model_data[pm.submodel_ptrs[m]]); |
| 537 |
|
| 538 |
const auto type = *data++; |
| 539 |
|
| 540 |
Assert(type == 7 || type == 1); |
| 541 |
|
| 542 |
const uint16_t nverts = *data++ - 1; |
| 543 |
|
| 544 |
if (type==7) |
| 545 |
data+=2; //skip start & pad |
| 546 |
|
| 547 |
auto vp = reinterpret_cast<const vms_vector *>(data); |
| 548 |
|
| 549 |
mn = mx = *vp++; |
| 550 |
|
| 551 |
if (m==0) |
| 552 |
big_mn = big_mx = mn; |
| 553 |
|
| 554 |
range_for (auto &v, unchecked_partial_range(vp, nverts)) |
| 555 |
{ |
| 556 |
assign_minmax(mn, mx, v); |
| 557 |
assign_minmax(big_mn, big_mx, vm_vec_build_add(v, ofs)); |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
void init_polygon_models(d_level_shared_polygon_model_state &LevelSharedPolygonModelState) |
| 564 |
{ |
| 565 |
LevelSharedPolygonModelState.N_polygon_models = 0; |
| 566 |
} |
| 567 |
|
| 568 |
} |
| 569 |
|
| 570 |
namespace dsx { |
| 571 |
|
| 572 |
//returns the number of this model |
| 573 |
polygon_model_index load_polygon_model(const char *filename, int n_textures, int first_texture, robot_info *const r) |
| 574 |
{ |
| 575 |
Assert(n_textures < MAX_POLYOBJ_TEXTURES); |
| 576 |
|
| 577 |
Assert(strlen(filename) <= 12); |
| 578 |
const auto n_models = build_polygon_model_index_from_untrusted(LevelSharedPolygonModelState.N_polygon_models); |
| 579 |
if (n_models == polygon_model_index::None) |
| 580 |
throw std::runtime_error("too many polygon models"); |
| 581 |
strcpy(LevelSharedPolygonModelState.Pof_names[n_models], filename); |
| 582 |
|
| 583 |
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models; |
| 584 |
auto &model = Polygon_models[n_models]; |
| 585 |
read_model_file(model, filename, r); |
| 586 |
polyobj_find_min_max(model); |
| 587 |
|
| 588 |
const auto highest_texture_num = g3_init_polygon_model(std::span{model.model_data.get(), model.model_data_size}); |
| 589 |
|
| 590 |
if (highest_texture_num+1 != n_textures) |
| 591 |
Error("Model <%s> references %d textures but specifies %d.",filename,highest_texture_num+1,n_textures); |
| 592 |
|
| 593 |
model.n_textures = n_textures; |
| 594 |
model.first_texture = first_texture; |
| 595 |
model.simpler_model = polygon_simpler_model_index{}; |
| 596 |
|
| 597 |
++LevelSharedPolygonModelState.N_polygon_models; |
| 598 |
return n_models; |
| 599 |
} |
| 600 |
|
| 601 |
} |
| 602 |
|
| 603 |
//compare against this size when figuring how far to place eye for picture |
| 604 |
#define BASE_MODEL_SIZE 0x28000 |
| 605 |
|
| 606 |
#define DEFAULT_VIEW_DIST 0x60000 |
| 607 |
|
| 608 |
//draws the given model in the current canvas. The distance is set to |
| 609 |
//more-or-less fill the canvas. Note that this routine actually renders |
| 610 |
//into an off-screen canvas that it creates, then copies to the current |
| 611 |
//canvas. |
| 612 |
void draw_model_picture(grs_canvas &canvas, const polymodel &mn, const vms_angvec &orient_angles) |
| 613 |
{ |
| 614 |
g3s_lrgb lrgb = { f1_0, f1_0, f1_0 }; |
| 615 |
|
| 616 |
gr_clear_canvas(canvas, BM_XRGB(0,0,0)); |
| 617 |
g3_start_frame(canvas); |
| 618 |
vms_vector temp_pos{}; |
| 619 |
g3_set_view_matrix(temp_pos,vmd_identity_matrix,0x9000); |
| 620 |
|
| 621 |
if (mn.rad != 0) |
| 622 |
temp_pos.z = fixmuldiv(DEFAULT_VIEW_DIST, mn.rad, BASE_MODEL_SIZE); |
| 623 |
else |
| 624 |
temp_pos.z = DEFAULT_VIEW_DIST; |
| 625 |
|
| 626 |
const auto &&temp_orient{vm_angles_2_matrix(orient_angles)}; |
| 627 |
draw_polygon_model(canvas, draw_tmap, temp_pos, temp_orient, nullptr, mn, 0, lrgb, nullptr, alternate_textures{}); |
| 628 |
g3_end_frame(); |
| 629 |
} |
| 630 |
|
| 631 |
namespace dcx { |
| 632 |
|
| 633 |
DEFINE_SERIAL_VMS_VECTOR_TO_MESSAGE(); |
| 634 |
DEFINE_SERIAL_UDT_TO_MESSAGE(polymodel, p, (p.n_models, p.model_data_size, serial::pad<4>(), p.submodel_ptrs, p.submodel_offsets, p.submodel_norms, p.submodel_pnts, p.submodel_rads, p.submodel_parents, p.submodel_mins, p.submodel_maxs, p.mins, p.maxs, p.rad, p.n_textures, p.first_texture, p.simpler_model)); |
| 635 |
ASSERT_SERIAL_UDT_MESSAGE_SIZE(polymodel, 12 + (10 * 4) + (10 * 3 * sizeof(vms_vector)) + (10 * sizeof(fix)) + 10 + (10 * 2 * sizeof(vms_vector)) + (2 * sizeof(vms_vector)) + 8); |
| 636 |
|
| 637 |
/* |
| 638 |
* reads a polymodel structure from a PHYSFS_File |
| 639 |
*/ |
| 640 |
void polymodel_read(polymodel &pm, const NamedPHYSFS_File fp) |
| 641 |
{ |
| 642 |
pm.model_data.reset(); |
| 643 |
PHYSFSX_serialize_read(fp, pm); |
| 644 |
} |
| 645 |
|
| 646 |
} |
| 647 |
|
| 648 |
#if 0 |
| 649 |
void polymodel_write(PHYSFS_File *fp, const polymodel &pm) |
| 650 |
{ |
| 651 |
PHYSFSX_serialize_write(fp, pm); |
| 652 |
} |
| 653 |
#endif |
| 654 |
|
| 655 |
/* |
| 656 |
* routine which allocates, reads, and inits a polymodel's model_data |
| 657 |
*/ |
| 658 |
namespace dsx { |
| 659 |
void polygon_model_data_read(polymodel *pm, PHYSFS_File *fp) |
| 660 |
{ |
| 661 |
auto model_data_size = pm->model_data_size; |
| 662 |
pm->model_data = std::make_unique<uint8_t[]>(model_data_size); |
| 663 |
PHYSFSX_readBytes(fp, pm->model_data, model_data_size); |
| 664 |
/* Aligning model data changes pm->model_data_size. Reload it |
| 665 |
* afterward. |
| 666 |
*/ |
| 667 |
if constexpr (DXX_WORDS_NEED_ALIGNMENT) |
| 668 |
{ |
| 669 |
align_polygon_model_data(pm); |
| 670 |
model_data_size = pm->model_data_size; |
| 671 |
} |
| 672 |
if constexpr (words_bigendian) |
| 673 |
swap_polygon_model_data(pm->model_data.get()); |
| 674 |
#if DXX_BUILD_DESCENT == 1 |
| 675 |
g3_validate_polygon_model(std::span{pm->model_data.get(), model_data_size}); |
| 676 |
#elif DXX_BUILD_DESCENT == 2 |
| 677 |
g3_init_polygon_model(std::span{pm->model_data.get(), model_data_size}); |
| 678 |
#endif |
| 679 |
} |
| 680 |
|
| 681 |
polygon_model_index build_polygon_model_index_from_untrusted(const unsigned i) |
| 682 |
{ |
| 683 |
if (i >= MAX_POLYGON_MODELS) |
| 684 |
return polygon_model_index::None; |
| 685 |
return static_cast<polygon_model_index>(i); |
| 686 |
} |
| 687 |
|
| 688 |
} |