mousetrap v0.2.0
Loading...
Searching...
No Matches
mousetrap::Shader Class Reference

shader, holds the OpenGL shader program, a vertex and a fragment shader More...

#include <shader.hpp>

Inheritance diagram for mousetrap::Shader:
mousetrap::SignalEmitter

Public Member Functions

 Shader ()
 construct, holds default shader program for both fragment and vertex
 
 ~Shader ()
 destruct, frees GPU-side memory
 
 Shader (detail::ShaderInternal *)
 construct from internal \for_internal_use_only
 
NativeObject get_internal () const override
 expose internal
 
 operator NativeObject () const override
 expose as GObject \for_internal_use_only
 
GLNativeHandle get_program_id () const
 get the native OpenGL id of the shader program
 
GLNativeHandle get_fragment_shader_id () const
 get the native OpenGL id of the fragment shader
 
GLNativeHandle get_vertex_shader_id () const
 get the native OpenGL id of the vertex shader
 
bool create_from_string (ShaderType type, const std::string &code)
 create shader from source code as string
 
bool create_from_file (ShaderType type, const std::string &path)
 create shader from a file, usually .glsl, .frag or .vert
 
int get_uniform_location (const std::string &name) const
 get location of a uniform with given name
 
void set_uniform_float (const std::string &uniform_name, float) const
 set uniform float
 
void set_uniform_int (const std::string &uniform_name, int) const
 set uniform int
 
void set_uniform_uint (const std::string &uniform_name, glm::uint) const
 set uniform uint
 
void set_uniform_vec2 (const std::string &uniform_name, Vector2f) const
 set uniform vec2
 
void set_uniform_vec3 (const std::string &uniform_name, Vector3f) const
 set uniform vec3
 
void set_uniform_vec4 (const std::string &uniform_name, Vector4f) const
 set uniform vec4
 
void set_uniform_transform (const std::string &uniform_name, GLTransform) const
 set uniform mat4x4
 
- Public Member Functions inherited from mousetrap::SignalEmitter
void set_signal_blocked (const std::string &signal_id, bool b)
 block a signal by id \for_internal_use_only
 
bool get_signal_blocked (const std::string &signal_id) const
 get whether a signal is currently blocked
 
std::vector< std::string > get_all_signal_names ()
 get list of possible signal ids \for_internal_use_only
 
template<typename Function_t >
void connect_signal (const std::string &signal_id, Function_t *function, void *data=nullptr)
 connect static function to signal, does not work with lambdas \for_internal_use_only
 
void disconnect_signal (const std::string &signal_id)
 disconnect signal, it may not be invoked until reconnected, if signal is not registered, does nothing. For internal use only
 
virtual operator NativeObject () const =0
 expose as NativeObject, this is the GLib instance that signals are emitted on \for_internal_use_only
 
virtual NativeObject get_internal () const
 expose internally managed object, \for_internal_use_only
 

Static Public Member Functions

static int get_vertex_position_location ()
 get position of the default _vertex_position uniform
 
static int get_vertex_color_location ()
 get position of the default _vertex_color uniform
 
static int get_vertex_texture_coordinate_location ()
 get position of the default _texture_coordinates uniform
 

Static Public Attributes

static const std::string noop_fragment_shader_code
 default fragment shader behavior, render a shape respecting its vertices colors and its optional texture
 
static const std::string noop_vertex_shader_code
 noop vertex shader behavior, render the shapes vertices after applying the set transform
 

Additional Inherited Members

- Protected Member Functions inherited from mousetrap::SignalEmitter
 SignalEmitter ()
 ctor
 
 ~SignalEmitter ()
 destructor
 

Detailed Description

shader, holds the OpenGL shader program, a vertex and a fragment shader

Member Function Documentation

◆ create_from_file()

bool mousetrap::Shader::create_from_file ( ShaderType  type,
const std::string &  path 
)

create shader from a file, usually .glsl, .frag or .vert

Parameters
typeOne of ShaderType::FRAGMENT or ShaderType::VERTEX
pathabsolute path to file with glsl code
Returns
true the file was accessed and compiled succesfully, false otherwise

References create_from_string(), and mousetrap::log::critical().

◆ create_from_string()

bool mousetrap::Shader::create_from_string ( ShaderType  type,
const std::string &  code 
)

create shader from source code as string

Parameters
typeOne of ShaderType::FRAGMENT or ShaderType::VERTEX
codeglsl code
Returns
true if compiled succesfully, false otherwise

Referenced by create_from_file().

◆ get_fragment_shader_id()

GLNativeHandle mousetrap::Shader::get_fragment_shader_id ( ) const

get the native OpenGL id of the fragment shader

Returns
id

◆ get_internal()

NativeObject mousetrap::Shader::get_internal ( ) const
overridevirtual

expose internal

Reimplemented from mousetrap::SignalEmitter.

◆ get_program_id()

GLNativeHandle mousetrap::Shader::get_program_id ( ) const

