mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 12:02:47 -05:00
move cgl to separate repo
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
namespace cgl {
|
||||
template<class T>
|
||||
class Buffer {
|
||||
GLuint id{};
|
||||
|
||||
public:
|
||||
Buffer() {
|
||||
glCreateBuffers(1, &id);
|
||||
}
|
||||
|
||||
Buffer(const T &data, GLenum usage = GL_STATIC_DRAW)
|
||||
: Buffer() {
|
||||
put(data, usage);
|
||||
}
|
||||
|
||||
Buffer(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW)
|
||||
: Buffer() {
|
||||
put(data, usage);
|
||||
}
|
||||
|
||||
Buffer(const Buffer &) = delete;
|
||||
|
||||
Buffer(Buffer &&o) noexcept {
|
||||
id = std::exchange(o.id, 0);
|
||||
};
|
||||
|
||||
~Buffer() {
|
||||
glDeleteBuffers(1, &id);
|
||||
id = 0;
|
||||
}
|
||||
|
||||
operator GLuint() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t size() const {
|
||||
GLint res;
|
||||
glGetNamedBufferParameteriv(id, GL_BUFFER_SIZE, &res);
|
||||
return (size_t) res;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t count() const {
|
||||
return size() / sizeof(T);
|
||||
}
|
||||
|
||||
void put(const T &data, GLenum usage = GL_STATIC_DRAW) {
|
||||
glNamedBufferData(id, sizeof(T), &data, usage);
|
||||
}
|
||||
|
||||
void put(const std::vector<T> &data, GLenum usage = GL_STATIC_DRAW) {
|
||||
glNamedBufferData(id, sizeof(T) * data.size(), &data[0], usage);
|
||||
}
|
||||
|
||||
void bound(GLenum target, const std::function<void()> &action) const {
|
||||
glBindBuffer(target, id);
|
||||
action();
|
||||
glBindBuffer(target, 0);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
namespace cgl {
|
||||
class GlError : public std::domain_error {
|
||||
public:
|
||||
explicit GlError(const std::string &arg) : domain_error(arg) {}
|
||||
|
||||
explicit GlError(const char *string) : domain_error(string) {}
|
||||
};
|
||||
|
||||
class ShaderError : public GlError {
|
||||
public:
|
||||
explicit ShaderError(const std::string &arg) : GlError(arg) {}
|
||||
|
||||
explicit ShaderError(const char *string) : GlError(string) {}
|
||||
};
|
||||
|
||||
class ProgramError : public GlError {
|
||||
public:
|
||||
explicit ProgramError(const std::string &arg) : GlError(arg) {}
|
||||
|
||||
explicit ProgramError(const char *string) : GlError(string) {}
|
||||
};
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <cgl/error.hpp>
|
||||
#include <cgl/shaderprogram.hpp>
|
||||
|
||||
#include <util.hpp>
|
||||
|
||||
namespace cgl {
|
||||
class pipeline {
|
||||
protected:
|
||||
GLuint id{};
|
||||
|
||||
public:
|
||||
pipeline() {
|
||||
glCreateProgramPipelines(1, &id);
|
||||
}
|
||||
|
||||
pipeline(pipeline &) = delete;
|
||||
|
||||
pipeline(pipeline &&o) noexcept {
|
||||
id = std::exchange(o.id, 0);
|
||||
}
|
||||
|
||||
~pipeline() {
|
||||
glDeleteProgramPipelines(1, &id);
|
||||
id = 0;
|
||||
}
|
||||
|
||||
operator GLuint() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
[[nodiscard]] int get(GLenum pname) const {
|
||||
GLint res;
|
||||
glGetProgramPipelineiv(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];
|
||||
glGetProgramPipelineInfoLog(id, len, nullptr, buffer);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
pipeline &unstage(GLenum stage_bits) {
|
||||
glUseProgramStages(id, stage_bits, 0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pipeline &stage(const ShaderProgram<GL_VERTEX_SHADER> &pgm) {
|
||||
glUseProgramStages(id, GL_VERTEX_SHADER_BIT, pgm);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pipeline &stage(const ShaderProgram<GL_TESS_CONTROL_SHADER> &pgm) {
|
||||
glUseProgramStages(id, GL_TESS_CONTROL_SHADER_BIT, pgm);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pipeline &stage(const ShaderProgram<GL_TESS_EVALUATION_SHADER> &pgm) {
|
||||
glUseProgramStages(id, GL_TESS_EVALUATION_SHADER_BIT, pgm);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pipeline &stage(const ShaderProgram<GL_GEOMETRY_SHADER> &pgm) {
|
||||
glUseProgramStages(id, GL_GEOMETRY_SHADER_BIT, pgm);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pipeline &stage(const ShaderProgram<GL_FRAGMENT_SHADER> &pgm) {
|
||||
glUseProgramStages(id, GL_FRAGMENT_SHADER_BIT, pgm);
|
||||
return *this;
|
||||
}
|
||||
|
||||
pipeline &stage(const ShaderProgram<GL_COMPUTE_SHADER> &pgm) {
|
||||
glUseProgramStages(id, GL_COMPUTE_SHADER_BIT, pgm);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void bound(const std::function<void()> &action) const {
|
||||
glBindProgramPipeline(id);
|
||||
action();
|
||||
glBindProgramPipeline(0);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <cgl/error.hpp>
|
||||
#include <cgl/shader.hpp>
|
||||
|
||||
#include <util.hpp>
|
||||
|
||||
namespace cgl {
|
||||
class Program {
|
||||
protected:
|
||||
GLuint id{};
|
||||
|
||||
public:
|
||||
Program() {
|
||||
id = glCreateProgram();
|
||||
}
|
||||
|
||||
Program(Program &) = delete;
|
||||
|
||||
Program(Program &&o) noexcept {
|
||||
id = std::exchange(o.id, 0);
|
||||
};
|
||||
|
||||
~Program() {
|
||||
glDeleteProgram(id);
|
||||
}
|
||||
|
||||
operator GLuint() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
[[nodiscard]] int get(GLenum pname) const {
|
||||
GLint res;
|
||||
glGetProgramiv(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];
|
||||
glGetProgramInfoLog(id, len, nullptr, buffer);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
template<GLenum mode>
|
||||
void attach(const Shader<mode> &sh) {
|
||||
glAttachShader(id, sh);
|
||||
}
|
||||
|
||||
template<GLenum mode>
|
||||
void detach(const Shader<mode> &sh) {
|
||||
glDetachShader(id, sh);
|
||||
}
|
||||
|
||||
bool link() {
|
||||
glLinkProgram(id);
|
||||
return (bool) get(GL_LINK_STATUS);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <glad/glad.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>;
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <cgl/error.hpp>
|
||||
#include <cgl/shader.hpp>
|
||||
#include <cgl/program.hpp>
|
||||
|
||||
#include <util.hpp>
|
||||
|
||||
namespace cgl{
|
||||
template<GLenum mode>
|
||||
class ShaderProgram : public Program {
|
||||
public:
|
||||
ShaderProgram() : Program() {
|
||||
glProgramParameteri(id, GL_PROGRAM_SEPARABLE, GL_TRUE);
|
||||
}
|
||||
|
||||
ShaderProgram(const std::string &src) : ShaderProgram() {
|
||||
Shader<mode> sh(src);
|
||||
|
||||
attach(sh);
|
||||
|
||||
if (!link())
|
||||
throw ShaderError(get_info_log());
|
||||
|
||||
detach(sh);
|
||||
}
|
||||
|
||||
static ShaderProgram<mode> file(const std::string &name) {
|
||||
return ShaderProgram<mode>(utilReadFile(name));
|
||||
}
|
||||
};
|
||||
|
||||
namespace pgm {
|
||||
using vert = ShaderProgram<GL_VERTEX_SHADER>;
|
||||
using tcs = ShaderProgram<GL_TESS_CONTROL_SHADER>;
|
||||
using tes = ShaderProgram<GL_TESS_EVALUATION_SHADER>;
|
||||
using geom = ShaderProgram<GL_GEOMETRY_SHADER>;
|
||||
using frag = ShaderProgram<GL_FRAGMENT_SHADER>;
|
||||
using comp = ShaderProgram<GL_COMPUTE_SHADER>;
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <cgl/error.hpp>
|
||||
#include <cgl/buffer.hpp>
|
||||
|
||||
namespace cgl {
|
||||
class VertexArray {
|
||||
GLuint id{};
|
||||
|
||||
public:
|
||||
VertexArray() {
|
||||
glCreateVertexArrays(1, &id);
|
||||
}
|
||||
|
||||
VertexArray(VertexArray &) = delete;
|
||||
|
||||
VertexArray(VertexArray &&o) noexcept {
|
||||
id = std::exchange(o.id, 0);
|
||||
}
|
||||
|
||||
~VertexArray() {
|
||||
glDeleteVertexArrays(1, &id);
|
||||
id = 0;
|
||||
}
|
||||
|
||||
operator GLuint() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
void bound(const std::function<void()> &action) const {
|
||||
glBindVertexArray(id);
|
||||
action();
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void pointer(
|
||||
GLuint index,
|
||||
const Buffer<T> &buf,
|
||||
unsigned size,
|
||||
GLenum type,
|
||||
bool normalized = false,
|
||||
unsigned stride = 0
|
||||
) const {
|
||||
bound([&]() {
|
||||
glEnableVertexAttribArray(index);
|
||||
buf.bound(GL_ARRAY_BUFFER, [&]() {
|
||||
glVertexAttribPointer(index, size, type, normalized, stride, nullptr);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void ipointer(
|
||||
GLuint index,
|
||||
const Buffer<T> &buf,
|
||||
unsigned size,
|
||||
GLenum type,
|
||||
unsigned stride = 0
|
||||
) const {
|
||||
bound([&]() {
|
||||
glEnableVertexAttribArray(index);
|
||||
buf.bound(GL_ARRAY_BUFFER, [&]() {
|
||||
glVertexAttribIPointer(index, size, type, stride, nullptr);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user