2021-11-27 19:16:41 +00:00
|
|
|
#pragma once
|
2021-11-21 06:23:53 +00:00
|
|
|
|
2021-12-02 21:58:15 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GL/gl.h>
|
2021-12-13 03:17:59 +00:00
|
|
|
#else
|
2021-11-21 06:23:53 +00:00
|
|
|
#include <gl/glew.h>
|
|
|
|
#include <gl/gl.h>
|
2021-12-02 21:58:15 +00:00
|
|
|
#endif
|
2021-11-21 06:23:53 +00:00
|
|
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
#include "event/event.h"
|
2021-11-27 18:52:48 +00:00
|
|
|
#include "gfx/shader.h"
|
2022-11-11 05:44:41 +00:00
|
|
|
#include "log/logger.h"
|
2021-11-21 06:23:53 +00:00
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace simpleengine {
|
|
|
|
class ShaderProgram : public simpleengine::Event {
|
|
|
|
private:
|
|
|
|
using super = simpleengine::Event;
|
|
|
|
public:
|
2021-12-13 03:17:59 +00:00
|
|
|
GLuint program;
|
2021-12-07 03:44:47 +00:00
|
|
|
std::vector<gfx::Shader> shaders;
|
|
|
|
|
2021-12-13 03:17:59 +00:00
|
|
|
ShaderProgram() : program(glCreateProgram()) {
|
2021-11-21 06:23:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ShaderProgram() {
|
2021-12-13 03:17:59 +00:00
|
|
|
glDeleteProgram(program);
|
2021-11-21 06:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a shader to this shader program. Also checks that its using the same `program` as this.
|
|
|
|
*
|
2021-11-27 18:52:48 +00:00
|
|
|
* @see ShaderProgram::add_shader(const gfx::ShaderType& type, const std::string& shader_path)
|
2021-11-21 06:23:53 +00:00
|
|
|
* @param shader The shader to add.
|
|
|
|
* @return ShaderProgram& self
|
|
|
|
*/
|
2021-11-27 18:52:48 +00:00
|
|
|
ShaderProgram& add_shader(gfx::Shader& shader) {
|
2021-11-21 06:23:53 +00:00
|
|
|
if (shader.program != this->program) {
|
2021-11-25 21:15:24 +00:00
|
|
|
throw std::runtime_error("The added shader does not have the same program as this shade program!");
|
2021-11-21 06:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shaders.push_back(shader);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create and add a shader from a string to this shader program.
|
|
|
|
*
|
|
|
|
* @param type The type of the shader.
|
|
|
|
* @param shader_path The path of the shader.
|
|
|
|
* @return ShaderProgram& self
|
|
|
|
*/
|
2021-11-27 18:52:48 +00:00
|
|
|
ShaderProgram& add_shader_from_source(const gfx::ShaderType& type, std::string& shader_source) {
|
|
|
|
gfx::Shader shd = gfx::Shader::from_source(program, type, shader_source);
|
2021-11-21 06:23:53 +00:00
|
|
|
|
|
|
|
shaders.emplace_back(shd);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create and add a shader from a filepath to this shader program.
|
|
|
|
*
|
|
|
|
* @param type The type of the shader.
|
|
|
|
* @param shader_path The path of the shader.
|
|
|
|
* @return ShaderProgram& self
|
|
|
|
*/
|
2021-11-27 18:52:48 +00:00
|
|
|
ShaderProgram& add_shader_from_path(const gfx::ShaderType& type, const std::string& shader_path) {
|
|
|
|
gfx::Shader shd = gfx::Shader::from_filepath(program, type, shader_path);
|
2021-11-21 06:23:53 +00:00
|
|
|
|
|
|
|
shaders.emplace_back(shd);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Link the shader program. Also removes unused shader resources.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void link() {
|
|
|
|
if (shaders.empty()) {
|
2021-11-25 21:15:24 +00:00
|
|
|
throw std::runtime_error("Shaders cannot be empty when running simpleengine::ShaderProgram::link()!");
|
2021-11-21 06:23:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 03:17:59 +00:00
|
|
|
glLinkProgram(program);
|
2021-11-21 06:23:53 +00:00
|
|
|
|
|
|
|
GLint success = false;
|
2021-12-13 03:17:59 +00:00
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
2021-11-21 06:23:53 +00:00
|
|
|
|
|
|
|
if (!success) {
|
2022-11-11 05:44:41 +00:00
|
|
|
SE_CERROR("Failed to link shader program!");
|
2021-11-27 18:52:48 +00:00
|
|
|
throw gfx::ShaderException("Failed to link shader program!");
|
2021-11-21 06:23:53 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 18:52:48 +00:00
|
|
|
for (gfx::Shader& shader : shaders) {
|
2021-11-21 06:23:53 +00:00
|
|
|
shader.delete_shader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void update(const float& delta_time) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2021-11-27 19:16:41 +00:00
|
|
|
}
|