Easy3D 2.5.3
ShaderProgram Class Reference

OpenGL Shader Compilation. More...

#include <easy3d/renderer/shader_program.h>

Public Types

enum  AttribType { POSITION , COLOR , NORMAL , TEXCOORD }
 
enum  ShaderType {
  VERTEX , FRAGMENT , GEOMETRY , TESS_CONTROL ,
  TESS_EVALUATION , COMPUTE , NUM_SHADER_TYPES
}
 
typedef std::pair< ShaderProgram::AttribType, std::string > Attribute
 

Public Member Functions

 ShaderProgram (const std::string &name="unknown")
 
void set_name (const std::string &name)
 
const std::string & name () const
 
void set_verbose (bool v)
 Sets true to log any issues found.
 
unsigned int get_program () const
 Returns the program index.
 
void clear ()
 Removes (deletes) all shaders.
 
bool load_shader_from_file (ShaderType st, const std::string &file_name, const std::string &inc_id="#include")
 
bool load_shader_from_code (ShaderType st, const std::string &code)
 
void set_attrib_name (ShaderProgram::AttribType at, const std::string &name) const
 
void set_attrib_names (const std::vector< ShaderProgram::Attribute > &attributes) const
 
bool link_program ()
 
void set_program_output (int index, const std::string &name) const
 
int program_output (const std::string &name) const
 
void bind () const
 Starts using the program.
 
ShaderProgramset_uniform (const std::string &name, const void *value)
 
ShaderProgramset_uniform (const std::string &name, int value)
 
ShaderProgramset_uniform (const std::string &name, unsigned int value)
 
ShaderProgramset_uniform (const std::string &name, float value)
 
ShaderProgramset_block (const std::string &name, const void *value)
 
ShaderProgramset_block_uniform (const std::string &blockName, const std::string &uniformName, const void *value)
 
ShaderProgramset_block_uniform_array_element (const std::string &blockName, const std::string &uniformName, int arrayIndex, const void *value)
 
ShaderProgrambind_texture (const std::string &name, unsigned int tex_id, int unit, unsigned int tex_target=0x0DE1)
 
ShaderProgramrelease_texture (unsigned int tex_target=0x0DE1)
 
void release () const
 Ends using the program.
 
int get_attribute_location (const std::string &name) const
 
bool is_uniform_used (const std::string &name)
 
bool is_attribute_used (const std::string &name)
 
bool is_program_valid () const
 
bool is_program_linked () const
 Returns true if program linked, false otherwise.
 
bool is_bound () const
 Returns true if the program is being used (i.e., between bind() and release()).
 
bool shader_info_log (std::string &log, unsigned int shader) const
 returns a string with a shader's infolog
 
bool program_info_log (std::string &log) const
 returns a string with the program's infolog
 
void print_active_attributes ()
 
void print_active_uniforms ()
 
void print_active_uniform_blocks ()
 
bool load_binary (const std::string &file_name)
 
bool save_binary (const std::string &file_name)
 

Static Public Member Functions

static bool is_supported ()
 
static std::string load_shader_source (const std::string &file_name, const std::string &inc_id="#include")
 

Friends

class ShaderManager
 

Detailed Description

OpenGL Shader Compilation.

To use the shader program class, you need to do the following:

  • Call load_shader_from_code(ShaderProgram::VERTEX, vert_file) to create vertex shader and load_shader_from_code(ShaderProgram::FRAGMENT, frag_file) to create fragment shader (you may also need to create other types of shaders depending on your needs).
  • Call set_attrib_name(ShaderProgram::POSITION, "position") for vertex attribute "position". You may also need to set other attributes like normal, color, etc. To know what vertex attributes need to be set, check your shader code or call print_active_attributes().
  • Call link_program() to link the program.

For rendering

To retrieve the model view projection matrix, call camera's modelViewProjectionMatrix()

Examples
Tutorial_207_RealCamera, Tutorial_501_AmbientOcclusion, Tutorial_505_EyeDomeLighting, and Tutorial_506_DepthMaps.

Constructor & Destructor Documentation

◆ ShaderProgram()

ShaderProgram ( const std::string &  name = "unknown")
explicit

Constructor.

Parameters
nameA 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.

Member Function Documentation

◆ link_program()

bool link_program ( )

Prepares program for usage. Links it and collects information about uniform variables and uniform blocks.

◆ load_shader_from_code()

bool load_shader_from_code ( ShaderType  st,
const std::string &  code 
)

Loads the code to the source of the specified shader

Parameters
stone of the enum values of ShaderType
codethe string of the shader source code

◆ load_shader_from_file()

bool load_shader_from_file ( ShaderType  st,
const std::string &  file_name,
const std::string &  inc_id = "#include" 
)

Loads the text in the file to the source of the specified shader

Parameters
stone of the enum values of ShaderType
file_namethe full path of the file where the source is to be found
inc_idthe include identifier string (e.g., "#include")

◆ load_shader_source()

std::string load_shader_source ( const std::string &  file_name,
const std::string &  inc_id = "#include" 
)
static

Auxiliary function that loads the shader source code from a file.

Parameters
file_namethe full path of the file where the source is to be found
inc_idthe include identifier string (e.g., "#include")

◆ program_output()

int program_output ( const std::string &  name) const

Returns the fragment shader color number bound to a user-defined varying out variable Note: linking is required for this operation to take effect (call method prepare_program afterwards)

Parameters
thename of the fragment's shader variable
Returns
the fragment color number

◆ set_attrib_name()

void set_attrib_name ( ShaderProgram::AttribType  at,
const std::string &  name 
) const

Defines semantics for the input vertex attributes. This is required for other libraries to know how to send data to the shader.

Parameters
atThe semantic of the attribute
nameThe name of the vertex attribute
Note
To specify a location for an attribute you must do it prior to linking the program, or, if the program is already linked, to link it again afterwards (call method link_program)

◆ set_block()

ShaderProgram * set_block ( const std::string &  name,
const void *  value 
)

Sets a uniform block as a whole

◆ set_block_uniform()

ShaderProgram * set_block_uniform ( const std::string &  blockName,
const std::string &  uniformName,
const void *  value 
)

Sets a uniform inside a named block Warning: Be careful when using uniform blocks. Please refer to OpenGL Specification Version 4.5 (Core Profile) - May 28, 2015 (https://www.opengl.org/registry/doc/glspec45.core.pdf#page=159) In "Standard Uniform Block Layout", it says "If the member is a three-component vector with components consuming N basic machine units, the base alignment is 4N" (see rule 3 of section 7.6.2.2 in page.159). That means the array stride (the bytes between array elements) is always rounded up to the size of a vec4 (i.e., 16-bytes). So we have to avoid using vec3/mat3 (use vec4/mat4/mat43 instead) in a uniform block. Otherwise, you have to manually pad your structures/arrays out.

Examples
Tutorial_501_AmbientOcclusion.

◆ set_block_uniform_array_element()

ShaderProgram * set_block_uniform_array_element ( const std::string &  blockName,
const std::string &  uniformName,
int  arrayIndex,
const void *  value 
)

Sets an element of an array of uniforms inside a block

◆ set_program_output()

void set_program_output ( int  index,
const std::string &  name 
) const

Bind a user-defined varying out variable to a fragment shader color number Note: linking is required for this operation to take effect (call method prepare_program afterwards)

Parameters
indexThe fragment color number
nameThe name of the fragment's shader variable

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