Graphics: Add support of skins to InstancedRenderable + big clean up

This commit is contained in:
Lynix
2017-01-09 01:52:00 +01:00
parent d2ee4744a9
commit 6b949afb9b
21 changed files with 579 additions and 504 deletions

View File

@@ -27,11 +27,8 @@ namespace Nz
void Billboard::AddToRenderQueue(AbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
{
if (!m_material)
return;
Nz::Vector3f position = instanceData.transformMatrix.GetTranslation();
renderQueue->AddBillboards(instanceData.renderOrder, m_material, 1, &position, &m_size, &m_sinCos, &m_color);
renderQueue->AddBillboards(instanceData.renderOrder, GetMaterial(), 1, &position, &m_size, &m_sinCos, &m_color);
}
/*

View File

@@ -77,7 +77,6 @@ namespace Nz
return false;
}
model->Reset();
model->SetMesh(mesh);
if (parameters.loadMaterials)
@@ -114,7 +113,6 @@ namespace Nz
return false;
}
model->Reset();
model->SetMesh(mesh);
if (parameters.loadMaterials)

View File

@@ -41,26 +41,9 @@ namespace Nz
}
/*!
* \brief Constructs a Model object by default
* \brief Destructs the object and cleans resources
*/
Model::Model() :
m_matCount(0),
m_skin(0),
m_skinCount(1)
{
}
/*!
* \brief Destructs the object and calls Reset
*
* \see Reset
*/
Model::~Model()
{
Reset();
}
Model::~Model() = default;
/*!
* \brief Adds this model to the render queue
@@ -75,7 +58,7 @@ namespace Nz
for (unsigned int i = 0; i < submeshCount; ++i)
{
const StaticMesh* mesh = static_cast<const StaticMesh*>(m_mesh->GetSubMesh(i));
Material* material = m_materials[mesh->GetMaterialIndex()];
const MaterialRef& material = GetMaterial(mesh->GetMaterialIndex());
MeshData meshData;
meshData.indexBuffer = mesh->GetIndexBuffer();
@@ -96,54 +79,20 @@ namespace Nz
* \remark Produces a NazaraError if there is no subMesh with that name
* \remark Produces a NazaraError if material is invalid
*/
Material* Model::GetMaterial(const String& subMeshName) const
const MaterialRef& Model::GetMaterial(const String& subMeshName) const
{
#if NAZARA_GRAPHICS_SAFE
if (!m_mesh)
{
NazaraError("Model has no mesh");
return nullptr;
}
#endif
NazaraAssert(m_mesh, "Model has no mesh");
SubMesh* subMesh = m_mesh->GetSubMesh(subMeshName);
if (!subMesh)
{
NazaraError("Mesh has no submesh \"" + subMeshName + '"');
return nullptr;
static MaterialRef Invalid;
return Invalid;
}
unsigned int matIndex = subMesh->GetMaterialIndex();
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount) + ')');
return nullptr;
}
return m_materials[m_skin * m_matCount + matIndex];
}
/*!
* \brief Gets the material by index
* \return Pointer to the current material
*
* \param matIndex Index of the material
*
* \remark Produces a NazaraError if index is invalid
*/
Material* Model::GetMaterial(unsigned int matIndex) const
{
#if NAZARA_GRAPHICS_SAFE
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount) + ')');
return nullptr;
}
#endif
return m_materials[m_skin * m_matCount + matIndex];
return GetMaterial(subMesh->GetMaterialIndex());
}
/*!
@@ -157,72 +106,20 @@ namespace Nz
* \remark Produces a NazaraError if there is no subMesh with that name
* \remark Produces a NazaraError if material index is invalid
*/
Material* Model::GetMaterial(unsigned int skinIndex, const String& subMeshName) const
const MaterialRef& Model::GetMaterial(std::size_t skinIndex, const String& subMeshName) const
{
#if NAZARA_GRAPHICS_SAFE
if (skinIndex >= m_skinCount)
{
NazaraError("Skin index out of range (" + String::Number(skinIndex) + " >= " + String::Number(m_skinCount) + ')');
return nullptr;
}
#endif
NazaraAssert(m_mesh, "Model has no mesh");
SubMesh* subMesh = m_mesh->GetSubMesh(subMeshName);
if (!subMesh)
{
NazaraError("Mesh has no submesh \"" + subMeshName + '"');
return nullptr;
static MaterialRef Invalid;
return Invalid;
}
unsigned int matIndex = subMesh->GetMaterialIndex();
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount) + ')');
return nullptr;
}
return m_materials[skinIndex * m_matCount + matIndex];
}
/*!
* \brief Gets the material by index with skin
* \return Pointer to the current material
*
* \param skinIndex Index of the skin
* \param matIndex Index of the material
*
* \remark Produces a NazaraError with NAZARA_GRAPHICS_SAFE defined if skinIndex is invalid
* \remark Produces a NazaraError with NAZARA_GRAPHICS_SAFE defined if matIndex is invalid
*/
Material* Model::GetMaterial(unsigned int skinIndex, unsigned int matIndex) const
{
#if NAZARA_GRAPHICS_SAFE
if (skinIndex >= m_skinCount)
{
NazaraError("Skin index out of range (" + String::Number(skinIndex) + " >= " + String::Number(m_skinCount) + ')');
return nullptr;
}
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount) + ')');
return nullptr;
}
#endif
return m_materials[skinIndex * m_matCount + matIndex];
}
/*!
* \brief Gets the number of materials
* \return Current number of materials
*/
unsigned int Model::GetMaterialCount() const
{
return m_matCount;
return GetMaterial(subMesh->GetMaterialIndex());
}
/*!
@@ -235,26 +132,6 @@ namespace Nz
return m_mesh;
}
/*!
* \brief Gets the skin
* \return Current skin
*/
unsigned int Model::GetSkin() const
{
return m_skin;
}
/*!
* \brief Gets the number of skins
* \return Current number of skins
*/
unsigned int Model::GetSkinCount() const
{
return m_skinCount;
}
/*!
* \brief Checks whether the model is animated
* \return false
@@ -305,22 +182,6 @@ namespace Nz
return ModelLoader::LoadFromStream(this, stream, params);
}
/*!
* \brief Resets the model, cleans everything
*/
void Model::Reset()
{
m_matCount = 0;
m_skinCount = 0;
if (m_mesh)
{
m_mesh.Reset();
m_materials.clear();
}
}
/*!
* \brief Sets the material of the named submesh
* \return true If successful
@@ -332,7 +193,7 @@ namespace Nz
* \remark Produces a NazaraError if material index is invalid
*/
bool Model::SetMaterial(const String& subMeshName, Material* material)
bool Model::SetMaterial(const String& subMeshName, MaterialRef material)
{
SubMesh* subMesh = m_mesh->GetSubMesh(subMeshName);
if (!subMesh)
@@ -341,51 +202,10 @@ namespace Nz
return false;
}
unsigned int matIndex = subMesh->GetMaterialIndex();
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount) + ')');
return false;
}
unsigned int index = m_skin * m_matCount + matIndex;
if (material)
m_materials[index] = material;
else
m_materials[index] = Material::GetDefault();
SetMaterial(subMesh->GetMaterialIndex(), std::move(material));
return true;
}
/*!
* \brief Sets the material by index
* \return true If successful
*
* \param matIndex Index of the material
* \param material Pointer to the material
*
* \remark Produces a NazaraError with if NAZARA_GRAPHICS_SAFE defined index is invalid
*/
void Model::SetMaterial(unsigned int matIndex, Material* material)
{
#if NAZARA_GRAPHICS_SAFE
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount));
return;
}
#endif
unsigned int index = m_skin * m_matCount + matIndex;
if (material)
m_materials[index] = material;
else
m_materials[index] = Material::GetDefault();
}
/*!
* \brief Sets the material by index of the named submesh
* \return true If successful
@@ -398,17 +218,8 @@ namespace Nz
* \remark Produces a NazaraError if there is no subMesh with that name
* \remark Produces a NazaraError if material index is invalid
*/
bool Model::SetMaterial(unsigned int skinIndex, const String& subMeshName, Material* material)
bool Model::SetMaterial(std::size_t skinIndex, const String& subMeshName, MaterialRef material)
{
#if NAZARA_GRAPHICS_SAFE
if (skinIndex >= m_skinCount)
{
NazaraError("Skin index out of range (" + String::Number(skinIndex) + " >= " + String::Number(m_skinCount));
return false;
}
#endif
SubMesh* subMesh = m_mesh->GetSubMesh(subMeshName);
if (!subMesh)
{
@@ -416,59 +227,10 @@ namespace Nz
return false;
}
unsigned int matIndex = subMesh->GetMaterialIndex();
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount));
return false;
}
unsigned int index = skinIndex * m_matCount + matIndex;
if (material)
m_materials[index] = material;
else
m_materials[index] = Material::GetDefault();
SetMaterial(skinIndex, subMesh->GetMaterialIndex(), std::move(material));
return true;
}
/*!
* \brief Sets the material by index with skin
* \return true If successful
*
* \param skinIndex Index of the skin
* \param matIndex Index of the material
* \param material Pointer to the material
*
* \remark Produces a NazaraError with NAZARA_GRAPHICS_SAFE defined if skinIndex is invalid
* \remark Produces a NazaraError with NAZARA_GRAPHICS_SAFE defined if matIndex is invalid
*/
void Model::SetMaterial(unsigned int skinIndex, unsigned int matIndex, Material* material)
{
#if NAZARA_GRAPHICS_SAFE
if (skinIndex >= m_skinCount)
{
NazaraError("Skin index out of range (" + String::Number(skinIndex) + " >= " + String::Number(m_skinCount));
return;
}
if (matIndex >= m_matCount)
{
NazaraError("Material index out of range (" + String::Number(matIndex) + " >= " + String::Number(m_matCount));
return;
}
#endif
unsigned int index = skinIndex * m_matCount + matIndex;
if (material)
m_materials[index] = material;
else
m_materials[index] = Material::GetDefault();
}
/*!
* \brief Sets the mesh
*
@@ -490,65 +252,13 @@ namespace Nz
m_mesh = mesh;
if (m_mesh)
{
m_matCount = mesh->GetMaterialCount();
m_materials.clear();
m_materials.resize(m_matCount, Material::GetDefault());
m_skinCount = 1;
}
ResetMaterials(mesh->GetMaterialCount());
else
{
m_matCount = 0;
m_materials.clear();
m_skinCount = 0;
}
ResetMaterials(0);
InvalidateBoundingVolume();
}
/*!
* \brief Sets the skin
*
* \param skin Skin to use
*
* \remark Produces a NazaraError if skin is invalid
*/
void Model::SetSkin(unsigned int skin)
{
#if NAZARA_GRAPHICS_SAFE
if (skin >= m_skinCount)
{
NazaraError("Skin index out of range (" + String::Number(skin) + " >= " + String::Number(m_skinCount) + ')');
return;
}
#endif
m_skin = skin;
}
/*!
* \brief Sets the number of skins
*
* \param skinCount Number of skins
*
* \remark Produces a NazaraError if skinCount equals zero
*/
void Model::SetSkinCount(unsigned int skinCount)
{
#if NAZARA_GRAPHICS_SAFE
if (skinCount == 0)
{
NazaraError("Skin count must be over zero");
return;
}
#endif
m_materials.resize(m_matCount*skinCount, Material::GetDefault());
m_skinCount = skinCount;
}
/*
* \brief Makes the bounding volume of this billboard
*/

