Refactor the way you set material properties

Instead	of using SMaterial::setFlag, you now set them directly on SMaterial or SMaterialLayer.
This commit is contained in:
Gregor Parzefall 2023-06-23 11:33:29 +02:00 committed by sfan5
parent b249e4523d
commit 9e0189019e
9 changed files with 48 additions and 243 deletions

View File

@ -100,10 +100,12 @@ int main(int argc, char *argv[])
scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh); scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
if (node) if (node)
{ {
node->setMaterialFlag(video::EMF_LIGHTING, false); node->forEachMaterial([tex] (video::SMaterial &mat) {
mat.Lighting = false;
mat.setTexture(0, tex);
});
node->setFrameLoop(0, 29); node->setFrameLoop(0, 29);
node->setAnimationSpeed(30); node->setAnimationSpeed(30);
node->setMaterialTexture(0, tex);
} }
} }

View File

@ -13,84 +13,70 @@ namespace video
//! Material flags //! Material flags
enum E_MATERIAL_FLAG enum E_MATERIAL_FLAG
{ {
//! Draw as wireframe or filled triangles? Default: false //! Corresponds to SMaterial::Wireframe.
EMF_WIREFRAME = 0x1, EMF_WIREFRAME = 0x1,
//! Draw as point cloud or filled triangles? Default: false //! Corresponds to SMaterial::PointCloud.
EMF_POINTCLOUD = 0x2, EMF_POINTCLOUD = 0x2,
//! Flat or Gouraud shading? Default: true //! Corresponds to SMaterial::GouraudShading.
EMF_GOURAUD_SHADING = 0x4, EMF_GOURAUD_SHADING = 0x4,
//! Will this material be lighted? Default: true //! Corresponds to SMaterial::Lighting.
EMF_LIGHTING = 0x8, EMF_LIGHTING = 0x8,
//! Is the ZBuffer enabled? Default: true //! Corresponds to SMaterial::ZBuffer.
EMF_ZBUFFER = 0x10, EMF_ZBUFFER = 0x10,
//! May be written to the zbuffer or is it readonly. Default: true //! Corresponds to SMaterial::ZWriteEnable.
/** This flag is ignored, if the material type is a transparent type. */
EMF_ZWRITE_ENABLE = 0x20, EMF_ZWRITE_ENABLE = 0x20,
//! Is backface culling enabled? Default: true //! Corresponds to SMaterial::BackfaceCulling.
EMF_BACK_FACE_CULLING = 0x40, EMF_BACK_FACE_CULLING = 0x40,
//! Is frontface culling enabled? Default: false //! Corresponds to SMaterial::FrontfaceCulling.
/** Overrides EMF_BACK_FACE_CULLING if both are enabled. */
EMF_FRONT_FACE_CULLING = 0x80, EMF_FRONT_FACE_CULLING = 0x80,
//! Is bilinear filtering enabled? Default: true //! Corresponds to SMaterialLayer::BilinearFilter.
EMF_BILINEAR_FILTER = 0x100, EMF_BILINEAR_FILTER = 0x100,
//! Is trilinear filtering enabled? Default: false //! Corresponds to SMaterialLayer::TrilinearFilter.
/** If the trilinear filter flag is enabled,
the bilinear filtering flag is ignored. */
EMF_TRILINEAR_FILTER = 0x200, EMF_TRILINEAR_FILTER = 0x200,
//! Is anisotropic filtering? Default: false //! Corresponds to SMaterialLayer::AnisotropicFilter.
/** In Irrlicht you can use anisotropic texture filtering in
conjunction with bilinear or trilinear texture filtering
to improve rendering results. Primitives will look less
blurry with this flag switched on. */
EMF_ANISOTROPIC_FILTER = 0x400, EMF_ANISOTROPIC_FILTER = 0x400,
//! Is fog enabled? Default: false //! Corresponds to SMaterial::FogEnable.
EMF_FOG_ENABLE = 0x800, EMF_FOG_ENABLE = 0x800,
//! Normalizes normals. Default: false //! Corresponds to SMaterial::NormalizeNormals.
/** You can enable this if you need to scale a dynamic lighted
model. Usually, its normals will get scaled too then and it
will get darker. If you enable the EMF_NORMALIZE_NORMALS flag,
the normals will be normalized again, and the model will look
as bright as it should. */
EMF_NORMALIZE_NORMALS = 0x1000, EMF_NORMALIZE_NORMALS = 0x1000,
//! Access to all layers texture wrap settings. Overwrites separate layer settings. //! Corresponds to SMaterialLayer::TextureWrapU, TextureWrapV and
/** Note that if you want to change TextureWrapU, TextureWrapV, TextureWrapW //! TextureWrapW.
independently, then you can't work with this flag, but will have to set the variables
directly. */
EMF_TEXTURE_WRAP = 0x2000, EMF_TEXTURE_WRAP = 0x2000,
//! AntiAliasing mode //! Corresponds to SMaterial::AntiAliasing.
EMF_ANTI_ALIASING = 0x4000, EMF_ANTI_ALIASING = 0x4000,
//! ColorMask bits, for enabling the color planes //! Corresponds to SMaterial::ColorMask.
EMF_COLOR_MASK = 0x8000, EMF_COLOR_MASK = 0x8000,
//! ColorMaterial enum for vertex color interpretation //! Corresponds to SMaterial::ColorMaterial.
EMF_COLOR_MATERIAL = 0x10000, EMF_COLOR_MATERIAL = 0x10000,
//! Flag for enabling/disabling mipmap usage //! Corresponds to SMaterial::UseMipMaps.
EMF_USE_MIP_MAPS = 0x20000, EMF_USE_MIP_MAPS = 0x20000,
//! Flag for blend operation //! Corresponds to SMaterial::BlendOperation.
EMF_BLEND_OPERATION = 0x40000, EMF_BLEND_OPERATION = 0x40000,
//! Flag for polygon offset //! Corresponds to SMaterial::PolygonOffsetFactor, PolygonOffsetDirection,
//! PolygonOffsetDepthBias and PolygonOffsetSlopeScale.
EMF_POLYGON_OFFSET = 0x80000, EMF_POLYGON_OFFSET = 0x80000,
//! Flag for blend factor //! Corresponds to SMaterial::BlendFactor.
EMF_BLEND_FACTOR = 0x160000 EMF_BLEND_FACTOR = 0x100000,
}; };
} // end namespace video } // end namespace video

