Model/material changes - addition of CMaterial clone functions, better encapsulation for CMaterialSet, removal of SModelData from model loading methods

This commit is contained in:
parax0 2015-07-31 23:44:50 -04:00
parent 4450606fbf
commit 086819b939
17 changed files with 324 additions and 229 deletions

View File

@ -180,7 +180,6 @@ HEADERS += \
Resource/model/CModel.h \
Resource/model/CStaticModel.h \
Resource/model/CVertex.h \
Resource/model/SModelData.h \
Resource/script/CProperty.h \
Resource/script/CScriptLayer.h \
Resource/script/CScriptObject.h \

View File

@ -44,40 +44,40 @@ void CGameArea::MergeTerrain()
if (mTerrainMerged) return;
// Nothing really complicated here - iterate through every terrain submesh, add each to a static model
for (u32 tm = 0; tm < mTerrainModels.size(); tm++)
for (u32 iMdl = 0; iMdl < mTerrainModels.size(); iMdl++)
{
CModel *mdl = mTerrainModels[tm];
u32 SubmeshCount = mdl->GetSurfaceCount();
CModel *pMdl = mTerrainModels[iMdl];
u32 SubmeshCount = pMdl->GetSurfaceCount();
for (u32 sub = 0; sub < SubmeshCount; sub++)
for (u32 iSurf = 0; iSurf < SubmeshCount; iSurf++)
{
SSurface *s = mdl->GetSurface(sub);
CMaterial *mat = mMaterialSet->materials[s->MaterialID];
SSurface *pSurf = pMdl->GetSurface(iSurf);
CMaterial *pMat = mMaterialSet->MaterialByIndex(pSurf->MaterialID);
bool NewMat = true;
bool newMat = true;
for (std::vector<CStaticModel*>::iterator it = mStaticTerrainModels.begin(); it != mStaticTerrainModels.end(); it++)
{
if ((*it)->GetMaterial() == mat)
if ((*it)->GetMaterial() == pMat)
{
// When we append a new submesh to an existing static model, we bump it to the back of the vector.
// This is because mesh ordering actually matters sometimes
// (particularly with multi-layered transparent meshes)
// so we need to at least try to maintain it.
// This is maybe not the most efficient way to do this, but it works.
CStaticModel *mdl = *it;
mdl->AddSurface(s);
CStaticModel *pStatic = *it;
pStatic->AddSurface(pSurf);
mStaticTerrainModels.erase(it);
mStaticTerrainModels.push_back(mdl);
NewMat = false;
mStaticTerrainModels.push_back(pStatic);
newMat = false;
break;
}
}
if (NewMat)
if (newMat)
{
CStaticModel *smdl = new CStaticModel(mat);
smdl->AddSurface(s);
mStaticTerrainModels.push_back(smdl);
CStaticModel *pStatic = new CStaticModel(pMat);
pStatic->AddSurface(pSurf);
mStaticTerrainModels.push_back(pStatic);
}
}
}

View File