◆ get_uniform_location()

int mousetrap::Shader::get_uniform_location ( const std::string &  name) const

get location of a uniform with given name

Parameters
nameexact name of a uniform mentioned in the shader source code
Returns
location or -1 if no uniform of that name exists

References get_program_id().

Referenced by mousetrap::Shape::render(), set_uniform_float(), set_uniform_int(), set_uniform_transform(), set_uniform_uint(), set_uniform_vec2(), set_uniform_vec3(), and set_uniform_vec4().

◆ get_vertex_color_location()

int mousetrap::Shader::get_vertex_color_location ( )
static

get position of the default _vertex_color uniform

Returns
position

◆ get_vertex_position_location()

int mousetrap::Shader::get_vertex_position_location ( )
static

get position of the default _vertex_position uniform

Returns
position

◆ get_vertex_shader_id()

GLNativeHandle mousetrap::Shader::get_vertex_shader_id ( ) const

get the native OpenGL id of the vertex shader

Returns
id

◆ get_vertex_texture_coordinate_location()

int mousetrap::Shader::get_vertex_texture_coordinate_location ( )
static

get position of the default _texture_coordinates uniform

Returns
position

◆ operator NativeObject()

mousetrap::Shader::operator NativeObject ( ) const
overridevirtual

expose as GObject \for_internal_use_only

Implements mousetrap::SignalEmitter.

◆ set_uniform_float()

void mousetrap::Shader::set_uniform_float ( const std::string &  uniform_name,
float  value 
) const

set uniform float

Parameters
uniform_nameexact name of a uniform mentioned in the shader source
value

References get_program_id(), and get_uniform_location().

◆ set_uniform_int()

void mousetrap::Shader::set_uniform_int ( const std::string &  uniform_name,
int  value 
) const

set uniform int

Parameters
uniform_nameexact name of a uniform mentioned in the shader source
value

References get_program_id(), and get_uniform_location().

◆ set_uniform_transform()

void mousetrap::Shader::set_uniform_transform ( const std::string &  uniform_name,
GLTransform  value 
) const

set uniform mat4x4

Parameters
uniform_nameexact name of a uniform mentioned in the shader source
value

References get_program_id(), get_uniform_location(), and mousetrap::GLTransform::transform.

◆ set_uniform_uint()

void mousetrap::Shader::set_uniform_uint ( const std::string &  uniform_name,
glm::uint  value 
) const

set uniform uint

Parameters
uniform_nameexact name of a uniform mentioned in the shader source
value

References get_program_id(), and get_uniform_location().

◆ set_uniform_vec2()

void mousetrap::Shader::set_uniform_vec2 ( const std::string &  uniform_name,
Vector2f  value 
) const

set uniform vec2

Parameters
uniform_nameexact name of a uniform mentioned in the shader source
value

References get_program_id(), and get_uniform_location().

◆ set_uniform_vec3()

void mousetrap::Shader::set_uniform_vec3 ( const std::string &  uniform_name,
Vector3f  value 
) const

set uniform vec3

Parameters
uniform_nameexact name of a uniform mentioned in the shader source
value

References get_program_id(), and get_uniform_location().

◆ set_uniform_vec4()

void mousetrap::Shader::set_uniform_vec4 ( const std::string &  uniform_name,
Vector4f  value 
) const

set uniform vec4

Parameters
uniform_nameexact name of a uniform mentioned in the shader source
value

References get_program_id(), and get_uniform_location().

Member Data Documentation

◆ noop_fragment_shader_code

const std::string mousetrap::Shader::noop_fragment_shader_code
inlinestatic
Initial value:
= R"(
in vec4 _vertex_color;
in vec2 _texture_coordinates;
in vec3 _vertex_position;
out vec4 _fragment_color;
uniform int _texture_set;
uniform sampler2D _texture;
void main()
{
if (_texture_set != 1)
_fragment_color = _vertex_color;
else
_fragment_color = texture2D(_texture, _texture_coordinates) * _vertex_color;
}
)"

default fragment shader behavior, render a shape respecting its vertices colors and its optional texture

Referenced by Shader().

◆ noop_vertex_shader_code

const std::string mousetrap::Shader::noop_vertex_shader_code
inlinestatic
Initial value:
= R"(
layout (location = 0) in vec3 _vertex_position_in;
layout (location = 1) in vec4 _vertex_color_in;
layout (location = 2) in vec2 _vertex_texture_coordinates_in;
uniform mat4 _transform;
out vec4 _vertex_color;
out vec2 _texture_coordinates;
out vec3 _vertex_position;
void main()
{
gl_Position = _transform * vec4(_vertex_position_in, 1.0);
_vertex_color = _vertex_color_in;
_vertex_position = _vertex_position_in;
_texture_coordinates = _vertex_texture_coordinates_in;
}
)"

noop vertex shader behavior, render the shapes vertices after applying the set transform

Referenced by Shader().


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