Skip to content

Commit

Permalink
Add custom GLSL imports
Browse files Browse the repository at this point in the history
  • Loading branch information
douze committed Nov 11, 2020
1 parent b12098e commit e1bd2f4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
6 changes: 3 additions & 3 deletions demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ int main() {
scene::Node& root{scene.getRoot()};

// Add test triangle
scene::Node triangleNode{std::make_unique<mesh::Triangle>(mesh::Triangle{}),
/*scene::Node triangleNode{std::make_unique<mesh::Triangle>(mesh::Triangle{}),
mesh::Transformation{glm::vec3{0.0f, 0.0f, -2.0f}},
std::make_unique<material::VertexColorMaterial>(
std ::make_unique<material::VertexColorMaterial>(
material::VertexColorMaterial{})};
root.addChild(triangleNode);
root.addChild(triangleNode);*/

// Add terrain
scene::Node terrainNode{
Expand Down
13 changes: 11 additions & 2 deletions renderer/include/material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,27 @@ class Material {
*/
static std::string readShaderFile(const std::string& path);

/**
* @brief Parse shader content and replace #import directives with
* corresponding string
* @param content of the shader to enrich
*/
void addImports(std::string& content) const;

/**
* @brief Create a shader program.
* @param type of shader
* @param path of the shader file
* */
GLuint createShaderProgram(GLenum type, const std::string& path) const;

/**
* @brief Check the program link status.
* @note Only print errors - no exception or exit code.
* @param program to check
* @param path of the shader program
*/
void checkLinkStatus(GLuint program) const;
void checkLinkStatus(GLuint program, const std::string& path) const;

/**
* @brief Shaders location.
Expand Down
31 changes: 24 additions & 7 deletions renderer/src/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,48 @@ Material::Material(const std::string& vsPath, const std::string& tcsPath,

GLuint Material::createShaderProgram(GLenum type,
const std::string& path) const {
const std::string source = readShaderFile(path);
std::string source = readShaderFile(path);
addImports(source);
const char* csource = source.c_str();
const GLuint shaderProgram = glCreateShaderProgramv(type, 1, &csource);
checkLinkStatus(shaderProgram);
checkLinkStatus(shaderProgram, path);
return shaderProgram;
}

std::string Material::readShaderFile(const std::string& path) {
spdlog::debug("Reading shader {0}", path);
std::ifstream file{SHADER_FOLDER + path};
return std::string{(std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()};
}

void Material::checkLinkStatus(GLuint program) const {
void Material::addImports(std::string& content) const {
const std::string import = "#import ";
const std::string eol = "\n";
std::string::size_type start = 0;
std::string::size_type end = 0;
std::string filename;
std::string subcontent;

while ((start = content.find(import, start)) != std::string::npos) {
end = content.find(eol, start);
filename =
content.substr(start + import.length(), end - start - import.length());
subcontent = readShaderFile(filename);
content.replace(start, end - start, subcontent + eol);
start = end + 1;
}
}

void Material::checkLinkStatus(GLuint program, const std::string& path) const {
GLint isLinked = 0;
glGetProgramiv(program, GL_LINK_STATUS, (int*)&isLinked);
if (isLinked == GL_FALSE) {
GLint maxLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
spdlog::error(" > Invalid shader: {0}", std::string{infoLog.begin(), infoLog.end()});
} else {
spdlog::debug(" > Valid shader");
spdlog::error("Invalid shader {0}: {1}", path,
std::string{infoLog.begin(), infoLog.end()});
}
}

Expand Down

0 comments on commit e1bd2f4

Please sign in to comment.