Skip to content

Commit

Permalink
add shader_base::set_uniform_sampler()
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Apr 15, 2024
1 parent 2391989 commit 1f11c1d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/ruis/render/opengl/render_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "shader_color_pos_tex.hpp"
#include "shader_color_pos_tex_alpha.hpp"
#include "shader_pos_clr.hpp"
#include "shader_texture.hpp"
#include "shader_pos_tex.hpp"
#include "texture_2d.hpp"
#include "util.hpp"
#include "vertex_array.hpp"
Expand Down Expand Up @@ -198,7 +198,7 @@ std::unique_ptr<ruis::render_factory::shaders> render_factory::create_shaders()
{
auto ret = std::make_unique<ruis::render_factory::shaders>();
// NOLINTNEXTLINE(bugprone-unused-return-value, "false positive")
ret->pos_tex = std::make_unique<shader_texture>();
ret->pos_tex = std::make_unique<shader_pos_tex>();
// NOLINTNEXTLINE(bugprone-unused-return-value, "false positive")
ret->color_pos = std::make_unique<shader_color>();
// NOLINTNEXTLINE(bugprone-unused-return-value, "false positive")
Expand Down
3 changes: 2 additions & 1 deletion src/ruis/render/opengl/shader_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <GL/glew.h>
#include <utki/debug.hpp>
#include <utki/string.hpp>

#include "index_buffer.hpp"
#include "util.hpp"
Expand Down Expand Up @@ -163,7 +164,7 @@ GLint shader_base::get_uniform(const char* n)
{
GLint ret = glGetUniformLocation(this->program.p, n);
if (ret < 0) {
throw std::logic_error("no uniform found in the shader program");
throw std::logic_error(utki::cat("no uniform found in the shader program: ", n));
}
return ret;
}
Expand Down
6 changes: 6 additions & 0 deletions src/ruis/render/opengl/shader_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ class shader_base
return GLuint(prog) == this->program.p;
}

void set_uniform_sampler(GLint id, GLint texture_unit_num) const
{
glUniform1i(id, texture_unit_num);
assert_opengl_no_error();
}

void set_uniform_matrix4f(GLint id, const r4::matrix4<float>& m) const
{
glUniformMatrix4fv(id, 1, GL_TRUE, m.front().data());
Expand Down
6 changes: 5 additions & 1 deletion src/ruis/render/opengl/shader_color_pos_tex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ shader_color_pos_tex::shader_color_pos_tex() :
}
)qwertyuiop"
),
texture_uniform(this->get_uniform("texture0")),
color_uniform(this->get_uniform("uniform_color"))
{}

Expand All @@ -63,11 +64,14 @@ void shader_color_pos_tex::render(
const ruis::texture_2d& tex
) const
{
constexpr auto texture_unit_number = 0;

ASSERT(dynamic_cast<const texture_2d*>(&tex))
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
static_cast<const texture_2d&>(tex).bind(0);
static_cast<const texture_2d&>(tex).bind(texture_unit_number);
this->bind();

this->set_uniform_sampler(this->texture_uniform, texture_unit_number);
this->set_uniform4f(this->color_uniform, color.x(), color.y(), color.z(), color.w());

this->shader_base::render(m, va);
Expand Down
1 change: 1 addition & 0 deletions src/ruis/render/opengl/shader_color_pos_tex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace ruis::render_opengl {

class shader_color_pos_tex : public ruis::coloring_texturing_shader, public shader_base
{
GLint texture_uniform;
GLint color_uniform;

public:
Expand Down
6 changes: 5 additions & 1 deletion src/ruis/render/opengl/shader_color_pos_tex_alpha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ shader_color_pos_tex_alpha::shader_color_pos_tex_alpha() :
}
)qwertyuiop"
),
texture_uniform(this->get_uniform("texture0")),
color_uniform(this->get_uniform("uniform_color"))
{}

Expand All @@ -68,11 +69,14 @@ void shader_color_pos_tex_alpha::render(
const ruis::texture_2d& tex
) const
{
constexpr auto texture_unit_number = 0;

ASSERT(dynamic_cast<const texture_2d*>(&tex))
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
static_cast<const texture_2d&>(tex).bind(0);
static_cast<const texture_2d&>(tex).bind(texture_unit_number);
this->bind();

this->set_uniform_sampler(this->texture_uniform, texture_unit_number);
this->set_uniform4f(this->color_uniform, color.x(), color.y(), color.z(), color.w());

this->shader_base::render(m, va);
Expand Down
5 changes: 4 additions & 1 deletion src/ruis/render/opengl/shader_color_pos_tex_alpha.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

namespace ruis::render_opengl {

class shader_color_pos_tex_alpha : public ruis::coloring_texturing_shader, public shader_base
class shader_color_pos_tex_alpha :
public ruis::coloring_texturing_shader, //
public shader_base
{
GLint texture_uniform;
GLint color_uniform;

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

/* ================ LICENSE END ================ */

#include "shader_texture.hpp"
#include "shader_pos_tex.hpp"

#include "index_buffer.hpp"
#include "texture_2d.hpp"
Expand All @@ -29,7 +29,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

using namespace ruis::render_opengl;

shader_texture::shader_texture() :
shader_pos_tex::shader_pos_tex() :
shader_base(
R"qwertyuiop(
attribute vec4 a0; // position
Expand Down Expand Up @@ -58,13 +58,17 @@ shader_texture::shader_texture() :
texture_uniform(this->get_uniform("texture0"))
{}

void shader_texture::render(const r4::matrix4<float>& m, const ruis::vertex_array& va, const ruis::texture_2d& tex)
void shader_pos_tex::render(const r4::matrix4<float>& m, const ruis::vertex_array& va, const ruis::texture_2d& tex)
const
{
constexpr auto texture_unit_number = 0;

ASSERT(dynamic_cast<const texture_2d*>(&tex))
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
static_cast<const texture_2d&>(tex).bind(0);
static_cast<const texture_2d&>(tex).bind(texture_unit_number);
this->bind();

this->set_uniform_sampler(this->texture_uniform, texture_unit_number);

this->shader_base::render(m, va);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

namespace ruis::render_opengl {

class shader_texture : public ruis::texturing_shader, public shader_base
class shader_pos_tex :
public ruis::texturing_shader, //
public shader_base
{
GLint texture_uniform;

public:
shader_texture();
shader_pos_tex();

void render(const r4::matrix4<float>& m, const ruis::vertex_array& va, const ruis::texture_2d& tex) const override;
};
Expand Down

0 comments on commit 1f11c1d

Please sign in to comment.