mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 12:02:47 -05:00
Migrate to NanoGUI / Eigen for GUI and linear algebra.
Also introduce a GUI play/pause button.
This commit is contained in:
80
vis/include/cgl/shader.hpp
Normal file
80
vis/include/cgl/shader.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <nanogui/opengl.h>
|
||||
|
||||
#include <cgl/error.hpp>
|
||||
|
||||
#include <util.hpp>
|
||||
|
||||
namespace cgl {
|
||||
template<GLenum mode>
|
||||
class Shader {
|
||||
protected:
|
||||
GLuint id{};
|
||||
|
||||
public:
|
||||
Shader() {
|
||||
id = glCreateShader(mode);
|
||||
}
|
||||
|
||||
Shader(const std::string &src) : Shader() {
|
||||
set_source(src);
|
||||
|
||||
if (!compile())
|
||||
throw ShaderError(get_info_log());
|
||||
}
|
||||
|
||||
static Shader<mode> file(const std::string &name) {
|
||||
return Shader<mode>(utilReadFile(name));
|
||||
}
|
||||
|
||||
Shader(Shader &) = delete;
|
||||
|
||||
Shader(Shader &&o) noexcept {
|
||||
id = std::exchange(o.id, 0);
|
||||
};
|
||||
|
||||
~Shader() {
|
||||
glDeleteShader(id);
|
||||
}
|
||||
|
||||
operator GLuint() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
[[nodiscard]] int get(GLenum pname) const {
|
||||
GLint res;
|
||||
glGetShaderiv(id, pname, &res);
|
||||
return (int) res;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string get_info_log() const {
|
||||
auto len = (size_t) get(GL_INFO_LOG_LENGTH);
|
||||
char buffer[len];
|
||||
glGetShaderInfoLog(id, len, nullptr, buffer);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
void set_source(const std::string &src) {
|
||||
const char *c_src = src.c_str();
|
||||
glShaderSource(id, 1, &c_src, nullptr);
|
||||
}
|
||||
|
||||
bool compile() {
|
||||
glCompileShader(id);
|
||||
return (bool) get(GL_COMPILE_STATUS);
|
||||
}
|
||||
};
|
||||
|
||||
namespace sh {
|
||||
using vert = Shader<GL_VERTEX_SHADER>;
|
||||
using tcs = Shader<GL_TESS_CONTROL_SHADER>;
|
||||
using tes = Shader<GL_TESS_EVALUATION_SHADER>;
|
||||
using geom = Shader<GL_GEOMETRY_SHADER>;
|
||||
using frag = Shader<GL_FRAGMENT_SHADER>;
|
||||
using comp = Shader<GL_COMPUTE_SHADER>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user