AGL  0
3D graphics library
Material Class Reference

Material class for entities. More...

Collaboration diagram for Material:

Public Member Functions

 Material ()
 Creates a material. More...
 
 ~Material ()
 
Material operator+ (const Material &other) const
 Mix two material. More...
 
Materialoperator+= (const Material &other)
 Same as operator+ but modifies the current material. More...
 
Material operator* (const Material &other) const
 Mix two material. More...
 
Materialoperator*= (const Material &other)
 Same as operator* but modifies the current material. More...
 
void setColor (glm::vec4 color=glm::vec4(1))
 Set color for all the components. More...
 
void setColor (float r=1, float g=1, float b=1, float a=1)
 Same as setColor. More...
 
void setColor (const Material &other)
 Sets each color components from another Material. More...
 
void createTexture (const char *path=nullptr)
 Creates a texture. More...
 
virtual std::pair< std::string, std::string > createShader (Entity *e=nullptr, std::vector< Light *> lights=std::vector< Light *>())
 Create a shader algorithmically. More...
 
virtual void setShader (std::string vertexShader, std::string fragmentShader)
 Compile and set the shader for the material. More...
 

Public Attributes

glm::vec4 emission
 Emission, self color of the material. More...
 
glm::vec4 ambient
 Ambient, color due to ambient light. More...
 
glm::vec4 diffuse
 Diffuse, color when light hits a surface. More...
 
glm::vec4 specular
 Specular, color due to the reflection of light. More...
 
glm::vec4 ratios
 
float shininess = 32
 Shininess, amount of light reflected by the material. More...
 
bool lightsEnabled = false
 If true, no lighting calculations are done. More...
 
bool customShader = false
 If true, Scene::prepare do not create shaders. More...
 
int shadingType = AGL_SHADING_PHONG
 Type of shading, currently unused. More...
 
int tex_width = -1
 Width of texture,if used. More...
 
int tex_height = -1
 Height of texture, if used. More...
 
int tex_channel = -1
 Number of channels in texture, if used. More...
 
GLubyte * texture = nullptr
 Pointer to texture data, if present. More...
 
GLuint progID = 0
 program ID More...
 
GLuint tID = 0
 texture ID More...
 
GLuint mvpID
 MVP matrix ID. More...
 
GLuint mID
 model matrix ID More...
 
GLuint nID
 normal matrix ID More...
 
GLuint eID
 emission color ID More...
 
GLuint aID
 ambient color ID More...
 
GLuint dID
 diffuse color ID More...
 
GLuint sID
 specular color ID More...
 
GLuint gID
 shininess color ID More...
 
GLuint vID
 camera position ID More...
 

Static Public Attributes

static const Material emerald = Material(0.0215, 0.1745, 0.0215, 0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 76.8)
 
static const Material jade = Material(0.135, 0.2225, 0.1575, 0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 12.8)
 
static const Material obsidian = Material(0.05375, 0.05, 0.06625, 0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 38.4)
 
static const Material pearl = Material(0.25, 0.20725, 0.20725, 1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 11.264)
 
static const Material ruby = Material(0.1745, 0.01175, 0.01175, 0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 76.8)
 
static const Material turquoise = Material(0.1, 0.18725, 0.1745, 0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 12.8)
 
static const Material brass = Material(0.329412, 0.223529, 0.027451, 0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843, 27.89743616)
 
static const Material bronze = Material(0.2125, 0.1275, 0.054, 0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 25.6)
 
static const Material chrome = Material(0.25, 0.25, 0.25, 0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 76.8)
 
static const Material copper = Material(0.19125, 0.0735, 0.0225, 0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 12.8)
 
static const Material gold = Material(0.24725, 0.1995, 0.0745, 0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 51.2)
 
static const Material silver = Material(0.19225, 0.19225, 0.19225, 0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 51.2)
 
static const Material black_plastic = Material(0, 0, 0, 0.01, 0.01, 0.01, 0.50, 0.50, 0.50, 32)
 
static const Material cyan_plastic = Material(0, 0.1, 0.06, 0, 0.50980392, 0.50980392, 0.50196078, 0.50196078, 0.50196078, 32)
 
static const Material green_plastic = Material(0, 0, 0, 0.1, 0.35, 0.1, 0.45, 0.55, 0.45, 32)
 
static const Material red_plastic = Material(0, 0, 0, 0.5, 0, 0, 0.7, 0.6, 0.6, 32)
 
static const Material white_plastic = Material(0, 0, 0, 0.55, 0.55, 0.55, 0.70, 0.70, 0.70, 32)
 
static const Material yellow_plastic = Material(0, 0, 0, 0.5, 0.5, 0, 0.60, 0.60, 0.50, 32)
 
static const Material black_rubber = Material(0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0.4, 0.4, 0.4, 10)
 
static const Material cyan_rubber = Material(0, 0.05, 0.05, 0.4, 0.5, 0.5, 0.04, 0.7, 0.7, 10)
 
static const Material green_rubber = Material(0, 0.05, 0, 0.4, 0.5, 0.4, 0.04, 0.7, 0.04, 10)
 
static const Material red_rubber = Material(0.05, 0, 0, 0.5, 0.4, 0.4, 0.7, 0.04, 0.04, 10)
 
static const Material white_rubber = Material(0.05, 0.05, 0.05, 0.5, 0.5, 0.5, 0.7, 0.7, 0.7, 10)
 
static const Material yellow_rubber = Material(0.05, 0.05, 0, 0.5, 0.5, 0.4, 0.7, 0.7, 0.04, 10)
 

Detailed Description

Material class for entities.

This class defines the kind of material used by the shader to render the Entity. It defines several different components (emission, ambient, diffuse and specular) for the materials or a single texture. Each of the components can be a valid color or a special color.

