Skip to content
Snippets Groups Projects
Commit 91e50f31 authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

add support for include extention

parent 0a9b8596
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,7 @@ if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) ...@@ -37,7 +37,7 @@ if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
glfw:shared=True glfw:shared=True
glad:gl_profile=core glad:gl_profile=core
glad:gl_version=4.5 glad:gl_version=4.5
glad:extensions="GL_ARB_get_program_binary" glad:extensions="GL_ARB_get_program_binary,GL_ARB_shading_language_include"
) )
if ( CMAKE_CONFIGURATION_TYPES ) if ( CMAKE_CONFIGURATION_TYPES )
......
/**
* OpenGL phong lighting model.
*/
// TODO write script
\ No newline at end of file
#version 330 core #version 330 core
#extension GL_ARB_shading_language_include : require
// based on http://www.opengl-tutorial.org, WTFPL // based on http://www.opengl-tutorial.org, WTFPL
// features lighting additions from https://learnopengl.com/, CC BY-NC 4.0 // features lighting additions from https://learnopengl.com/, CC BY-NC 4.0
...@@ -24,7 +26,7 @@ out vec4 FragColor; ...@@ -24,7 +26,7 @@ out vec4 FragColor;
void main() void main()
{ {
vec3 lightColour = vec3( 1.0, 1.0, 1.0 ); vec3 lightColour = vec3(1, 1, 1);
float lightPower = 200.0; float lightPower = 200.0;
vec3 n = normalize( normal_cs ); vec3 n = normalize( normal_cs );
......
...@@ -47,6 +47,24 @@ ShaderCache::ShaderCache(std::shared_ptr<fggl::data::Storage> storage) : m_stora ...@@ -47,6 +47,24 @@ ShaderCache::ShaderCache(std::shared_ptr<fggl::data::Storage> storage) : m_stora
m_binary = false; m_binary = false;
} }
if ( GLAD_GL_ARB_shading_language_include ) {
setupIncludes();
}
}
void ShaderCache::setupIncludes() {
auto root = m_storage->resolvePath( data::StorageType::Data, "include" );
auto paths = m_storage->findResources( root, ".glsl");
for ( auto& path : paths ){
std::string source;
m_storage->load( fggl::data::Data, path, &source );
auto relPath = std::filesystem::relative(path, root);
const auto relPathStr = "/" + std::string(relPath);
glNamedStringARB(GL_SHADER_INCLUDE_ARB, -1, relPathStr.c_str(), -1, source.c_str() );
}
} }
bool ShaderCache::loadFromDisk(GLuint pid, const ShaderConfig& config) { bool ShaderCache::loadFromDisk(GLuint pid, const ShaderConfig& config) {
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <filesystem> #include <filesystem>
#include <vector>
namespace fggl::data { namespace fggl::data {
...@@ -28,6 +29,20 @@ namespace fggl::data { ...@@ -28,6 +29,20 @@ namespace fggl::data {
return fggl_deserialize<T>(path, out); return fggl_deserialize<T>(path, out);
} }
std::vector<std::filesystem::path> findResources(std::filesystem::path root, const std::string ext) {
if ( !std::filesystem::exists(root) ) {
return {};
}
std::vector<std::filesystem::path> paths;
for ( const auto& entry : std::filesystem::recursive_directory_iterator(root) ) {
if ( entry.is_regular_file() && entry.path().extension() == ext ) {
paths.push_back( entry );
}
}
return paths;
}
template<typename T> template<typename T>
void save(StorageType pool, const std::string &name, const T *out) { void save(StorageType pool, const std::string &name, const T *out) {
auto path = resolvePath(pool, name); auto path = resolvePath(pool, name);
......
...@@ -46,10 +46,14 @@ namespace fggl::gfx { ...@@ -46,10 +46,14 @@ namespace fggl::gfx {
GLuint get(const std::string &name); GLuint get(const std::string &name);
private: private:
std::shared_ptr<fggl::data::Storage> m_storage; std::shared_ptr<fggl::data::Storage> m_storage;
std::unordered_map<std::string, GLuint> m_shaders; std::unordered_map<std::string, GLuint> m_shaders;
// extensions
void setupIncludes();
// opengl operations // opengl operations
bool compileShader(const std::string &, GLuint); bool compileShader(const std::string &, GLuint);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment