Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
shader.hpp
Go to the documentation of this file.
1
3#pragma once
4
7#include <aegis2d/render/export.hpp>
9
10#include <cstdint>
11#include <functional>
12#include <map>
13#include <stdexcept>
14#include <string>
15#include <string_view>
16
18
21class AEGIS2D_RENDER_EXPORT ShaderError : public std::runtime_error {
22public:
23 using std::runtime_error::runtime_error;
24};
25
29 std::string_view vertex;
31 std::string_view fragment;
32};
33
44class AEGIS2D_RENDER_EXPORT ShaderProgram {
45public:
50 explicit ShaderProgram(const ShaderSources& sources);
51
52 ShaderProgram(const ShaderProgram&) = delete;
56 ShaderProgram(ShaderProgram&& other) noexcept;
63
67 [[nodiscard]] static std::string_view version_directive() noexcept;
68
70 void use() const;
71
74 [[nodiscard]] std::uint32_t id() const noexcept;
75
79 [[nodiscard]] bool has_uniform(std::string_view name);
80
84 void set_uniform(std::string_view name, std::int32_t value);
88 void set_uniform(std::string_view name, float value);
92 void set_uniform(std::string_view name, Vec2 value);
96 void set_uniform(std::string_view name, Color value);
100 void set_uniform(std::string_view name, const Vec4& value);
104 void set_uniform(std::string_view name, const Mat4& matrix);
105
106private:
107 [[nodiscard]] std::int32_t location(std::string_view name);
108
109 std::uint32_t id_ = 0;
110 std::map<std::string, std::int32_t, std::less<>> locations_;
111};
112
113} // namespace aegis2d::render::opengl
Thrown when a shader does not compile or a program does not link.
Definition shader.hpp:21
Vertex and fragment shaders linked into one OpenGL program.
Definition shader.hpp:44
~ShaderProgram()
Deletes the program.
ShaderProgram & operator=(ShaderProgram &&other) noexcept
Deletes this program and takes over the program of other.
ShaderProgram & operator=(const ShaderProgram &)=delete
ShaderProgram(const ShaderProgram &)=delete
ShaderProgram(const ShaderSources &sources)
Compiles both stages and links them.
ShaderProgram(ShaderProgram &&other) noexcept
Takes over the program of other, which becomes empty.
static std::string_view version_directive() noexcept
#version line prepended to every stage.
RGBA color used by the renderer.
Vector and matrix types of the engine (GLM behind engine names).
OpenGL backend of the renderer.
Definition context.hpp:18
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
glm::vec4 Vec4
4D vector of floats (glm::vec4), e.g. homogeneous positions.
Definition math.hpp:25
glm::mat4 Mat4
4x4 matrix of floats (glm::mat4), stored column by column like GLSL and HLSL constant buffers,...
Definition math.hpp:31
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
GLSL sources of a program, without #version line.
Definition shader.hpp:27
std::string_view fragment
Fragment stage.
Definition shader.hpp:31
std::string_view vertex
Vertex stage.
Definition shader.hpp:29