Program.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Jan 2024 01:04:15 +0100
branchv_0
changeset 33 3aae6275b683
parent 5 ee4ba9f5a053
permissions -rw-r--r--
xattr: tolerate ENOTSUP (behave like there are no attributes when not supported on given FS)

/**
 * ShaderShark
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <string>
#include <exception>

#include "Program.h"

class Program::Impl {
public:
	GLuint id;
};

Program::Program() : impl(new Impl()) {
	impl->id = glCreateProgram();
}

Program::~Program() {
	glDeleteProgram(impl->id);
	delete impl;
}

void Program::attachShader(const Shader& shader) {
	glAttachShader(impl->id, shader.getId());
}

void Program::detachShader(const Shader& shader) {
	glDetachShader(impl->id, shader.getId());
}

void Program::link() {
	glLinkProgram(impl->id);
	GLint linkStatus;
	glGetProgramiv(impl->id, GL_LINK_STATUS, &linkStatus);
	if (linkStatus != GL_TRUE) {
		char error[512];
		glGetProgramInfoLog(impl->id, sizeof (error), NULL, error);
		throw std::logic_error(
				std::string("GLSL: program failed to link: ") + error);
	}
}

void Program::use() {
	glUseProgram(impl->id);
	checkError();
}

GLint Program::getAttribLocation(const std::string& name) {
	return glGetAttribLocation(impl->id, name.c_str());
}

GLint Program::getUniformLocation(const std::string& name) {
	return glGetUniformLocation(impl->id, name.c_str());
}

GLint Program::getFragDataLocation(const std::string& name) {
	return glGetFragDataLocation(impl->id, name.c_str());
}

void Program::bindFragDataLocation(const std::string& name, const GLint color) {
	glBindFragDataLocation(impl->id, color, name.c_str());
}