GithubHelp home page GithubHelp logo

tntmeijs / glsl-shader-includes Goto Github PK

View Code? Open in Web Editor NEW
57.0 57.0 5.0 8 KB

A utility class which adds a way to include external files in a shader file.

License: MIT License

C++ 100.00%
glsl graphics-programming opengl shaders source-code

glsl-shader-includes's Introduction

glsl-shader-includes's People

Contributors

tntmeijs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

glsl-shader-includes's Issues

Loading same file recursively

Congratulations on the job of creating a nice feature to load files recursively, it's something very useful, because OpenGL doesn't have the feature to include files.

I had a small problem when 2 files included the same file, causing it to generate repeated code, here is a more critical example.

Having these 3 files:
file1.txt
file2.txt
file3.txt

[file1.txt]

//some code
#include file2.txt

// add code

[file2.txt]

//some code
#include file3.txt
// add code

[file3.txt]

// add code
#include file2.txt

The code will load the files:
file1.txt
file2.txt
file3.txt
file2.txt
file3.txt
file2.txt
file3.txt
file2.txt
file3.txt
...

Until the moment it fills up memory or fails to load any of the files, as it is loading recursively and does not have the implementation to check if the file has already been included or not.

But it is possible to make it not add the same file more than once with the following modification:

...
class Shadinclude {
public:
	// Return the source code of the complete shader
	static std::string load(std::string path, std::string includeIndentifier = "#include") {
		std::list<std::string> fileList;
		// add the original path to the list to avoid reuse
		fileList.push_back(path);
		return load2(path, includeIndentifier + ' ', fileList);
	}

private:
	static std::string load2(std::string path, std::string includeIndentifier, std::list<std::string> &fileList) {
		//includeIndentifier += ' ';
		static bool isRecursiveCall = false;
...
		auto it = std::find(fileList.begin(), fileList.end(), lineBuffer);
		// test if the path is not on the list
		if (fileList.end() == it) {
			isRecursiveCall = true;
			//add path to list
			fileList.push_back(lineBuffer);
			// By using recursion, the new include file can be extracted
			// and inserted at this location in the shader source code
			// if it has not already been inserted
			fullSourceCode += load2(lineBuffer, includeIndentifier, fileList);
		}
		else {
			// file already included, no need to include it again
		}

...

Hope I helped improve something :)

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.