More seperate cc files, font & overlay rendering
This commit is contained in:
142
shaders.hh
142
shaders.hh
@@ -4,22 +4,20 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <glbinding/gl/gl.h>
|
||||
#include <glbinding/gl/types.h>
|
||||
|
||||
#include "common.hh"
|
||||
|
||||
using namespace gl;
|
||||
|
||||
class ShaderException : public GLException {
|
||||
public:
|
||||
ShaderException(std::string const& msg) : GLException(glGetError()), _msg(msg) {
|
||||
}
|
||||
|
||||
virtual ~ShaderException() {}
|
||||
ShaderException(std::string const& msg);
|
||||
|
||||
//~ShaderException() {}
|
||||
|
||||
std::string const& getMsg() const {return _msg;}
|
||||
|
||||
virtual std::string toString() const {
|
||||
std::string toString() const override {
|
||||
return "ShaderException: " + _msg;
|
||||
}
|
||||
|
||||
@@ -33,33 +31,12 @@ class Shader {
|
||||
public:
|
||||
Shader() : _shaderID(0) {
|
||||
}
|
||||
|
||||
Shader(std::string const& program, GLenum type) : _shaderID(glCreateShader(type)) {
|
||||
const char* const arr[] = {program.c_str()};
|
||||
glShaderSource(_shaderID, 1, arr, NULL);
|
||||
glCompileShader(_shaderID);
|
||||
|
||||
int state;
|
||||
glGetShaderiv(_shaderID, GL_COMPILE_STATUS, &state);
|
||||
|
||||
if (!state) {
|
||||
int logLength;
|
||||
glGetShaderiv(_shaderID, GL_INFO_LOG_LENGTH, &logLength);
|
||||
|
||||
char *log = new char[logLength];
|
||||
glGetShaderInfoLog(_shaderID, logLength, NULL, log);
|
||||
std::string msg(log);
|
||||
delete[] log;
|
||||
|
||||
glDeleteShader(_shaderID);
|
||||
|
||||
throw ShaderException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~Shader() {
|
||||
glDeleteShader(_shaderID);
|
||||
}
|
||||
protected:
|
||||
Shader(std::string const& program, gl::GLenum type);
|
||||
|
||||
public:
|
||||
virtual ~Shader();
|
||||
|
||||
unsigned getID() const {
|
||||
return _shaderID;
|
||||
@@ -75,118 +52,33 @@ class VertexShader : public Shader {
|
||||
public:
|
||||
VertexShader(std::string const& program) : Shader(program, GL_VERTEX_SHADER) {
|
||||
}
|
||||
|
||||
~VertexShader() override {
|
||||
}
|
||||
};
|
||||
|
||||
class FragmentShader : public Shader {
|
||||
public:
|
||||
FragmentShader(std::string const& program) : Shader(program, GL_FRAGMENT_SHADER) {
|
||||
}
|
||||
|
||||
virtual ~FragmentShader() {
|
||||
}
|
||||
};
|
||||
|
||||
class GeometryShader : public Shader {
|
||||
public:
|
||||
GeometryShader(std::string const& program) : Shader(program, GL_GEOMETRY_SHADER) {
|
||||
}
|
||||
|
||||
virtual ~GeometryShader() {
|
||||
}
|
||||
};
|
||||
|
||||
class Program {
|
||||
public:
|
||||
Program(VertexShader& vertex, FragmentShader& frag)
|
||||
: _geom(nullptr), _vertex(vertex), _frag(frag), _progID(glCreateProgram()) {
|
||||
glAttachShader(_progID, _vertex.getID());
|
||||
glAttachShader(_progID, _frag.getID());
|
||||
Program(VertexShader& vertex, FragmentShader& frag);
|
||||
|
||||
glLinkProgram(_progID);
|
||||
|
||||
int state;
|
||||
glGetProgramiv(_progID, GL_LINK_STATUS, &state);
|
||||
|
||||
if (!state) {
|
||||
int logLength;
|
||||
glGetProgramiv(_progID, GL_INFO_LOG_LENGTH, &logLength);
|
||||
|
||||
char *log = new char[logLength];
|
||||
glGetProgramInfoLog(_progID, logLength, NULL, log);
|
||||
std::string msg(log);
|
||||
delete[] log;
|
||||
|
||||
glDeleteProgram(_progID);
|
||||
|
||||
throw ShaderException(msg);
|
||||
}
|
||||
|
||||
glDetachShader(_progID, _vertex.getID());
|
||||
glDetachShader(_progID, _frag.getID());
|
||||
}
|
||||
|
||||
Program(GeometryShader& geom, VertexShader& vertex, FragmentShader& frag)
|
||||
: _geom(&geom), _vertex(vertex), _frag(frag), _progID(glCreateProgram()) {
|
||||
glAttachShader(_progID, _geom->getID());
|
||||
glAttachShader(_progID, _vertex.getID());
|
||||
glAttachShader(_progID, _frag.getID());
|
||||
|
||||
glLinkProgram(_progID);
|
||||
|
||||
int state;
|
||||
glGetProgramiv(_progID, GL_LINK_STATUS, &state);
|
||||
|
||||
if (!state) {
|
||||
int logLength;
|
||||
glGetProgramiv(_progID, GL_INFO_LOG_LENGTH, &logLength);
|
||||
|
||||
char *log = new char[logLength];
|
||||
glGetProgramInfoLog(_progID, logLength, NULL, log);
|
||||
std::string msg(log);
|
||||
delete[] log;
|
||||
|
||||
glDeleteProgram(_progID);
|
||||
|
||||
throw ShaderException(msg);
|
||||
}
|
||||
|
||||
glDetachShader(_progID, _geom->getID());
|
||||
glDetachShader(_progID, _vertex.getID());
|
||||
glDetachShader(_progID, _frag.getID());
|
||||
}
|
||||
Program(GeometryShader& geom, VertexShader& vertex, FragmentShader& frag);
|
||||
|
||||
~Program() {
|
||||
glDeleteProgram(_progID);
|
||||
}
|
||||
~Program();
|
||||
|
||||
void use() const {
|
||||
glUseProgram(_progID);
|
||||
}
|
||||
void use() const;
|
||||
|
||||
GLint getUniformLocation(std::string const& name) const {
|
||||
auto search = _uniformLocCache.find(name);
|
||||
if (search != _uniformLocCache.end())
|
||||
return search->second;
|
||||
|
||||
GLint ret = glGetUniformLocation(_progID, name.c_str());
|
||||
if (ret != -1)
|
||||
_uniformLocCache.emplace(name, ret);
|
||||
return ret;
|
||||
}
|
||||
gl::GLint getUniformLocation(std::string const& name) const;
|
||||
|
||||
GLint getAttribLocation(std::string const& name) const {
|
||||
auto search = _attribLocCache.find(name);
|
||||
if (search != _attribLocCache.end())
|
||||
return search->second;
|
||||
|
||||
GLint ret = glGetAttribLocation(_progID, name.c_str());
|
||||
if (ret != -1)
|
||||
_attribLocCache.emplace(name, ret);
|
||||
return ret;
|
||||
}
|
||||
gl::GLint getAttribLocation(std::string const& name) const;
|
||||
|
||||
private:
|
||||
GeometryShader* _geom;
|
||||
@@ -194,7 +86,7 @@ private:
|
||||
FragmentShader& _frag;
|
||||
unsigned _progID;
|
||||
|
||||
mutable std::unordered_map<std::string, GLint> _uniformLocCache, _attribLocCache;
|
||||
mutable std::unordered_map<std::string, gl::GLint> _uniformLocCache, _attribLocCache;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user