From 6e7920993125d51373bafafc43414af1379eba9e Mon Sep 17 00:00:00 2001 From: Marisa Kirisame Date: Sat, 11 Jul 2020 12:59:14 +0200 Subject: [PATCH] Optimize UE1 vertex normal computation. --- src/common/models/model_ue1.h | 6 ++++-- src/common/models/models_ue1.cpp | 30 ++++++++++++++++++------------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/common/models/model_ue1.h b/src/common/models/model_ue1.h index 5696cbf43..b92713da4 100644 --- a/src/common/models/model_ue1.h +++ b/src/common/models/model_ue1.h @@ -13,7 +13,7 @@ public: PT_TwoSided = 1, // like normal, but don't cull backfaces PT_Translucent = 2, // additive blending PT_Masked = 3, // draw with alpha testing - PT_Modulated = 4, // overlay-like blending (rgb values below 128 darken, 128 is unchanged, and above 128 lighten) + PT_Modulated = 4, // modulated blending (src*dest*2) // types mask PT_Type = 7, // flags @@ -86,6 +86,8 @@ private: struct UE1Vertex { FVector3 Pos, Normal; + TArray P; // polys that reference this vertex, used in normal computation to save time + int nP; // count of those polys }; struct UE1Poly { @@ -103,7 +105,7 @@ private: int numFrames; int numPolys; int numGroups; - int weaponPoly; // for future model attachment support, unused for now + TArray specialPolys; // for future model attachment support, unused for now TArray verts; TArray polys; diff --git a/src/common/models/models_ue1.cpp b/src/common/models/models_ue1.cpp index 1fb6146a1..3066b7759 100644 --- a/src/common/models/models_ue1.cpp +++ b/src/common/models/models_ue1.cpp @@ -85,7 +85,6 @@ void FUE1Model::LoadGeometry() averts = (uint32_t*)(buffer2+sizeof(a3dhead)); dxverts = NULL; } - weaponPoly = -1; // set counters numVerts = dhead->numverts; numFrames = ahead->numframes; @@ -111,6 +110,9 @@ void FUE1Model::LoadGeometry() unpackuvert(averts[j+i*numVerts],2), -unpackuvert(averts[j+i*numVerts],1)); } + // refs will be set later + Vert.P.Reset(); + Vert.nP = 0; // push vertex (without normals, will be calculated later) verts.Push(Vert); } @@ -132,23 +134,26 @@ void FUE1Model::LoadGeometry() dir[0] = verts[Poly.V[1]+numVerts*j].Pos-verts[Poly.V[0]+numVerts*j].Pos; dir[1] = verts[Poly.V[2]+numVerts*j].Pos-verts[Poly.V[0]+numVerts*j].Pos; Poly.Normals.Push((dir[0]^dir[1]).Unit()); + // since we're iterating frames, also set references for later + for ( int k=0; k<3; k++ ) + { + verts[Poly.V[k]+numVerts*j].P.Push(i); + verts[Poly.V[k]+numVerts*j].nP++; + } } // push polys.Push(Poly); } - // compute normals for vertex arrays - // iterates through all polys and compute the average of all facet normals - // from those who use this vertex. not pretty, but does the job + // compute normals for vertex arrays (average of all referenced poly normals) + // since we have references listed from before, this saves a lot of time + // without having to loop through the entire model each vertex (especially true for very complex models) for ( int i=0; i