21 #include "hx3d/graphics/shader.hpp" 23 #include "hx3d/utils/file.hpp" 24 #include "hx3d/utils/log.hpp" 26 #include <glm/gtc/type_ptr.hpp> 38 std::string vertexFile = pathToShader +
".vert.glsl";
39 std::string fragmentFile = pathToShader +
".frag.glsl";
44 if (vertexContent ==
"" || fragmentContent ==
"") {
48 Log.
Info(
"Loading shader `%s`...", pathToShader.c_str());
50 if (
compile(vertexContent, fragmentContent)) {
51 Log.
Info(
"Shader `%s` loaded.", pathToShader.c_str());
53 Log.
Error(
"Shader `%s` has failed.", pathToShader.c_str());
101 shaderId = glCreateShader(type);
102 const GLchar* code = content.c_str();
104 glShaderSource(shaderId, 1, &code, 0);
105 glCompileShader(shaderId);
108 glGetShaderiv(shaderId, GL_COMPILE_STATUS, &error);
110 if (error != GL_TRUE) {
112 glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &error_size);
114 char* error_msg =
new char[error_size + 1];
115 glGetShaderInfoLog(shaderId, error_size, &error_size, error_msg);
116 error_msg[error_size] =
'\0';
121 glDeleteShader(shaderId);
138 glGetProgramiv(
_programID, GL_LINK_STATUS, &error);
140 if (error != GL_TRUE) {
142 glGetProgramiv(
_programID, GL_INFO_LOG_LENGTH, &error_size);
144 char* error_msg =
new char[error_size + 1];
145 glGetShaderInfoLog(
_programID, error_size, &error_size, error_msg);
146 error_msg[error_size] =
'\0';
175 Log.
Error(
"Attribute `%s` does not exists", name.c_str());
184 Log.
Error(
"Uniform `%s` does not exists", name.c_str());
190 GLint attributesNum(0);
191 GLint attributeMaxSize(0);
193 glGetProgramiv(
_programID, GL_ACTIVE_ATTRIBUTES, &attributesNum);
194 glGetProgramiv(
_programID, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &attributeMaxSize);
197 Log.
Shader(
"Attributes: %d", attributesNum);
200 GLchar* buf =
new char[attributeMaxSize + 1];
202 for (GLint i = 0; i < attributesNum; ++i) {
206 glGetActiveAttrib(
_programID, i, attributeMaxSize,
nullptr, &size, &type, buf);
212 GLint loc = glGetAttribLocation(
_programID, buf);
214 Log.
Error(
"Attribute location error: %s", buf);
227 GLint uniformsNum(0);
228 GLint uniformMaxSize(0);
230 glGetProgramiv(
_programID, GL_ACTIVE_UNIFORMS, &uniformsNum);
231 glGetProgramiv(
_programID, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxSize);
237 GLchar* buf =
new char[uniformMaxSize + 1];
239 for (GLint i = 0; i < uniformsNum; ++i) {
243 glGetActiveUniform(
_programID, i, uniformMaxSize,
nullptr, &size, &type, buf);
249 GLint loc = glGetUniformLocation(
_programID, buf);
251 Log.
Error(
"Uniform location error: %s", buf);
304 case GL_SAMPLER_CUBE:
305 return "samplerCube";
313 glUseProgram(shader->_programID);
330 int loc = glGetUniformLocation(
_programID, uniform.c_str());
331 glUniform1f(loc, value);
335 int loc = glGetUniformLocation(
_programID, uniform.c_str());
336 glUniform2f(loc, vector.x, vector.y);
340 int loc = glGetUniformLocation(
_programID, uniform.c_str());
341 glUniform3f(loc, vector.x, vector.y, vector.z);
345 int loc = glGetUniformLocation(
_programID, uniform.c_str());
346 glUniform4f(loc, vector.x, vector.y, vector.z, vector.w);
350 int loc = glGetUniformLocation(
_programID, uniform.c_str());
351 glUniformMatrix3fv(loc, 1, GL_FALSE, glm::value_ptr(matrix));
355 int loc = glGetUniformLocation(
_programID, uniform.c_str());
356 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(matrix));
GLuint _vertexID
Vertex shader ID.
void setUniform4f(std::string uniform, glm::vec4 vector)
Send a vec4 to the shader.
GLuint _programID
Shader program ID.
void setUniform2f(std::string uniform, glm::vec2 vector)
Send a vec2 to the shader.
static void use(Ptr< Shader > shader)
Use the shader as the current shader.
void setUniformMatrix3f(std::string uniform, glm::mat3 matrix)
Send a mat3 to the shader.
static bool _programsAnalyzed
Are the programs analyzed ?
std::string getParameterType(GLenum type)
Get the parameter type (debug mode).
static Ptr< File > loadAsciiFile(std::string path)
Load an ascii file from a path.
bool compile(const std::string &vert, const std::string &frag)
Compile the vertex and fragment shaders.
GLint getUniform(std::string name)
Get the wanted uniform id.
void setUniform1f(std::string uniform, float value)
Send a single float to the shader.
GLuint _fragmentID
Fragment shader ID.
GLuint getProgramID() const
Get the program ID.
void analyzeAttributes()
Analyze the shader attributes (debug mode).
void Error(const std::string fmt,...)
Write an error message.
std::map< std::string, GLint > _activeAttributes
Active attributes map.
void analyzeUniforms()
Analyze the shader uniforms (debug mode).
static hx3d::LogImpl Log
Current log implementation.
void setUniform3f(std::string uniform, glm::vec3 vector)
Send a vec3 to the shader.
std::map< std::string, GLint > _activeUniforms
Active uniforms map.
bool createProgram()
Create the shader program.
static void disable()
Clear the current shader.
static void setProgramAnalyzing(bool value)
Analyse the shaders (debug mode).
Shader(std::string pathToShader)
Create a shader from a path. The path must be without extension, e.g. shaders/test.
GLint getAttribute(std::string name)
Get the wanted attribute id.
void setUniformMatrix4f(std::string uniform, glm::mat4 matrix)
Send a mat4 to the shader.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
void Shader(const std::string fmt,...)
Write a shader message.
void Info(const std::string fmt,...)
Write an info message.