Easy3D 2.6.1
|
OpenGL Shader Compilation. More...
#include <easy3d/renderer/shader_program.h>
Public Types | |
enum | AttribType { POSITION , COLOR , NORMAL , TEXCOORD } |
Types of vertex attributes. More... | |
enum | ShaderType { VERTEX , FRAGMENT , GEOMETRY , TESS_CONTROL , TESS_EVALUATION , COMPUTE , NUM_SHADER_TYPES } |
Types of Shaders. More... | |
typedef std::pair< AttribType, std::string > | Attribute |
Attribute: a pair of attribute type and attribute name. | |
Public Member Functions | |
ShaderProgram (const std::string &name="unknown") | |
~ShaderProgram () | |
Destructor. | |
void | set_name (const std::string &name) |
Set the name of the shader program. | |
const std::string & | name () const |
Get the name of the shader program. | |
void | set_verbose (bool v) |
Set verbose mode. | |
unsigned int | get_program () const |
Get the program index. | |
void | clear () |
Remove (delete) all shaders. | |
bool | load_shader_from_file (ShaderType st, const std::string &file_name, const std::string &inc_id="#include") |
Load the text in the file to the source of the specified shader. | |
bool | load_shader_from_code (ShaderType st, const std::string &code) |
Load the code to the source of the specified shader. | |
void | set_attrib_name (ShaderProgram::AttribType at, const std::string &name) const |
Define semantics for the input vertex attributes. This is required for other libraries to know how to send data to the shader. | |
void | set_attrib_names (const std::vector< ShaderProgram::Attribute > &attributes) const |
Define semantics for multiple input vertex attributes. | |
bool | link_program () |
Prepare program for usage. Links it and collects information about uniform variables and uniform blocks. | |
void | set_program_output (int index, const std::string &name) const |
Bind a user-defined varying out variable to a fragment shader color number. | |
int | program_output (const std::string &name) const |
Get the fragment shader color number bound to a user-defined varying out variable. | |
void | bind () const |
Start using the program. | |
ShaderProgram * | set_uniform (const std::string &name, const void *value) |
Set the uniform to value. | |
ShaderProgram * | set_uniform (const std::string &name, int value) |
Set the uniform to the int value. | |
ShaderProgram * | set_uniform (const std::string &name, unsigned int value) |
Set the uniform to the unsigned int value. | |
ShaderProgram * | set_uniform (const std::string &name, float value) |
Set the uniform to the float value. | |
ShaderProgram * | set_block (const std::string &name, const void *value) |
Set a uniform block as a whole. | |
ShaderProgram * | set_block_uniform (const std::string &blockName, const std::string &uniformName, const void *value) |
Set a uniform inside a named block. | |
ShaderProgram * | set_block_uniform_array_element (const std::string &blockName, const std::string &uniformName, int arrayIndex, const void *value) |
Set an element of an array of uniforms inside a block. | |
ShaderProgram * | bind_texture (const std::string &name, unsigned int tex_id, int unit, unsigned int tex_target=0x0DE1) |
Bind a texture to the shader program. | |
ShaderProgram * | release_texture (unsigned int tex_target=0x0DE1) |
Release a texture from the shader program. | |
void | release () const |
End using the program. | |
int | get_attribute_location (const std::string &name) const |
Get the location of an attribute. | |
bool | is_uniform_used (const std::string &name) |
Check if a uniform is used by the shader. | |
bool | is_attribute_used (const std::string &name) |
Check if an attribute is used by the shader. | |
bool | is_program_valid () const |
Check if the program is valid. | |
bool | is_program_linked () const |
Check if the program is linked. | |
bool | is_bound () const |
Check if the program is bound. | |
bool | shader_info_log (std::string &log, unsigned int shader) const |
Get the shader info log. | |
bool | program_info_log (std::string &log) const |
Get the program info log. | |
void | print_active_attributes () |
Print active attributes. | |
void | print_active_uniforms () |
Print active uniforms. | |
void | print_active_uniform_blocks () |
Print active uniform blocks. | |
bool | load_binary (const std::string &file_name) |
Load a binary (i.e., compiled and linked) program. | |
bool | save_binary (const std::string &file_name) |
Save the program to a binary file. | |
Static Public Member Functions | |
static bool | is_supported () |
Is shader program supported? | |
static std::string | load_shader_source (const std::string &file_name, const std::string &inc_id="#include") |
Auxiliary function that loads the shader source code from a file. | |
Friends | |
class | ShaderManager |
OpenGL Shader Compilation.
This class allows to create programs, load shaders from files, associate vertex attribute names with locations, and work with uniforms, including blocks. It also provides access to the info logs, and many more useful functionalities. To use the shader program class, you need to do the following:
For rendering
To retrieve the model view projection matrix, call camera's modelViewProjectionMatrix()
enum AttribType |
Types of vertex attributes.
Enumerator | |
---|---|
POSITION | Position. |
COLOR | Color. |
NORMAL | Normal. |
TEXCOORD | Texture coordinates. |
enum ShaderType |
|
explicit |
Constructor.
name | A unique name for a program, convenient to distinguish multiple programs. This is quite helpful in identifying issues when multiple programs are used. It is suggested to use the source file name. |
ShaderProgram * bind_texture | ( | const std::string & | name, |
unsigned int | tex_id, | ||
int | unit, | ||
unsigned int | tex_target = 0x0DE1 ) |
Bind a texture to the shader program.
name | The name of the texture. |
tex_id | The texture ID. |
unit | The texture unit. |
tex_target | The texture target, which can be GL_TEXTURE_2D (default), GL_TEXTURE_2D_MULTISAMPLE, or GL_TEXTURE_2D_ARRAY. |
int get_attribute_location | ( | const std::string & | name | ) | const |
Get the location of an attribute.
name | The name of the attribute. |
unsigned int get_program | ( | ) | const |
Get the program index.
bool is_attribute_used | ( | const std::string & | name | ) |
Check if an attribute is used by the shader.
name | The name of the attribute. |
bool is_bound | ( | ) | const |
bool is_program_linked | ( | ) | const |
Check if the program is linked.
bool is_program_valid | ( | ) | const |
Check if the program is valid.
|
static |
Is shader program supported?
true
if supported, false
otherwise. bool is_uniform_used | ( | const std::string & | name | ) |
Check if a uniform is used by the shader.
This can save some compute time if the uniforms or attributes are expensive to compute.
name | The name of the uniform. |
bool link_program | ( | ) |
Prepare program for usage. Links it and collects information about uniform variables and uniform blocks.
bool load_binary | ( | const std::string & | file_name | ) |
Load a binary (i.e., compiled and linked) program.
file_name | The name of the file to load the binary from. |
bool load_shader_from_code | ( | ShaderType | st, |
const std::string & | code ) |
Load the code to the source of the specified shader.
st | One of the enum values of ShaderType. |
code | The string of the shader source code. |
bool load_shader_from_file | ( | ShaderType | st, |
const std::string & | file_name, | ||
const std::string & | inc_id = "#include" ) |
Load the text in the file to the source of the specified shader.
st | One of the enum values of ShaderType. |
file_name | The full path of the file where the source is to be found. |
inc_id | The include identifier string (e.g., "#include"). |
|
static |
Auxiliary function that loads the shader source code from a file.
file_name | The full path of the file where the source is to be found. |
inc_id | The include identifier string (e.g., "#include"). |
|
inline |
Get the name of the shader program.
void print_active_attributes | ( | ) |
Print active attributes.
void print_active_uniform_blocks | ( | ) |
Print active uniform blocks.
void print_active_uniforms | ( | ) |
Print active uniforms.
bool program_info_log | ( | std::string & | log | ) | const |
Get the program info log.
returns a string with the program's infolog
log | The string to store the program's info log. |
int program_output | ( | const std::string & | name | ) | const |
Get the fragment shader color number bound to a user-defined varying out variable.
name | The name of the fragment's shader variable. |
ShaderProgram * release_texture | ( | unsigned int | tex_target = 0x0DE1 | ) |
Release a texture from the shader program.
tex_target | The texture target (default is GL_TEXTURE_2D). |
bool save_binary | ( | const std::string & | file_name | ) |
Save the program to a binary file.
file_name | The name of the file to save the binary to. |
void set_attrib_name | ( | ShaderProgram::AttribType | at, |
const std::string & | name ) const |
Define semantics for the input vertex attributes. This is required for other libraries to know how to send data to the shader.
at | The semantic of the attribute. |
name | The name of the vertex attribute. |
void set_attrib_names | ( | const std::vector< ShaderProgram::Attribute > & | attributes | ) | const |
Define semantics for multiple input vertex attributes.
attributes | A vector of attributes to set. |
ShaderProgram * set_block | ( | const std::string & | name, |
const void * | value ) |
Set a uniform block as a whole.
name | The name of the uniform block. |
value | The value to set. |
ShaderProgram * set_block_uniform | ( | const std::string & | blockName, |
const std::string & | uniformName, | ||
const void * | value ) |
Set a uniform inside a named block.
blockName | The name of the uniform block. |
uniformName | The name of the uniform. |
value | The value to set. |
ShaderProgram * set_block_uniform_array_element | ( | const std::string & | blockName, |
const std::string & | uniformName, | ||
int | arrayIndex, | ||
const void * | value ) |
Set an element of an array of uniforms inside a block.
blockName | The name of the uniform block. |
uniformName | The name of the uniform. |
arrayIndex | The index of the array element. |
value | The value to set. |
|
inline |
Set the name of the shader program.
name | The name to set. |
void set_program_output | ( | int | index, |
const std::string & | name ) const |
Bind a user-defined varying out variable to a fragment shader color number.
index | The fragment color number. |
name | The name of the fragment's shader variable. |
ShaderProgram * set_uniform | ( | const std::string & | name, |
const void * | value ) |
Set the uniform to value.
name | The name of the uniform. |
value | The value to set. |
ShaderProgram * set_uniform | ( | const std::string & | name, |
float | value ) |
Set the uniform to the float value.
name | The name of the uniform. |
value | The float value to set. |
ShaderProgram * set_uniform | ( | const std::string & | name, |
int | value ) |
Set the uniform to the int value.
name | The name of the uniform. |
value | The int value to set. |
ShaderProgram * set_uniform | ( | const std::string & | name, |
unsigned int | value ) |
Set the uniform to the unsigned int value.
name | The name of the uniform. |
value | The unsigned int value to set. |
|
inline |
Set verbose mode.
v | True to enable verbose mode (log any issues found), false to disable. |
bool shader_info_log | ( | std::string & | log, |
unsigned int | shader ) const |
Get the shader info log.
returns a string with a shader's infolog
log | The string to store the shader's info log. |
shader | The shader ID. |