View File

@ -97,11 +97,6 @@ namespace scene
/** \param box New bounding box to use for the mesh. */ /** \param box New bounding box to use for the mesh. */
virtual void setBoundingBox( const core::aabbox3df& box) = 0; virtual void setBoundingBox( const core::aabbox3df& box) = 0;
//! Sets a flag of all contained materials to a new value.
/** \param flag: Flag to set in all materials.
\param newvalue: New value to set in all materials. */
virtual void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue) = 0;
//! Set the hardware mapping hint //! Set the hardware mapping hint
/** This methods allows to define optimization hints for the /** This methods allows to define optimization hints for the
hardware. This enables, e.g., the use of hardware buffers on hardware. This enables, e.g., the use of hardware buffers on

View File

@ -352,38 +352,14 @@ namespace scene
} }
//! Sets all material flags at once to a new value. //! Execute a function on all materials of this scene node.
/** Useful, for example, if you want the whole mesh to be /** Useful for setting material properties, e.g. if you want the whole
affected by light. mesh to be affected by light. */
\param flag Which flag of all materials to be set. template <typename F>
\param newvalue New value of that flag. */ void forEachMaterial(F &&fn) {
void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue) for (u32 i = 0; i < getMaterialCount(); i++) {
{ fn(getMaterial(i));
for (u32 i=0; i<getMaterialCount(); ++i)
getMaterial(i).setFlag(flag, newvalue);
} }
//! Sets the texture of the specified layer in all materials of this scene node to the new texture.
/** \param textureLayer Layer of texture to be set. Must be a
value smaller than MATERIAL_MAX_TEXTURES.
\param texture New texture to be used. */
void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
{
if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
return;
for (u32 i=0; i<getMaterialCount(); ++i)
getMaterial(i).setTexture(textureLayer, texture);
}
//! Sets the material type of all materials in this scene node to a new material type.
/** \param newType New type of material to be set. */
void setMaterialType(video::E_MATERIAL_TYPE newType)
{
for (u32 i=0; i<getMaterialCount(); ++i)
getMaterial(i).MaterialType = newType;
} }

View File

