|
|
@@ -24,99 +24,10 @@
|
|
|
#include "game/util/files.hpp"
|
|
|
#include "game/util/time.hpp"
|
|
|
|
|
|
-namespace {
|
|
|
- typedef void (*GLGetIntFor)(GLuint, GLenum, GLint *);
|
|
|
- typedef void (*GLGetLog)(GLuint, GLsizei, GLsizei *, GLchar *);
|
|
|
- struct error_formatter {
|
|
|
- void operator()() const;
|
|
|
- static void shader_error(GLuint id, std::string const & path);
|
|
|
- static void shader_program_error(GLuint id, std::string const & frag_path,
|
|
|
- std::string const & vert_path);
|
|
|
-
|
|
|
- GLuint obj;
|
|
|
- std::string fileName, path, eNoticeMessage, eFileMessage, eWindowTitle;
|
|
|
- GLGetIntFor getIntv;
|
|
|
- GLGetLog getInfoLog;
|
|
|
- };
|
|
|
-
|
|
|
- void error_formatter::operator()() const {
|
|
|
- std::stringstream errorString;
|
|
|
- std::string errorLineNumber;
|
|
|
-
|
|
|
- int infologLength = 0;
|
|
|
- int maxLength = 0;
|
|
|
-
|
|
|
- getIntv(obj, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
- errorString << eNoticeMessage << "\n";
|
|
|
-
|
|
|
- std::unique_ptr<char[]> infoLog(new char[maxLength + 1]);
|
|
|
- getInfoLog(obj, maxLength, &infologLength, infoLog.get());
|
|
|
-
|
|
|
- std::string errorLog(infoLog.get());
|
|
|
- infoLog.reset();
|
|
|
- if (infologLength <= 0) return;
|
|
|
-
|
|
|
- std::size_t openParen = errorLog.find('(');
|
|
|
- std::size_t closeParen = errorLog.find(')');
|
|
|
- errorLineNumber =
|
|
|
- errorLog.substr(openParen + 1, closeParen - openParen - 1);
|
|
|
-
|
|
|
- std::string errorStart = errorLog.find("error") != std::string::npos
|
|
|
- ? errorLog.substr(errorLog.find("error"))
|
|
|
- : errorLog;
|
|
|
-
|
|
|
- errorString << errorLog << "\n";
|
|
|
-
|
|
|
- errorString << "OpenGL Version: " << glGetString(GL_VERSION) << "\n";
|
|
|
- errorString << "GLSL Shading Version: "
|
|
|
- << glGetString(GL_SHADING_LANGUAGE_VERSION) << "\n";
|
|
|
-
|
|
|
- errorString << eFileMessage << "\n";
|
|
|
- errorString << "RAW ERROR LOG: " << errorLog;
|
|
|
-
|
|
|
- std::stringstream consoleOutput;
|
|
|
-
|
|
|
- consoleOutput << "1>" << path << '(' << errorLineNumber << ')' << ": "
|
|
|
- << errorStart
|
|
|
- << "in OpenGL version: " << glGetString(GL_VERSION) << " and "
|
|
|
- << "GLSL Shading Version: "
|
|
|
- << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
|
|
|
-
|
|
|
-#ifdef _WIN32
|
|
|
- OutputDebugStringA(consoleOutput.str().c_str());
|
|
|
- MessageBoxA(nullptr, errorString.str().c_str(), eWindowTitle.c_str(),
|
|
|
- MB_OK);
|
|
|
-#else
|
|
|
- std::cerr << errorString.str() << std::endl;
|
|
|
-#endif
|
|
|
- }
|
|
|
-
|
|
|
- void error_formatter::shader_error(GLuint id, std::string const & path) {
|
|
|
- std::string fileName = path.substr(path.find_last_of("/") + 1);
|
|
|
- error_formatter{id,
|
|
|
- fileName,
|
|
|
- path,
|
|
|
- "GLSL shader compile error!",
|
|
|
- "File location: " + path,
|
|
|
- "GLSL Compile Error in " + fileName,
|
|
|
- glGetShaderiv,
|
|
|
- glGetShaderInfoLog}();
|
|
|
- }
|
|
|
-
|
|
|
- void error_formatter::shader_program_error(GLuint id,
|
|
|
- std::string const & frag_path,
|
|
|
- std::string const & vert_path) {
|
|
|
- std::string fileName = frag_path.substr(frag_path.find_last_of("/") + 1);
|
|
|
- error_formatter{id,
|
|
|
- fileName,
|
|
|
- frag_path,
|
|
|
- "GLSL program link error!",
|
|
|
- "File location of vertex shader: " + vert_path +
|
|
|
- "\nFile location of fragment shader: " + frag_path,
|
|
|
- "GLSL Link Error",
|
|
|
- glGetProgramiv,
|
|
|
- glGetProgramInfoLog}();
|
|
|
- }
|
|
|
+namespace graphics {
|
|
|
+ extern void print_shader_error(GLuint, std::string const &);
|
|
|
+ extern void print_shader_program_error(GLuint, std::string const &,
|
|
|
+ std::string const &);
|
|
|
}
|
|
|
|
|
|
namespace graphics { namespace textures {
|
|
|
@@ -244,14 +155,14 @@ namespace graphics { namespace shaders {
|
|
|
glGetShaderiv(id, GL_COMPILE_STATUS, &return_code);
|
|
|
|
|
|
if (return_code != GL_TRUE) {
|
|
|
- error_formatter::shader_error(id, abs_path);
|
|
|
+ print_shader_error(id, abs_path);
|
|
|
throw compilation_error("Could not compile the shader file");
|
|
|
}
|
|
|
return id;
|
|
|
}
|
|
|
|
|
|
- unsigned int init(flyweight<shader> const & fragmentShader,
|
|
|
- flyweight<shader> const & vertexShader) {
|
|
|
+ unsigned int init(identity<shader> const & fragmentShader,
|
|
|
+ identity<shader> const & vertexShader) {
|
|
|
// 9. Create a new shader program ID
|
|
|
unsigned int id = glCreateProgram();
|
|
|
|
|
|
@@ -274,16 +185,16 @@ namespace graphics { namespace shaders {
|
|
|
glGetProgramiv(id, GL_LINK_STATUS, &return_code);
|
|
|
|
|
|
if (return_code != GL_TRUE) {
|
|
|
- error_formatter::shader_program_error(id, fragmentShader.actual().path,
|
|
|
- vertexShader.actual().path);
|
|
|
+ // print_shader_program_error(id, fragmentShader.actual().path,
|
|
|
+ // vertexShader.actual().path);
|
|
|
throw linker_error("Could not link shader program");
|
|
|
}
|
|
|
return id;
|
|
|
}
|
|
|
|
|
|
- void activate(unsigned int id) {
|
|
|
+ void activate(identity<shader_program> program) {
|
|
|
// 100. Use the shader program ID to "turn it on" for all subsequent drawing
|
|
|
- glUseProgram(id);
|
|
|
+ glUseProgram(program.id);
|
|
|
|
|
|
/*
|
|
|
// 101. Enable texturing and Bind texture(s) to GPU texture units
|
|
|
@@ -306,13 +217,13 @@ namespace graphics { namespace shaders {
|
|
|
|
|
|
// 102. Get the location # of each named uniform you wish to pass in to the
|
|
|
// shader
|
|
|
- int timeUniformLocation = glGetUniformLocation(id, "u_time");
|
|
|
- int scale = glGetUniformLocation(id, "Scale");
|
|
|
- int diffuseMapUniformLocation = glGetUniformLocation(id, "u_diffuseMap");
|
|
|
- int normalMapUniformLocation = glGetUniformLocation(id, "u_normalMap");
|
|
|
- int specularMapUniformLocation = glGetUniformLocation(id, "u_specularMap");
|
|
|
- int emissiveMapUniformLocation = glGetUniformLocation(id, "u_emissiveMap");
|
|
|
- int debugWave = glGetUniformLocation(id, "g_debugWave");
|
|
|
+ int timeUniformLocation = glGetUniformLocation(program.id, "u_time");
|
|
|
+ int scale = glGetUniformLocation(program.id, "Scale");
|
|
|
+ int diffuseMap = glGetUniformLocation(program.id, "u_diffuseMap");
|
|
|
+ int normalMap = glGetUniformLocation(program.id, "u_normalMap");
|
|
|
+ int specularMap = glGetUniformLocation(program.id, "u_specularMap");
|
|
|
+ int emissiveMap = glGetUniformLocation(program.id, "u_emissiveMap");
|
|
|
+ int debugWave = glGetUniformLocation(program.id, "g_debugWave");
|
|
|
|
|
|
// 103. Set the uniform values, including the texture unit numbers for
|
|
|
// texture (sampler) uniforms
|
|
|
@@ -321,22 +232,23 @@ namespace graphics { namespace shaders {
|
|
|
glUniform1f(scale, 2.f);
|
|
|
glUniform1i(debugWave, 1); // TODO: m_waveEffectOn in ShaderProgram??
|
|
|
// for GL_TEXTURE0, texture unit 0
|
|
|
- glUniform1i(diffuseMapUniformLocation, 0);
|
|
|
+ glUniform1i(diffuseMap, 0);
|
|
|
// for GL_TEXTURE1, texture unit 1
|
|
|
- glUniform1i(normalMapUniformLocation, 1);
|
|
|
+ glUniform1i(normalMap, 1);
|
|
|
// for GL_TEXTURE2, texture unit 2
|
|
|
- glUniform1i(specularMapUniformLocation, 2);
|
|
|
+ glUniform1i(specularMap, 2);
|
|
|
// for GL_TEXTURE3, texture unit 3
|
|
|
- glUniform1i(emissiveMapUniformLocation, 3);
|
|
|
+ glUniform1i(emissiveMap, 3);
|
|
|
}
|
|
|
|
|
|
- int uniform_location(unsigned int id, std::string const & uniform) {
|
|
|
- return glGetUniformLocation(id, uniform.c_str());
|
|
|
+ int uniform_location(identity<shader_program> program,
|
|
|
+ std::string const & uniform) {
|
|
|
+ return glGetUniformLocation(program.id, uniform.c_str());
|
|
|
}
|
|
|
}}
|
|
|
|
|
|
namespace graphics { namespace materials {
|
|
|
- void activate(flyweight<shader_program> program,
|
|
|
+ void activate(identity<shader_program> program,
|
|
|
std::vector<uniform> const & uniforms) {
|
|
|
glUseProgram(program.id);
|
|
|
|