Program.cpp
branchv_0
changeset 5 ee4ba9f5a053
parent 3 48dc4ae894b0
equal deleted inserted replaced
4:9aba96f0b001 5:ee4ba9f5a053
       
     1 /**
       
     2  * ShaderShark
       
     3  * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include <string>
       
    19 #include <exception>
       
    20 
       
    21 #include "Program.h"
       
    22 
       
    23 class Program::Impl {
       
    24 public:
       
    25 	GLuint id;
       
    26 };
       
    27 
       
    28 Program::Program() : impl(new Impl()) {
       
    29 	impl->id = glCreateProgram();
       
    30 }
       
    31 
       
    32 Program::~Program() {
       
    33 	glDeleteProgram(impl->id);
       
    34 	delete impl;
       
    35 }
       
    36 
       
    37 void Program::attachShader(const Shader& shader) {
       
    38 	glAttachShader(impl->id, shader.getId());
       
    39 }
       
    40 
       
    41 void Program::detachShader(const Shader& shader) {
       
    42 	glDetachShader(impl->id, shader.getId());
       
    43 }
       
    44 
       
    45 void Program::link() {
       
    46 	glLinkProgram(impl->id);
       
    47 	GLint linkStatus;
       
    48 	glGetProgramiv(impl->id, GL_LINK_STATUS, &linkStatus);
       
    49 	if (linkStatus != GL_TRUE) {
       
    50 		char error[512];
       
    51 		glGetProgramInfoLog(impl->id, sizeof (error), NULL, error);
       
    52 		throw std::logic_error(
       
    53 				std::string("GLSL: program failed to link: ") + error);
       
    54 	}
       
    55 }
       
    56 
       
    57 void Program::use() {
       
    58 	glUseProgram(impl->id);
       
    59 	checkError();
       
    60 }
       
    61 
       
    62 GLint Program::getAttribLocation(const std::string& name) {
       
    63 	return glGetAttribLocation(impl->id, name.c_str());
       
    64 }
       
    65 
       
    66 GLint Program::getUniformLocation(const std::string& name) {
       
    67 	return glGetUniformLocation(impl->id, name.c_str());
       
    68 }
       
    69 
       
    70 GLint Program::getFragDataLocation(const std::string& name) {
       
    71 	return glGetFragDataLocation(impl->id, name.c_str());
       
    72 }
       
    73 
       
    74 void Program::bindFragDataLocation(const std::string& name, const GLint color) {
       
    75 	glBindFragDataLocation(impl->id, color, name.c_str());
       
    76 }