@ -147,13 +147,6 @@ namespace scene
return Meshes[0]->getMeshBuffer(material); return Meshes[0]->getMeshBuffer(material);
} }
//! Set a material flag for all meshbuffers of this mesh.
void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue) override
{
for (u32 i=0; i<Meshes.size(); ++i)
Meshes[i]->setMaterialFlag(flag, newvalue);
}
//! set the hardware mapping hint, for driver //! set the hardware mapping hint, for driver
void setHardwareMappingHint( E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) override void setHardwareMappingHint( E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) override
{ {

View File

@ -256,7 +256,7 @@ namespace video
EZW_OFF = 0, EZW_OFF = 0,
//! This is the default setting for SMaterial and tries to handle things automatically. //! This is the default setting for SMaterial and tries to handle things automatically.
//! This is also the value which is set when SMaterial::setFlag(EMF_ZWRITE_ENABLE) is enabled. //! This is what you want to set to enable zwriting.
//! Usually zwriting is enabled non-transparent materials - as far as Irrlicht can recognize those. //! Usually zwriting is enabled non-transparent materials - as far as Irrlicht can recognize those.
//! Basically Irrlicht tries to handle the zwriting for you and assumes transparent materials don't need it. //! Basically Irrlicht tries to handle the zwriting for you and assumes transparent materials don't need it.
//! This is addionally affected by IVideoDriver::setAllowZWriteOnTransparent //! This is addionally affected by IVideoDriver::setAllowZWriteOnTransparent
@ -426,9 +426,7 @@ namespace video
f32 PolygonOffsetSlopeScale; f32 PolygonOffsetSlopeScale;
//! Draw as wireframe or filled triangles? Default: false //! Draw as wireframe or filled triangles? Default: false
/** The user can access a material flag using /** The user can access a material flag using \code material.Wireframe = true; \endcode */
\code material.Wireframe=true \endcode
or \code material.setFlag(EMF_WIREFRAME, true); \endcode */
bool Wireframe:1; bool Wireframe:1;
//! Draw as point cloud or filled triangles? Default: false //! Draw as point cloud or filled triangles? Default: false
@ -462,6 +460,16 @@ namespace video
/** Sometimes, disabling mipmap usage can be useful. Default: true */ /** Sometimes, disabling mipmap usage can be useful. Default: true */
bool UseMipMaps:1; bool UseMipMaps:1;
//! Execute a function on all texture layers.
/** Useful for setting properties which are not per material, but per
texture layer, e.g. bilinear filtering. */
template <typename F>
void forEachTexture(F &&fn) {
for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; i++) {
fn(TextureLayer[i]);
}
}
//! Gets the texture transformation matrix for level i //! Gets the texture transformation matrix for level i
/** \param i The desired level. Must not be larger than MATERIAL_MAX_TEXTURES /** \param i The desired level. Must not be larger than MATERIAL_MAX_TEXTURES
\return Texture matrix for texture level i. */ \return Texture matrix for texture level i. */
@ -510,143 +518,6 @@ namespace video
TextureLayer[i].Texture = tex; TextureLayer[i].Texture = tex;
} }
//! Sets the Material flag to the given value
/** \param flag The flag to be set.
\param value The new value for the flag. */
void setFlag(E_MATERIAL_FLAG flag, bool value)
{
switch (flag)
{
case EMF_WIREFRAME:
Wireframe = value; break;
case EMF_POINTCLOUD:
PointCloud = value; break;
case EMF_GOURAUD_SHADING:
GouraudShading = value; break;
case EMF_LIGHTING:
Lighting = value; break;
case EMF_ZBUFFER:
ZBuffer = value; break;
case EMF_ZWRITE_ENABLE:
ZWriteEnable = value ? EZW_AUTO : EZW_OFF; break;
case EMF_BACK_FACE_CULLING:
BackfaceCulling = value; break;
case EMF_FRONT_FACE_CULLING:
FrontfaceCulling = value; break;
case EMF_BILINEAR_FILTER:
{
for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
TextureLayer[i].BilinearFilter = value;
}
break;
case EMF_TRILINEAR_FILTER:
{
for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
TextureLayer[i].TrilinearFilter = value;
}
break;
case EMF_ANISOTROPIC_FILTER:
{
if (value)
for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
TextureLayer[i].AnisotropicFilter = 0xFF;
else
for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
TextureLayer[i].AnisotropicFilter = 0;
}
break;
case EMF_FOG_ENABLE:
FogEnable = value; break;
case EMF_NORMALIZE_NORMALS:
NormalizeNormals = value; break;
case EMF_TEXTURE_WRAP:
{
for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
{
TextureLayer[i].TextureWrapU = (E_TEXTURE_CLAMP)value;
TextureLayer[i].TextureWrapV = (E_TEXTURE_CLAMP)value;
TextureLayer[i].TextureWrapW = (E_TEXTURE_CLAMP)value;
}
}
break;
case EMF_ANTI_ALIASING:
AntiAliasing = value?EAAM_SIMPLE:EAAM_OFF; break;
case EMF_COLOR_MASK:
ColorMask = value?ECP_ALL:ECP_NONE; break;
case EMF_COLOR_MATERIAL:
ColorMaterial = value?ECM_DIFFUSE:ECM_NONE; break;
case EMF_USE_MIP_MAPS:
UseMipMaps = value; break;
case EMF_BLEND_OPERATION:
BlendOperation = value?EBO_ADD:EBO_NONE; break;
case EMF_BLEND_FACTOR:
break;
case EMF_POLYGON_OFFSET:
PolygonOffsetFactor = value?1:0;
PolygonOffsetDirection = EPO_BACK;
PolygonOffsetSlopeScale = value?1.f:0.f;
PolygonOffsetDepthBias = value?1.f:0.f;
default:
break;
}
}
//! Gets the Material flag
/** \param flag The flag to query.
\return The current value of the flag. */
bool getFlag(E_MATERIAL_FLAG flag) const
{
switch (flag)
{
case EMF_WIREFRAME:
return Wireframe;
case EMF_POINTCLOUD:
return PointCloud;
case EMF_GOURAUD_SHADING:
return GouraudShading;
case EMF_LIGHTING:
return Lighting;
case EMF_ZBUFFER:
return ZBuffer!=ECFN_DISABLED;
case EMF_ZWRITE_ENABLE:
return ZWriteEnable != EZW_OFF;
case EMF_BACK_FACE_CULLING:
return BackfaceCulling;
case EMF_FRONT_FACE_CULLING:
return FrontfaceCulling;
case EMF_BILINEAR_FILTER:
return TextureLayer[0].BilinearFilter;
case EMF_TRILINEAR_FILTER:
return TextureLayer[0].TrilinearFilter;
case EMF_ANISOTROPIC_FILTER:
return TextureLayer[0].AnisotropicFilter!=0;
case EMF_FOG_ENABLE:
return FogEnable;
case EMF_NORMALIZE_NORMALS:
return NormalizeNormals;
case EMF_TEXTURE_WRAP:
return !(TextureLayer[0].TextureWrapU ||
TextureLayer[0].TextureWrapV ||
TextureLayer[0].TextureWrapW);
case EMF_ANTI_ALIASING:
return (AntiAliasing==1);
case EMF_COLOR_MASK:
return (ColorMask!=ECP_NONE);
case EMF_COLOR_MATERIAL:
return (ColorMaterial != ECM_NONE);
case EMF_USE_MIP_MAPS:
return UseMipMaps;
case EMF_BLEND_OPERATION:
return BlendOperation != EBO_NONE;
case EMF_BLEND_FACTOR:
return BlendFactor != 0.f;
case EMF_POLYGON_OFFSET:
return PolygonOffsetFactor != 0 || PolygonOffsetDepthBias != 0.f;
}
return false;
}
//! Inequality operator //! Inequality operator
/** \param b Material to compare to. /** \param b Material to compare to.
\return True if the materials differ, else false. */ \return True if the materials differ, else false. */