Special Colors

AGL supports several special colors for the materials. This allows for procedurally generated textures for the rendering. These colors are defined with a negative alpha value for the components. Each of these color can take several parameters, defined by values in the rgb part of the colors. These colors are hard-coded in the generated shaders, ie. they can't be animated without recreating the shaders. Available special colors are:

Position based color

This is enabled by AGL_COLOR_POS2RGB. The position of the fragment is mapped to a color for the same. All the fragments are scaled between 0 to 1 and the value is used as RGB. The R, G, and B part of the color must be -1 for the corresponding coordinates (x, y, and z) to contribute to the final color. If not -1, the value is used as-is in the final color.

Normal based color

This is enabled by AGL_COLOR_NORM2RGB. The normal of the fragment is mapped to a color for the same. This mode requires that the normal is included with the buffers. The x, y, and z components of the normal is directly used for the R, G, and B components of the color.

Checkerboard pattern

This is enabled by AGL_COLOR_CHECKER. The position of the fragment is used to generate either black or white color to give a checkerboard pattern finally. The R, G, or B component must be set to -1 to use the same for the color calculation. If set to anything else, they're ignored. The equation used is \(\left(\sum_{i\text{th component of color is -1}} \left\lfloor\text{normal}_i\right\rfloor\right) \mod 2\).

Example

material.emission = glm::vec4(-1, -1, -1, AGL_COLOR_POS2RGB); // This will use position based colors for the material. All the color components will be used.
material.emission = glm::vec4(-1, 0, -1, AGL_COLOR_CHECKER); // This will create a checker board pattern with the x and z components. Equation used: mod(floor(pos.x) + floor(pos.z), 2)

Constructor & Destructor Documentation

◆ Material()

Material ( )

Creates a material.

◆ ~Material()

~Material ( )

Member Function Documentation

◆ createShader()

std::pair< std::string, std::string > createShader ( Entity e = nullptr,
std::vector< Light *>  lights = std::vector<Light*>() 
)
virtual

Create a shader algorithmically.

Parameters
eEntity, needed for which to create the position based coloring.
lightsAll the lights in the scene.
Returns
The generated vertex and fragment shader.

◆ createTexture()

void createTexture ( const char *  path = nullptr)

Creates a texture.

Parameters
pathPath to the texture.

If no path is provided, assumes that the texture is already loaded, else loads the texture, and loads and creates the ID.

Note
Textures loaded with this method, or the ones loaded manually, are not freed when the Material is destroyed. Make sure to do it manually with delete or stbi_image_free to avoid memory leak. This might change in future.

◆ operator*()

Material operator* ( const Material other) const

Mix two material.

Parameters
otherThe material to mix with.
Returns
The mixed material.

This mixes two materials by multiplying the colors. The shininess is geometrically meaned.

◆ operator*=()

Material & operator*= ( const Material other)

Same as operator* but modifies the current material.

◆ operator+()

Material operator+ ( const Material other) const

Mix two material.

Parameters
otherThe material to mix with.
Returns
The mixed material.

This mixes two materials by averaging the colors. The shininess is geometrically meaned.

◆ operator+=()

Material & operator+= ( const Material other)

Same as operator+ but modifies the current material.

◆ setColor() [1/3]

void setColor ( glm::vec4  color = glm::vec4(1))

Set color for all the components.

Parameters
colorColor for all the components.

◆ setColor() [2/3]

void setColor ( float  r = 1,
float  g = 1,
float  b = 1,
float  a = 1 
)

Same as setColor.

Parameters
rRed.
gGreen.
bBlue.
aAlpha.

◆ setColor() [3/3]

void setColor ( const Material other)

Sets each color components from another Material.

Parameters
otherThe Material to copy the colors from.

◆ setShader()

void setShader ( std::string  vertexShader,
std::string  fragmentShader 
)
virtual

Compile and set the shader for the material.

Parameters
vertexShaderVertex shader.
fragmentShaderFragment shader.

This can load shaders that were created by createShader or those loaded manually. This will also set all the required IDs automatically.

Member Data Documentation

◆ aID

GLuint aID

ambient color ID

◆ ambient

glm::vec4 ambient

Ambient, color due to ambient light.

◆ customShader

bool customShader = false

If true, Scene::prepare do not create shaders.

◆ dID

GLuint dID

diffuse color ID

◆ diffuse

glm::vec4 diffuse

Diffuse, color when light hits a surface.

◆ eID

GLuint eID

emission color ID

◆ emission

glm::vec4 emission

Emission, self color of the material.

◆ gID

GLuint gID

shininess color ID

◆ lightsEnabled

bool lightsEnabled = false

If true, no lighting calculations are done.

◆ mID

GLuint mID

model matrix ID

◆ mvpID

GLuint mvpID

MVP matrix ID.

◆ nID

GLuint nID

normal matrix ID

◆ progID

GLuint progID = 0

program ID

◆ ratios

glm::vec4 ratios

◆ shadingType

int shadingType = AGL_SHADING_PHONG

Type of shading, currently unused.

Warning
Might be removed.

◆ shininess

float shininess = 32

Shininess, amount of light reflected by the material.

◆ sID

GLuint sID

specular color ID

◆ specular

glm::vec4 specular

Specular, color due to the reflection of light.

◆ tex_channel

int tex_channel = -1

Number of channels in texture, if used.

◆ tex_height

int tex_height = -1

Height of texture, if used.

◆ tex_width

int tex_width = -1

Width of texture,if used.

◆ texture

GLubyte* texture = nullptr

Pointer to texture data, if present.

◆ tID

GLuint tID = 0

texture ID

◆ vID

GLuint vID

camera position ID


The documentation for this class was generated from the following files: