Program.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 28 Dec 2023 00:32:23 +0100
branchv_0
changeset 34 7ea796b00538
parent 29 dc3c102e1264
permissions -rw-r--r--
convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()

/**
 * OHP3D
 * 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());
}