Skip to content
Fjodor Schelichow edited this page Apr 17, 2020 · 9 revisions

Creating a shader

In order to use a shader you will first need to create an object of the Shader class.

sgltk::Shader my_shader;

Loading shader code

In order to load the shader code from a file you need to call attach_file.

my_shader.attach_file("path/to/file", GL_VERTEX_SHADER);

If you don't use an absolute path, this function will first look for files in the current working directory first and then in all the directories added to the list of shader file paths by calls to add_path in the order they were added.

You can also attach shaders from strings by calling attach_string.

std::string source_string = "#version 130                        \n"
                            "void main() {                       \n"
                            "    gl_Position = vec4(0, 0, 0, 1); \n"
                            "    gl_FrontColor =  vec4(1);       \n"
                            "}"

my_shader.attach_string(source_string, GL_VERTEX_SHADER);

You can attach multiple shaders of the same type.

std::string source_string1 = "#version 130                      \n"
                             "vec4 get_position() {             \n"
                             "    return vec4(0, 0, 0, 1);      \n"
                             "}"

std::string source_string2 = "#version 130                      \n"
                             "vec4 get_position();              \n"
                             "void main() {                     \n"
                             "    gl_Position = get_position(); \n"
                             "    gl_FrontColor =  vec4(1);     \n"
                             "}"

my_shader.attach_string(source_string1, GL_VERTEX_SHADER);
my_shader.attach_string(source_string2, GL_VERTEX_SHADER);

Linking the shader

In order to be able to use the shader, you need to link all the attached shaders.

my_shader.link();

Transform feedback

If you want to use transform feedback you will need to specify the attributes to track.

std::vector<std::string> variables = {"position_out"};
my_shader.set_transform_feedback_variables(variables, GL_SEPARATE_ATTRIBS);

If you already linked the program, it will be re-linked automatically.

Clone this wiki locally