3 #include "../app/error.hpp"
8 #include <glm/gtc/type_ptr.hpp>
13 Shader Shader::Vertex(const GLchar *source) {
14 return Shader(GL_VERTEX_SHADER, source);
17 Shader Shader::Fragment(const GLchar *source) {
18 return Shader(GL_FRAGMENT_SHADER, source);
21 Shader::Shader(GLenum type, const GLchar *source)
22 : shader(glCreateShader(type)) {
24 throw GLError("glCreateShader");
26 const GLchar *sources[] = { source };
27 glShaderSource(shader, 1, sources, nullptr);
28 glCompileShader(shader);
29 GLint compiled = GL_FALSE;
30 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
31 if (compiled != GL_TRUE) {
32 int log_length = 0, max_length = 0;
33 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &max_length);
35 std::unique_ptr<char[]> log(new char[max_length]);
36 glGetShaderInfoLog(shader, max_length, &log_length, log.get());
37 log[log_length] = '\0';
38 glDeleteShader(shader);
39 throw GLCompileError("glCompileShader", log.get());
43 Shader::~Shader() noexcept {
45 glDeleteShader(shader);
49 Shader::Shader(Shader &&other) noexcept
50 : shader(other.shader) {
54 Shader &Shader::operator =(Shader &&other) noexcept {
55 std::swap(shader, other.shader);
61 : program(glCreateProgram()) {
63 throw GLError("glCreateProgram");
67 Program::~Program() noexcept {
68 glDeleteProgram(program);
71 void Program::Attach(const Shader &shader) noexcept {
72 glAttachShader(program, shader.shader);
75 void Program::Link() {
76 glLinkProgram(program);
77 GLint linked = GL_FALSE;
78 glGetProgramiv(program, GL_LINK_STATUS, &linked);
79 if (linked != GL_TRUE) {
80 int log_length = 0, max_length = 0;
81 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &max_length);
83 std::unique_ptr<char[]> log(new char[max_length]);
84 glGetProgramInfoLog(program, max_length, &log_length, log.get());
85 log[log_length] = '\0';
86 throw GLCompileError("glLinkProgram", log.get());
90 void Program::Use() noexcept {
91 glUseProgram(program);
94 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
95 return glGetAttribLocation(program, name);
98 GLint Program::UniformLocation(const GLchar *name) const noexcept {
99 return glGetUniformLocation(program, name);
102 void Program::Uniform(GLint loc, GLint val) noexcept {
103 glUniform1i(loc, val);
106 void Program::Uniform(GLint loc, float val) noexcept {
107 glUniform1f(loc, val);
110 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
111 glUniform3fv(loc, 1, glm::value_ptr(val));
114 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
115 glUniform4fv(loc, 1, glm::value_ptr(val));
118 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
119 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));