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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#pragma once
#include "glGrib/OpenGL.h"
#include "glGrib/Options.h"
#include <string>
#include <glm/glm.hpp>
namespace glGrib
{
class Program
{
public:
static void clearCache ();
static Program * load (const std::string &);
void set (const std::string & key, bool b)
{
set (key, b ? 1 : 0);
}
void set (const std::string & key, const OptionColor & color)
{
set (key, float (color.r) / 255.0f, float (color.g) / 255.0f,
float (color.b) / 255.0f, float (color.a) / 255.0f);
}
void set (const std::string & key, float val)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform1f (id, val);
}
void set (const std::string & key, double val)
{
set (key, float (val));
}
void set (const std::string & key, const std::vector<float> & val)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform1fv (id, val.size (), val.data ());
}
void set (const std::string & key, const std::vector<glm::vec4> & val)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform4fv (id, val.size (), &val[0][0]);
}
void set (const std::string & key, const std::vector<int> & val)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform1iv (id, val.size (), val.data ());
}
void set (const std::string & key, long int val)
{
set (key, int (val));
}
void set (const std::string & key, size_t val)
{
set (key, int (val));
}
void set (const std::string & key, int val)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform1i (id, val);
}
void set (const std::string & key, const glm::vec3 & val)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform3f (id, val.x, val.y, val.z);
}
void set (const std::string & key, float x, float y, float z)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform3f (id, x, y, z);
}
void set (const std::string & key, const glm::vec4 & val)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform4f (id, val.x, val.y, val.z, val.w);
}
void set (const std::string & key, float x, float y, float z, float w)
{
int id = getUniformLocation (key);
if (id != -1)
glUniform4f (id, x, y, z, w);
}
void set (const std::string & key, const glm::mat4 & mat)
{
int id = getUniformLocation (key);
if (id != -1)
glUniformMatrix4fv (id, 1, GL_FALSE, &mat[0][0]);
}
void set (const std::string & key, const glm::mat3 & mat)
{
int id = getUniformLocation (key);
if (id != -1)
glUniformMatrix3fv (id, 1, GL_FALSE, &mat[0][0]);
}
void set (const OptionsLight &);
void compile ();
virtual ~Program ();
void use () const;
void read (const std::string &);
GLint getAttributeLocation (const std::string & name) const
{
return glGetAttribLocation (programID, name.c_str ());
}
GLuint getId ()
{
return programID;
}
Program () {}
private:
explicit Program (const std::string & fsc, const std::string & vsc, const std::string & gsc)
: FragmentShaderCode (fsc), VertexShaderCode (vsc), GeometryShaderCode (gsc) { }
explicit Program (const std::string & fsc, const std::string & vsc)
: FragmentShaderCode (fsc), VertexShaderCode (vsc), GeometryShaderCode ("") { }
std::string FragmentShaderCode;
std::string VertexShaderCode;
std::string GeometryShaderCode;
GLuint programID;
bool loaded = false;
mutable bool active = false;
std::map<std::string,int> uniformLocationCache;
bool hasUniform (const std::string & key) const
{
return uniformLocationCache.find (key) != uniformLocationCache.end ();
}
int getUniformLocation (const std::string & key)
{
auto it = uniformLocationCache.find (key);
if (it != uniformLocationCache.end ())
return it->second;
int id = glGetUniformLocation (programID, key.c_str ());
uniformLocationCache.insert (std::pair<std::string,int> (key, id));
return id;
}
std::string name;
};
}
|