models: prepare PrepareFrameVertex

This commit is contained in:
Denis Pauk 2024-02-03 23:35:51 +02:00
parent 8cc3d90789
commit ed5046bcf5
3 changed files with 79 additions and 38 deletions

View file

@ -25,7 +25,7 @@
* ======================================================================= * =======================================================================
*/ */
#include "../ref_shared.h" #include "models.h"
/* /*
================= =================
@ -632,7 +632,7 @@ Mod_LoadModel_MD2(const char *mod_name, const void *buffer, int modfilelen,
return NULL; return NULL;
} }
for (i=0 ; i<sizeof(dmdl_t)/sizeof(int) ; i++) for (i = 0; i < sizeof(pinmodel) / sizeof(int); i++)
{ {
((int *)&pinmodel)[i] = LittleLong(((int *)buffer)[i]); ((int *)&pinmodel)[i] = LittleLong(((int *)buffer)[i]);
} }
@ -801,7 +801,7 @@ Mod_LoadSkinList_MD2(const char *mod_name, const void *buffer, int modfilelen,
__func__, mod_name, modfilelen, sizeof(pinmodel)); __func__, mod_name, modfilelen, sizeof(pinmodel));
} }
for (i=0 ; i<sizeof(dmdl_t)/sizeof(int) ; i++) for (i = 0; i < sizeof(pinmodel) / sizeof(int); i++)
{ {
((int *)&pinmodel)[i] = LittleLong(((int *)buffer)[i]); ((int *)&pinmodel)[i] = LittleLong(((int *)buffer)[i]);
} }
@ -1431,6 +1431,11 @@ Mod_LoadModel(const char *mod_name, const void *buffer, int modfilelen,
{ {
void *extradata = NULL; void *extradata = NULL;
if (!buffer || modfilelen < sizeof(unsigned))
{
return NULL;
}
switch (LittleLong(*(unsigned *)buffer)) switch (LittleLong(*(unsigned *)buffer))
{ {
case DKMHEADER: case DKMHEADER:

View file

@ -0,0 +1,39 @@
/*
* Copyright (C) 1997-2001 Id Software, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* =======================================================================
*
* The shared model structures file format
*
* =======================================================================
*/
#ifndef SRC_CLIENT_REFRESH_FILES_MODELS_H_
#define SRC_CLIENT_REFRESH_FILES_MODELS_H_
#include "../ref_shared.h"
/* Unpacked vertex info model convert */
typedef struct dmdx_vert_s
{
vec3_t xyz;
vec3_t norm;
} dmdx_vert_t;
#endif /* SRC_CLIENT_REFRESH_FILES_MODELS_H_ */

View file