View File

@@ -62,7 +62,7 @@ namespace Nz
for (unsigned int i = 0; i < submeshCount; ++i)
{
const SkeletalMesh* mesh = static_cast<const SkeletalMesh*>(m_mesh->GetSubMesh(i));
const Material* material = m_materials[mesh->GetMaterialIndex()];
const Material* material = GetMaterial(mesh->GetMaterialIndex());
MeshData meshData;
meshData.indexBuffer = mesh->GetIndexBuffer();
@@ -261,17 +261,6 @@ namespace Nz
return SkeletalModelLoader::LoadFromStream(this, stream, params);
}
/*!
* \brief Resets the model
*/
void SkeletalModel::Reset()
{
Model::Reset();
m_skeleton.Destroy();
}
/*!
* \brief Sets the animation for the model
* \return true If successful

View File

@@ -26,11 +26,8 @@ namespace Nz
void Sprite::AddToRenderQueue(AbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
{
if (!m_material)
return;
const VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<const VertexStruct_XYZ_Color_UV*>(instanceData.data.data());
renderQueue->AddSprites(instanceData.renderOrder, m_material, vertices, 1);
renderQueue->AddSprites(instanceData.renderOrder, GetMaterial(), vertices, 1);
}
/*!
@@ -73,7 +70,37 @@ namespace Nz
}
/*!
* \brief Sets the texture of the sprite from a name
* \brief Sets the material of the sprite from a name for a specific skin
*
* Tries to get a material from the MaterialLibrary and then the MaterialManager (which will treat the name as a path)
* Fails if the texture name is not a part of the MaterialLibrary nor the MaterialManager (which fails if it couldn't load the texture from its filepath)
*
* \param skinIndex Skin index to change
* \param materialName Named texture for the material
* \param resizeSprite Should the sprite be resized to the material diffuse map size?
*
* \return True if the material was found or loaded from its name/path, false if it couldn't
*/
bool Sprite::SetMaterial(std::size_t skinIndex, String materialName, bool resizeSprite)
{
MaterialRef material = MaterialLibrary::Query(materialName);
if (!material)
{
material = MaterialManager::Get(materialName);
if (!material)
{
NazaraError("Failed to get material \"" + materialName + "\"");
return false;
}
}
SetMaterial(skinIndex, std::move(material), resizeSprite);
return true;
}
/*!
* \brief Sets the texture of the sprite from a name for the current skin
* \return True if the texture was found or loaded from its name/path, false if it couldn't
*
* Tries to get a texture from the TextureLibrary and then the TextureManager (which will treat the name as a path)
* Fails if the texture name is not a part of the TextureLibrary nor the TextureManager (which fails if it couldn't load the texture from its filepath)
@@ -81,8 +108,6 @@ namespace Nz
* \param textureName Named texture for the sprite
* \param resizeSprite Should the sprite be resized to the texture size?
*
* \return True if the texture was found or loaded from its name/path, false if it couldn't
*
* \remark The sprite material gets copied to prevent accidentally changing other drawable materials
*/
bool Sprite::SetTexture(String textureName, bool resizeSprite)
@@ -102,6 +127,36 @@ namespace Nz
return true;
}
/*!
* \brief Sets the texture of the sprite from a name for a specific skin
* \return True if the texture was found or loaded from its name/path, false if it couldn't
*
* Tries to get a texture from the TextureLibrary and then the TextureManager (which will treat the name as a path)
* Fails if the texture name is not a part of the TextureLibrary nor the TextureManager (which fails if it couldn't load the texture from its filepath)
*
* \param skinIndex Named texture for the sprite
* \param textureName Named texture for the sprite
* \param resizeSprite Should the sprite be resized to the texture size?
*
* \remark The sprite material gets copied to prevent accidentally changing other drawable materials
*/
bool Sprite::SetTexture(std::size_t skinIndex, String textureName, bool resizeSprite)
{
TextureRef texture = TextureLibrary::Query(textureName);
if (!texture)
{
texture = TextureManager::Get(textureName);
if (!texture)
{
NazaraError("Failed to get texture \"" + textureName + "\"");
return false;
}
}
SetTexture(skinIndex, std::move(texture), resizeSprite);
return true;
}
/*!
* \brief Updates the data of the sprite
*

View File

@@ -28,9 +28,6 @@ namespace Nz
void TextSprite::AddToRenderQueue(AbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
{
if (!m_material)
return;
for (auto& pair : m_renderInfos)
{
Texture* overlay = pair.first;
@@ -39,7 +36,7 @@ namespace Nz
if (indices.count > 0)
{
const VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<const VertexStruct_XYZ_Color_UV*>(instanceData.data.data());
renderQueue->AddSprites(instanceData.renderOrder, m_material, &vertices[indices.first * 4], indices.count, overlay);
renderQueue->AddSprites(instanceData.renderOrder, GetMaterial(), &vertices[indices.first * 4], indices.count, overlay);
}
}
}

View File

@@ -29,11 +29,11 @@ namespace Nz
{
const VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<const VertexStruct_XYZ_Color_UV*>(instanceData.data.data());
std::size_t matCount = 0;
std::size_t spriteCount = 0;
for (const Layer& layer : m_layers)
{
if (layer.material)
renderQueue->AddSprites(instanceData.renderOrder, layer.material, &vertices[spriteCount], layer.tiles.size());
renderQueue->AddSprites(instanceData.renderOrder, GetMaterial(matCount++), &vertices[spriteCount], layer.tiles.size());
spriteCount += layer.tiles.size();
}