hold all material properties necessary to create dynamic shader - light models textures and colors More...
#include <material.h>
Public Member Functions | |
TMaterial (const char *_name, TVector amb, TVector diff, TVector spec, GLfloat shin, GLfloat reflect, GLfloat transp, GLint lm) | |
Direct creation of desired material. | |
~TMaterial () | |
Destroy material data, detache shaders. | |
string | GetName () |
get material name | |
GLfloat | GetTrasparency () |
get transparency value | |
GLint | AddTexture (const char *file, GLint textype, GLint texmode, GLfloat intensity, GLfloat tileX, GLfloat tileY, bool mipmap, bool aniso, GLint cache) |
Add texture from external file. If texture has been loaded, a texture pointer from cache is used instead of reloading from file. | |
GLint | AddTexture (const char **files, GLint textype, GLint texmode, GLfloat intensity, GLfloat tileX, GLfloat tileY, bool aniso, GLint cache) |
Add cubemap texture from external file. If texture has been loaded, a texture pointer from cache is used instead of reloading from file. | |
string | NextTexture (string textype) |
Finds and returns next free texture name in list (important to synchronize generated texture names to them stored in TMaterial::textures field). | |
void | DeleteTexture (string texname) |
Delete texture specified by name. | |
void | AddShadowMap (GLuint map, GLfloat intensity) |
Add shadow map. | |
void | SetShadowMatrix (TMatrix &m, GLint l_num) |
Set shadow map matrix for texture attached to material (Texture::SetShadowMatrix()). | |
bool | CustomShader (const char *vert_source, const char *geom_source, const char *frag_source, const char *vert_defines, const char *frag_defines, const char *geom_defines) |
Loads and sets custom shader from source file. Fragment and vertex shader is mandatory, geometry shader optional. | |
void | SetUniform (const char *v_name, float value) |
Set float uniform value in shader. | |
void | SetUniform (const char *v_name, double value) |
Set float/double uniform value in shader. | |
void | SetUniform (const char *v_name, int value) |
Set int uniform value in shader. | |
void | SetUniforms (const char *v_name, float *value, int values) |
Set multiple float uniform values in shader (vectors, matrices...). | |
void | UseGShaderCubeMapRender (bool flag) |
Toggle use of geometry shader in dynamic cubemapping. | |
void | UseMRT (bool flag) |
Toggle use of MRT. | |
bool | BakeMaterial (bool use_geom=false) |
Dynamically generates shader from all material data (if hasn't been generated before). | |
void | RenderMaterial (bool render_all=true) |
Render material. If hasn't been baked, bake him first(TMaterial::BakeMaterial()). | |
void | SetLights (int lights) |
set number of lights to use in shader | |
void | ReceiveShadow (bool flag) |
toggle receiving shadows | |
float | GetReflect () |
should we render material as reflective? |
hold all material properties necessary to create dynamic shader - light models textures and colors
TMaterial::TMaterial | ( | const char * | _name, | |
TVector | amb, | |||
TVector | diff, | |||
TVector | spec, | |||
GLfloat | shin, | |||
GLfloat | reflect, | |||
GLfloat | transp, | |||
GLint | lm | |||
) |
Direct creation of desired material.
_name | material name (must be unique) | |
amb | ambient color (as RGB TVector) | |
diff | diffuse color (as RGB TVector) | |
spec | specular color (as RGB TVector) | |
shin | shininess (0.0 - most shiny, 128.0 - least shiny) | |
reflect |
transp | material transparency | |
lm | light model (can be PHONG,GOURAUD,NONE) |
void TMaterial::AddShadowMap | ( | GLuint | map, | |
GLfloat | intensity | |||
) |
Add shadow map.
map | pointer to texture data | |
intensity | shadow intensity (0 - transparent, 1 - opaque) |
1. generate new shadow texture name using TMaterial::NextTexture()
GLint TMaterial::AddTexture | ( | const char ** | files, | |
GLint | textype, | |||
GLint | texmode, | |||
GLfloat | intensity, | |||
GLfloat | tileX, | |||
GLfloat | tileY, | |||
bool | aniso, | |||
GLint | cache | |||
) |
Add cubemap texture from external file. If texture has been loaded, a texture pointer from cache is used instead of reloading from file.
files | external texture files with every side of cube texture (.tga) | |
textype | texture type (CUBEMAP) | |
texmode | texture addition mode (can be ADD,MODULATE,DECAL,BLEND,REPLACE) | |
intensity | texture color intensity (or bump intensity) | |
tileX | count of horizontal tiles | |
tileY | count of vertical tiles | |
aniso | should we use anisotropic filtering? | |
cache | whether texture should be cached |
1. generate new texture name (material name + texture mode (base, env, bump...) using TMaterial::NextTexture()
2. load new texture into map array (using Texture::Load())
GLint TMaterial::AddTexture | ( | const char * | file, | |
GLint | textype, | |||
GLint | texmode, | |||
GLfloat | intensity, | |||
GLfloat | tileX, | |||
GLfloat | tileY, | |||
bool | mipmap, | |||
bool | aniso, | |||
GLint | cache | |||
) |
Add texture from external file. If texture has been loaded, a texture pointer from cache is used instead of reloading from file.
file | external texture file (.tga) | |
textype | texture type (can be BASE,ENV,BUMP) | |
texmode | texture addition mode (can be ADD,MODULATE,DECAL,BLEND,REPLACE) | |
intensity | texture color intensity (or bump intensity) | |
tileX | count of horizontal tiles | |
tileY | count of vertical tiles | |
mipmap | should we generate mipmaps for texture? | |
aniso | should we use anisotropic filtering? | |
cache | whether texture should be cached |
1. generate new texture name (material name + texture mode (base, env, bump...) using TMaterial::NextTexture()
2. load new texture into map array (using Texture::Load())
bool TMaterial::BakeMaterial | ( | bool | use_geom = false |
) |
Dynamically generates shader from all material data (if hasn't been generated before).
use_geom | should we generate geometry shader? |
1 CREATION OF VERTEX SHADER
1.1 Vertex shader variables
1.1.1 if we have displacement texture, modify vertex position and normal
1.2 Light model: PHONG or GOURAUD
1.2.1 if PHONG, light calculation will be done in fragment shader, so send necessary variables to it (also required for env mapping)
1.2.3 if GOURAUD, done light calculation per-vertex
1.3 send texture coords to fragment shader (or geometry shader)
1.4 calculate shadow matrices for frag. shader (projected shadow and texture matrix)
1.5 send 3D coordinates for cube map
1.6 send 3D coordinates for environment cube map
1.5 Finalize, vertex transform
2 CREATION OF GEOMETRY SHADER (OPTIONAL)
2.1 Geometry shader variables
2.2 passing varyings to the fragment shader
2.2.1 if PHONG, light calculation will be done in fragment shader, so send necessary variables to it (also required for env mapping)
2.4 send shadow matrices for frag. shader (projected shadow and texture matrix)
2.5 send 3D coordinates for cube map
2.6 send 3D coordinates for environment cube map
3 CREATION OF FRAGMENT SHADER
3.1 fragment shader variables
3.2 Texture pre-setup: create texture samplers for every texture (shadow maps use shadow samplers), texture tiles and coordinates
3.3.1 if light model is set to PHONG, calculate lighting by using varying variables sent from vertex shader (normal, eye vector...)
3.3.1.1 if is present bump texture, modify normal (bump mapping)
3.3.2 if GOURAUD model is set, only get color value from vertex shader
3.3.3 else constant shading - only material diffuse is in computation
3.4 texture application according to texture type
3.4.1 shadow maps - set shadow samplers and call function to project and create soft shadows
3.4.2 environment maps - add normal and eye vector variables(if per-pixel)
3.5 Finalize (with transparency or alpha test)
4 Create, compile and link shaders
4 Get uniform variables for textures (using Texture::GetUniforms() )
set materials parameters
bool TMaterial::CustomShader | ( | const char * | vert_source, | |
const char * | geom_source, | |||
const char * | frag_source, | |||
const char * | vert_defines, | |||
const char * | frag_defines, | |||
const char * | geom_defines | |||
) |
Loads and sets custom shader from source file. Fragment and vertex shader is mandatory, geometry shader optional.
vert_source | source file for vertex shader | |
geom_source | source file for geometry shader | |
frag_source | source file for fragment shader | |
vert_defines | additional strings for vertex shader (eg. "#define SOMETHING\n"), must be at the end | |
frag_defines | additional strings for fragment shader (eg. "#define SOMETHING\n"), must be at the end | |
geom_defines | additional strings for geometry shader (eg. "#define SOMETHING\n"), must be at the end |
4 Get uniform variables for textures (using Texture::GetUniforms() )
set materials parameters
void TMaterial::DeleteTexture | ( | string | texname | ) | [inline] |
Delete texture specified by name.
texname | texture name |
string TMaterial::NextTexture | ( | string | texname | ) |
Finds and returns next free texture name in list (important to synchronize generated texture names to them stored in TMaterial::textures field).
texname | suffix, texture type (like Base, Bump, Env...) |
find first empty texture, add index to it's name and return this name
void TMaterial::RenderMaterial | ( | bool | render_all = true |
) |
Render material. If hasn't been baked, bake him first(TMaterial::BakeMaterial()).
render_all | if we are rendering to shadow map, render material only if material is alpha-tested |
enable shaders
activate textures attached to material (Texture::ActivateTexture() )
void TMaterial::SetShadowMatrix | ( | TMatrix & | m, | |
GLint | l_num | |||
) |
Set shadow map matrix for texture attached to material (Texture::SetShadowMatrix()).
m | shadow projection matrix | |
l_num | light number (correction for multiple shadows) |
void TMaterial::SetUniform | ( | const char * | v_name, | |
int | value | |||
) |
Set int uniform value in shader.
v_name | variable name | |
value | variable value |
void TMaterial::SetUniform | ( | const char * | v_name, | |
double | value | |||
) |
Set float/double uniform value in shader.
v_name | variable name | |
value | variable value |
void TMaterial::SetUniform | ( | const char * | v_name, | |
float | value | |||
) |
Set float uniform value in shader.
v_name | variable name | |
value | variable value |
void TMaterial::SetUniforms | ( | const char * | v_name, | |
float * | value, | |||
int | values | |||
) |
Set multiple float uniform values in shader (vectors, matrices...).
v_name | variable name | |
value | variable values | |
values | number of values |