Skip to content

Commit

Permalink
Remember indices in ModelPolygonVertex instead of pointer conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamyCecil committed Sep 29, 2023
1 parent 2e8d74b commit 3d7d5f6
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 84 deletions.
44 changes: 21 additions & 23 deletions Sources/Engine/Models/EditModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1523,10 +1523,8 @@ void CEditModel::AddMipModel( CObject3D *pO3D)
*/
ULONG trans_vtx_idx = pO3D->ob_aoscSectors[0].osc_aovxVertices[ aesv[ mpvct].esv_MipGlobalIndex].ovx_Tag;

pmp->mp_PolygonVertices[ j].mpv_ptvTransformedVertex =
&edm_md.md_TransformedVertices[ (INDEX) trans_vtx_idx ]; // remapped ptr to transformed vertex
pmp->mp_PolygonVertices[ j].mpv_ptvTextureVertex =
&pmmpi->mmpi_TextureVertices[ aesv[ mpvct].esv_TextureVertexRemap]; // ptr to unique vertex in surface
pmp->mp_PolygonVertices[j].SetTransVertex(&edm_md, trans_vtx_idx); // remapped ptr to transformed vertex
pmp->mp_PolygonVertices[j].SetTexVertex(pmmpi, aesv[mpvct].esv_TextureVertexRemap); // ptr to unique vertex in surface
mpvct ++;
}
}
Expand Down Expand Up @@ -1943,14 +1941,14 @@ void CEditModel::DrawWireSurface( CDrawPort *pDP, INDEX iCurrentMip, INDEX iCurr
struct ModelPolygon *pPoly = &edm_md.md_MipInfos[iCurrentMip].mmpi_Polygons[iPoly];
if( pPoly->mp_Surface == iCurrentSurface)
{ // readout poly vertices
f3dTr0(1) = (FLOAT)pPoly->mp_PolygonVertices[0].mpv_ptvTextureVertex->mtv_UV(1);
f3dTr0(2) = (FLOAT)pPoly->mp_PolygonVertices[0].mpv_ptvTextureVertex->mtv_UV(2);
f3dTr0(1) = (FLOAT)pPoly->mp_PolygonVertices[0].GetTexVertex()->mtv_UV(1);
f3dTr0(2) = (FLOAT)pPoly->mp_PolygonVertices[0].GetTexVertex()->mtv_UV(2);
f3dTr0(3) = 0.0f;
f3dTr1(1) = (FLOAT)pPoly->mp_PolygonVertices[1].mpv_ptvTextureVertex->mtv_UV(1);
f3dTr1(2) = (FLOAT)pPoly->mp_PolygonVertices[1].mpv_ptvTextureVertex->mtv_UV(2);
f3dTr1(1) = (FLOAT)pPoly->mp_PolygonVertices[1].GetTexVertex()->mtv_UV(1);
f3dTr1(2) = (FLOAT)pPoly->mp_PolygonVertices[1].GetTexVertex()->mtv_UV(2);
f3dTr1(3) = 0.0f;
f3dTr2(1) = (FLOAT)pPoly->mp_PolygonVertices[2].mpv_ptvTextureVertex->mtv_UV(1);
f3dTr2(2) = (FLOAT)pPoly->mp_PolygonVertices[2].mpv_ptvTextureVertex->mtv_UV(2);
f3dTr2(1) = (FLOAT)pPoly->mp_PolygonVertices[2].GetTexVertex()->mtv_UV(1);
f3dTr2(2) = (FLOAT)pPoly->mp_PolygonVertices[2].GetTexVertex()->mtv_UV(2);
f3dTr2(3) = 0.0f;

// determine line visibility
Expand All @@ -1967,16 +1965,16 @@ void CEditModel::DrawWireSurface( CDrawPort *pDP, INDEX iCurrentMip, INDEX iCurr
// draw lines
PIX pixX0, pixY0, pixX1, pixY1;
for( INDEX iVtx=0; iVtx<pPoly->mp_PolygonVertices.Count()-1; iVtx++) {
pVtx0 = pPoly->mp_PolygonVertices[iVtx+0].mpv_ptvTextureVertex;
pVtx1 = pPoly->mp_PolygonVertices[iVtx+1].mpv_ptvTextureVertex;
pVtx0 = pPoly->mp_PolygonVertices[iVtx + 0].GetTexVertex();
pVtx1 = pPoly->mp_PolygonVertices[iVtx + 1].GetTexVertex();
pixX0 = (PIX)(pVtx0->mtv_UV(1) * fMagnifyFactor) - offx;
pixY0 = (PIX)(pVtx0->mtv_UV(2) * fMagnifyFactor) - offy;
pixX1 = (PIX)(pVtx1->mtv_UV(1) * fMagnifyFactor) - offx;
pixY1 = (PIX)(pVtx1->mtv_UV(2) * fMagnifyFactor) - offy;
pDP->DrawLine( pixX0, pixY0, pixX1, pixY1, clrWire|CT_OPAQUE, ulLineType);
}
// draw last line
pVtx0 = pPoly->mp_PolygonVertices[0].mpv_ptvTextureVertex;
pVtx0 = pPoly->mp_PolygonVertices[0].GetTexVertex();
pixX0 = (PIX)(pVtx0->mtv_UV(1) * fMagnifyFactor) - offx;
pixY0 = (PIX)(pVtx0->mtv_UV(2) * fMagnifyFactor) - offy;
pDP->DrawLine( pixX0, pixY0, pixX1, pixY1, clrWire|CT_OPAQUE, ulLineType);
Expand All @@ -2001,14 +1999,14 @@ void CEditModel::DrawFilledSurface( CDrawPort *pDP, INDEX iCurrentMip, INDEX iCu
struct ModelPolygon *pPoly = &edm_md.md_MipInfos[iCurrentMip].mmpi_Polygons[iPoly];
if( pPoly->mp_Surface == iCurrentSurface)
{ // readout poly vertices
f3dTr0(1) = (FLOAT)pPoly->mp_PolygonVertices[0].mpv_ptvTextureVertex->mtv_UV(1);
f3dTr0(2) = (FLOAT)pPoly->mp_PolygonVertices[0].mpv_ptvTextureVertex->mtv_UV(2);
f3dTr0(1) = (FLOAT)pPoly->mp_PolygonVertices[0].GetTexVertex()->mtv_UV(1);
f3dTr0(2) = (FLOAT)pPoly->mp_PolygonVertices[0].GetTexVertex()->mtv_UV(2);
f3dTr0(3) = 0.0f;
f3dTr1(1) = (FLOAT)pPoly->mp_PolygonVertices[1].mpv_ptvTextureVertex->mtv_UV(1);
f3dTr1(2) = (FLOAT)pPoly->mp_PolygonVertices[1].mpv_ptvTextureVertex->mtv_UV(2);
f3dTr1(1) = (FLOAT)pPoly->mp_PolygonVertices[1].GetTexVertex()->mtv_UV(1);
f3dTr1(2) = (FLOAT)pPoly->mp_PolygonVertices[1].GetTexVertex()->mtv_UV(2);
f3dTr1(3) = 0.0f;
f3dTr2(1) = (FLOAT)pPoly->mp_PolygonVertices[2].mpv_ptvTextureVertex->mtv_UV(1);
f3dTr2(2) = (FLOAT)pPoly->mp_PolygonVertices[2].mpv_ptvTextureVertex->mtv_UV(2);
f3dTr2(1) = (FLOAT)pPoly->mp_PolygonVertices[2].GetTexVertex()->mtv_UV(1);
f3dTr2(2) = (FLOAT)pPoly->mp_PolygonVertices[2].GetTexVertex()->mtv_UV(2);
f3dTr2(3) = 0.0f;

// determine poly visibility
Expand All @@ -2019,12 +2017,12 @@ void CEditModel::DrawFilledSurface( CDrawPort *pDP, INDEX iCurrentMip, INDEX iCu

// draw traingle(s) fan
pDP->InitTexture( NULL);
pVtx0 = pPoly->mp_PolygonVertices[0].mpv_ptvTextureVertex;
pVtx0 = pPoly->mp_PolygonVertices[0].GetTexVertex();
PIX pixX0 = (PIX)(pVtx0->mtv_UV(1) * fMagnifyFactor) - offx;
PIX pixY0 = (PIX)(pVtx0->mtv_UV(2) * fMagnifyFactor) - offy;
for( INDEX iVtx=1; iVtx<pPoly->mp_PolygonVertices.Count()-1; iVtx++) {
pVtx1 = pPoly->mp_PolygonVertices[iVtx+0].mpv_ptvTextureVertex;
pVtx2 = pPoly->mp_PolygonVertices[iVtx+1].mpv_ptvTextureVertex;
pVtx1 = pPoly->mp_PolygonVertices[iVtx + 0].GetTexVertex();
pVtx2 = pPoly->mp_PolygonVertices[iVtx + 1].GetTexVertex();
PIX pixX1 = (PIX)(pVtx1->mtv_UV(1) * fMagnifyFactor) - offx;
PIX pixY1 = (PIX)(pVtx1->mtv_UV(2) * fMagnifyFactor) - offy;
PIX pixX2 = (PIX)(pVtx2->mtv_UV(1) * fMagnifyFactor) - offx;
Expand Down Expand Up @@ -2324,7 +2322,7 @@ void CEditModel::CalculatePatchesPerPolygon(void)
MEXaabbox2D boxMapping;
for( INDEX iVertex=0; iVertex<pMP->mp_PolygonVertices.Count(); iVertex++)
{
ModelTextureVertex *pMTV = pMP->mp_PolygonVertices[iVertex].mpv_ptvTextureVertex;
ModelTextureVertex *pMTV = pMP->mp_PolygonVertices[iVertex].GetTexVertex();
// calculate bounding box of mapping coordinates
boxMapping |= MEXaabbox2D(pMTV->mtv_UV);
}
Expand Down
74 changes: 42 additions & 32 deletions Sources/Engine/Models/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,19 +473,30 @@ ModelTextureVertex::ModelTextureVertex(void)
//------------------------------------------ WRITE
void ModelPolygonVertex::Write_t( CTStream *pFile) // throw char *
{
(*pFile) << (INDEX) mpv_ptvTransformedVertex;
(*pFile) << (INDEX) mpv_ptvTextureVertex;
// [Cecil] Write indices instead of pointers
*pFile << mpv_iTransformedVertex;
*pFile << mpv_iTextureVertex;
}
//------------------------------------------ READ
void ModelPolygonVertex::Read_t( CTStream *pFile) // throw char *
{
INDEX itmp;

(*pFile) >> itmp;
mpv_ptvTransformedVertex = (struct TransformedVertexData *) itmp;
(*pFile) >> itmp;
mpv_ptvTextureVertex = (ModelTextureVertex *) itmp;
// [Cecil] Read into indices instead of pointers
*pFile >> mpv_iTransformedVertex;
*pFile >> mpv_iTextureVertex;
}

// [Cecil] Set transformed vertex data from a model data array
void ModelPolygonVertex::SetTransVertex(CModelData *md, INDEX iVtx) {
mpv_ptvTransformedVertex = &md->md_TransformedVertices[iVtx];
mpv_iTransformedVertex = iVtx;
};

// [Cecil] Set model texture vertex from a model data array
void ModelPolygonVertex::SetTexVertex(ModelMipInfo *mmpi, INDEX iVtx) {
mpv_ptvTextureVertex = &mmpi->mmpi_TextureVertices[iVtx];
mpv_iTextureVertex = iVtx;
};

//--------------------------------------------------------------------------------------------
//------------------------------------------ WRITE
void ModelPolygon::Write_t( CTStream *pFile) // throw char *
Expand Down Expand Up @@ -750,8 +761,8 @@ void CModelData::LinkDataForSurfaces(BOOL bFirstMip)
for( INDEX iVertex = 0; iVertex<pMMI->mmpi_Polygons[iPolygon].mp_PolygonVertices.Count(); iVertex++)
{
ModelPolygonVertex *pmpvPolygonVertex = &pMMI->mmpi_Polygons[iPolygon].mp_PolygonVertices[iVertex];
INDEX iTransformed = md_TransformedVertices.Index( pmpvPolygonVertex->mpv_ptvTransformedVertex);
pmpvPolygonVertex->mpv_ptvTextureVertex->mtv_iTransformedVertex = iTransformed;
INDEX iTransformed = md_TransformedVertices.Index(pmpvPolygonVertex->GetTransVertex());
pmpvPolygonVertex->GetTexVertex()->mtv_iTransformedVertex = iTransformed;
}
}}

Expand Down Expand Up @@ -813,8 +824,7 @@ void CModelData::LinkDataForSurfaces(BOOL bFirstMip)
for( INDEX iVertex=0; iVertex<mpPolygon.mp_PolygonVertices.Count(); iVertex++)
{
// get texture vertex
ModelTextureVertex *ptv =
mpPolygon.mp_PolygonVertices[ iVertex].mpv_ptvTextureVertex;
ModelTextureVertex *ptv = mpPolygon.mp_PolygonVertices[iVertex].GetTexVertex();
// if it is not added yet
if( !cmtvInSurface.IsMember( ptv))
{
Expand Down Expand Up @@ -1012,7 +1022,8 @@ void ModelMipInfo::Read_t(CTStream *pFile,
*/
void CModelData::PtrsToIndices()
{
INDEX i, j;
// [Cecil] Obsolete due to new index fields
/*size_t i, j;
for( i=0; i<md_MipCt; i++)
{
Expand All @@ -1025,17 +1036,17 @@ void CModelData::PtrsToIndices()
if( it2.Current().mpv_ptvTransformedVertex == &md_TransformedVertices[ j])
break;
}
it2.Current().mpv_ptvTransformedVertex = (struct TransformedVertexData *) j;
it2.Current().mpv_ptvTransformedVertex = (TransformedVertexData *)j;
for( j=0; j<md_MipInfos[ i].mmpi_TextureVertices.Count(); j++)
{
if( it2.Current().mpv_ptvTextureVertex == &md_MipInfos[ i].mmpi_TextureVertices[ j])
break;
}
it2.Current().mpv_ptvTextureVertex = (ModelTextureVertex *) j;
it2.Current().mpv_ptvTextureVertex = (ModelTextureVertex *)j;
}
}
}
}*/
}

//--------------------------------------------------------------------------------------------
Expand All @@ -1052,11 +1063,10 @@ void CModelData::IndicesToPtrs()
{
FOREACHINSTATICARRAY(it1.Current().mp_PolygonVertices, ModelPolygonVertex, it2)
{
struct ModelPolygonVertex * pMPV = &it2.Current();
j = (INDEX) it2.Current().mpv_ptvTransformedVertex;
it2.Current().mpv_ptvTransformedVertex = &md_TransformedVertices[ j];
j = (INDEX) it2.Current().mpv_ptvTextureVertex;
it2.Current().mpv_ptvTextureVertex = &md_MipInfos[ i].mmpi_TextureVertices[ j];
// [Cecil] Find pointers to structures using read indices
ModelPolygonVertex &mpv = it2.Current();
mpv.SetTransVertex(this, mpv.GetTransIndex());
mpv.SetTexVertex(&md_MipInfos[i], mpv.GetTexIndex());
}
}
}
Expand Down Expand Up @@ -2252,7 +2262,7 @@ void CModelObject::ColorizeRegion( CDrawPort *pDP, CProjection3D *pProjection, P
{
struct ModelPolygon *pPoly;
CModelData *pMD = (CModelData *) GetData();
struct TransformedVertexData *pTransformedVertice;
const TransformedVertexData *pTransformedVertice;
PIX pixDPHeight = pDP->GetHeight();
// project vertices for given mip model
ProjectFrameVertices( pProjection, mo_iLastRenderMipLevel);
Expand All @@ -2261,7 +2271,7 @@ void CModelObject::ColorizeRegion( CDrawPort *pDP, CProjection3D *pProjection, P
pPoly = &pMD->md_MipInfos[ mo_iLastRenderMipLevel].mmpi_Polygons[ j];
for( INDEX i=0; i<pPoly->mp_PolygonVertices.Count(); i++)
{
pTransformedVertice = pPoly->mp_PolygonVertices[ i].mpv_ptvTransformedVertex;
pTransformedVertice = pPoly->mp_PolygonVertices[i].GetTransVertex();
PIXaabbox2D ptBox = PIXaabbox2D( PIX2D( (SWORD) pTransformedVertice->tvd_TransformedPoint(1),
pixDPHeight - (SWORD) pTransformedVertice->tvd_TransformedPoint(2)));
if( !((box & ptBox).IsEmpty()) )
Expand Down Expand Up @@ -2293,7 +2303,7 @@ void CModelObject::ApplySurfaceToPolygonsInRegion( CDrawPort *pDP, CProjection3D
ProjectFrameVertices( pProjection, mo_iLastRenderMipLevel);

struct ModelPolygon *pPoly;
struct TransformedVertexData *pTransformedVertice;
const TransformedVertexData *pTransformedVertice;
CModelData *pMD = (CModelData *) GetData();
PIX pixDPHeight = pDP->GetHeight();

Expand All @@ -2302,7 +2312,7 @@ void CModelObject::ApplySurfaceToPolygonsInRegion( CDrawPort *pDP, CProjection3D
pPoly = &pMD->md_MipInfos[ mo_iLastRenderMipLevel].mmpi_Polygons[ j];
for( INDEX i=0; i<pPoly->mp_PolygonVertices.Count(); i++)
{
pTransformedVertice = pPoly->mp_PolygonVertices[ i].mpv_ptvTransformedVertex;
pTransformedVertice = pPoly->mp_PolygonVertices[i].GetTransVertex();
PIXaabbox2D ptBox = PIXaabbox2D( PIX2D( (SWORD) pTransformedVertice->tvd_TransformedPoint(1),
pixDPHeight - (SWORD) pTransformedVertice->tvd_TransformedPoint(2)));
if( !((box & ptBox).IsEmpty()) )
Expand Down Expand Up @@ -2436,16 +2446,16 @@ struct ModelPolygon *CModelObject::PolygonHitModelData(CModelData *pMD,
// get next vertex index (first is i)
INDEX next = (i+1) % pPoly->mp_PolygonVertices.Count();
// add edge to intersection object
Intersector.AddEdge( pPoly->mp_PolygonVertices[ i].mpv_ptvTransformedVertex->tvd_TransformedPoint(1),
pPoly->mp_PolygonVertices[ i].mpv_ptvTransformedVertex->tvd_TransformedPoint(2),
pPoly->mp_PolygonVertices[ next].mpv_ptvTransformedVertex->tvd_TransformedPoint(1),
pPoly->mp_PolygonVertices[ next].mpv_ptvTransformedVertex->tvd_TransformedPoint(2));
Intersector.AddEdge(pPoly->mp_PolygonVertices[i].GetTransVertex()->tvd_TransformedPoint(1),
pPoly->mp_PolygonVertices[i].GetTransVertex()->tvd_TransformedPoint(2),
pPoly->mp_PolygonVertices[next].GetTransVertex()->tvd_TransformedPoint(1),
pPoly->mp_PolygonVertices[next].GetTransVertex()->tvd_TransformedPoint(2));
}
if( Intersector.IsIntersecting())
{
FLOAT3D f3dTr0 = pPoly->mp_PolygonVertices[ 0].mpv_ptvTransformedVertex->tvd_TransformedPoint;
FLOAT3D f3dTr1 = pPoly->mp_PolygonVertices[ 1].mpv_ptvTransformedVertex->tvd_TransformedPoint;
FLOAT3D f3dTr2 = pPoly->mp_PolygonVertices[ 2].mpv_ptvTransformedVertex->tvd_TransformedPoint;
FLOAT3D f3dTr0 = pPoly->mp_PolygonVertices[0].GetTransVertex()->tvd_TransformedPoint;
FLOAT3D f3dTr1 = pPoly->mp_PolygonVertices[1].GetTransVertex()->tvd_TransformedPoint;
FLOAT3D f3dTr2 = pPoly->mp_PolygonVertices[2].GetTransVertex()->tvd_TransformedPoint;
FLOATplane3D fplPlane = FLOATplane3D( f3dTr0, f3dTr1, f3dTr2);

FLOAT3D f3dHitted3DPoint = FLOAT3D(0,0,0);
Expand Down
59 changes: 45 additions & 14 deletions Sources/Engine/Models/Model_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,52 @@ struct ENGINE_API ModelTextureVertex {
FLOAT3D mtv_vU, mtv_vV; // bump directions
};

struct ENGINE_API TransformedVertexData {
FLOAT3D tvd_TransformedPoint; // for transformed point vector
PolyVertex2D tvd_pv2; // vertex structure for software
FLOAT tvd_fX, tvd_fY, tvd_fZ; // view space original coords
FLOAT tvd_fU, tvd_fV; // texture mapping temp vars for clipping purposes
BOOL tvd_bClipped; // is clipped to near clip plane or screen boundaries?
};

struct ENGINE_API ModelPolygonVertex
{
struct TransformedVertexData *mpv_ptvTransformedVertex; // buffer where vertices really rotate
struct ModelTextureVertex *mpv_ptvTextureVertex; // needed by modeler to calculate U,V
void Read_t( CTStream *istrFile); // throw char *
void Write_t( CTStream *ostrFile); // throw char *
private:
TransformedVertexData *mpv_ptvTransformedVertex; // buffer where vertices really rotate
INDEX mpv_iTransformedVertex; // [Cecil] Index of the vertex in model data

ModelTextureVertex *mpv_ptvTextureVertex; // needed by modeler to calculate U,V
INDEX mpv_iTextureVertex; // [Cecil] Index of the texture vertex in model data

public:
void Read_t( CTStream *istrFile); // throw char *
void Write_t( CTStream *ostrFile); // throw char *

// [Cecil] Get transformed vertex data
inline TransformedVertexData *GetTransVertex(void) const {
return mpv_ptvTransformedVertex;
};

// [Cecil] Get index of transformed vertex data
inline INDEX GetTransIndex(void) const {
return mpv_iTransformedVertex;
};

// [Cecil] Set transformed vertex data from model data
void SetTransVertex(class CModelData *md, INDEX iVtx);

// [Cecil] Get model texture vertex
inline ModelTextureVertex *GetTexVertex(void) const {
return mpv_ptvTextureVertex;
};

// [Cecil] Get index of model texture vertex
inline INDEX GetTexIndex(void) const {
return mpv_iTextureVertex;
};

// [Cecil] Set model texture vertex from a model mip
void SetTexVertex(struct ModelMipInfo *mmpi, INDEX iVtx);
};

#define SC_ALLWAYS_ON (1UL << 30)
Expand All @@ -160,7 +200,7 @@ struct ENGINE_API ModelPolygon
{
ModelPolygon(); // constructor
~ModelPolygon(); // destructor
CStaticArray<struct ModelPolygonVertex> mp_PolygonVertices; // this polygon's vertices
CStaticArray<ModelPolygonVertex> mp_PolygonVertices; // this polygon's vertices
ULONG mp_RenderFlags; // flags which define rendering of this polygon
ULONG mp_ColorAndAlpha; // color and global alpha for this polygon
INDEX mp_Surface; // in which surface this polygon belongs
Expand Down Expand Up @@ -221,15 +261,6 @@ struct ENGINE_API PolygonsPerPatch
inline void Clear() { ppp_iPolygons.Clear();};
};

/* rcg 10042001 removed anonymous structs, dangerous union. */
struct ENGINE_API TransformedVertexData {
FLOAT3D tvd_TransformedPoint; // for transformed point vector
PolyVertex2D tvd_pv2; // vertex structure for software
FLOAT tvd_fX, tvd_fY, tvd_fZ; // view space original coords
FLOAT tvd_fU, tvd_fV; // texture mapping temp vars for clipping purposes
BOOL tvd_bClipped; // is clipped to near clip plane or screen boundaries?
};

class ENGINE_API CModelCollisionBox {
public:
FLOAT3D mcb_vCollisionBoxMin; // min vector of collision box
Expand Down
Loading

0 comments on commit 3d7d5f6

Please sign in to comment.