BIG REFACTOR Part 2
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
@@ -17,7 +18,7 @@
|
||||
#endif
|
||||
|
||||
ShaderProgram::ShaderProgram() :
|
||||
programID(0), vertexShaderID(0), fragmentShaderID(0) {
|
||||
m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram() {
|
||||
@@ -25,7 +26,7 @@ ShaderProgram::~ShaderProgram() {
|
||||
}
|
||||
|
||||
void ShaderProgram::start() const {
|
||||
glUseProgram(programID);
|
||||
glUseProgram(m_ProgramID);
|
||||
}
|
||||
|
||||
void ShaderProgram::stop() const {
|
||||
@@ -33,74 +34,78 @@ void ShaderProgram::stop() const {
|
||||
}
|
||||
|
||||
int ShaderProgram::getUniformLocation(const std::string& uniformName) const {
|
||||
const int location = glGetUniformLocation(programID, uniformName.c_str());
|
||||
const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
|
||||
if (location == -1) {
|
||||
std::cout << "Warning ! Uniform variable " << uniformName << " not found !\n";
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
void ShaderProgram::loadFloat(const int location, const float value) const {
|
||||
glUniform1f(location, value);
|
||||
void ShaderProgram::loadFloat(unsigned int location, float value) const {
|
||||
glUniform1f(static_cast<GLint>(location), value);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadInt(const int& location, const int& value) const {
|
||||
glUniform1i(location, value);
|
||||
void ShaderProgram::loadInt(unsigned int location, int value) const {
|
||||
glUniform1i(static_cast<GLint>(location), value);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadVector(const int& location,
|
||||
void ShaderProgram::loadVector(unsigned int location,
|
||||
const glm::vec2& vector) const {
|
||||
glUniform2f(location, vector.x, vector.y);
|
||||
glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadVector(const int& location,
|
||||
void ShaderProgram::loadVector(unsigned int location,
|
||||
const glm::vec3& vector) const {
|
||||
glUniform3f(location, vector.x, vector.y, vector.z);
|
||||
glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadVector(const int& location,
|
||||
void ShaderProgram::loadVector(unsigned int location,
|
||||
const glm::vec4& vector) const {
|
||||
glUniform4f(location, vector.x, vector.y, vector.z, vector.w);
|
||||
glUniform4f(static_cast<GLint>(location), vector.x, vector.y, vector.z, vector.w);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadBoolean(const int& location, const bool& value) const {
|
||||
glUniform1i(location, value);
|
||||
void ShaderProgram::loadBoolean(unsigned int location, bool value) const {
|
||||
glUniform1i(static_cast<GLint>(location), value);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadMatrix(unsigned int location, const glm::mat4& matrix) const {
|
||||
glUniformMatrix4fv(static_cast<GLint>(location), 1, false, glm::value_ptr(matrix));
|
||||
}
|
||||
|
||||
void ShaderProgram::cleanUp() const {
|
||||
stop();
|
||||
glDetachShader(programID, vertexShaderID);
|
||||
glDetachShader(programID, fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteProgram(programID);
|
||||
glDetachShader(m_ProgramID, m_VertexShaderID);
|
||||
glDetachShader(m_ProgramID, m_FragmentShaderID);
|
||||
glDeleteShader(m_VertexShaderID);
|
||||
glDeleteShader(m_FragmentShaderID);
|
||||
glDeleteProgram(m_ProgramID);
|
||||
}
|
||||
|
||||
void ShaderProgram::loadProgramFile(const std::string& vertexFile,
|
||||
const std::string& fragmentFile) {
|
||||
vertexShaderID = loadShaderFromFile(vertexFile, GL_VERTEX_SHADER);
|
||||
fragmentShaderID = loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER);
|
||||
programID = glCreateProgram();
|
||||
glAttachShader(programID, vertexShaderID);
|
||||
glAttachShader(programID, fragmentShaderID);
|
||||
glLinkProgram(programID);
|
||||
glValidateProgram(programID);
|
||||
m_VertexShaderID = static_cast<unsigned int>(loadShaderFromFile(vertexFile, GL_VERTEX_SHADER));
|
||||
m_FragmentShaderID = static_cast<unsigned int>(loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER));
|
||||
m_ProgramID = glCreateProgram();
|
||||
glAttachShader(m_ProgramID, m_VertexShaderID);
|
||||
glAttachShader(m_ProgramID, m_FragmentShaderID);
|
||||
glLinkProgram(m_ProgramID);
|
||||
glValidateProgram(m_ProgramID);
|
||||
getAllUniformLocation();
|
||||
}
|
||||
|
||||
void ShaderProgram::loadProgram(const std::string& vertexSource,
|
||||
const std::string& fragmentSource) {
|
||||
vertexShaderID = loadShader(vertexSource, GL_VERTEX_SHADER);
|
||||
fragmentShaderID = loadShader(fragmentSource, GL_FRAGMENT_SHADER);
|
||||
programID = glCreateProgram();
|
||||
glAttachShader(programID, vertexShaderID);
|
||||
glAttachShader(programID, fragmentShaderID);
|
||||
glLinkProgram(programID);
|
||||
glValidateProgram(programID);
|
||||
m_VertexShaderID = static_cast<unsigned int>(loadShader(vertexSource, GL_VERTEX_SHADER));
|
||||
m_FragmentShaderID = static_cast<unsigned int>(loadShader(fragmentSource, GL_FRAGMENT_SHADER));
|
||||
m_ProgramID = glCreateProgram();
|
||||
glAttachShader(m_ProgramID, m_VertexShaderID);
|
||||
glAttachShader(m_ProgramID, m_FragmentShaderID);
|
||||
glLinkProgram(m_ProgramID);
|
||||
glValidateProgram(m_ProgramID);
|
||||
getAllUniformLocation();
|
||||
}
|
||||
|
||||
int ShaderProgram::loadShader(const std::string& source, GLenum type) {
|
||||
unsigned int ShaderProgram::loadShader(const std::string& source, GLenum type) {
|
||||
unsigned int shaderID = glCreateShader(type);
|
||||
|
||||
const char* c_str = source.c_str();
|
||||
@@ -113,9 +118,9 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type) {
|
||||
std::cout << "Could not compile shader !\n";
|
||||
GLsizei size;
|
||||
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
|
||||
char error[size];
|
||||
glGetShaderInfoLog(shaderID, size, &size, error);
|
||||
std::cout << error << std::endl;
|
||||
std::vector<char> shaderError(static_cast<std::size_t>(size));
|
||||
glGetShaderInfoLog(shaderID, size, &size, shaderError.data());
|
||||
std::cout << shaderError.data() << std::endl;
|
||||
#ifdef __ANDROID__
|
||||
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "Could not compile shader !\n %s", error);
|
||||
#endif
|
||||
@@ -123,35 +128,15 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type) {
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) {
|
||||
std::string shaderSource = "";
|
||||
unsigned int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) {
|
||||
std::stringstream stream;
|
||||
std::ifstream fileStream(file);
|
||||
if (fileStream.is_open()) {
|
||||
std::string line;
|
||||
while (getline(fileStream, line)) {
|
||||
shaderSource += line + "\n";
|
||||
}
|
||||
fileStream.close();
|
||||
}
|
||||
unsigned int shaderID = glCreateShader(type);
|
||||
|
||||
const char* c_str = shaderSource.c_str();
|
||||
int* null = 0;
|
||||
glShaderSource(shaderID, 1, &c_str, null); // @suppress("Function cannot be resolved")
|
||||
glCompileShader(shaderID);
|
||||
GLint compilesuccessful;
|
||||
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
|
||||
if (compilesuccessful == false) {
|
||||
std::cout << "Could not compile shader !\n";
|
||||
GLsizei size;
|
||||
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
|
||||
char error[size];
|
||||
glGetShaderInfoLog(shaderID, size, &size, error);
|
||||
std::cout << error << std::endl;
|
||||
if(fileStream) {
|
||||
stream << fileStream.rdbuf();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
void ShaderProgram::loadMatrix(const int& location, const glm::mat4& matrix) {
|
||||
glUniformMatrix4fv(location, 1, false, glm::value_ptr(matrix));
|
||||
return loadShader(stream.str(), type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user