+
+BlendedSprite::BlendedSprite()
+: program()
+, vp(1.0f)
+, mvp_handle(0)
+, sampler_handle(0) {
+ program.LoadShader(
+ GL_VERTEX_SHADER,
+ "#version 330 core\n"
+ "layout(location = 0) in vec3 vtx_position;\n"
+ "layout(location = 1) in vec2 vtx_tex_uv;\n"
+ "uniform mat4 MVP;\n"
+ "out vec2 frag_tex_uv;\n"
+ "void main() {\n"
+ "gl_Position = MVP * vec4(vtx_position, 1);\n"
+ "frag_tex_uv = vtx_tex_uv;\n"
+ "}\n"
+ );
+ program.LoadShader(
+ GL_FRAGMENT_SHADER,
+ "#version 330 core\n"
+ "in vec2 frag_tex_uv;\n"
+ "uniform sampler2D tex_sampler;\n"
+ "uniform vec4 fg_factor;\n"
+ "uniform vec4 bg_factor;\n"
+ "out vec4 color;\n"
+ "void main() {\n"
+ "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
+ "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
+ "color = tex_color * factor;\n"
+ "color.a = factor.a;\n"
+ "}\n"
+ );
+ program.Link();
+ if (!program.Linked()) {
+ program.Log(std::cerr);
+ throw std::runtime_error("link program");
+ }
+
+ mvp_handle = program.UniformLocation("MVP");
+ sampler_handle = program.UniformLocation("tex_sampler");
+ fg_handle = program.UniformLocation("fg_factor");
+ bg_handle = program.UniformLocation("bg_factor");
+
+ Activate();
+ SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
+ SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
+}
+
+
+void BlendedSprite::Activate() noexcept {
+ program.Use();
+}
+
+void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
+ program.Uniform(mvp_handle, vp * m);
+}
+
+void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
+ projection = p;
+ vp = p * view;
+}
+
+void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
+ view = v;
+ vp = projection * v;
+}
+
+void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
+ projection = p;
+ view = v;
+ vp = p * v;
+}
+
+void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
+ SetVP(v, p);
+ SetM(m);
+}
+
+void BlendedSprite::SetTexture(Texture &tex) noexcept {
+ glActiveTexture(GL_TEXTURE0);
+ tex.Bind();
+ program.Uniform(sampler_handle, GLint(0));
+}
+
+void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
+ program.Uniform(fg_handle, v);
+}
+
+void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
+ program.Uniform(bg_handle, v);
+}
+