Shader.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 <stdexcept>
       
    20 
       
    21 #include "Shader.h"
       
    22 
       
    23 class Shader::Impl {
       
    24 public:
       
    25 	Shader::Type type;
       
    26 	GLuint id;
       
    27 	std::string fileName;
       
    28 };
       
    29 
       
    30 Shader::Shader(
       
    31 		const Type type,
       
    32 		const Buffer& source,
       
    33 		const std::string& fileName) : impl(new Impl()) {
       
    34 	impl->type = type;
       
    35 	impl->fileName = fileName;
       
    36 	impl->id = glCreateShader((GLenum) impl->type);
       
    37 	update(source);
       
    38 }
       
    39 
       
    40 Shader::~Shader() {
       
    41 	glDeleteShader(impl->id);
       
    42 	delete impl;
       
    43 }
       
    44 
       
    45 GLuint Shader::getId() const {
       
    46 	return impl->id;
       
    47 }
       
    48 
       
    49 const std::string Shader::getFileName() const {
       
    50 	return impl->fileName;
       
    51 }
       
    52 
       
    53 void Shader::update(const Buffer& source) {
       
    54 	auto fileData = source.getData();
       
    55 	GLint fileSize = source.getSize();
       
    56 	glShaderSource(impl->id, 1, &fileData, &fileSize);
       
    57 	glCompileShader(impl->id);
       
    58 
       
    59 	GLint compileStatus;
       
    60 	glGetShaderiv(impl->id, GL_COMPILE_STATUS, &compileStatus);
       
    61 
       
    62 	if (compileStatus != GL_TRUE) {
       
    63 		char error[512];
       
    64 		glGetShaderInfoLog(impl->id, sizeof (error), NULL, error);
       
    65 		throw std::logic_error(
       
    66 				std::string("GLSL: shader failed to compile: ") + error);
       
    67 	}
       
    68 }