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

Basic Material Creation
#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

Texture-based Material
#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

Assigning Material to 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

Material()

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
Material( std::shared_ptr<Texture> albedo, std::shared_ptr<Texture> normal = nullptr, std::shared_ptr<Texture> roughness = nullptr, std::shared_ptr<Texture> metallic = nullptr, const glm::vec3& albedoColor = glm::vec3(1.0f), float shininess = 32.0f, float roughnessValue = 0.5f, float metallicValue = 0.0f, float fresnel = 1.0f, float scatt = 0.0f )

Constructs a material with specified textures and properties.

Parameters:

  • albedo - Albedo (diffuse) texture
  • normal - Normal map texture
  • roughness - Roughness texture
  • metallic - Metallic texture
  • albedoColor - Base color when no albedo texture is provided
  • shininess - 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 coefficient
  • scatt - Scattering factor for subsurface scattering

Texture Management Methods

void setAlbedo(std::shared_ptr<Texture> texture)

Sets the albedo (diffuse) texture of the material.

Parameters:

  • texture - The texture to set as albedo
void setNormal(std::shared_ptr<Texture> texture)

Sets the normal map texture of the material.

Parameters:

  • texture - The texture to set as normal map
void setRoughness(std::shared_ptr<Texture> texture)

Sets the roughness texture of the material.

Parameters:

  • texture - The texture to set as roughness map
void setMetallic(std::shared_ptr<Texture> texture)

Sets the metallic texture of the material.

Parameters:

  • texture - The texture to set as metallic map
void deleteAlbedo()

Removes the albedo texture from the material.

void deleteNormal()

Removes the normal texture from the material.

void deleteRoughness()

Removes the roughness texture from the material.

void deleteMetallic()

Removes the metallic texture from the material.

Texture Retrieval Methods

std::shared_ptr<Texture> getAlbedoText()

Gets the albedo (diffuse) texture of the material.

Returns: A shared pointer to the albedo texture, or nullptr if none is assigned

std::shared_ptr<Texture> getNormalText()

Gets the normal map texture of the material.

Returns: A shared pointer to the normal texture, or nullptr if none is assigned

std::shared_ptr<Texture> getRoughnessText()

Gets the roughness texture of the material.

Returns: A shared pointer to the roughness texture, or nullptr if none is assigned

std::shared_ptr<Texture> getMetallicText()

Gets the metallic texture of the material.

Returns: A shared pointer to the metallic texture, or nullptr if none is assigned

Property Setting Methods

void setAlbedoColor(const glm::vec3& color)

Sets the albedo color of the material, used when no albedo texture is provided.

Parameters:

  • color - The color to set as albedo
void setShininess(float value)

Sets the shininess value of the material.

Parameters:

  • value - The shininess value
void setRoughness(float 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)
void setMetallic(float value)

Sets the metallic value of the material, used when no metallic texture is provided.

Parameters:

  • value - The metallic value (0.0 to 1.0)
void setFresnel(float value)

Sets the Fresnel coefficient of the material.

Parameters:

  • value - The Fresnel value
void setScattering(float value)

Sets the subsurface scattering factor of the material.

Parameters:

  • value - The scattering value (0.0 to 1.0)

Property Retrieval Methods

glm::vec3 getAlbedoColor()

Gets the albedo color of the material.

Returns: The albedo color as a glm::vec3

float getShininessValue()

Gets the shininess value of the material.

Returns: The shininess value

float getRoughnessValue()

Gets the roughness value of the material.

Returns: The roughness value (0.0 to 1.0)

float getMetallicValue()

Gets the metallic value of the material.

Returns: The metallic value (0.0 to 1.0)

float getFresnelValue()

Gets the Fresnel coefficient of the material.

Returns: The Fresnel value

float getScatteringValue()

Gets the subsurface scattering factor of the material.

Returns: The scattering value (0.0 to 1.0)

int GetNTextures()

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:

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:

By combining these properties, Raftel Engine can simulate a wide range of materials, from smooth metals like chrome to rough dielectrics like concrete.

Common Material Presets
// 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