Skip to content

Commit

Permalink
Bugfix: Assign body textures after loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ataulien committed Sep 30, 2019
1 parent 2b6414e commit e080948
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/RTTI/RTTI_VisualCharacter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace REGoth
BS_RTTI_MEMBER_PLAIN_NAMED(teethTextureIdx, mBodyState.teethTextureIdx, 3)
BS_RTTI_MEMBER_PLAIN_NAMED(bodySkinColorIdx, mBodyState.bodySkinColorIdx, 4)
BS_RTTI_MEMBER_PLAIN_NAMED(bodyTextureIdx, mBodyState.bodyTextureIdx, 5)
BS_RTTI_MEMBER_REFL_NAMED(bodyTexture, mBodyState.bodyTexture, 6)
BS_RTTI_MEMBER_REFL_NAMED(headTexture, mBodyState.headTexture, 7)
BS_END_RTTI_MEMBERS

public:
Expand Down
35 changes: 23 additions & 12 deletions src/components/VisualCharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ namespace REGoth
setName("VisualCharacter");
}

void VisualCharacter::onInitialized()
{
VisualSkeletalAnimation::onInitialized();

setBodyTexture(mBodyState.bodyTexture);
setHeadTexture(mBodyState.headTexture);
}

void VisualCharacter::setBodyMesh(const bs::String& bodyMesh, bs::UINT32 bodyTextureIdx,
bs::UINT32 bodySkinColorIdx)
{
Expand Down Expand Up @@ -89,6 +97,8 @@ namespace REGoth
mBodyState.headVisual += ".MMB";
}

// TODO: Save head texture once it is set

updateHeadMesh();
}

Expand All @@ -114,7 +124,9 @@ namespace REGoth
newTextureName, "_C0", "_C" + bs::toString(mBodyState.bodySkinColorIdx));
}

setBodyTexture(newTextureName);
mBodyState.bodyTexture = gOriginalGameResources().texture(newTextureName);

setBodyTexture(mBodyState.bodyTexture);
}

bs::String VisualCharacter::getCurrentBodyTextureName() const
Expand All @@ -129,18 +141,9 @@ namespace REGoth
return albedo->getName();
}

void VisualCharacter::setBodyTexture(const bs::String& originalFileName)
void VisualCharacter::setBodyTexture(bs::HTexture texture)
{
bs::HTexture texture = gOriginalGameResources().texture(originalFileName);

if (!texture)
{
REGOTH_THROW(InvalidParametersException, "Could not load body texture: " + originalFileName);
}

REGOTH_LOG(Info, Uncategorized, "Set body texture: {0}", originalFileName);

material(0)->setTexture("gAlbedoTex", texture);
if (material(0)) material(0)->setTexture("gAlbedoTex", texture);
}

void VisualCharacter::updateHeadMesh()
Expand All @@ -157,6 +160,14 @@ namespace REGoth
}

// TODO: Fix texture and color
mBodyState.headTexture = {};

setHeadTexture(mBodyState.headTexture);
}

void VisualCharacter::setHeadTexture(bs::HTexture texture)
{
// TODO: Implement setHeadTexture
}

bs::Vector<bs::String> VisualCharacter::listPossibleDefaultAnimations() const
Expand Down
10 changes: 6 additions & 4 deletions src/components/VisualCharacter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace REGoth
bs::UINT32 teethTextureIdx = 0);

protected:
void onInitialized() override;

bs::Vector<bs::String> listPossibleDefaultAnimations() const override;

private:
Expand All @@ -65,11 +67,9 @@ namespace REGoth

/**
* Sets the texture to be displayed on the body mesh.
*
* @param originalFileName Name of the texture file as in the original game files.
* For example, "STONE.TGA".
*/
void setBodyTexture(const bs::String& originalFileName);
void setBodyTexture(bs::HTexture texture);
void setHeadTexture(bs::HTexture texture);

