111 lines
2.9 KiB
C++
111 lines
2.9 KiB
C++
|
#include <glad/glad.h>
|
||
|
#include <sstream>
|
||
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
#include "Shader.h"
|
||
|
|
||
|
Shader::Shader(): ID(0){}
|
||
|
|
||
|
Shader::Shader(const char* vPath, const char* fPath)
|
||
|
{
|
||
|
std::string vCode;
|
||
|
std::string fCode;
|
||
|
std::ifstream vShaderFile;
|
||
|
std::ifstream fShaderFile;
|
||
|
|
||
|
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||
|
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||
|
try
|
||
|
{
|
||
|
vShaderFile.open(vPath);
|
||
|
fShaderFile.open(fPath);
|
||
|
std::stringstream vShaderStream, fShaderStream;
|
||
|
|
||
|
vShaderStream << vShaderFile.rdbuf();
|
||
|
fShaderStream << fShaderFile.rdbuf();
|
||
|
|
||
|
vShaderFile.close();
|
||
|
fShaderFile.close();
|
||
|
|
||
|
vCode = vShaderStream.str();
|
||
|
fCode = fShaderStream.str();
|
||
|
}
|
||
|
catch(std::ifstream::failure& e)
|
||
|
{
|
||
|
std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ::" << e.what() << std::endl;
|
||
|
std::cerr << "VPATH::" << vPath << std::endl << "FPATH::" << fPath << std::endl;
|
||
|
}
|
||
|
const char* vShaderCode = vCode.c_str();
|
||
|
const char* fShaderCode = fCode.c_str();
|
||
|
|
||
|
unsigned int vertex, fragment;
|
||
|
int success;
|
||
|
char infoLog[512];
|
||
|
|
||
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||
|
glShaderSource(vertex, 1, &vShaderCode, nullptr);
|
||
|
glCompileShader(vertex);
|
||
|
|
||
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||
|
if(!success)
|
||
|
{
|
||
|
glGetShaderInfoLog(vertex, 512, nullptr, infoLog);
|
||
|
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||
|
};
|
||
|
|
||
|
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||
|
glShaderSource(fragment, 1, &fShaderCode, nullptr);
|
||
|
glCompileShader(fragment);
|
||
|
|
||
|
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
|
||
|
if(!success)
|
||
|
{
|
||
|
glGetShaderInfoLog(fragment, 512, nullptr, infoLog);
|
||
|
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||
|
};
|
||
|
|
||
|
ID = glCreateProgram();
|
||
|
glAttachShader(ID, vertex);
|
||
|
glAttachShader(ID, fragment);
|
||
|
glLinkProgram(ID);
|
||
|
|
||
|
glGetProgramiv(ID, GL_LINK_STATUS, &success);
|
||
|
if(!success)
|
||
|
{
|
||
|
glGetProgramInfoLog(ID, 512, nullptr, infoLog);
|
||
|
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
|
||
|
}
|
||
|
|
||
|
glDeleteShader(vertex);
|
||
|
glDeleteShader(fragment);
|
||
|
}
|
||
|
|
||
|
Shader::~Shader(){};
|
||
|
|
||
|
void Shader::Use()
|
||
|
{
|
||
|
glUseProgram(ID);
|
||
|
}
|
||
|
|
||
|
unsigned int Shader::GetID()
|
||
|
{
|
||
|
return ID;
|
||
|
}
|
||
|
|
||
|
void Shader::SetBool(const char* name, bool value) const
|
||
|
{
|
||
|
glUniform1i(glGetUniformLocation(ID, name), (int)value);
|
||
|
}
|
||
|
void Shader::SetInt(const char* name, int value) const
|
||
|
{
|
||
|
glUniform1i(glGetUniformLocation(ID, name), value);
|
||
|
}
|
||
|
void Shader::SetFloat(const char* name, float value) const
|
||
|
{
|
||
|
glUniform1f(glGetUniformLocation(ID, name), value);
|
||
|
}
|
||
|
void Shader::SetMat4(const char* name, glm::mat4 value) const
|
||
|
{
|
||
|
glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, glm::value_ptr(value));
|
||
|
}
|