@ -25,7 +25,7 @@
* ======================================================================= * =======================================================================
*/ */
#include "../ref_shared.h" #include "models.h"
/* Joint */ /* Joint */
typedef struct md5_joint_s typedef struct md5_joint_s
@ -82,17 +82,11 @@ typedef struct md5_mesh_s
char shader[256]; char shader[256];
} md5_mesh_t; } md5_mesh_t;
typedef struct md5_fvert_s
{
vec3_t xyz;
vec3_t norm;
} md5_fvert_t;
typedef struct md5_frame_s typedef struct md5_frame_s
{ {
md5_bbox_t bbox; md5_bbox_t bbox;
md5_joint_t *skelJoints; md5_joint_t *skelJoints;
md5_fvert_t *vertexArray; dmdx_vert_t *vertexArray;
} md5_frame_t; } md5_frame_t;
/* MD5 model structure */ /* MD5 model structure */
@ -327,8 +321,8 @@ AllocateFrames(md5_model_t *anim)
/* Allocate memory for joints of each frame */ /* Allocate memory for joints of each frame */
anim->skelFrames[i].skelJoints = (md5_joint_t *) anim->skelFrames[i].skelJoints = (md5_joint_t *)
malloc(sizeof(md5_joint_t) * anim->num_joints); malloc(sizeof(md5_joint_t) * anim->num_joints);
anim->skelFrames[i].vertexArray = (md5_fvert_t *) anim->skelFrames[i].vertexArray = (dmdx_vert_t *)
malloc(sizeof(md5_fvert_t) * anim->num_verts); malloc(sizeof(dmdx_vert_t) * anim->num_verts);
memcpy(anim->skelFrames[i].skelJoints, anim->baseSkel, memcpy(anim->skelFrames[i].skelJoints, anim->baseSkel,
sizeof(md5_joint_t) * anim->num_joints); sizeof(md5_joint_t) * anim->num_joints);
} }
@ -902,7 +896,7 @@ PrepareMeshVertex(const md5_mesh_t *mesh, const md5_joint_t *skeleton,
} }
static void static void
PrepareFrameVertex(md5_frame_t *frame_in, int num_verts, daliasxframe_t *frame_out) PrepareFrameVertex(dmdx_vert_t *vertexArray, int num_verts, daliasxframe_t *frame_out)
{ {
int i; int i;
vec3_t mins, maxs; vec3_t mins, maxs;
@ -921,14 +915,14 @@ PrepareFrameVertex(md5_frame_t *frame_in, int num_verts, daliasxframe_t *frame_o
for (j = 0; j < 3; j++) for (j = 0; j < 3; j++)
{ {
if (mins[j] > frame_in->vertexArray[i].xyz[j]) if (mins[j] > vertexArray[i].xyz[j])
{ {
mins[j] = frame_in->vertexArray[i].xyz[j]; mins[j] = vertexArray[i].xyz[j];
} }
if (maxs[j] < frame_in->vertexArray[i].xyz[j]) if (maxs[j] < vertexArray[i].xyz[j])
{ {
maxs[j] = frame_in->vertexArray[i].xyz[j]; maxs[j] = vertexArray[i].xyz[j];
} }
} }
} }
@ -946,11 +940,11 @@ PrepareFrameVertex(md5_frame_t *frame_in, int num_verts, daliasxframe_t *frame_o
for (j = 0; j < 3; j++) for (j = 0; j < 3; j++)
{ {
frame_out->verts[i].v[j] = ( frame_out->verts[i].v[j] = (
frame_in->vertexArray[i].xyz[j] - frame_out->translate[j] vertexArray[i].xyz[j] - frame_out->translate[j]
) / frame_out->scale[j]; ) / frame_out->scale[j];
frame_out->verts[i].lightnormalindex = frame_out->verts[i].lightnormalindex =
R_CompressNormalMDL(frame_in->vertexArray[i].norm); R_CompressNormalMDL(vertexArray[i].norm);
} }
} }
} }
@ -993,7 +987,7 @@ MD5_ComputeNormals(md5_model_t *md5file)
for (j = 0; j < md5file->meshes[k].num_tris; ++j) for (j = 0; j < md5file->meshes[k].num_tris; ++j)
{ {
md5_fvert_t *v0, *v1, *v2; dmdx_vert_t *v0, *v1, *v2;
vec3_t d1, d2, norm; vec3_t d1, d2, norm;
v0 = frame_in->vertexArray + md5file->meshes[k].triangles[j].index[0] + vert_step; v0 = frame_in->vertexArray + md5file->meshes[k].triangles[j].index[0] + vert_step;
@ -1040,6 +1034,13 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen,
md5_model_t *md5file; md5_model_t *md5file;
void *extradata = NULL; void *extradata = NULL;
byte *startbuffer, *endbuffer; byte *startbuffer, *endbuffer;
int i, num_verts = 0, num_tris = 0, num_glcmds = 0;
int framesize, ofs_skins, ofs_frames, ofs_glcmds, ofs_meshes, ofs_tris, ofs_st, ofs_end;
dmdx_t *pheader = NULL;
int *pglcmds, *baseglcmds;
dmdxmesh_t *mesh_nodes;
dtriangle_t *tris;
dstvert_t *st;
startbuffer = (byte*) buffer; startbuffer = (byte*) buffer;
endbuffer = startbuffer + modfilelen; endbuffer = startbuffer + modfilelen;
@ -1082,8 +1083,6 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen,
sizeof(md5_joint_t) * md5file->num_joints); sizeof(md5_joint_t) * md5file->num_joints);
} }
int i, num_verts = 0, num_tris = 0, num_glcmds = 0;
for (i = 0; i < md5file->num_meshes; ++i) for (i = 0; i < md5file->num_meshes; ++i)
{ {
int j; int j;
@ -1108,16 +1107,14 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen,
MD5_ComputeNormals(md5file); MD5_ComputeNormals(md5file);
int framesize = sizeof(daliasxframe_t) + sizeof(dxtrivertx_t) * num_verts; framesize = sizeof(daliasxframe_t) + sizeof(dxtrivertx_t) * num_verts;
int ofs_skins = sizeof(dmdx_t); ofs_skins = sizeof(dmdx_t);
int ofs_frames = ofs_skins + md5file->num_skins * MAX_SKINNAME; ofs_frames = ofs_skins + md5file->num_skins * MAX_SKINNAME;
int ofs_glcmds = ofs_frames + framesize * md5file->num_frames; ofs_glcmds = ofs_frames + framesize * md5file->num_frames;
int ofs_meshes = ofs_glcmds + num_glcmds * sizeof(int); ofs_meshes = ofs_glcmds + num_glcmds * sizeof(int);
int ofs_tris = ofs_meshes + md5file->num_tris * sizeof(dtriangle_t); ofs_tris = ofs_meshes + md5file->num_meshes * sizeof(dmdxmesh_t);
int ofs_st = ofs_tris + md5file->num_tris * 3 * sizeof(dmdxmesh_t); ofs_st = ofs_tris + md5file->num_tris * 3 * sizeof(dtriangle_t);
int ofs_end = ofs_st + md5file->num_tris * 3 * sizeof(dstvert_t); ofs_end = ofs_st + md5file->num_tris * 3 * sizeof(dstvert_t);
dmdx_t *pheader = NULL;
*numskins = md5file->num_skins; *numskins = md5file->num_skins;
extradata = Hunk_Begin(ofs_end + Q_max(*numskins, MAX_MD2SKINS) * sizeof(struct image_s *)); extradata = Hunk_Begin(ofs_end + Q_max(*numskins, MAX_MD2SKINS) * sizeof(struct image_s *));
@ -1147,16 +1144,16 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen,
daliasxframe_t *frame = (daliasxframe_t *)( daliasxframe_t *frame = (daliasxframe_t *)(
(byte *)pheader + pheader->ofs_frames + i * pheader->framesize); (byte *)pheader + pheader->ofs_frames + i * pheader->framesize);
snprintf(frame->name, 15, "%d", i); snprintf(frame->name, 15, "%d", i);
PrepareFrameVertex(md5file->skelFrames + i, num_verts, frame); PrepareFrameVertex((md5file->skelFrames + i)->vertexArray,
num_verts, frame);
} }
num_tris = 0; num_tris = 0;
int *pglcmds, *baseglcmds;
pglcmds = baseglcmds = (int *)((byte *)pheader + pheader->ofs_glcmds); pglcmds = baseglcmds = (int *)((byte *)pheader + pheader->ofs_glcmds);
dmdxmesh_t *mesh_nodes = (dmdxmesh_t *)((byte *)pheader + pheader->ofs_meshes); mesh_nodes = (dmdxmesh_t *)((byte *)pheader + pheader->ofs_meshes);
dtriangle_t *tris = (dtriangle_t*)((byte *)pheader + pheader->ofs_tris); tris = (dtriangle_t*)((byte *)pheader + pheader->ofs_tris);
dstvert_t *st = (dstvert_t*)((byte *)pheader + pheader->ofs_st); st = (dstvert_t*)((byte *)pheader + pheader->ofs_st);
for(i = 0; i < pheader->num_st; i ++) for(i = 0; i < pheader->num_st; i ++)
{ {