2024-11-27 17:51:31 +00:00
|
|
|
// Shader.cpp
|
2024-11-25 23:08:00 +00:00
|
|
|
#include "Shader.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
2024-11-27 17:51:31 +00:00
|
|
|
#include <filesystem> // C++17 feature for file existence
|
2024-11-25 23:08:00 +00:00
|
|
|
|
2024-11-27 17:51:31 +00:00
|
|
|
namespace fs = std::filesystem;
|
2024-11-26 03:49:37 +00:00
|
|
|
|
2024-11-27 17:51:31 +00:00
|
|
|
Shader::Shader(const char* vertexPath, const char* fragmentPath)
|
2024-11-25 23:08:00 +00:00
|
|
|
{
|
|
|
|
// 1. Retrieve the vertex/fragment source code from filePath
|
|
|
|
std::string vertexCode;
|
|
|
|
std::string fragmentCode;
|
|
|
|
std::ifstream vShaderFile;
|
|
|
|
std::ifstream fShaderFile;
|
|
|
|
|
2024-11-27 17:51:31 +00:00
|
|
|
// Check if shader files exist
|
|
|
|
if (!fs::exists(vertexPath)) {
|
|
|
|
std::cerr << "ERROR::SHADER::VERTEX_SHADER_FILE_NOT_FOUND: " << vertexPath << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!fs::exists(fragmentPath)) {
|
|
|
|
std::cerr << "ERROR::SHADER::FRAGMENT_SHADER_FILE_NOT_FOUND: " << fragmentPath << "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure ifstream objects can throw exceptions:
|
|
|
|
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
|
|
|
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
|
|
|
try
|
2024-11-25 23:08:00 +00:00
|
|
|
{
|
|
|
|
// Open files
|
|
|
|
vShaderFile.open(vertexPath);
|
|
|
|
fShaderFile.open(fragmentPath);
|
|
|
|
std::stringstream vShaderStream, fShaderStream;
|
2024-11-27 17:51:31 +00:00
|
|
|
|
2024-11-25 23:08:00 +00:00
|
|
|
// Read file's buffer contents into streams
|
|
|
|
vShaderStream << vShaderFile.rdbuf();
|
2024-11-27 17:51:31 +00:00
|
|
|
fShaderStream << fShaderFile.rdbuf();
|
|
|
|
|
2024-11-25 23:08:00 +00:00
|
|
|
// Close file handlers
|
|
|
|
vShaderFile.close();
|
|
|
|
fShaderFile.close();
|
2024-11-27 17:51:31 +00:00
|
|
|
|
2024-11-25 23:08:00 +00:00
|
|
|
// Convert stream into string
|
|
|
|
vertexCode = vShaderStream.str();
|
2024-11-27 17:51:31 +00:00
|
|
|
fragmentCode = fShaderStream.str();
|
2024-11-25 23:08:00 +00:00
|
|
|
}
|
|
|
|
catch (std::ifstream::failure& e)
|
|
|
|
{
|
|
|
|
std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ\n";
|
2024-11-27 17:51:31 +00:00
|
|
|
std::cerr << "Failed to read shader files: " << vertexPath << " and " << fragmentPath << "\n";
|
|
|
|
return;
|
2024-11-25 23:08:00 +00:00
|
|
|
}
|
2024-11-27 17:51:31 +00:00
|
|
|
const char* vShaderCode = vertexCode.c_str();
|
|
|
|
const char * fShaderCode = fragmentCode.c_str();
|
2024-11-25 23:08:00 +00:00
|
|
|
|
|
|
|
// 2. Compile shaders
|
|
|
|
GLuint vertex, fragment;
|
|
|
|
GLint success;
|
2024-11-27 17:51:31 +00:00
|
|
|
GLchar infoLog[1024];
|
2024-11-25 23:08:00 +00:00
|
|
|
|
|
|
|
// Vertex Shader
|
|
|
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
|
|
|
glCompileShader(vertex);
|
2024-11-27 17:51:31 +00:00
|
|
|
// Print compile errors if any
|
2024-11-25 23:08:00 +00:00
|
|
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
2024-11-27 17:51:31 +00:00
|
|
|
if(!success)
|
2024-11-25 23:08:00 +00:00
|
|
|
{
|
2024-11-27 17:51:31 +00:00
|
|
|
glGetShaderInfoLog(vertex, 1024, NULL, infoLog);
|
|
|
|
std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << "\n";
|
2024-11-25 23:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fragment Shader
|
|
|
|
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
|
|
|
glCompileShader(fragment);
|
2024-11-27 17:51:31 +00:00
|
|
|
// Print compile errors if any
|
2024-11-25 23:08:00 +00:00
|
|
|
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
|
2024-11-27 17:51:31 +00:00
|
|
|
if(!success)
|
2024-11-25 23:08:00 +00:00
|
|
|
{
|
2024-11-27 17:51:31 +00:00
|
|
|
glGetShaderInfoLog(fragment, 1024, NULL, infoLog);
|
|
|
|
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << "\n";
|
2024-11-25 23:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shader Program
|
2024-11-27 17:51:31 +00:00
|
|
|
ID = glCreateProgram();
|
|
|
|
glAttachShader(ID, vertex);
|
|
|
|
glAttachShader(ID, fragment);
|
|
|
|
glLinkProgram(ID);
|
|
|
|
// Print linking errors if any
|
|
|
|
glGetProgramiv(ID, GL_LINK_STATUS, &success);
|
|
|
|
if(!success)
|
2024-11-25 23:08:00 +00:00
|
|
|
{
|
2024-11-27 17:51:31 +00:00
|
|
|
glGetProgramInfoLog(ID, 1024, NULL, infoLog);
|
|
|
|
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << "\n";
|
2024-11-25 23:08:00 +00:00
|
|
|
}
|
|
|
|
|
2024-11-27 17:51:31 +00:00
|
|
|
// Delete the shaders as they're linked into our program now and no longer necessary
|
2024-11-25 23:08:00 +00:00
|
|
|
glDeleteShader(vertex);
|
|
|
|
glDeleteShader(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-27 17:51:31 +00:00
|
|
|
|
|
|
|
void Shader::Use()
|
|
|
|
{
|
|
|
|
glUseProgram(ID);
|
2024-11-25 23:08:00 +00:00
|
|
|
}
|
2024-11-26 03:49:37 +00:00
|
|
|
|
|
|
|
void Shader::SetBool(const std::string &name, bool value) const
|
|
|
|
{
|
2024-11-27 17:51:31 +00:00
|
|
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
2024-11-26 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetInt(const std::string &name, int value) const
|
|
|
|
{
|
2024-11-27 17:51:31 +00:00
|
|
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
2024-11-26 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetFloat(const std::string &name, float value) const
|
|
|
|
{
|
2024-11-27 17:51:31 +00:00
|
|
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
2024-11-26 03:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetVec3(const std::string &name, const glm::vec3 &value) const
|
|
|
|
{
|
2024-11-27 17:51:31 +00:00
|
|
|
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shader::SetMat4(const std::string &name, const glm::mat4 &mat) const
|
|
|
|
{
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
|
2024-11-26 03:49:37 +00:00
|
|
|
}
|