@ -29,6 +29,23 @@ CMaterial::CMaterial()
mpIndirectTexture = nullptr;
}
CMaterial::CMaterial(EGame version, EVertexDescription vtxDesc)
{
mpShader = nullptr;
mShaderStatus = eNoShader;
mRecalcHash = true;
mEnableBloom = (version == eCorruption);
mVersion = version;
mOptions = eDepthWrite;
mVtxDesc = vtxDesc;
mBlendSrcFac = GL_ONE;
mBlendDstFac = GL_ZERO;
mLightingEnabled = true;
mEchoesUnknownA = 0;
mEchoesUnknownB = 0;
mpIndirectTexture = nullptr;
}
CMaterial::~CMaterial()
{
for (u32 iPass = 0; iPass < mPasses.size(); iPass++)
@ -37,6 +54,31 @@ CMaterial::~CMaterial()
delete mpShader;
}
CMaterial* CMaterial::Clone()
{
CMaterial *pOut = new CMaterial();
pOut->mName = mName;
pOut->mEnableBloom = mEnableBloom;
pOut->mVersion = mVersion;
pOut->mOptions = mOptions;
pOut->mVtxDesc = mVtxDesc;
for (u32 iKonst = 0; iKonst < 4; iKonst++)
pOut->mKonstColors[iKonst] = mKonstColors[iKonst];
pOut->mBlendSrcFac = mBlendSrcFac;
pOut->mBlendDstFac = mBlendDstFac;
pOut->mLightingEnabled = mLightingEnabled;
pOut->mEchoesUnknownA = mEchoesUnknownA;
pOut->mEchoesUnknownB = mEchoesUnknownB;
pOut->mpIndirectTexture = mpIndirectTexture;
pOut->mIndTextureToken = CToken(pOut->mpIndirectTexture);
pOut->mPasses.resize(mPasses.size());
for (u32 iPass = 0; iPass < mPasses.size(); iPass++)
pOut->mPasses[iPass] = mPasses[iPass]->Clone(pOut);
return pOut;
}
void CMaterial::GenerateShader()
{
delete mpShader;
@ -159,6 +201,11 @@ void CMaterial::Update()
}
// ************ GETTERS ************
std::string CMaterial::Name() const
{
return mName;
}
EGame CMaterial::Version() const
{
return mVersion;
@ -220,6 +267,11 @@ CMaterialPass* CMaterial::Pass(u32 PassIndex) const
// ************ SETTERS ************
void CMaterial::SetName(const std::string& name)
{
mName = name;
}
void CMaterial::SetOptions(EMaterialOptions Options)
{
mOptions = Options;

View File

@ -50,6 +50,7 @@ private:
static CColor sCurrentTint; // The tint for the currently bound material
// Members
std::string mName; // Name of the material
CShader *mpShader; // This material's generated shader. Created with GenerateShader().
EShaderStatus mShaderStatus; // A status variable so that PWE won't crash if a shader fails to compile.
u64 mParametersHash; // A hash of all the parameters that can identify this TEV setup.
@ -72,13 +73,16 @@ private:
public:
CMaterial();
CMaterial(EGame version, EVertexDescription vtxDesc);
~CMaterial();
CMaterial* Clone();
void GenerateShader();
bool SetCurrent(ERenderOptions Options);
u64 HashParameters();
void Update();
// Getters
std::string Name() const;
EGame Version() const;
EMaterialOptions Options() const;
EVertexDescription VtxDesc() const;
@ -93,6 +97,7 @@ public:
CMaterialPass* Pass(u32 PassIndex) const;
// Setters
void SetName(const std::string& name);
void SetOptions(EMaterialOptions Options);
void SetBlendMode(GLenum SrcFac, GLenum DstFac);
void SetKonst(CColor& Konst, u32 KIndex);

View File

@ -30,6 +30,31 @@ CMaterialPass::~CMaterialPass()
{
}
CMaterialPass* CMaterialPass::Clone(CMaterial *pParent)
{
CMaterialPass *pOut = new CMaterialPass(pParent);
pOut->mPassType = mPassType;
pOut->mSettings = mSettings;
for (u32 iIn = 0; iIn < 4; iIn++) {
pOut->mColorInputs[iIn] = mColorInputs[iIn];
pOut->mAlphaInputs[iIn] = mAlphaInputs[iIn];
}
pOut->mColorOutput = mColorOutput;
pOut->mAlphaOutput = mAlphaOutput;
pOut->mKColorSel = mKColorSel;
pOut->mKAlphaSel = mKAlphaSel;
pOut->mRasSel = mRasSel;
pOut->mTexCoordSource = mTexCoordSource;
pOut->mpTexture = mpTexture;
pOut->mTexToken = CToken(pOut->mpTexture);
pOut->mAnimMode = mAnimMode;
for (u32 iParam = 0; iParam < 4; iParam++)
pOut->mAnimParams[iParam] = mAnimParams[iParam];
pOut->mEnabled = mEnabled;
return pOut;
}
void CMaterialPass::HashParameters(CHashFNV1A &Hash)
{
if (mEnabled)

View File

@ -44,6 +44,7 @@ private:
public:
CMaterialPass(CMaterial *pParent);
~CMaterialPass();
CMaterialPass* Clone(CMaterial *pParent);
void HashParameters(CHashFNV1A& Hash);
void LoadTexture(u32 PassIndex);
void SetAnimCurrent(ERenderOptions Options, u32 PassIndex);

View File

@ -2,10 +2,41 @@
#include <Core/CResCache.h>
#include <iostream>
CMaterialSet::CMaterialSet() {}
CMaterialSet::CMaterialSet()
{
}
CMaterialSet::~CMaterialSet()
{
for (u32 m = 0; m < materials.size(); m++)
delete materials[m];
for (u32 iMat = 0; iMat < mMaterials.size(); iMat++)
delete mMaterials[iMat];
}
CMaterialSet* CMaterialSet::Clone()
{
CMaterialSet *pOut = new CMaterialSet();
pOut->mMaterials.resize(mMaterials.size());
for (u32 iMat = 0; iMat < mMaterials.size(); iMat++)
pOut->mMaterials[iMat] = mMaterials[iMat]->Clone();
return pOut;
}
u32 CMaterialSet::NumMaterials()
{
return mMaterials.size();
}
CMaterial* CMaterialSet::MaterialByIndex(u32 index)
{
if (index >= NumMaterials()) return nullptr;
return mMaterials[index];
}
CMaterial* CMaterialSet::MaterialByName(const std::string &name)
{
for (auto it = mMaterials.begin(); it != mMaterials.end(); it++)
if ((*it)->Name() == name) return *it;
return nullptr;
}

View File

@ -8,12 +8,18 @@
class CMaterialSet
{
public:
std::vector<CTexture*> textures;
std::vector<CMaterial*> materials;
friend class CMaterialLoader;
friend class CMaterialCooker;
std::vector<CMaterial*> mMaterials;
public:
CMaterialSet();
~CMaterialSet();
CMaterialSet* Clone();
u32 NumMaterials();
CMaterial* MaterialByIndex(u32 index);
CMaterial* MaterialByName(const std::string& name);
};
#endif // CMATERIALSET_H

View File

@ -11,11 +11,11 @@ void CMaterialCooker::WriteMatSetPrime(COutputStream& Out)
{
// Gather texture list from the materials before starting
mTextureIDs.clear();
u32 NumMats = mpSet->materials.size();
u32 NumMats = mpSet->mMaterials.size();
for (u32 iMat = 0; iMat < NumMats; iMat++)
{
CMaterial *pMat = mpSet->materials[iMat];
CMaterial *pMat = mpSet->mMaterials[iMat];
u32 NumPasses = pMat->PassCount();
for (u32 iPass = 0; iPass < NumPasses; iPass++)
@ -49,7 +49,7 @@ void CMaterialCooker::WriteMatSetPrime(COutputStream& Out)
for (u32 iMat = 0; iMat < NumMats; iMat++)
{
mpMat = mpSet->materials[iMat];
mpMat = mpSet->mMaterials[iMat];
WriteMaterialPrime(Out);
MatEndOffsets[iMat] = Out.Tell() - MatsStart;
}

View File

@ -75,8 +75,7 @@ void CAreaLoader::ReadGeometryPrime()
for (u32 m = 0; m < mNumMeshes; m++) {
std::cout << "\rLoading mesh " << std::dec << m + 1 << "/" << mNumMeshes;
SModelData *data = CModelLoader::LoadWorldModel(*mpMREA, *mBlockMgr, *mpArea->mMaterialSet, mVersion);
CModel *pTerrainModel = new CModel(data, mpArea->mMaterialSet);
CModel *pTerrainModel = CModelLoader::LoadWorldModel(*mpMREA, *mBlockMgr, *mpArea->mMaterialSet, mVersion);
mpArea->AddWorldModel(pTerrainModel);
if (mVersion >= eEchoes) {
@ -326,8 +325,7 @@ void CAreaLoader::ReadGeometryCorruption()
{
std::cout << "\rLoading mesh " << std::dec << iMesh + 1 << "/" << mNumMeshes;
SModelData *pData = CModelLoader::LoadCorruptionWorldModel(*mpMREA, *mBlockMgr, *mpArea->mMaterialSet, CurWOBJSection, CurGPUSection, mVersion);
CModel *pWorldModel = new CModel(pData, mpArea->mMaterialSet);
CModel *pWorldModel = CModelLoader::LoadCorruptionWorldModel(*mpMREA, *mBlockMgr, *mpArea->mMaterialSet, CurWOBJSection, CurGPUSection, mVersion);
mpArea->AddWorldModel(pWorldModel);
CurWOBJSection += 4;

View File

@ -33,28 +33,29 @@ CMaterialSet* CMaterialLoader::LoadMaterialSet(CInputStream& Mat, EGame Version)
void CMaterialLoader::ReadPrimeMatSet()
{
// Textures
u32 TexCount = mpFile->ReadLong();
mpSet->textures.resize(TexCount);
u32 numTextures = mpFile->ReadLong();
mTextures.resize(numTextures);
for (u32 t = 0; t < TexCount; t++)
for (u32 iTex = 0; iTex < numTextures; iTex++)
{
u32 TextureID = mpFile->ReadLong();
mpSet->textures[t] = (CTexture*) gResCache.GetResource(TextureID, "TXTR");
mTextures[iTex] = (CTexture*) gResCache.GetResource(TextureID, "TXTR");
}
// Materials
u32 MatCount = mpFile->ReadLong();
std::vector<u32> offsets(MatCount);
for (u32 m = 0; m < MatCount; m++)
offsets[m] = mpFile->ReadLong();
u32 numMats = mpFile->ReadLong();
std::vector<u32> offsets(numMats);
for (u32 iMat = 0; iMat < numMats; iMat++)
offsets[iMat] = mpFile->ReadLong();
u32 mats_start = mpFile->Tell();
mpSet->materials.resize(MatCount);
for (u32 m = 0; m < MatCount; m++)
u32 matsStart = mpFile->Tell();
mpSet->mMaterials.resize(numMats);
for (u32 iMat = 0; iMat < numMats; iMat++)
{
mpSet->materials[m] = ReadPrimeMaterial();
mpSet->materials[m]->mVersion = mVersion;
mpFile->Seek(mats_start + offsets[m], SEEK_SET);
mpSet->mMaterials[iMat] = ReadPrimeMaterial();
mpSet->mMaterials[iMat]->mVersion = mVersion;
mpSet->mMaterials[iMat]->mName = std::string("Material #") + std::to_string(iMat);
mpFile->Seek(matsStart + offsets[iMat], SEEK_SET);
}
}
@ -108,7 +109,7 @@ CMaterial* CMaterialLoader::ReadPrimeMaterial()
if (pMat->mOptions & CMaterial::eIndStage)
{
u32 IndTexIndex = mpFile->ReadLong();
pMat->mpIndirectTexture = mpSet->textures[IndTexIndex];
pMat->mpIndirectTexture = mTextures[IndTexIndex];
}
// Color channels
@ -149,13 +150,14 @@ CMaterial* CMaterialLoader::ReadPrimeMaterial()
CMaterialPass *pPass = pMat->Pass(iTev);
u8 TexSel = mpFile->ReadByte();
if ((TexSel == 0xFF) || (TexSel >= mpSet->textures.size()))
if ((TexSel == 0xFF) || (TexSel >= mTextures.size()))
{
pPass->mpTexture = nullptr;
}
else
{
pPass->mpTexture = mpSet->textures[ TextureIndices[TexSel] ];
pPass->mpTexture = mTextures[TextureIndices[TexSel]];
pPass->mTexToken = CToken(pPass->mpTexture);
}
@ -245,14 +247,15 @@ CMaterial* CMaterialLoader::ReadPrimeMaterial()
void CMaterialLoader::ReadCorruptionMatSet()
{
u32 NumMats = mpFile->ReadLong();
mpSet->materials.resize(NumMats);
mpSet->mMaterials.resize(NumMats);
for (u32 iMat = 0; iMat < NumMats; iMat++)
{
u32 Size = mpFile->ReadLong();
u32 Next = mpFile->Tell() + Size;
mpSet->materials[iMat] = ReadCorruptionMaterial();
mpSet->materials[iMat]->mVersion = mVersion;
mpSet->mMaterials[iMat] = ReadCorruptionMaterial();
mpSet->mMaterials[iMat]->mVersion = mVersion;
mpSet->mMaterials[iMat]->mName = std::string("Material #") + std::to_string(iMat);
mpFile->Seek(Next, SEEK_SET);
}
}
@ -357,7 +360,6 @@ CMaterial* CMaterialLoader::ReadCorruptionMaterial()
}
CTexture *pTex = (CTexture*) gResCache.GetResource(TextureID, "TXTR");
mpSet->textures.push_back(pTex);
pPass->mpTexture = pTex;
pPass->mTexToken = CToken(pTex);

View File

@ -12,6 +12,7 @@ class CMaterialLoader
CMaterialSet *mpSet;
CInputStream *mpFile;
EGame mVersion;
std::vector<CTexture*> mTextures;
bool mHasOPAC;
bool mHas0x400;

View File

@ -115,128 +115,116 @@ void CModelLoader::LoadSurfaceOffsets(CInputStream& Model)
mpBlockMgr->ToNextBlock();
}
SModelData* CModelLoader::LoadSurfaces(CInputStream& Model)
SSurface* CModelLoader::LoadSurface(CInputStream& Model)
{
// This function is meant to be called at the start of the first surface
SModelData *pData = new SModelData;
u32 Offset = Model.Tell();
SSurface *pSurf = new SSurface;
// Surfaces
pData->mSurfaces.resize(mSurfaceCount);
// Surface header
if (mVersion < eReturns)
LoadSurfaceHeaderPrime(Model, pSurf);
else
LoadSurfaceHeaderDKCR(Model, pSurf);
for (u32 iSurf = 0; iSurf < mSurfaceCount; iSurf++)
bool HasAABB = (pSurf->AABox != CAABox::skInfinite);
CMaterial *pMat = mMaterials[0]->MaterialByIndex(pSurf->MaterialID);
// Primitive table
u8 Flag = Model.ReadByte();
u32 NextSurface = mpBlockMgr->NextOffset();
while ((Flag != 0) && ((u32) Model.Tell() < NextSurface))
{
SSurface *pSurf = new SSurface;
pData->mSurfaces[iSurf] = pSurf;
u32 NextSurface = mpBlockMgr->NextOffset();
SSurface::SPrimitive Prim;
Prim.Type = EGXPrimitiveType(Flag & 0xF8);
u16 VertexCount = Model.ReadShort();
// Surface header
if (mVersion < eReturns)
LoadSurfaceHeaderPrime(Model, pSurf);
else
LoadSurfaceHeaderDKCR(Model, pSurf);
bool HasAABB = (pSurf->AABox != CAABox::skInfinite);
CMaterial *pMat = mMaterials[0]->materials[pSurf->MaterialID];
// Primitive table
u8 Flag = Model.ReadByte();
while ((Flag != 0) && ((u32) Model.Tell() < NextSurface))
for (u16 iVtx = 0; iVtx < VertexCount; iVtx++)
{
SSurface::SPrimitive Prim;
Prim.Type = EGXPrimitiveType(Flag & 0xF8);
u16 VertexCount = Model.ReadShort();
CVertex Vtx;
EVertexDescription VtxDesc = pMat->VtxDesc();
for (u16 iVtx = 0; iVtx < VertexCount; iVtx++)
for (u32 iMtxAttr = 0; iMtxAttr < 8; iMtxAttr++)
if (VtxDesc & (ePosMtx << iMtxAttr)) Model.Seek(0x1, SEEK_CUR);
// Only thing to do here is check whether each attribute is present, and if so, read it.
// A couple attributes have special considerations; normals can be floats or shorts, as can tex0, depending on vtxfmt.
// tex0 can also be read from either UV buffer; depends what the material says.
// Position
if (VtxDesc & ePosition)
{
CVertex Vtx;
EVertexDescription VtxDesc = pMat->VtxDesc();
u16 PosIndex = Model.ReadShort() & 0xFFFF;
Vtx.Position = mPositions[PosIndex];
Vtx.ArrayPosition = PosIndex;
for (u32 iMtxAttr = 0; iMtxAttr < 8; iMtxAttr++)
if (VtxDesc & (ePosMtx << iMtxAttr)) Model.Seek(0x1, SEEK_CUR);
// Only thing to do here is check whether each attribute is present, and if so, read it.
// A couple attributes have special considerations; normals can be floats or shorts, as can tex0, depending on vtxfmt.
// tex0 can also be read from either UV buffer; depends what the material says.
// Position
if (VtxDesc & ePosition)
{
u16 PosIndex = Model.ReadShort() & 0xFFFF;
Vtx.Position = mPositions[PosIndex];
Vtx.ArrayPosition = PosIndex;
if (!HasAABB) pSurf->AABox.ExpandBounds(Vtx.Position);
}
// Normal
if (VtxDesc & eNormal)
Vtx.Normal = mNormals[Model.ReadShort() & 0xFFFF];
// Color
for (u32 c = 0; c < 2; c++)
if (VtxDesc & (eColor0 << (c * 2)))
Vtx.Color[c] = mColors[Model.ReadShort() & 0xFFFF];
// Tex Coords - these are done a bit differently in DKCR than in the Prime series
if (mVersion < eReturns)
{
// Tex0
if (VtxDesc & eTex0)
{
if ((mFlags & eHasTex1) && (pMat->Options() & CMaterial::eShortTexCoord))
Vtx.Tex[0] = mTex1[Model.ReadShort() & 0xFFFF];
else
Vtx.Tex[0] = mTex0[Model.ReadShort() & 0xFFFF];
}
// Tex1-7
for (u32 iTex = 1; iTex < 7; iTex++)
if (VtxDesc & (eTex0 << (iTex * 2)))
Vtx.Tex[iTex] = mTex0[Model.ReadShort() & 0xFFFF];
}
else
{
// Tex0-7
for (u32 iTex = 0; iTex < 7; iTex++)
{
if (VtxDesc & (eTex0 << iTex * 2))
{
if (!mSurfaceUsingTex1)
Vtx.Tex[iTex] = mTex0[Model.ReadShort() & 0xFFFF];
else
Vtx.Tex[iTex] = mTex1[Model.ReadShort() & 0xFFFF];
}
}
}
Prim.Vertices.push_back(Vtx);
} // Vertex array end
// Update vertex/triangle count
pSurf->VertexCount += VertexCount;
switch (Prim.Type)
{
case eGX_Triangles:
pSurf->TriangleCount += VertexCount / 3;
break;
case eGX_TriangleFan:
case eGX_TriangleStrip:
pSurf->TriangleCount += VertexCount - 2;
break;
if (!HasAABB) pSurf->AABox.ExpandBounds(Vtx.Position);
}
pSurf->Primitives.push_back(Prim);
Flag = Model.ReadByte();
} // Primitive table end
// Normal
if (VtxDesc & eNormal)
Vtx.Normal = mNormals[Model.ReadShort() & 0xFFFF];
mpBlockMgr->ToNextBlock();
} // Submesh table end
// Color
for (u32 c = 0; c < 2; c++)
if (VtxDesc & (eColor0 << (c * 2)))
Vtx.Color[c] = mColors[Model.ReadShort() & 0xFFFF];
return pData;
// Tex Coords - these are done a bit differently in DKCR than in the Prime series
if (mVersion < eReturns)
{
// Tex0
if (VtxDesc & eTex0)
{
if ((mFlags & eHasTex1) && (pMat->Options() & CMaterial::eShortTexCoord))
Vtx.Tex[0] = mTex1[Model.ReadShort() & 0xFFFF];
else
Vtx.Tex[0] = mTex0[Model.ReadShort() & 0xFFFF];
}
// Tex1-7
for (u32 iTex = 1; iTex < 7; iTex++)
if (VtxDesc & (eTex0 << (iTex * 2)))
Vtx.Tex[iTex] = mTex0[Model.ReadShort() & 0xFFFF];
}
else
{
// Tex0-7
for (u32 iTex = 0; iTex < 7; iTex++)
{
if (VtxDesc & (eTex0 << iTex * 2))
{
if (!mSurfaceUsingTex1)
Vtx.Tex[iTex] = mTex0[Model.ReadShort() & 0xFFFF];
else
Vtx.Tex[iTex] = mTex1[Model.ReadShort() & 0xFFFF];
}
}
}
Prim.Vertices.push_back(Vtx);
} // Vertex array end
// Update vertex/triangle count
pSurf->VertexCount += VertexCount;
switch (Prim.Type)
{
case eGX_Triangles:
pSurf->TriangleCount += VertexCount / 3;
break;
case eGX_TriangleFan:
case eGX_TriangleStrip:
pSurf->TriangleCount += VertexCount - 2;
break;
}
pSurf->Primitives.push_back(Prim);
Flag = Model.ReadByte();
} // Primitive table end
mpBlockMgr->ToNextBlock();
return pSurf;
}
void CModelLoader::LoadSurfaceHeaderPrime(CInputStream& Model, SSurface *pSurf)
@ -378,19 +366,23 @@ CModel* CModelLoader::LoadCMDL(CInputStream& CMDL)
// Mesh
Loader.LoadAttribArrays(CMDL);
Loader.LoadSurfaceOffsets(CMDL);
SModelData *pData = Loader.LoadSurfaces(CMDL);
pModel->mSurfaces.reserve(Loader.mSurfaceCount);
for (u32 iSurf = 0; iSurf < Loader.mSurfaceCount; iSurf++)
{
SSurface *pSurf = Loader.LoadSurface(CMDL);
pModel->mSurfaces.push_back(pSurf);
}
pModel->SetData(pData);
pModel->mAABox = AABox;
pModel->mHasOwnSurfaces = true;
// Cleanup
delete pData;
delete Loader.mpBlockMgr;
return pModel;
}
SModelData* CModelLoader::LoadWorldModel(CInputStream& MREA, CBlockMgrIn& BlockMgr, CMaterialSet& MatSet, EGame Version)
CModel* CModelLoader::LoadWorldModel(CInputStream& MREA, CBlockMgrIn& BlockMgr, CMaterialSet& MatSet, EGame Version)
{
CModelLoader Loader;
Loader.mpBlockMgr = &BlockMgr;
@ -403,13 +395,27 @@ SModelData* CModelLoader::LoadWorldModel(CInputStream& MREA, CBlockMgrIn& BlockM
Loader.LoadWorldMeshHeader(MREA);
Loader.LoadAttribArrays(MREA);
Loader.LoadSurfaceOffsets(MREA);
SModelData *pData = Loader.LoadSurfaces(MREA);
pData->mAABox = Loader.mAABox;
return pData;
CModel *pModel = new CModel();
pModel->mMaterialSets.resize(1);
pModel->mMaterialSets[0] = &MatSet;
pModel->mHasOwnMaterials = false;
pModel->mSurfaces.reserve(Loader.mSurfaceCount);
pModel->mHasOwnSurfaces = true;
for (u32 iSurf = 0; iSurf < Loader.mSurfaceCount; iSurf++)
{
SSurface *pSurf = Loader.LoadSurface(MREA);
pModel->mSurfaces.push_back(pSurf);
pModel->mVertexCount += pSurf->VertexCount;
pModel->mTriangleCount += pSurf->TriangleCount;
}
pModel->mAABox = Loader.mAABox;
return pModel;
}
SModelData* CModelLoader::LoadCorruptionWorldModel(CInputStream &MREA, CBlockMgrIn &BlockMgr, CMaterialSet &MatSet, u32 HeaderSecNum, u32 GPUSecNum, EGame Version)
CModel* CModelLoader::LoadCorruptionWorldModel(CInputStream &MREA, CBlockMgrIn &BlockMgr, CMaterialSet &MatSet, u32 HeaderSecNum, u32 GPUSecNum, EGame Version)
{
CModelLoader Loader;
Loader.mpBlockMgr = &BlockMgr;
@ -423,12 +429,26 @@ SModelData* CModelLoader::LoadCorruptionWorldModel(CInputStream &MREA, CBlockMgr
BlockMgr.ToBlock(HeaderSecNum);
Loader.LoadWorldMeshHeader(MREA);
Loader.LoadSurfaceOffsets(MREA);
BlockMgr.ToBlock(GPUSecNum);
Loader.LoadAttribArrays(MREA);
SModelData *pData = Loader.LoadSurfaces(MREA);
pData->mAABox = Loader.mAABox;
return pData;
CModel *pModel = new CModel();
pModel->mMaterialSets.resize(1);
pModel->mMaterialSets[0] = &MatSet;
pModel->mHasOwnMaterials = false;
pModel->mSurfaces.reserve(Loader.mSurfaceCount);
pModel->mHasOwnSurfaces = true;
for (u32 iSurf = 0; iSurf < Loader.mSurfaceCount; iSurf++)
{
SSurface *pSurf = Loader.LoadSurface(MREA);
pModel->mSurfaces.push_back(pSurf);
pModel->mVertexCount += pSurf->VertexCount;
pModel->mTriangleCount += pSurf->TriangleCount;
}
pModel->mAABox = Loader.mAABox;
return pModel;
}
EGame CModelLoader::GetFormatVersion(u32 Version)

View File

@ -47,14 +47,14 @@ private:
void LoadAttribArrays(CInputStream& Model);
void LoadAttribArraysDKCR(CInputStream& Model);
void LoadSurfaceOffsets(CInputStream& Model);
SModelData* LoadSurfaces(CInputStream& Model);
SSurface* LoadSurface(CInputStream& Model);
void LoadSurfaceHeaderPrime(CInputStream& Model, SSurface *pSurf);
void LoadSurfaceHeaderDKCR(CInputStream& Model, SSurface *pSurf);
public:
static CModel* LoadCMDL(CInputStream& CMDL);
static SModelData* LoadWorldModel(CInputStream& MREA, CBlockMgrIn& BlockMgr, CMaterialSet& MatSet, EGame Version);
static SModelData* LoadCorruptionWorldModel(CInputStream& MREA, CBlockMgrIn& BlockMgr, CMaterialSet& MatSet, u32 HeaderSecNum, u32 GPUSecNum, EGame Version);
static CModel* LoadWorldModel(CInputStream& MREA, CBlockMgrIn& BlockMgr, CMaterialSet& MatSet, EGame Version);
static CModel* LoadCorruptionWorldModel(CInputStream& MREA, CBlockMgrIn& BlockMgr, CMaterialSet& MatSet, u32 HeaderSecNum, u32 GPUSecNum, EGame Version);
static EGame GetFormatVersion(u32 Version);
};

View File

@ -6,22 +6,8 @@ CModel::CModel() : CBasicModel()
{
mHasOwnMaterials = true;
mHasOwnSurfaces = true;
}
CModel::CModel(SModelData *pModelData) : CBasicModel()
{
SetData(pModelData);
mHasOwnMaterials = false;
mHasOwnSurfaces = true;
}
CModel::CModel(SModelData *data, CMaterialSet *pMatSet) : CBasicModel()
{
SetData(data);
mMaterialSets.resize(1);
mMaterialSets[0] = pMatSet;
mHasOwnMaterials = false;
mHasOwnSurfaces = true;
mVertexCount = 0;
mTriangleCount = 0;
}
CModel::~CModel()
@ -31,22 +17,6 @@ CModel::~CModel()
delete mMaterialSets[m];
}
void CModel::SetData(SModelData *pModelData)
{
mAABox = pModelData->mAABox;
mSurfaces = pModelData->mSurfaces;
mVertexCount = 0;
mTriangleCount = 0;
for (u32 iSurf = 0; iSurf < mSurfaces.size(); iSurf++)
{
SSurface *pSurf = mSurfaces[iSurf];
mVertexCount += pSurf->VertexCount;
mTriangleCount += pSurf->TriangleCount;
}
}
void CModel::BufferGL()
{
mVBO.Clear();
@ -117,7 +87,7 @@ void CModel::DrawSurface(ERenderOptions Options, u32 Surface, u32 MatSet)
// Bind material
SSurface *pSurf = mSurfaces[Surface];
CMaterial *pMat = mMaterialSets[MatSet]->materials[pSurf->MaterialID];
CMaterial *pMat = mMaterialSets[MatSet]->MaterialByIndex(pSurf->MaterialID);
if ((Options & eNoMaterialSetup) == 0)
{
@ -147,7 +117,7 @@ u32 CModel::GetMatSetCount()
u32 CModel::GetMatCount()
{
if (mMaterialSets.empty()) return 0;
else return mMaterialSets[0]->materials.size();
else return mMaterialSets[0]->NumMaterials();
}
CMaterialSet* CModel::GetMatSet(u32 MatSet)
@ -163,7 +133,7 @@ CMaterial* CModel::GetMaterialByIndex(u32 MatSet, u32 Index)
if (GetMatCount() == 0)
return nullptr;
return mMaterialSets[MatSet]->materials[Index];
return mMaterialSets[MatSet]->MaterialByIndex(Index);
}
CMaterial* CModel::GetMaterialBySurface(u32 MatSet, u32 Surface)
@ -176,8 +146,8 @@ bool CModel::HasTransparency(u32 MatSet)
if (MatSet >= mMaterialSets.size())
MatSet = mMaterialSets.size() - 1;
for (u32 iMat = 0; iMat < mMaterialSets[MatSet]->materials.size(); iMat++)
if (mMaterialSets[MatSet]->materials[iMat]->Options() & CMaterial::eTransparent ) return true;
for (u32 iMat = 0; iMat < mMaterialSets[MatSet]->NumMaterials(); iMat++)
if (mMaterialSets[MatSet]->MaterialByIndex(iMat)->Options() & CMaterial::eTransparent ) return true;
return false;
}
@ -188,7 +158,7 @@ bool CModel::IsSurfaceTransparent(u32 Surface, u32 MatSet)
MatSet = mMaterialSets.size() - 1;
u32 matID = mSurfaces[Surface]->MaterialID;
return (mMaterialSets[MatSet]->materials[matID]->Options() & CMaterial::eTransparent) != 0;
return (mMaterialSets[MatSet]->MaterialByIndex(matID)->Options() & CMaterial::eTransparent) != 0;
}
CIndexBuffer* CModel::InternalGetIBO(u32 Surface, EGXPrimitiveType Primitive)

View File

@ -21,10 +21,8 @@ class CModel : public CBasicModel
public:
CModel();
CModel(SModelData *pModelData);
CModel(SModelData *pModelData, CMaterialSet *pMatSet);
CModel(CMaterialSet *pSet);
~CModel();
void SetData(SModelData *pModelData);
void BufferGL();
void ClearGLBuffer();

View File

@ -1,13 +0,0 @@
#ifndef SMODELDATA_H
#define SMODELDATA_H
#include "SSurface.h"
#include <Common/CAABox.h>
struct SModelData
{
CAABox mAABox;
std::vector<SSurface*> mSurfaces;
};
#endif // SMODELDATA_H