/**
* Replaces the current headmesh of this model from the current body-state
Expand All @@ -84,6 +84,8 @@ namespace REGoth
bs::UINT32 teethTextureIdx = 0;
bs::UINT32 bodySkinColorIdx = 0;
bs::UINT32 bodyTextureIdx = 0;
bs::HTexture bodyTexture;
bs::HTexture headTexture;
};

// Body Visual Settings ---------------------------------------------------
Expand Down
58 changes: 57 additions & 1 deletion src/original-content/OriginalGameResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace REGoth
{
bs::HTexture htexture;

auto it = mTextures.find(originalFileName);

if (it != mTextures.end())
{
return it->second;
}

if (BsZenLib::HasCachedTexture(originalFileName))
{
htexture = BsZenLib::LoadCachedTexture(originalFileName);
Expand All @@ -37,6 +44,8 @@ namespace REGoth
BsZenLib::ImportAndCacheTexture(originalFileName, gVirtualFileSystem().getFileIndex());
}

mTextures[originalFileName] = htexture;

return htexture;
}

Expand All @@ -45,6 +54,13 @@ namespace REGoth
{
BsZenLib::Res::HModelScriptFile hmodelScript;

auto it = mModelScripts.find(originalFileName);

if (it != mModelScripts.end())
{
return it->second;
}

if (BsZenLib::HasCachedMDS(originalFileName))
{
hmodelScript = BsZenLib::LoadCachedMDS(originalFileName);
Expand All @@ -58,6 +74,8 @@ namespace REGoth
BsZenLib::ImportAndCacheMDS(originalFileName, gVirtualFileSystem().getFileIndex());
}

mModelScripts[originalFileName] = hmodelScript;

return hmodelScript;
}

Expand All @@ -66,6 +84,13 @@ namespace REGoth
{
BsZenLib::Res::HMeshWithMaterials hmesh;

auto it = mStaticMeshes.find(originalFileName);

if (it != mStaticMeshes.end())
{
return it->second;
}

if (BsZenLib::HasCachedStaticMesh(originalFileName))
{
hmesh = BsZenLib::LoadCachedStaticMesh(originalFileName);
Expand All @@ -79,6 +104,8 @@ namespace REGoth
BsZenLib::ImportAndCacheStaticMesh(originalFileName, gVirtualFileSystem().getFileIndex());
}

mStaticMeshes[originalFileName] = hmesh;

return hmesh;
}

Expand All @@ -87,6 +114,13 @@ namespace REGoth
{
BsZenLib::Res::HMeshWithMaterials hmesh;

auto it = mMorphMeshes.find(originalFileName);

if (it != mMorphMeshes.end())
{
return it->second;
}

if (BsZenLib::HasCachedMorphMesh(originalFileName))
{
hmesh = BsZenLib::LoadCachedMorphMesh(originalFileName);
Expand All @@ -100,13 +134,22 @@ namespace REGoth
BsZenLib::ImportAndCacheMorphMesh(originalFileName, gVirtualFileSystem().getFileIndex());
}

mMorphMeshes[originalFileName] = hmesh;

return hmesh;
}

bs::HFont OriginalGameResources::font(const bs::String& originalFileName)
{
bs::HFont hfont;

auto it = mFonts.find(originalFileName);

if (it != mFonts.end())
{
return it->second;
}

if (BsZenLib::HasCachedFont(originalFileName))
{
hfont = BsZenLib::LoadCachedFont(originalFileName);
Expand All @@ -119,11 +162,20 @@ namespace REGoth
hfont = BsZenLib::ImportAndCacheFont(originalFileName, gVirtualFileSystem().getFileIndex());
}

mFonts[originalFileName] = hfont;

return hfont;
}

bs::HSpriteTexture OriginalGameResources::sprite(const bs::String& originalFileName)
{
auto it = mSprites.find(originalFileName);

if (it != mSprites.end())
{
return it->second;
}

bs::HTexture t = texture(originalFileName);

if (!t)
Expand All @@ -134,7 +186,11 @@ namespace REGoth
return {};
}

return bs::SpriteTexture::create(t);
bs::HSpriteTexture hsprite = bs::SpriteTexture::create(t);

mSprites[originalFileName] = hsprite;

return hsprite;
}

OriginalGameResources& gOriginalGameResources()
Expand Down
11 changes: 11 additions & 0 deletions src/original-content/OriginalGameResources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ namespace REGoth
* @return Sprite with the given texture. Empty handle if loading failed.
*/
bs::HSpriteTexture sprite(const bs::String& originalFileName);

private:
/**
* Caches so that we don't have to re-load resources.
*/
bs::Map<bs::String, bs::HTexture> mTextures;
bs::Map<bs::String, BsZenLib::Res::HModelScriptFile> mModelScripts;
bs::Map<bs::String, BsZenLib::Res::HMeshWithMaterials> mStaticMeshes;
bs::Map<bs::String, BsZenLib::Res::HMeshWithMaterials> mMorphMeshes;
bs::Map<bs::String, bs::HFont> mFonts;
bs::Map<bs::String, bs::HSpriteTexture> mSprites;
};

/**
Expand Down

0 comments on commit e080948

Please sign in to comment.