2023-03-12 16:24:48 +01:00
|
|
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
2022-12-11 19:52:49 +01:00
|
|
|
#ifndef SHADER_CPP
|
|
|
|
|
#define SHADER_CPP
|
2022-12-11 18:49:16 +01:00
|
|
|
|
|
|
|
|
#include <glad/glad.h>
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <iostream>
|
2023-03-12 17:32:42 +01:00
|
|
|
#include "./shader.hpp"
|
2022-12-11 18:49:16 +01:00
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
const std::string Shader::fileToShader(const std::string shaderPath) {
|
2022-12-11 18:49:16 +01:00
|
|
|
// 1. retrieve the vertex/fragment source code from filePath
|
2022-12-11 20:17:11 +01:00
|
|
|
std::string shaderCode;
|
|
|
|
|
std::ifstream shaderFile;
|
2022-12-11 18:49:16 +01:00
|
|
|
// ensure ifstream objects can throw exceptions:
|
2023-03-12 17:32:42 +01:00
|
|
|
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
|
|
|
|
try {
|
2022-12-11 18:49:16 +01:00
|
|
|
// open files
|
2022-12-11 20:17:11 +01:00
|
|
|
shaderFile.open(shaderPath);
|
|
|
|
|
std::stringstream shaderStream;
|
2022-12-11 18:49:16 +01:00
|
|
|
// read file's buffer contents into streams
|
2022-12-11 20:17:11 +01:00
|
|
|
shaderStream << shaderFile.rdbuf();
|
2022-12-11 18:49:16 +01:00
|
|
|
// close file handlers
|
2022-12-11 20:17:11 +01:00
|
|
|
shaderFile.close();
|
2022-12-11 18:49:16 +01:00
|
|
|
// convert stream into string
|
2022-12-11 20:17:11 +01:00
|
|
|
shaderCode = shaderStream.str();
|
2022-12-11 18:49:16 +01:00
|
|
|
}
|
2023-03-12 17:32:42 +01:00
|
|
|
catch (std::ifstream::failure& e) {
|
|
|
|
|
std::cout <<
|
|
|
|
|
"ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: "
|
|
|
|
|
<< e.what() << std::endl;
|
2022-12-11 18:49:16 +01:00
|
|
|
}
|
2022-12-11 20:17:11 +01:00
|
|
|
return shaderCode;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
Shader::Shader(const std::string vertexPath, const std::string fragmentPath) {
|
2022-12-11 20:17:11 +01:00
|
|
|
const std::string vshaderCodeString = this -> fileToShader(vertexPath);
|
2023-03-12 17:32:42 +01:00
|
|
|
|
|
|
|
|
const std::string fShaderCodeString =
|
|
|
|
|
this -> fileToShader(fragmentPath);
|
|
|
|
|
|
2022-12-11 20:17:11 +01:00
|
|
|
const char* vShaderCode = vshaderCodeString.c_str();
|
|
|
|
|
const char * fShaderCode = fShaderCodeString.c_str();
|
2022-12-11 18:49:16 +01:00
|
|
|
// 2. compile shaders
|
|
|
|
|
unsigned int vertex, fragment;
|
|
|
|
|
// vertex shader
|
|
|
|
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
|
|
|
|
glCompileShader(vertex);
|
|
|
|
|
checkCompileErrors(vertex, "VERTEX");
|
|
|
|
|
// fragment Shader
|
|
|
|
|
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
|
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
|
|
|
|
glCompileShader(fragment);
|
|
|
|
|
checkCompileErrors(fragment, "FRAGMENT");
|
|
|
|
|
// shader Program
|
|
|
|
|
ID = glCreateProgram();
|
|
|
|
|
glAttachShader(ID, vertex);
|
|
|
|
|
glAttachShader(ID, fragment);
|
|
|
|
|
glLinkProgram(ID);
|
|
|
|
|
checkCompileErrors(ID, "PROGRAM");
|
2023-03-12 17:32:42 +01:00
|
|
|
// delete the shaders as they're linked
|
|
|
|
|
// into our program now and no longer necessary
|
2022-12-11 18:49:16 +01:00
|
|
|
glDeleteShader(vertex);
|
|
|
|
|
glDeleteShader(fragment);
|
2022-12-11 19:52:49 +01:00
|
|
|
}
|
2022-12-11 18:49:16 +01:00
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
void Shader::use() {
|
|
|
|
|
glUseProgram(ID);
|
2022-12-11 19:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
void Shader::setBool(const std::string &name, bool value) const {
|
|
|
|
|
glUniform1i(
|
|
|
|
|
glGetUniformLocation(ID, name.c_str()),
|
|
|
|
|
reinterpret_cast<int>(value));
|
2022-12-11 19:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
void Shader::setInt(const std::string &name, int value) const {
|
|
|
|
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
2022-12-11 19:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
void Shader::setFloat(const std::string &name, float value) const {
|
|
|
|
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
2022-12-11 19:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
void Shader::checkCompileErrors(unsigned int shader, std::string type) {
|
2022-12-11 19:52:49 +01:00
|
|
|
int success;
|
|
|
|
|
char infoLog[1024];
|
2023-03-12 17:32:42 +01:00
|
|
|
if (type != "PROGRAM") {
|
2022-12-11 19:52:49 +01:00
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
2023-03-12 17:32:42 +01:00
|
|
|
if (!success) {
|
2022-12-11 19:52:49 +01:00
|
|
|
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
2023-03-12 17:32:42 +01:00
|
|
|
std::cout <<
|
|
|
|
|
"ERROR::SHADER_COMPILATION_ERROR of type: "
|
|
|
|
|
<< type << "\n" << infoLog <<
|
|
|
|
|
"\n -- --------------------------------------------------- -- "
|
|
|
|
|
<< std::endl;
|
2022-12-11 18:49:16 +01:00
|
|
|
}
|
2023-03-12 17:32:42 +01:00
|
|
|
} else {
|
2022-12-11 19:52:49 +01:00
|
|
|
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
2023-03-12 17:32:42 +01:00
|
|
|
if (!success) {
|
2022-12-11 19:52:49 +01:00
|
|
|
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
2023-03-12 17:32:42 +01:00
|
|
|
std::cout <<
|
|
|
|
|
"ERROR::PROGRAM_LINKING_ERROR of type: "
|
|
|
|
|
<< type << "\n" << infoLog <<
|
|
|
|
|
"\n -- --------------------------------------------------- -- "
|
|
|
|
|
<< std::endl;
|
2022-12-11 18:49:16 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-11 19:52:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 17:32:42 +01:00
|
|
|
#endif // SHADER_CPP
|