Materials
Materials in Raftel Engine define the surface properties and appearance of 3D objects. Materials control how light interacts with meshes, determining whether surfaces appear shiny, rough, metallic, or matte. Raftel Engine's material system supports both scalar values and textures for various surface properties.
Key Features
- Support for physically-based rendering (PBR) properties
- Multiple texture maps (albedo, normal, roughness, metallic)
- Combined texture and scalar material property system
- Easy integration with meshes and shaders
- Support for subsurface scattering effects
Material Properties Overview
A material in Raftel Engine consists of both textures and scalar values that define its visual appearance:
Albedo (Diffuse)
The base color of the material. This can be specified as a solid color or through a texture map. Albedo represents the color that is reflected when light hits the surface.
Normal Map
A texture that provides surface detail without adding geometry. Normal maps create the illusion of bumps, dents, and other surface irregularities by modifying the direction of surface normals.
Roughness
Defines how rough or smooth a surface is. Rougher surfaces scatter light in multiple directions, creating a diffuse appearance, while smoother surfaces reflect light more consistently, creating a glossy appearance.
Metallic
Determines the metalness of a surface. Metallic surfaces reflect light differently than non-metallic (dielectric) surfaces, with more pronounced specular highlights and tinted reflections.
Shininess
Controls the tightness and intensity of specular highlights. Higher shininess values create smaller, sharper highlights, resembling polished surfaces.
Fresnel
Controls the intensity of reflections at glancing angles. Most surfaces reflect more light as the viewing angle becomes shallower, and the Fresnel value accentuates this effect.
Scattering
Controls subsurface scattering, which simulates light that penetrates the surface of translucent materials and scatters within them. This is essential for materials like skin, wax, or marble.
Basic Usage
Here are some examples of how to create and use materials in Raftel Engine:
Creating a Simple Material
#include "raftel/material.hpp"
// Create a basic material with default properties (white, somewhat rough, non-metallic)
auto material = std::make_shared();
// Customize the material properties
material->setAlbedoColor(glm::vec3(0.9f, 0.1f, 0.1f)); // Red color
material->setRoughness(0.7f); // Fairly rough surface
material->setMetallic(0.2f); // Slight metallic appearance
material->setShininess(24.0f); // Moderate shininess
Creating a Material with Textures
#include "raftel/material.hpp"
#include "raftel/texture.hpp"
// Load texture maps
auto albedoTexture = Raftel::Texture::loadTexture("assets/textures/wood_albedo.jpg");
auto normalTexture = Raftel::Texture::loadTexture("assets/textures/wood_normal.jpg");
auto roughnessTexture = Raftel::Texture::loadTexture("assets/textures/wood_roughness.jpg");
// Create a material with textures
auto woodMaterial = std::make_shared(
albedoTexture,
normalTexture,
roughnessTexture,
nullptr, // No metallic texture
glm::vec3(1.0f), // Default color (only used if no albedo texture)
32.0f, // Shininess
0.8f, // Default roughness (only used if no roughness texture)
0.0f // Default metallic (non-metallic)
);
// Assign the material to a mesh
myMesh->setMaterial(woodMaterial);
Adding a Material to a Mesh
#include "raftel/mesh.hpp"
#include "raftel/material.hpp"
// Create a mesh and a material
auto cubeMesh = Raftel::MeshFactory::createCube(2.0f);
auto material = std::make_shared();
// Configure material
material->setAlbedoColor(glm::vec3(0.2f, 0.5f, 0.8f)); // Blue color
material->setRoughness(0.3f); // Smooth surface
material->setMetallic(0.7f); // Metallic appearance
// Assign the material to the mesh
cubeMesh->setMaterial(material);
// If the mesh has multiple submeshes, you can add more materials
auto secondMaterial = std::make_shared();
secondMaterial->setAlbedoColor(glm::vec3(0.8f, 0.2f, 0.2f)); // Red color
cubeMesh->addMaterial(secondMaterial);
Material Class Reference
Constructors
Default constructor that creates a basic white material with no textures.
Initial values:
- Albedo Color: (1.0, 1.0, 1.0) - White
- Shininess: 16.0
- Roughness: 0.8
- Metallic: 0.0
- Fresnel: 1.0
- Scattering: 0.0
Constructs a material with specified textures and properties.
Parameters:
albedo
- Albedo (diffuse) texturenormal
- Normal map textureroughness
- Roughness texturemetallic
- Metallic texturealbedoColor
- Base color when no albedo texture is providedshininess
- Shininess factor (for Phong lighting model)roughnessValue
- Scalar roughness value (0.0 to 1.0)metallicValue
- Scalar metallic value (0.0 to 1.0)fresnel
- Fresnel reflection coefficientscatt
- Scattering factor for subsurface scattering
Texture Management Methods
Sets the albedo (diffuse) texture of the material.
Parameters:
texture
- The texture to set as albedo
Sets the normal map texture of the material.
Parameters:
texture
- The texture to set as normal map
Sets the roughness texture of the material.
Parameters:
texture
- The texture to set as roughness map
Sets the metallic texture of the material.
Parameters:
texture
- The texture to set as metallic map
Removes the albedo texture from the material.
Removes the normal texture from the material.
Removes the roughness texture from the material.
Removes the metallic texture from the material.
Texture Retrieval Methods
Gets the albedo (diffuse) texture of the material.
Returns: A shared pointer to the albedo texture, or nullptr if none is assigned
Gets the normal map texture of the material.
Returns: A shared pointer to the normal texture, or nullptr if none is assigned
Gets the roughness texture of the material.
Returns: A shared pointer to the roughness texture, or nullptr if none is assigned
Gets the metallic texture of the material.
Returns: A shared pointer to the metallic texture, or nullptr if none is assigned
Property Setting Methods
Sets the albedo color of the material, used when no albedo texture is provided.
Parameters:
color
- The color to set as albedo
Sets the shininess value of the material.
Parameters:
value
- The shininess value
Sets the roughness value of the material, used when no roughness texture is provided.
Parameters:
value
- The roughness value (0.0 to 1.0)
Sets the metallic value of the material, used when no metallic texture is provided.
Parameters:
value
- The metallic value (0.0 to 1.0)
Sets the Fresnel coefficient of the material.
Parameters:
value
- The Fresnel value
Sets the subsurface scattering factor of the material.
Parameters:
value
- The scattering value (0.0 to 1.0)
Property Retrieval Methods
Gets the albedo color of the material.
Returns: The albedo color as a glm::vec3
Gets the shininess value of the material.
Returns: The shininess value
Gets the roughness value of the material.
Returns: The roughness value (0.0 to 1.0)
Gets the metallic value of the material.
Returns: The metallic value (0.0 to 1.0)
Gets the Fresnel coefficient of the material.
Returns: The Fresnel value
Gets the subsurface scattering factor of the material.
Returns: The scattering value (0.0 to 1.0)
Gets the number of textures currently assigned to the material.
Returns: The number of active textures
Material Property Flags
The Material class includes boolean flags that indicate whether specific textures are assigned:
bAlbedo
- True if an albedo texture is assignedbNormal
- True if a normal map texture is assignedbRoughness
- True if a roughness texture is assignedbMetallic
- True if a metallic texture is assigned
These flags can be checked to determine whether to use texture-based or scalar material properties in your shaders.
Physically-Based Rendering (PBR)
Raftel Engine's material system is designed with physically-based rendering (PBR) in mind. PBR is a rendering approach that simulates how light interacts with surfaces in the real world, resulting in more realistic graphics. The key material properties for PBR are:
- Albedo (Base Color) - The color of the surface without any lighting effects
- Roughness - How rough or smooth a surface is, affecting how light scatters
- Metallic - Whether a surface is metallic or dielectric (non-metallic)
- Normal - Surface detail without additional geometry
By combining these properties, Raftel Engine can simulate a wide range of materials, from smooth metals like chrome to rough dielectrics like concrete.
// Create a polished metal material
auto chromeMaterial = std::make_shared();
chromeMaterial->setAlbedoColor(glm::vec3(0.95f, 0.95f, 0.95f));
chromeMaterial->setRoughness(0.05f); // Very smooth
chromeMaterial->setMetallic(1.0f); // Fully metallic
// Create a plastic material
auto plasticMaterial = std::make_shared();
plasticMaterial->setAlbedoColor(glm::vec3(0.2f, 0.8f, 0.2f)); // Green
plasticMaterial->setRoughness(0.4f); // Moderately smooth
plasticMaterial->setMetallic(0.0f); // Non-metallic
// Create a ceramic material
auto ceramicMaterial = std::make_shared();
ceramicMaterial->setAlbedoColor(glm::vec3(0.9f, 0.9f, 0.9f)); // White
ceramicMaterial->setRoughness(0.2f); // Smooth
ceramicMaterial->setMetallic(0.0f); // Non-metallic
ceramicMaterial->setShininess(64.0f); // High shininess
// Create a wood material
auto woodMaterial = std::make_shared();
woodMaterial->setAlbedoColor(glm::vec3(0.65f, 0.45f, 0.25f)); // Brown
woodMaterial->setRoughness(0.9f); // Very rough
woodMaterial->setMetallic(0.0f); // Non-metallic
Best Practices
- Texture Management - Use shared pointers for textures to avoid unnecessary duplicates. Multiple materials can reference the same texture.
- Texture vs. Scalar Properties - Use textures for detailed surfaces and scalar values
for simple or uniform materials. Check the boolean flags (e.g.,
bAlbedo
) to determine whether a texture is available. - PBR Guidelines - Follow physically-based ranges for material properties. Roughness and metallic values should typically be in the 0-1 range.
- Material Complexity - Start with simple materials and add complexity as needed. Often, a well-configured albedo and roughness can achieve good results before adding normal maps or other details.
- LOD Considerations - Consider using simpler materials (fewer textures) for objects that will be viewed from a distance.