From 89a8a04334e41869a575e4539d0540385210738a Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Thu, 7 Mar 2024 18:51:14 +0200 Subject: [PATCH] soft: reuse R_LerpVerts code --- src/client/refresh/files/mesh.c | 6 +- src/client/refresh/files/models_md5.c | 23 +++- src/client/refresh/gl1/gl1_mesh.c | 6 +- src/client/refresh/gl3/gl3_mesh.c | 12 +- src/client/refresh/gl4/gl4_mesh.c | 12 +- src/client/refresh/ref_shared.h | 2 +- src/client/refresh/soft/sw_alias.c | 173 +++++++++++++------------- src/client/refresh/soft/sw_main.c | 2 + src/client/refresh/vk/vk_mesh.c | 6 +- 9 files changed, 127 insertions(+), 115 deletions(-) diff --git a/src/client/refresh/files/mesh.c b/src/client/refresh/files/mesh.c index 3ba46d8f..2546d0e4 100644 --- a/src/client/refresh/files/mesh.c +++ b/src/client/refresh/files/mesh.c @@ -109,8 +109,8 @@ R_CompressNormalMDL(const float *normal) void R_LerpVerts(qboolean powerUpEffect, int nverts, const dxtrivertx_t *v, const dxtrivertx_t *ov, - const dxtrivertx_t *verts, float *lerp, - const float move[3], const float frontv[3], const float backv[3]) + float *lerp, const float move[3], + const float frontv[3], const float backv[3]) { int i; @@ -118,7 +118,7 @@ R_LerpVerts(qboolean powerUpEffect, int nverts, { for (i = 0; i < nverts; i++, v++, ov++, lerp += 4) { - const float *normal = r_avertexnormals[verts[i].lightnormalindex]; + const float *normal = r_avertexnormals[v->lightnormalindex]; lerp[0] = move[0] + ov->v[0] * backv[0] + v->v[0] * frontv[0] + normal[0] * POWERSUIT_SCALE; diff --git a/src/client/refresh/files/models_md5.c b/src/client/refresh/files/models_md5.c index 30d6aa48..394346b8 100644 --- a/src/client/refresh/files/models_md5.c +++ b/src/client/refresh/files/models_md5.c @@ -402,7 +402,7 @@ ReadMD5Anim(md5_model_t *anim, const char *buffer, size_t size) md5_joint_info_t *jointInfos = NULL; md5_baseframe_joint_t *baseFrame = NULL; float *animFrameData = NULL; - int numAnimatedComponents; + int numAnimatedComponents = 0; /* buffer has not always had final zero */ safe_buffer = malloc(size + 1); @@ -612,13 +612,26 @@ ReadMD5Anim(md5_model_t *anim, const char *buffer, size_t size) animFrameData[i] = (float)strtod(token, (char **)NULL); } - if (frame_index < anim->num_frames) + if (frame_index < 0 || frame_index >= anim->num_frames) { - /* Build frame skeleton from the collected data */ - BuildFrameSkeleton(jointInfos, baseFrame, animFrameData, - anim->skelFrames[frame_index].skelJoints, anim->num_joints); + R_Printf(PRINT_ALL, "Error: unknown frame number\n"); + /* broken file */ + FreeModelMd5Frames(anim); + break; } + if (!jointInfos || !baseFrame || !animFrameData) + { + R_Printf(PRINT_ALL, "Error: unknown size of frame\n"); + /* broken file */ + FreeModelMd5Frames(anim); + break; + } + + /* Build frame skeleton from the collected data */ + BuildFrameSkeleton(jointInfos, baseFrame, animFrameData, + anim->skelFrames[frame_index].skelJoints, anim->num_joints); + token = COM_Parse(&curr_buff); if (strcmp(token, "}")) { diff --git a/src/client/refresh/gl1/gl1_mesh.c b/src/client/refresh/gl1/gl1_mesh.c index a4e55dae..b671c8d1 100644 --- a/src/client/refresh/gl1/gl1_mesh.c +++ b/src/client/refresh/gl1/gl1_mesh.c @@ -177,7 +177,7 @@ R_DrawAliasFrameLerp(entity_t *currententity, dmdx_t *paliashdr, float backlerp, vec4_t *s_lerped) { daliasxframe_t *frame, *oldframe; - dxtrivertx_t *v, *ov, *verts; + dxtrivertx_t *ov, *verts; int *order; float frontlerp; float alpha; @@ -193,7 +193,7 @@ R_DrawAliasFrameLerp(entity_t *currententity, dmdx_t *paliashdr, float backlerp, frame = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + currententity->frame * paliashdr->framesize); - verts = v = frame->verts; + verts = frame->verts; oldframe = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + currententity->oldframe * paliashdr->framesize); @@ -237,7 +237,7 @@ R_DrawAliasFrameLerp(entity_t *currententity, dmdx_t *paliashdr, float backlerp, lerp = s_lerped[0]; - R_LerpVerts(colorOnly, paliashdr->num_xyz, v, ov, verts, lerp, move, frontv, backv); + R_LerpVerts(colorOnly, paliashdr->num_xyz, verts, ov, lerp, move, frontv, backv); num_mesh_nodes = paliashdr->num_meshes; mesh_nodes = (dmdxmesh_t *)((char*)paliashdr + paliashdr->ofs_meshes); diff --git a/src/client/refresh/gl3/gl3_mesh.c b/src/client/refresh/gl3/gl3_mesh.c index 5a6fa9d3..089b4a66 100644 --- a/src/client/refresh/gl3/gl3_mesh.c +++ b/src/client/refresh/gl3/gl3_mesh.c @@ -216,7 +216,7 @@ static void DrawAliasFrameLerp(dmdx_t *paliashdr, entity_t* entity, vec3_t shadelight) { daliasxframe_t *frame, *oldframe; - dxtrivertx_t *v, *ov, *verts; + dxtrivertx_t *ov, *verts; int *order; float alpha; vec3_t move, delta, vectors[3]; @@ -240,7 +240,7 @@ DrawAliasFrameLerp(dmdx_t *paliashdr, entity_t* entity, vec3_t shadelight) frame = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->frame * paliashdr->framesize); - verts = v = frame->verts; + verts = frame->verts; oldframe = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->oldframe * paliashdr->framesize); @@ -295,7 +295,7 @@ DrawAliasFrameLerp(dmdx_t *paliashdr, entity_t* entity, vec3_t shadelight) lerp = s_lerped[0]; - R_LerpVerts(colorOnly, paliashdr->num_xyz, v, ov, verts, lerp, move, frontv, backv); + R_LerpVerts(colorOnly, paliashdr->num_xyz, verts, ov, lerp, move, frontv, backv); YQ2_STATIC_ASSERT(sizeof(gl3_alias_vtx_t) == 9*sizeof(GLfloat), "invalid gl3_alias_vtx_t size"); @@ -441,7 +441,7 @@ DrawAliasShadow(gl3_shadowinfo_t* shadowInfo) // all in this scope is to set s_lerped { daliasxframe_t *frame, *oldframe; - dxtrivertx_t *v, *ov, *verts; + dxtrivertx_t *ov, *verts; float backlerp = entity->backlerp; float frontlerp = 1.0f - backlerp; vec3_t move, delta, vectors[3]; @@ -449,7 +449,7 @@ DrawAliasShadow(gl3_shadowinfo_t* shadowInfo) frame = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->frame * paliashdr->framesize); - verts = v = frame->verts; + verts = frame->verts; oldframe = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->oldframe * paliashdr->framesize); @@ -475,7 +475,7 @@ DrawAliasShadow(gl3_shadowinfo_t* shadowInfo) // false: don't extrude vertices for powerup - this means the powerup shell // is not seen in the shadow, only the underlying model.. - R_LerpVerts(false, paliashdr->num_xyz, v, ov, verts, s_lerped[0], move, frontv, backv); + R_LerpVerts(false, paliashdr->num_xyz, verts, ov, s_lerped[0], move, frontv, backv); } lheight = entity->origin[2] - shadowInfo->lightspot[2]; diff --git a/src/client/refresh/gl4/gl4_mesh.c b/src/client/refresh/gl4/gl4_mesh.c index f9a1a5bf..4148827c 100644 --- a/src/client/refresh/gl4/gl4_mesh.c +++ b/src/client/refresh/gl4/gl4_mesh.c @@ -71,7 +71,7 @@ static void DrawAliasFrameLerp(dmdx_t *paliashdr, entity_t* entity, vec3_t shadelight) { daliasxframe_t *frame, *oldframe; - dxtrivertx_t *v, *ov, *verts; + dxtrivertx_t *ov, *verts; int *order; float alpha; vec3_t move, delta, vectors[3]; @@ -94,7 +94,7 @@ DrawAliasFrameLerp(dmdx_t *paliashdr, entity_t* entity, vec3_t shadelight) frame = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->frame * paliashdr->framesize); - verts = v = frame->verts; + verts = frame->verts; oldframe = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->oldframe * paliashdr->framesize); @@ -149,7 +149,7 @@ DrawAliasFrameLerp(dmdx_t *paliashdr, entity_t* entity, vec3_t shadelight) lerp = s_lerped[0]; - R_LerpVerts(colorOnly, paliashdr->num_xyz, v, ov, verts, lerp, move, frontv, backv); + R_LerpVerts(colorOnly, paliashdr->num_xyz, verts, ov, lerp, move, frontv, backv); YQ2_STATIC_ASSERT(sizeof(gl4_alias_vtx_t) == 9*sizeof(GLfloat), "invalid gl4_alias_vtx_t size"); @@ -313,7 +313,7 @@ DrawAliasShadow(gl4_shadowinfo_t* shadowInfo) // all in this scope is to set s_lerped { daliasxframe_t *frame, *oldframe; - dxtrivertx_t *v, *ov, *verts; + dxtrivertx_t *ov, *verts; float backlerp = entity->backlerp; float frontlerp = 1.0f - backlerp; vec3_t move, delta, vectors[3]; @@ -322,7 +322,7 @@ DrawAliasShadow(gl4_shadowinfo_t* shadowInfo) frame = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->frame * paliashdr->framesize); - verts = v = frame->verts; + verts = frame->verts; oldframe = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + entity->oldframe * paliashdr->framesize); @@ -348,7 +348,7 @@ DrawAliasShadow(gl4_shadowinfo_t* shadowInfo) // false: don't extrude vertices for powerup - this means the powerup shell // is not seen in the shadow, only the underlying model.. - R_LerpVerts(false, paliashdr->num_xyz, v, ov, verts, s_lerped[0], move, frontv, backv); + R_LerpVerts(false, paliashdr->num_xyz, verts, ov, s_lerped[0], move, frontv, backv); } lheight = entity->origin[2] - shadowInfo->lightspot[2]; diff --git a/src/client/refresh/ref_shared.h b/src/client/refresh/ref_shared.h index d1f8d9df..9312fa21 100644 --- a/src/client/refresh/ref_shared.h +++ b/src/client/refresh/ref_shared.h @@ -385,7 +385,7 @@ extern qboolean R_CullAliasMeshModel(dmdx_t *paliashdr, cplane_t *frustum, int frame, int oldframe, vec3_t e_angles, vec3_t e_origin, vec3_t bbox[8]); extern void R_LerpVerts(qboolean powerUpEffect, int nverts, const dxtrivertx_t *v, const dxtrivertx_t *ov, - const dxtrivertx_t *verts, float *lerp, const float move[3], + float *lerp, const float move[3], const float frontv[3], const float backv[3]); extern byte R_CompressNormalMDL(const float *normal); extern vec4_t *R_VertBufferRealloc(int num); diff --git a/src/client/refresh/soft/sw_alias.c b/src/client/refresh/soft/sw_alias.c index 3d99450d..39431bab 100644 --- a/src/client/refresh/soft/sw_alias.c +++ b/src/client/refresh/soft/sw_alias.c @@ -61,8 +61,6 @@ static const float r_avertexnormals[NUMVERTEXNORMALS][3] = { static void R_AliasTransformVector(const vec3_t in, vec3_t out, const float xf[3][4]); -static void R_AliasTransformFinalVerts(const entity_t *currententity, int numpoints, finalvert_t *fv, dxtrivertx_t *oldv, dxtrivertx_t *newv ); - void R_AliasProjectAndClipTestFinalVert(finalvert_t *fv); @@ -233,6 +231,81 @@ R_AliasTransformVector(const vec3_t in, vec3_t out, const float xf[3][4] ) out[2] = DotProduct(in, xf[2]) + xf[2][3]; } +/* +================ +R_AliasTransformFinalVerts + +================ +*/ +static void +R_AliasTransformFinalVerts(const entity_t *currententity, int numpoints, + finalvert_t *fv, dxtrivertx_t *oldv, dxtrivertx_t *newv, vec4_t *s_lerped) +{ + qboolean colorOnly; + float *lerp; + int i; + + colorOnly = 0 != (currententity->flags & + (RF_SHELL_RED | RF_SHELL_GREEN | RF_SHELL_BLUE | RF_SHELL_DOUBLE | + RF_SHELL_HALF_DAM)); + + lerp = s_lerped[0]; + + R_LerpVerts(colorOnly, numpoints, newv, oldv, lerp, r_lerp_move, + r_lerp_frontv, r_lerp_backv); + + for ( i = 0; i < numpoints; i++, fv++, newv++, lerp += 4) + { + float lightcos; + const float *plightnormal; + + plightnormal = r_avertexnormals[newv->lightnormalindex]; + + fv->xyz[0] = DotProduct(lerp, aliastransform[0]) + aliastransform[0][3]; + fv->xyz[1] = DotProduct(lerp, aliastransform[1]) + aliastransform[1][3]; + fv->xyz[2] = DotProduct(lerp, aliastransform[2]) + aliastransform[2][3]; + + fv->flags = 0; + + // lighting + lightcos = DotProduct (plightnormal, r_plightvec); + + if (lightcos < 0) + { + int j; + + for(j=0; j<3; j++) + { + int temp; + + temp = r_ambientlight[j]; + + temp += (r_shadelight[j] * lightcos); + + // clamp; because we limited the minimum ambient and shading light, we + // don't have to clamp low light, just bright + if (temp < 0) + temp = 0; + + fv->cv.l[j] = temp; + } + } + else + { + memcpy(fv->cv.l, r_ambientlight, sizeof(light3_t)); + } + + if ( fv->xyz[2] < ALIAS_Z_CLIP_PLANE ) + { + fv->flags |= ALIAS_Z_CLIP; + } + else + { + R_AliasProjectAndClipTestFinalVert( fv ); + } + } +} + /* ================ @@ -243,12 +316,13 @@ General clipped case */ static void -R_AliasPreparePoints (const entity_t *currententity, finalvert_t *verts, const finalvert_t *verts_max) +R_AliasPreparePoints(const entity_t *currententity, finalvert_t *verts, const finalvert_t *verts_max) { int i; dstvert_t *pstverts; dtriangle_t *ptri; finalvert_t *pfv[3]; + vec4_t *s_lerped; if ((verts + s_pmdl->num_xyz) >= verts_max) { @@ -256,12 +330,15 @@ R_AliasPreparePoints (const entity_t *currententity, finalvert_t *verts, const f return; } + /* buffer for scalled vert from frame */ + s_lerped = R_VertBufferRealloc(s_pmdl->num_xyz); + R_AliasTransformFinalVerts(currententity, - s_pmdl->num_xyz, - verts, // destination for transformed verts - r_lastframe->verts, // verts from the last frame - r_thisframe->verts // verts from this frame - ); + s_pmdl->num_xyz, + verts, /* destination for transformed verts */ + r_lastframe->verts, /* verts from the last frame */ + r_thisframe->verts, /* verts from this frame */ + s_lerped); // clip and draw all triangles // @@ -409,86 +486,6 @@ R_AliasSetUpTransform(const entity_t *currententity) aliasoldworldtransform[2][3] = currententity->oldorigin[2]; } - -/* -================ -R_AliasTransformFinalVerts - -TODO: Combine with R_LerpVerts -================ -*/ -static void -R_AliasTransformFinalVerts(const entity_t *currententity, int numpoints, finalvert_t *fv, dxtrivertx_t *oldv, dxtrivertx_t *newv ) -{ - int i; - qboolean colorOnly = 0 != (currententity->flags & - (RF_SHELL_RED | RF_SHELL_GREEN | RF_SHELL_BLUE | RF_SHELL_DOUBLE | - RF_SHELL_HALF_DAM)); - - for ( i = 0; i < numpoints; i++, fv++, oldv++, newv++ ) - { - float lightcos; - const float *plightnormal; - vec3_t lerped_vert; - - lerped_vert[0] = r_lerp_move[0] + oldv->v[0] * r_lerp_backv[0] + newv->v[0] * r_lerp_frontv[0]; - lerped_vert[1] = r_lerp_move[1] + oldv->v[1] * r_lerp_backv[1] + newv->v[1] * r_lerp_frontv[1]; - lerped_vert[2] = r_lerp_move[2] + oldv->v[2] * r_lerp_backv[2] + newv->v[2] * r_lerp_frontv[2]; - - plightnormal = r_avertexnormals[newv->lightnormalindex]; - - // added double damage shell - if ( colorOnly ) - { - lerped_vert[0] += plightnormal[0] * POWERSUIT_SCALE; - lerped_vert[1] += plightnormal[1] * POWERSUIT_SCALE; - lerped_vert[2] += plightnormal[2] * POWERSUIT_SCALE; - } - - fv->xyz[0] = DotProduct(lerped_vert, aliastransform[0]) + aliastransform[0][3]; - fv->xyz[1] = DotProduct(lerped_vert, aliastransform[1]) + aliastransform[1][3]; - fv->xyz[2] = DotProduct(lerped_vert, aliastransform[2]) + aliastransform[2][3]; - - fv->flags = 0; - - // lighting - lightcos = DotProduct (plightnormal, r_plightvec); - - if (lightcos < 0) - { - int j; - - for(j=0; j<3; j++) - { - int temp; - - temp = r_ambientlight[j]; - - temp += (r_shadelight[j] * lightcos); - - // clamp; because we limited the minimum ambient and shading light, we - // don't have to clamp low light, just bright - if (temp < 0) - temp = 0; - - fv->cv.l[j] = temp; - } - } - else - memcpy(fv->cv.l, r_ambientlight, sizeof(light3_t)); - - if ( fv->xyz[2] < ALIAS_Z_CLIP_PLANE ) - { - fv->flags |= ALIAS_Z_CLIP; - } - else - { - R_AliasProjectAndClipTestFinalVert( fv ); - } - } - -} - /* ================ R_AliasProjectAndClipTestFinalVert diff --git a/src/client/refresh/soft/sw_main.c b/src/client/refresh/soft/sw_main.c index 90b0e376..bb1619b2 100644 --- a/src/client/refresh/soft/sw_main.c +++ b/src/client/refresh/soft/sw_main.c @@ -456,6 +456,7 @@ static qboolean RE_Init(void) { R_RegisterVariables (); + R_VertBufferInit(); R_InitImages (); Mod_Init (); Draw_InitLocal (); @@ -522,6 +523,7 @@ RE_Shutdown (void) R_UnRegister (); Mod_FreeAll (); R_ShutdownImages (); + R_VertBufferFree(); RE_ShutdownContext(); } diff --git a/src/client/refresh/vk/vk_mesh.c b/src/client/refresh/vk/vk_mesh.c index 00cb4c35..f46fd57f 100644 --- a/src/client/refresh/vk/vk_mesh.c +++ b/src/client/refresh/vk/vk_mesh.c @@ -403,7 +403,7 @@ Vk_DrawAliasFrameLerp(entity_t *currententity, dmdx_t *paliashdr, float backlerp float *modelMatrix, int leftHandOffset, int translucentIdx, vec4_t *s_lerped) { daliasxframe_t *frame, *oldframe; - dxtrivertx_t *v, *ov, *verts; + dxtrivertx_t *ov, *verts; int *order; float frontlerp; float alpha; @@ -418,7 +418,7 @@ Vk_DrawAliasFrameLerp(entity_t *currententity, dmdx_t *paliashdr, float backlerp frame = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + currententity->frame * paliashdr->framesize); - verts = v = frame->verts; + verts = frame->verts; oldframe = (daliasxframe_t *)((byte *)paliashdr + paliashdr->ofs_frames + currententity->oldframe * paliashdr->framesize); @@ -455,7 +455,7 @@ Vk_DrawAliasFrameLerp(entity_t *currententity, dmdx_t *paliashdr, float backlerp backv[i] = backlerp * oldframe->scale[i]; } - R_LerpVerts(colorOnly, paliashdr->num_xyz, v, ov, verts, (float*)s_lerped, + R_LerpVerts(colorOnly, paliashdr->num_xyz, verts, ov, (float*)s_lerped, move, frontv, backv); num_mesh_nodes = paliashdr->num_meshes;