jes/src/gltk/program.d

67 lines
1.4 KiB
D

module gltk.program;
import gltk.shader;
import derelict.opengl;
import std.string;
class Program
{
private GLuint m_id;
this()
{
m_id = glCreateProgram();
if (m_id == 0u)
{
throw new Exception("Failed to allocate an OpenGL program");
}
}
~this()
{
glDeleteProgram(m_id);
}
void attach_shader(Shader shader) const
{
glAttachShader(m_id, shader.id);
}
void link() const
{
glLinkProgram(m_id);
GLint link_status;
glGetProgramiv(m_id, GL_LINK_STATUS, &link_status);
if (link_status != GL_TRUE)
{
string message = "Failed to link program";
GLint log_length = 0;
glGetProgramiv(m_id, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0)
{
char[] log = new char[log_length];
glGetProgramInfoLog(m_id, log_length, &log_length, log.ptr);
message ~= "\n";
message ~= log;
message ~= "\n";
}
throw new Exception(message);
}
}
GLint get_uniform_location(string uniform_name)
{
return glGetUniformLocation(m_id, uniform_name.toStringz());
}
@property GLuint id()
{
return m_id;
}
void use() const
{
glUseProgram(m_id);
}
}