Allow passing shaders and attribute locations to Program constructor.

This commit is contained in:
Josh Holtrop 2020-12-07 15:40:11 -05:00
parent b16d6fa43f
commit 52d7eb5eab
2 changed files with 26 additions and 9 deletions

View File

@ -7,13 +7,17 @@ class Program
{ {
private GLuint m_id; private GLuint m_id;
this() this(Args...)(Args args)
{ {
m_id = glCreateProgram(); m_id = glCreateProgram();
if (m_id == 0u) if (m_id == 0u)
{ {
throw new Exception("Failed to allocate an OpenGL program"); throw new Exception("Failed to allocate an OpenGL program");
} }
static if (args.length != 0u)
{
build(args);
}
} }
~this() ~this()
@ -26,7 +30,7 @@ class Program
glAttachShader(m_id, shader.id); glAttachShader(m_id, shader.id);
} }
void bind_attrib_location(uint index, string name) void bind_attrib_location(string name, uint index) const
{ {
glBindAttribLocation(m_id, index, name.toStringz()); glBindAttribLocation(m_id, index, name.toStringz());
} }
@ -54,12 +58,12 @@ class Program
} }
} }
GLint get_uniform_location(string uniform_name) GLint get_uniform_location(string uniform_name) const
{ {
return glGetUniformLocation(m_id, uniform_name.toStringz()); return glGetUniformLocation(m_id, uniform_name.toStringz());
} }
@property GLuint id() @property GLuint id() const
{ {
return m_id; return m_id;
} }
@ -68,4 +72,21 @@ class Program
{ {
glUseProgram(m_id); glUseProgram(m_id);
} }
private void build(Args...)(Shader s, Args args) const
{
attach_shader(s);
build(args);
}
private void build(Args...)(string attrib_name, uint index, Args args) const
{
bind_attrib_location(attrib_name, index);
build(args);
}
private void build() const
{
link();
}
} }

View File

@ -47,11 +47,7 @@ class Gui
v_shader.set_source_from_file("share/jes/shaders/text.v.glsl"); v_shader.set_source_from_file("share/jes/shaders/text.v.glsl");
auto f_shader = new gltk.Shader(GL_FRAGMENT_SHADER); auto f_shader = new gltk.Shader(GL_FRAGMENT_SHADER);
f_shader.set_source_from_file("share/jes/shaders/text.f.glsl"); f_shader.set_source_from_file("share/jes/shaders/text.f.glsl");
m_text_program = new gltk.Program(); m_text_program = new gltk.Program(v_shader, f_shader, "coords", 0);
m_text_program.attach_shader(v_shader);
m_text_program.attach_shader(f_shader);
m_text_program.bind_attrib_location(0u, "coords");
m_text_program.link();
m_viewport_size = m_text_program.get_uniform_location("viewport_size"); m_viewport_size = m_text_program.get_uniform_location("viewport_size");
m_texture = m_text_program.get_uniform_location("texture"); m_texture = m_text_program.get_uniform_location("texture");
m_color = m_text_program.get_uniform_location("color"); m_color = m_text_program.get_uniform_location("color");