View File

@ -117,13 +117,6 @@ namespace scene
} }
} }
//! sets a flag of all contained materials to a new value
void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue) override
{
for (u32 i=0; i<MeshBuffers.size(); ++i)
MeshBuffers[i]->getMaterial().setFlag(flag, newvalue);
}
//! set the hardware mapping hint, for driver //! set the hardware mapping hint, for driver
void setHardwareMappingHint( E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) override void setHardwareMappingHint( E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX ) override
{ {

View File

@ -690,14 +690,6 @@ void CSkinnedMesh::setBoundingBox( const core::aabbox3df& box)
} }
//! sets a flag of all contained materials to a new value
void CSkinnedMesh::setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
{
for (u32 i=0; i<LocalBuffers.size(); ++i)
LocalBuffers[i]->Material.setFlag(flag,newvalue);
}
//! set the hardware mapping hint, for driver //! set the hardware mapping hint, for driver
void CSkinnedMesh::setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, void CSkinnedMesh::setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint,
E_BUFFER_TYPE buffer) E_BUFFER_TYPE buffer)

View File

@ -72,9 +72,6 @@ namespace scene
//! set user axis aligned bounding box //! set user axis aligned bounding box
void setBoundingBox( const core::aabbox3df& box) override; void setBoundingBox( const core::aabbox3df& box) override;
//! sets a flag of all contained materials to a new value
void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue) override;
//! set the hardware mapping hint, for driver //! set the hardware mapping hint, for driver
void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX) override; void setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint, E_BUFFER_TYPE buffer=EBT_VERTEX_AND_INDEX) override;