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>
|
2022-12-11 19:52:49 +01:00
|
|
|
#include "shader.hpp"
|
2022-12-11 18:49:16 +01:00
|
|
|
|
2022-12-11 20:17:11 +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:
|
2022-12-11 20:17:11 +01:00
|
|
|
shaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
2022-12-11 18:49:16 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
catch (std::ifstream::failure& e)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
|
|
|
|
|
}
|
2022-12-11 20:17:11 +01:00
|
|
|
return shaderCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
|
|
|
|
|
{
|
|
|
|
|
const std::string vshaderCodeString = this -> fileToShader(vertexPath);
|
|
|
|
|
const std::string fShaderCodeString = this -> fileToShader(fragmentPath);
|
|
|
|
|
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");
|
|
|
|
|
// delete the shaders as they're linked into our program now and no longer necessary
|
|
|
|
|
glDeleteShader(vertex);
|
|
|
|
|
glDeleteShader(fragment);
|
2022-12-11 19:52:49 +01:00
|
|
|
}
|
2022-12-11 18:49:16 +01:00
|
|
|
|
2022-12-11 19:52:49 +01:00
|
|
|
void Shader::use()
|
|
|
|
|
{
|
|
|
|
|
glUseProgram(ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shader::setBool(const std::string &name, bool value) const
|
|
|
|
|
{
|
|
|
|
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shader::setInt(const std::string &name, int value) const
|
|
|
|
|
{
|
|
|
|
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shader::setFloat(const std::string &name, float value) const
|
|
|
|
|
{
|
|
|
|
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shader::checkCompileErrors(unsigned int shader, std::string type)
|
|
|
|
|
{
|
|
|
|
|
int success;
|
|
|
|
|
char infoLog[1024];
|
|
|
|
|
if (type != "PROGRAM")
|
2022-12-11 18:49:16 +01:00
|
|
|
{
|
2022-12-11 19:52:49 +01:00
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
|
|
|
|
if (!success)
|
2022-12-11 18:49:16 +01:00
|
|
|
{
|
2022-12-11 19:52:49 +01:00
|
|
|
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
|
|
|
|
std::cout << "ERROR::SHADER_COMPILATION_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
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
|
|
|
|
if (!success)
|
2022-12-11 18:49:16 +01:00
|
|
|
{
|
2022-12-11 19:52:49 +01:00
|
|
|
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // SHADER_CPP
|