Program Listing for File Shader.hpp

Return to documentation for file (gui/Demeter/Renderer/Shader.hpp)

#pragma once

#include <memory>

#include <GL/glew.h>

class Shader {
protected:
  GLenum _type = 0;    // NOLINT
  GLuint _shader = 0;  // NOLINT

public:
  Shader(GLenum type) : _type(type) {}

  Shader(const Shader &) = delete;
  Shader &operator=(const Shader &) = delete;
  ~Shader();

  [[nodiscard]] bool Init(const std::string &path);

  [[nodiscard]] virtual GLuint Get() const
  {
    return _shader;
  }
};

class VertexShader : public Shader {
public:
  VertexShader() : Shader(GL_VERTEX_SHADER) {}
};

class FragmentShader : public Shader {
public:
  FragmentShader() : Shader(GL_FRAGMENT_SHADER) {}
};

struct ShaderProgram {
private:
  GLuint program = 0;
  std::unique_ptr<VertexShader> vs;
  std::unique_ptr<FragmentShader> fs;

public:
  ShaderProgram() = default;
  ShaderProgram(const ShaderProgram &) = delete;
  ShaderProgram &operator=(const ShaderProgram &) = delete;
  ~ShaderProgram();

  [[nodiscard]] bool Init(
    std::unique_ptr<VertexShader> vertexShader,
    std::unique_ptr<FragmentShader> fragmentShader);

  [[nodiscard]] GLuint Get() const
  {
    return program;
  }

  void Use() const
  {
    glUseProgram(program);
  }

  [[nodiscard]] GLint GetUniformLocation(const std::string &name) const
  {
    return glGetUniformLocation(program, name.c_str());
  }
};