Skip to content

Instantly share code, notes, and snippets.

@MehdiNS
Last active May 4, 2019 08:27
Show Gist options
  • Save MehdiNS/7ca618f14c9a5d8138dacb1bad739ce3 to your computer and use it in GitHub Desktop.
Save MehdiNS/7ca618f14c9a5d8138dacb1bad739ce3 to your computer and use it in GitHub Desktop.
Support #include in GLSL
#define RED (vec4(1,0,0,1))
#define PI (3.14159)
// Credits to "Mon Chic Prof" Pat for the Lines trick.
// This contains clever things, and other not so much.
// But it does the job, and we sometimes need ugliness in order to appreciate the beauty of things.
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std::chrono;
std::vector<std::string> split(const std::string& s, const char delim)
{
std::vector<std::string> v;
auto p = std::begin(s);
for (auto q = std::find(p, std::end(s), delim); q != std::end(s); q = std::find(++p, std::end(s), delim))
{
v.emplace_back(p, q);
p = q;
}
if (p != std::end(s))
v.emplace_back(p, std::end(s));
return v;
}
bool fileExists(const std::string &filename)
{
return std::ifstream{ filename }.good();
}
class Lines
{
std::istream &is;
std::string cur;
void read_one() { std::getline(is, cur); }
public:
Lines(std::istream &is) : is{ is } { read_one(); }
class iterator
{
public:
using value_type = std::string;
using reference = std::string&;
using pointer = std::string*;
using const_reference = const std::string&;
using const_pointer = const std::string*;
using iterator_category = std::forward_iterator_tag;
using difference_type = int;
private:
Lines *src;
friend class Lines;
iterator(Lines *src) noexcept : src{ src } {
}
public:
iterator() noexcept : src{} {}
reference operator*() noexcept { return src->cur; }
const_reference operator*() const noexcept { return src->cur; }
bool operator==(const iterator &other) const noexcept
{
return (src == other.src) || (!src && !other.src->is) || (!src->is && !other.src);
}
bool operator!=(const iterator &other) const noexcept
{
return !(*this == other);
}
iterator& operator++()
{
src->read_one();
return *this;
}
iterator operator++(int)
{
auto temp = *this;
operator++();
return temp;
}
};
iterator begin() noexcept { return{ this }; }
iterator end() noexcept { return{}; }
private:
friend class iterator;
};
int main()
{
auto before = system_clock::now();
std::ifstream in{ "quad.frag" };
for (auto& s : Lines{ in })
{
auto& token = split(s, ' ');
if (token.size() >= 2 && token[0] == "#include")
{
token[1].erase(remove(token[1].begin(), token[1].end(), '\"'), token[1].end());
std::ifstream sourceFile(token[1]);
s.assign((std::istreambuf_iterator<char>(sourceFile)), std::istreambuf_iterator<char>());
}
//std::cout << s << std::endl;
}
/*
Here you can compile your shader like you usually do
*/
auto after = system_clock::now();
auto deltaTime = duration_cast<milliseconds>(after - before).count();
//std::cout << deltaTime << std::endl;
}
#version 420
#include "constant.glsl"
layout (location = 0) out vec2 outTexCoord;
void main()
{
float x = -1.0 + float((gl_VertexID & 1) << 2);
float y = -1.0 + float((gl_VertexID & 2) << 1);
outTexCoord.x = (x+1.0)*0.5;
outTexCoord.y = (y+1.0)*0.5;
gl_Position = vec4(x, y, 0, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment