2006-03-20 17:12:09 +00:00
|
|
|
/*
|
2014-06-01 17:55:23 +00:00
|
|
|
* Portions of this file are copyright Rebirth contributors and licensed as
|
|
|
|
* described in COPYING.txt.
|
|
|
|
* Portions of this file are copyright Parallax Software and licensed
|
|
|
|
* according to the Parallax license below.
|
|
|
|
* See COPYING.txt for license details.
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
|
|
|
|
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
|
|
|
|
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
|
|
|
|
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
|
|
|
|
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
|
|
|
|
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
|
|
|
|
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
|
|
|
|
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
|
|
|
|
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
|
|
|
|
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Hacked-in polygon objects
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "inferno.h"
|
2015-04-02 02:36:57 +00:00
|
|
|
#include "robot.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "vecmat.h"
|
2015-07-25 23:10:48 +00:00
|
|
|
#include "cntrlcen.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "interp.h"
|
2012-07-07 18:35:06 +00:00
|
|
|
#include "dxxerror.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "u_mem.h"
|
|
|
|
#include "args.h"
|
2014-07-24 03:25:00 +00:00
|
|
|
#include "physfs-serial.h"
|
2014-07-20 01:09:55 +00:00
|
|
|
#include "physfsx.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#ifndef DRIVE
|
|
|
|
#include "texmap.h"
|
|
|
|
#include "bm.h"
|
|
|
|
#include "textures.h"
|
|
|
|
#include "object.h"
|
|
|
|
#include "lighting.h"
|
|
|
|
#include "piggy.h"
|
|
|
|
#endif
|
2008-04-06 20:23:28 +00:00
|
|
|
#include "render.h"
|
2016-09-24 18:06:11 +00:00
|
|
|
#if DXX_USE_OGL
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "ogl_init.h"
|
|
|
|
#endif
|
2019-12-12 15:25:11 +00:00
|
|
|
#include "bm.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2020-05-22 02:40:26 +00:00
|
|
|
#include "d_zip.h"
|
2014-09-20 23:47:27 +00:00
|
|
|
#include "partial_range.h"
|
2020-05-02 21:18:43 +00:00
|
|
|
#include <memory>
|
2014-08-16 04:15:16 +00:00
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
#define PM_COMPATIBLE_VERSION 6
|
|
|
|
#define PM_OBJFILE_VERSION 8
|
|
|
|
|
2014-09-20 23:47:27 +00:00
|
|
|
static unsigned Pof_file_end;
|
|
|
|
static unsigned Pof_addr;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
#define MODEL_BUF_SIZE 32768
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static void _pof_cfseek(int len,int type)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case SEEK_SET: Pof_addr = len; break;
|
|
|
|
case SEEK_CUR: Pof_addr += len; break;
|
|
|
|
case SEEK_END:
|
|
|
|
Assert(len <= 0); // seeking from end, better be moving back.
|
|
|
|
Pof_addr = Pof_file_end + len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Pof_addr > MODEL_BUF_SIZE)
|
|
|
|
Int3();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define pof_cfseek(_buf,_len,_type) _pof_cfseek((_len),(_type))
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static int pof_read_int(ubyte *bufp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-07-03 00:54:15 +00:00
|
|
|
i = *(reinterpret_cast<int *>(&bufp[Pof_addr]));
|
2006-03-20 17:12:09 +00:00
|
|
|
Pof_addr += 4;
|
|
|
|
return INTEL_INT(i);
|
|
|
|
|
2011-06-01 07:59:55 +00:00
|
|
|
// if (PHYSFS_read(f,&i,sizeof(i),1) != 1)
|
2006-03-20 17:12:09 +00:00
|
|
|
// Error("Unexpected end-of-file while reading object");
|
|
|
|
//
|
|
|
|
// return i;
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static size_t pof_cfread(void *dst, size_t elsize, size_t nelem, ubyte *bufp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
if (Pof_addr + nelem*elsize > Pof_file_end)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memcpy(dst, &bufp[Pof_addr], elsize*nelem);
|
|
|
|
|
|
|
|
Pof_addr += elsize*nelem;
|
|
|
|
|
|
|
|
if (Pof_addr > MODEL_BUF_SIZE)
|
|
|
|
Int3();
|
|
|
|
|
|
|
|
return nelem;
|
|
|
|
}
|
|
|
|
|
2011-06-01 07:59:55 +00:00
|
|
|
// #define new_read_int(i,f) PHYSFS_read((f),&(i),sizeof(i),1)
|
2006-03-20 17:12:09 +00:00
|
|
|
#define new_pof_read_int(i,f) pof_cfread(&(i),sizeof(i),1,(f))
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static short pof_read_short(ubyte *bufp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
short s;
|
|
|
|
|
2016-07-03 00:54:14 +00:00
|
|
|
s = *(reinterpret_cast<int16_t *>(&bufp[Pof_addr]));
|
2006-03-20 17:12:09 +00:00
|
|
|
Pof_addr += 2;
|
|
|
|
return INTEL_SHORT(s);
|
2011-06-01 07:59:55 +00:00
|
|
|
// if (PHYSFS_read(f,&s,sizeof(s),1) != 1)
|
2006-03-20 17:12:09 +00:00
|
|
|
// Error("Unexpected end-of-file while reading object");
|
|
|
|
//
|
|
|
|
// return s;
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static void pof_read_string(char *buf,int max_char, ubyte *bufp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-09-26 02:42:10 +00:00
|
|
|
for (int i=0; i<max_char; i++) {
|
2006-03-20 17:12:09 +00:00
|
|
|
if ((*buf++ = bufp[Pof_addr++]) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-01 07:59:55 +00:00
|
|
|
// while (max_char-- && (*buf=PHYSFSX_fgetc(f)) != 0) buf++;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static void pof_read_vecs(vms_vector *vecs,int n,ubyte *bufp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2011-06-01 07:59:55 +00:00
|
|
|
// PHYSFS_read(f,vecs,sizeof(vms_vector),n);
|
2013-06-23 18:23:11 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
vecs[i].x = pof_read_int(bufp);
|
|
|
|
vecs[i].y = pof_read_int(bufp);
|
|
|
|
vecs[i].z = pof_read_int(bufp);
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (Pof_addr > MODEL_BUF_SIZE)
|
|
|
|
Int3();
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static void pof_read_angs(vms_angvec *angs,int n,ubyte *bufp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2013-06-23 18:23:11 +00:00
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
angs[i].p = pof_read_short(bufp);
|
|
|
|
angs[i].b = pof_read_short(bufp);
|
|
|
|
angs[i].h = pof_read_short(bufp);
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (Pof_addr > MODEL_BUF_SIZE)
|
|
|
|
Int3();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ID_OHDR 0x5244484f // 'RDHO' //Object header
|
|
|
|
#define ID_SOBJ 0x4a424f53 // 'JBOS' //Subobject header
|
|
|
|
#define ID_GUNS 0x534e5547 // 'SNUG' //List of guns on this object
|
|
|
|
#define ID_ANIM 0x4d494e41 // 'MINA' //Animation data
|
|
|
|
#define ID_IDTA 0x41544449 // 'ATDI' //Interpreter data
|
|
|
|
#define ID_TXTR 0x52545854 // 'RTXT' //Texture filename list
|
|
|
|
|
2020-05-02 21:18:42 +00:00
|
|
|
static std::array<std::array<vms_angvec, MAX_SUBMODELS>, N_ANIM_STATES> anim_angs;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
//set the animation angles for this robot. Gun fields of robot info must
|
|
|
|
//be filled in.
|
|
|
|
|
|
|
|
//reads a binary file containing a 3d model
|
2013-06-08 22:24:17 +00:00
|
|
|
static polymodel *read_model_file(polymodel *pm,const char *filename,robot_info *r)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
short version;
|
2016-05-21 17:24:50 +00:00
|
|
|
int len, next_chunk;
|
2013-06-23 18:25:36 +00:00
|
|
|
ubyte model_buf[MODEL_BUF_SIZE];
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-01-17 18:31:42 +00:00
|
|
|
auto ifile = PHYSFSX_openReadBuffered(filename);
|
2015-01-17 18:31:42 +00:00
|
|
|
if (!ifile)
|
2006-03-20 17:12:09 +00:00
|
|
|
Error("Can't open file <%s>",filename);
|
|
|
|
|
2011-06-01 07:59:55 +00:00
|
|
|
Assert(PHYSFS_fileLength(ifile) <= MODEL_BUF_SIZE);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
Pof_addr = 0;
|
2011-06-01 07:59:55 +00:00
|
|
|
Pof_file_end = PHYSFS_read(ifile, model_buf, 1, PHYSFS_fileLength(ifile));
|
2015-01-17 18:31:42 +00:00
|
|
|
ifile.reset();
|
2016-05-21 17:24:50 +00:00
|
|
|
const int model_id = pof_read_int(model_buf);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
if (model_id != 0x4f505350) /* 'OPSP' */
|
2006-03-20 17:12:09 +00:00
|
|
|
Error("Bad ID in model file <%s>",filename);
|
|
|
|
|
|
|
|
version = pof_read_short(model_buf);
|
|
|
|
|
|
|
|
if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
|
|
|
|
Error("Bad version (%d) in model file <%s>",version,filename);
|
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
int pof_id;
|
|
|
|
while (new_pof_read_int(pof_id, model_buf) == 1)
|
|
|
|
{
|
|
|
|
pof_id = INTEL_INT(pof_id);
|
2006-03-20 17:12:09 +00:00
|
|
|
//id = pof_read_int(model_buf);
|
|
|
|
len = pof_read_int(model_buf);
|
|
|
|
next_chunk = Pof_addr + len;
|
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
switch (pof_id)
|
|
|
|
{
|
2006-03-20 17:12:09 +00:00
|
|
|
case ID_OHDR: { //Object header
|
|
|
|
vms_vector pmmin,pmmax;
|
|
|
|
|
|
|
|
pm->n_models = pof_read_int(model_buf);
|
|
|
|
pm->rad = pof_read_int(model_buf);
|
|
|
|
|
|
|
|
Assert(pm->n_models <= MAX_SUBMODELS);
|
|
|
|
|
|
|
|
pof_read_vecs(&pmmin,1,model_buf);
|
|
|
|
pof_read_vecs(&pmmax,1,model_buf);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ID_SOBJ: { //Subobject header
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = pof_read_short(model_buf);
|
|
|
|
|
|
|
|
Assert(n < MAX_SUBMODELS);
|
|
|
|
|
|
|
|
pm->submodel_parents[n] = pof_read_short(model_buf);
|
|
|
|
|
|
|
|
pof_read_vecs(&pm->submodel_norms[n],1,model_buf);
|
|
|
|
pof_read_vecs(&pm->submodel_pnts[n],1,model_buf);
|
|
|
|
pof_read_vecs(&pm->submodel_offsets[n],1,model_buf);
|
|
|
|
|
|
|
|
pm->submodel_rads[n] = pof_read_int(model_buf); //radius
|
|
|
|
|
|
|
|
pm->submodel_ptrs[n] = pof_read_int(model_buf); //offset
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef DRIVE
|
|
|
|
case ID_GUNS: { //List of guns on this object
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
vms_vector gun_dir;
|
|
|
|
|
|
|
|
r->n_guns = pof_read_int(model_buf);
|
|
|
|
|
|
|
|
Assert(r->n_guns <= MAX_GUNS);
|
|
|
|
|
2014-09-26 02:42:10 +00:00
|
|
|
for (int i=0;i<r->n_guns;i++) {
|
2016-05-21 17:24:50 +00:00
|
|
|
const uint_fast32_t gun_id = pof_read_short(model_buf);
|
2014-08-24 18:30:25 +00:00
|
|
|
/*
|
|
|
|
* D1 v1.0 boss02.pof has id=4 and r->n_guns==4.
|
|
|
|
* Relax the assert to check only for memory
|
|
|
|
* corruption.
|
|
|
|
*/
|
2020-07-05 23:34:33 +00:00
|
|
|
assert(gun_id < std::size(r->gun_submodels));
|
2016-05-21 17:24:50 +00:00
|
|
|
auto &submodel = r->gun_submodels[gun_id];
|
|
|
|
submodel = pof_read_short(model_buf);
|
|
|
|
Assert(submodel != 0xff);
|
|
|
|
pof_read_vecs(&r->gun_points[gun_id], 1, model_buf);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (version >= 7)
|
|
|
|
pof_read_vecs(&gun_dir,1,model_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pof_cfseek(model_buf,len,SEEK_CUR);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ID_ANIM: //Animation data
|
|
|
|
if (r) {
|
2014-10-26 22:51:27 +00:00
|
|
|
unsigned n_frames;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
n_frames = pof_read_short(model_buf);
|
|
|
|
|
|
|
|
Assert(n_frames == N_ANIM_STATES);
|
|
|
|
|
2014-09-26 02:42:10 +00:00
|
|
|
for (int m=0;m<pm->n_models;m++)
|
2015-04-02 02:36:52 +00:00
|
|
|
range_for (auto &f, partial_range(anim_angs, n_frames))
|
|
|
|
pof_read_angs(&f[m], 1, model_buf);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
robot_set_angles(r,pm,anim_angs);
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pof_cfseek(model_buf,len,SEEK_CUR);
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case ID_TXTR: { //Texture filename list
|
|
|
|
int n;
|
|
|
|
char name_buf[128];
|
|
|
|
|
|
|
|
n = pof_read_short(model_buf);
|
|
|
|
while (n--) {
|
|
|
|
pof_read_string(name_buf,128,model_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ID_IDTA: //Interpreter data
|
|
|
|
pm->model_data_size = len;
|
2020-05-02 21:18:42 +00:00
|
|
|
pm->model_data = std::make_unique<uint8_t[]>(pm->model_data_size);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-07-24 03:12:57 +00:00
|
|
|
pof_cfread(pm->model_data.get(),1,len,model_buf);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
pof_cfseek(model_buf,len,SEEK_CUR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
if ( version >= 8 ) // Version 8 needs 4-byte alignment!!!
|
|
|
|
pof_cfseek(model_buf,next_chunk,SEEK_SET);
|
|
|
|
}
|
|
|
|
|
2016-10-09 17:35:03 +00:00
|
|
|
#if DXX_WORDS_NEED_ALIGNMENT
|
2006-03-20 17:12:09 +00:00
|
|
|
align_polygon_model_data(pm);
|
|
|
|
#endif
|
2020-07-05 23:34:33 +00:00
|
|
|
if constexpr (words_bigendian)
|
|
|
|
swap_polygon_model_data(pm->model_data.get());
|
2006-03-20 17:12:09 +00:00
|
|
|
return pm;
|
|
|
|
}
|
|
|
|
|
|
|
|
//reads the gun information for a model
|
|
|
|
//fills in arrays gun_points & gun_dirs, returns the number of guns read
|
2015-07-25 23:10:48 +00:00
|
|
|
void read_model_guns(const char *filename, reactor &r)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2015-07-25 23:10:48 +00:00
|
|
|
auto &gun_points = r.gun_points;
|
|
|
|
auto &gun_dirs = r.gun_dirs;
|
2006-03-20 17:12:09 +00:00
|
|
|
short version;
|
2016-05-21 17:24:50 +00:00
|
|
|
int len;
|
2006-03-20 17:12:09 +00:00
|
|
|
int n_guns=0;
|
2013-06-23 18:25:36 +00:00
|
|
|
ubyte model_buf[MODEL_BUF_SIZE];
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-01-17 18:31:42 +00:00
|
|
|
auto ifile = PHYSFSX_openReadBuffered(filename);
|
2015-01-17 18:31:42 +00:00
|
|
|
if (!ifile)
|
2006-03-20 17:12:09 +00:00
|
|
|
Error("Can't open file <%s>",filename);
|
|
|
|
|
2011-06-01 07:59:55 +00:00
|
|
|
Assert(PHYSFS_fileLength(ifile) <= MODEL_BUF_SIZE);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
Pof_addr = 0;
|
2011-06-01 07:59:55 +00:00
|
|
|
Pof_file_end = PHYSFS_read(ifile, model_buf, 1, PHYSFS_fileLength(ifile));
|
2015-01-17 18:31:42 +00:00
|
|
|
ifile.reset();
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
const int model_id = pof_read_int(model_buf);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
if (model_id != 0x4f505350) /* 'OPSP' */
|
2006-03-20 17:12:09 +00:00
|
|
|
Error("Bad ID in model file <%s>",filename);
|
|
|
|
|
|
|
|
version = pof_read_short(model_buf);
|
|
|
|
|
|
|
|
Assert(version >= 7); //must be 7 or higher for this data
|
|
|
|
|
|
|
|
if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
|
|
|
|
Error("Bad version (%d) in model file <%s>",version,filename);
|
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
int pof_id;
|
|
|
|
while (new_pof_read_int(pof_id,model_buf) == 1)
|
|
|
|
{
|
|
|
|
pof_id = INTEL_INT(pof_id);
|
2006-03-20 17:12:09 +00:00
|
|
|
//id = pof_read_int(model_buf);
|
|
|
|
len = pof_read_int(model_buf);
|
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
if (pof_id == ID_GUNS)
|
|
|
|
{ //List of guns on this object
|
2006-03-20 17:12:09 +00:00
|
|
|
n_guns = pof_read_int(model_buf);
|
|
|
|
|
2014-09-26 02:42:10 +00:00
|
|
|
for (int i=0;i<n_guns;i++) {
|
2016-05-21 17:24:50 +00:00
|
|
|
int sm;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-05-21 17:24:50 +00:00
|
|
|
const int gun_id = pof_read_short(model_buf);
|
2006-03-20 17:12:09 +00:00
|
|
|
sm = pof_read_short(model_buf);
|
2014-11-01 03:08:27 +00:00
|
|
|
if (sm!=0)
|
2006-03-20 17:12:09 +00:00
|
|
|
Error("Invalid gun submodel in file <%s>",filename);
|
2016-05-21 17:24:50 +00:00
|
|
|
pof_read_vecs(&gun_points[gun_id], 1, model_buf);
|
|
|
|
pof_read_vecs(&gun_dirs[gun_id], 1, model_buf);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pof_cfseek(model_buf,len,SEEK_CUR);
|
|
|
|
|
|
|
|
}
|
2015-07-25 23:10:48 +00:00
|
|
|
r.n_guns = n_guns;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//free up a model, getting rid of all its memory
|
2013-10-26 03:52:09 +00:00
|
|
|
#if defined(DXX_BUILD_DESCENT_I)
|
|
|
|
static
|
|
|
|
#endif
|
2017-02-19 19:33:44 +00:00
|
|
|
void free_model(polymodel &po)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2017-02-19 19:33:44 +00:00
|
|
|
po.model_data.reset();
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//draw a polygon model
|
|
|
|
|
2015-12-22 04:18:50 +00:00
|
|
|
namespace dsx {
|
|
|
|
|
2017-08-11 23:43:54 +00:00
|
|
|
void draw_polygon_model(grs_canvas &canvas, const vms_vector &pos, const vms_matrix &orient, const submodel_angles anim_angles, const unsigned model_num, unsigned flags, const g3s_lrgb light, const glow_values_t *const glow_values, alternate_textures alt_textures)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2018-12-30 00:43:59 +00:00
|
|
|
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
|
2020-05-22 02:40:26 +00:00
|
|
|
const polymodel *po = &Polygon_models[model_num];
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
//check if should use simple model
|
|
|
|
if (po->simpler_model ) //must have a simpler model
|
|
|
|
if (flags==0) //can't switch if this is debris
|
|
|
|
//alt textures might not match, but in the one case we're using this
|
|
|
|
//for on 11/14/94, they do match. So we leave it in.
|
|
|
|
{
|
|
|
|
int cnt=1;
|
2014-10-26 22:01:04 +00:00
|
|
|
const auto depth = g3_calc_point_depth(pos); //gets 3d depth
|
2006-03-20 17:12:09 +00:00
|
|
|
while (po->simpler_model && depth > cnt++ * Simple_model_threshhold_scale * po->rad)
|
|
|
|
po = &Polygon_models[po->simpler_model-1];
|
|
|
|
}
|
|
|
|
|
2020-05-22 02:40:26 +00:00
|
|
|
std::array<grs_bitmap *, MAX_POLYOBJ_TEXTURES> texture_list;
|
2020-05-22 02:40:26 +00:00
|
|
|
{
|
|
|
|
const unsigned n_textures = po->n_textures;
|
|
|
|
std::array<bitmap_index, MAX_POLYOBJ_TEXTURES> texture_list_index;
|
|
|
|
auto &&tlir = partial_range(texture_list_index, n_textures);
|
|
|
|
if (alt_textures)
|
|
|
|
{
|
|
|
|
for (auto &&[at, tli] : zip(unchecked_partial_range(static_cast<const bitmap_index *>(alt_textures), n_textures), tlir))
|
|
|
|
tli = at;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
2020-05-22 02:40:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const unsigned first_texture = po->first_texture;
|
|
|
|
for (auto &&[obp, tli] : zip(partial_range(ObjBitmapPtrs, first_texture, first_texture + n_textures), tlir))
|
|
|
|
tli = ObjBitmaps[obp];
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the textures for this object are paged in...
|
2020-05-22 02:40:26 +00:00
|
|
|
for (auto &&[tli, tl] : zip(tlir, partial_range(texture_list, n_textures)))
|
|
|
|
{
|
|
|
|
tl = &GameBitmaps[tli.index];
|
|
|
|
PIGGY_PAGE_IN(tli);
|
|
|
|
}
|
|
|
|
}
|
2006-03-20 17:12:09 +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.
|
|
|
|
|
2017-08-26 19:47:51 +00:00
|
|
|
g3_start_instance_matrix(pos, orient);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-02-05 03:03:50 +00:00
|
|
|
polygon_model_points robot_points;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (flags == 0) //draw entire object
|
|
|
|
|
2017-02-19 19:33:43 +00:00
|
|
|
g3_draw_polygon_model(&texture_list[0], robot_points, canvas, anim_angles, light, glow_values, po->model_data.get());
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
else {
|
2014-09-26 02:42:10 +00:00
|
|
|
for (int i=0;flags;flags>>=1,i++)
|
2006-03-20 17:12:09 +00:00
|
|
|
if (flags & 1) {
|
|
|
|
Assert(i < po->n_models);
|
|
|
|
|
|
|
|
//if submodel, rotate around its center point, not pivot point
|
|
|
|
|
2017-08-26 19:47:51 +00:00
|
|
|
g3_start_instance_matrix();
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2017-02-19 19:33:43 +00:00
|
|
|
g3_draw_polygon_model(&texture_list[0], robot_points, canvas, anim_angles, light, glow_values, &po->model_data[po->submodel_ptrs[i]]);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
g3_done_instance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g3_done_instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_polygon_models()
|
|
|
|
{
|
2020-08-24 01:31:28 +00:00
|
|
|
for (auto &i : partial_range(LevelSharedPolygonModelState.Polygon_models, LevelSharedPolygonModelState.N_polygon_models))
|
2017-02-19 19:33:44 +00:00
|
|
|
free_model(i);
|
2019-12-18 11:39:07 +00:00
|
|
|
#if defined(DXX_BUILD_DESCENT_II)
|
|
|
|
Exit_models_loaded = false;
|
|
|
|
#endif
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace dcx {
|
|
|
|
|
2014-11-02 03:43:17 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <fix vms_vector::*p>
|
|
|
|
static void update_bounds(vms_vector &minv, vms_vector &maxv, const vms_vector &vp)
|
|
|
|
{
|
2014-11-04 03:21:23 +00:00
|
|
|
auto &mx = maxv.*p;
|
|
|
|
assign_max(mx, vp.*p);
|
|
|
|
auto &mn = minv.*p;
|
|
|
|
assign_min(mn, vp.*p);
|
2014-11-02 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void assign_minmax(vms_vector &minv, vms_vector &maxv, const vms_vector &v)
|
|
|
|
{
|
|
|
|
update_bounds<&vms_vector::x>(minv, maxv, v);
|
|
|
|
update_bounds<&vms_vector::y>(minv, maxv, v);
|
|
|
|
update_bounds<&vms_vector::z>(minv, maxv, v);
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:00:14 +00:00
|
|
|
static void polyobj_find_min_max(polymodel *pm)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-11-02 03:43:17 +00:00
|
|
|
auto &big_mn = pm->mins;
|
|
|
|
auto &big_mx = pm->maxs;
|
2014-09-26 02:42:10 +00:00
|
|
|
for (int m=0;m<pm->n_models;m++) {
|
2014-11-02 03:43:17 +00:00
|
|
|
auto &mn = pm->submodel_mins[m];
|
|
|
|
auto &mx = pm->submodel_maxs[m];
|
|
|
|
const auto &ofs = pm->submodel_offsets[m];
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-06-25 23:21:36 +00:00
|
|
|
auto data = reinterpret_cast<const uint16_t *>(&pm->model_data[pm->submodel_ptrs[m]]);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-06-25 23:21:36 +00:00
|
|
|
const auto type = *data++;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
Assert(type == 7 || type == 1);
|
|
|
|
|
2016-06-25 23:21:36 +00:00
|
|
|
const uint16_t nverts = *data++ - 1;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (type==7)
|
|
|
|
data+=2; //skip start & pad
|
|
|
|
|
2014-11-02 03:43:17 +00:00
|
|
|
auto vp = reinterpret_cast<const vms_vector *>(data);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-11-02 03:43:17 +00:00
|
|
|
mn = mx = *vp++;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (m==0)
|
2014-11-02 03:43:17 +00:00
|
|
|
big_mn = big_mx = mn;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2016-06-25 23:21:36 +00:00
|
|
|
range_for (auto &v, unchecked_partial_range(vp, nverts))
|
|
|
|
{
|
|
|
|
assign_minmax(mn, mx, v);
|
|
|
|
assign_minmax(big_mn, big_mx, vm_vec_add(v, ofs));
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 01:31:28 +00:00
|
|
|
void init_polygon_models(d_level_shared_polygon_model_state &LevelSharedPolygonModelState)
|
|
|
|
{
|
|
|
|
LevelSharedPolygonModelState.N_polygon_models = 0;
|
|
|
|
}
|
|
|
|
|
2016-06-25 23:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace dsx {
|
|
|
|
|
2020-05-02 21:18:42 +00:00
|
|
|
std::array<char[FILENAME_LEN], MAX_POLYGON_MODELS> Pof_names;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
//returns the number of this model
|
2013-06-08 22:24:17 +00:00
|
|
|
int load_polygon_model(const char *filename,int n_textures,int first_texture,robot_info *r)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
Assert(n_textures < MAX_POLYOBJ_TEXTURES);
|
|
|
|
|
|
|
|
Assert(strlen(filename) <= 12);
|
2020-08-24 01:31:28 +00:00
|
|
|
const auto n_models = LevelSharedPolygonModelState.N_polygon_models;
|
2015-02-28 22:34:07 +00:00
|
|
|
strcpy(Pof_names[n_models], filename);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2018-12-30 00:43:59 +00:00
|
|
|
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
|
2015-02-28 22:34:07 +00:00
|
|
|
auto &model = Polygon_models[n_models];
|
|
|
|
read_model_file(&model, filename, r);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-02-28 22:34:07 +00:00
|
|
|
polyobj_find_min_max(&model);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2019-03-06 05:03:48 +00:00
|
|
|
const auto highest_texture_num = g3_init_polygon_model(model.model_data.get(), model.model_data_size);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
if (highest_texture_num+1 != n_textures)
|
|
|
|
Error("Model <%s> references %d textures but specifies %d.",filename,highest_texture_num+1,n_textures);
|
|
|
|
|
2015-02-28 22:34:07 +00:00
|
|
|
model.n_textures = n_textures;
|
|
|
|
model.first_texture = first_texture;
|
|
|
|
model.simpler_model = 0;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2020-08-24 01:31:28 +00:00
|
|
|
return LevelSharedPolygonModelState.N_polygon_models++;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:21:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
//compare against this size when figuring how far to place eye for picture
|
|
|
|
#define BASE_MODEL_SIZE 0x28000
|
|
|
|
|
|
|
|
#define DEFAULT_VIEW_DIST 0x60000
|
|
|
|
|
|
|
|
//draws the given model in the current canvas. The distance is set to
|
|
|
|
//more-or-less fill the canvas. Note that this routine actually renders
|
|
|
|
//into an off-screen canvas that it creates, then copies to the current
|
|
|
|
//canvas.
|
2017-04-30 16:25:16 +00:00
|
|
|
void draw_model_picture(grs_canvas &canvas, const uint_fast32_t mn, const vms_angvec &orient_angles)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2011-04-07 20:32:51 +00:00
|
|
|
g3s_lrgb lrgb = { f1_0, f1_0, f1_0 };
|
2007-08-08 12:38:13 +00:00
|
|
|
|
2017-02-19 19:33:44 +00:00
|
|
|
gr_clear_canvas(canvas, BM_XRGB(0,0,0));
|
|
|
|
g3_start_frame(canvas);
|
2015-08-13 03:15:53 +00:00
|
|
|
vms_vector temp_pos{};
|
2015-02-05 03:03:51 +00:00
|
|
|
g3_set_view_matrix(temp_pos,vmd_identity_matrix,0x9000);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2018-12-30 00:43:59 +00:00
|
|
|
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
|
2006-03-20 17:12:09 +00:00
|
|
|
if (Polygon_models[mn].rad != 0)
|
|
|
|
temp_pos.z = fixmuldiv(DEFAULT_VIEW_DIST,Polygon_models[mn].rad,BASE_MODEL_SIZE);
|
|
|
|
else
|
|
|
|
temp_pos.z = DEFAULT_VIEW_DIST;
|
|
|
|
|
2017-04-30 16:25:16 +00:00
|
|
|
const auto &&temp_orient = vm_angles_2_matrix(orient_angles);
|
2017-08-11 23:43:54 +00:00
|
|
|
draw_polygon_model(canvas, temp_pos, temp_orient, nullptr, mn, 0, lrgb, nullptr, nullptr);
|
2006-03-20 17:12:09 +00:00
|
|
|
g3_end_frame();
|
|
|
|
}
|
|
|
|
|
2015-12-13 18:00:49 +00:00
|
|
|
namespace dcx {
|
2015-12-13 18:00:48 +00:00
|
|
|
|
2014-07-24 03:25:00 +00:00
|
|
|
DEFINE_SERIAL_VMS_VECTOR_TO_MESSAGE();
|
|
|
|
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));
|
|
|
|
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);
|
|
|
|
|
2006-03-20 17:12:09 +00:00
|
|
|
/*
|
2016-01-09 16:38:14 +00:00
|
|
|
* reads a polymodel structure from a PHYSFS_File
|
2006-03-20 17:12:09 +00:00
|
|
|
*/
|
2016-01-09 16:38:14 +00:00
|
|
|
void polymodel_read(polymodel *pm, PHYSFS_File *fp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-07-24 03:25:00 +00:00
|
|
|
pm->model_data.reset();
|
|
|
|
PHYSFSX_serialize_read(fp, *pm);
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2015-12-13 18:00:48 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 03:36:31 +00:00
|
|
|
#if 0
|
2016-01-09 16:38:14 +00:00
|
|
|
void polymodel_write(PHYSFS_File *fp, const polymodel &pm)
|
2014-07-24 03:25:00 +00:00
|
|
|
{
|
|
|
|
PHYSFSX_serialize_write(fp, pm);
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
2015-12-04 03:36:31 +00:00
|
|
|
#endif
|
2006-03-20 17:12:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* routine which allocates, reads, and inits a polymodel's model_data
|
|
|
|
*/
|
2016-08-25 04:05:32 +00:00
|
|
|
namespace dsx {
|
2016-01-09 16:38:14 +00:00
|
|
|
void polygon_model_data_read(polymodel *pm, PHYSFS_File *fp)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2020-07-05 23:34:33 +00:00
|
|
|
auto model_data_size = pm->model_data_size;
|
2020-05-02 21:18:42 +00:00
|
|
|
pm->model_data = std::make_unique<uint8_t[]>(model_data_size);
|
2019-03-06 05:03:48 +00:00
|
|
|
PHYSFS_read(fp, pm->model_data, sizeof(uint8_t), model_data_size);
|
2016-10-09 17:35:03 +00:00
|
|
|
#if DXX_WORDS_NEED_ALIGNMENT
|
2020-07-05 23:34:33 +00:00
|
|
|
/* Aligning model data changes pm->model_data_size. Reload it
|
|
|
|
* afterward.
|
|
|
|
*/
|
2006-03-20 17:12:09 +00:00
|
|
|
align_polygon_model_data(pm);
|
2020-07-05 23:34:33 +00:00
|
|
|
model_data_size = pm->model_data_size;
|
2006-03-20 17:12:09 +00:00
|
|
|
#endif
|
2020-07-05 23:34:33 +00:00
|
|
|
if constexpr (words_bigendian)
|
|
|
|
swap_polygon_model_data(pm->model_data.get());
|
2019-03-20 03:57:15 +00:00
|
|
|
#if defined(DXX_BUILD_DESCENT_I)
|
2020-07-07 04:09:44 +00:00
|
|
|
g3_validate_polygon_model(pm->model_data.get(), model_data_size);
|
2019-03-20 03:57:15 +00:00
|
|
|
#elif defined(DXX_BUILD_DESCENT_II)
|
2020-07-07 04:09:44 +00:00
|
|
|
g3_init_polygon_model(pm->model_data.get(), model_data_size);
|
2019-03-20 03:57:15 +00:00
|
|
|
#endif
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
2016-08-25 04:05:32 +00:00
|
|
|
}
|