1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#ifndef OPENGL_SHADER_PROGRAM_H
#define OPENGL_SHADER_PROGRAM_H
#pragma once
#include "globalincs/pstypes.h"
#include "gropenglshader.h"
#include <glad/glad.h>
namespace opengl {
class ShaderProgram;
class ShaderUniforms {
struct uniform_bind
{
SCP_string name;
int value;
};
ShaderProgram* _program;
SCP_vector<uniform_bind> _uniforms;
SCP_unordered_map<SCP_string, size_t> _uniform_lookup;
SCP_unordered_map<SCP_string, GLint> _uniform_locations;
size_t findUniform(const SCP_string &name);
GLint findUniformLocation(const SCP_string& name);
public:
explicit ShaderUniforms(ShaderProgram* shaderProgram);
void setTextureUniform(const SCP_string &name, const int texture_unit);
};
enum ShaderStage {
STAGE_VERTEX,
STAGE_GEOMETRY,
STAGE_FRAGMENT
};
class ShaderProgram {
GLuint _program_id;
SCP_vector<GLuint> _compiled_shaders;
SCP_unordered_map<opengl_vert_attrib::attrib_id, GLint> _attribute_locations;
void freeCompiledShaders();
public:
explicit ShaderProgram(const SCP_string& program_name);
~ShaderProgram();
ShaderUniforms Uniforms;
ShaderProgram(const ShaderProgram&) = delete;
ShaderProgram& operator=(const ShaderProgram&) = delete;
ShaderProgram(ShaderProgram&& other) noexcept;
ShaderProgram& operator=(ShaderProgram&& other) noexcept;
void use();
void addShaderCode(ShaderStage stage, const SCP_string& name, const SCP_vector<SCP_string>& codeParts);
void linkProgram();
void initAttribute(const SCP_string& name, opengl_vert_attrib::attrib_id attr_id, const vec4& default_value);
GLint getAttributeLocation(opengl_vert_attrib::attrib_id attribute);
GLuint getShaderHandle();
};
}
#endif // OPENGL_SHADER_PROGRAM_H
|