Skip to content

Instantly share code, notes, and snippets.

@Bigfoot71
Last active June 24, 2023 01:03
Show Gist options
  • Select an option

  • Save Bigfoot71/72c52b12ff0a62607d3ec2f14d61218a to your computer and use it in GitHub Desktop.

Select an option

Save Bigfoot71/72c52b12ff0a62607d3ec2f14d61218a to your computer and use it in GitHub Desktop.
Lua script (>=5.1) to convert a GLSL shader into a C string.
local function formatString(inputString)
local outputString = ""
for line in inputString:gmatch("[^\r\n]+") do
if line:match("%S") then -- Check if row is empty
local formattedLine = line:gsub("^%s*", "") -- Remove spaces before the first character of the line
if not formattedLine:match("^%s*//") then -- Check if the line is a comment
local codeLine = formattedLine:gsub("%s*//.*$", "") -- Delete end of line comment
codeLine = codeLine:gsub("^", '"') -- Add double quotes (") at the beginning of the line
if line:match("^%s*#") then
codeLine = codeLine .. '\\n"\n' -- Add "\n" at the end of the line if necessary
else
codeLine = codeLine .. '"\n' -- Otherwise, add only a double quote
end
outputString = outputString .. codeLine -- Append the formatted line to the output string
end
end
end
return outputString
end
local function main(...)
local path = select(1, ...)
local file = assert(io.open(path, "rb"), ("Error: File not found [%s]"):format(path))
local formattedString = formatString(file:read("*all"))
file:close()
print(formattedString)
end
main(...)
#version 330 core
#define UNUSED_MACRO 0
uniform vec3 lightPosition;
in vec3 vertexPosition;
out vec4 fragColor;
// GLSL COMMENT
void main() {
vec3 lightDirection = normalize(lightPosition - vertexPosition); // LINE 1
float intensity = dot(vec3(0.0, 1.0, 0.0), lightDirection); // LINE 2
fragColor = vec4(intensity, intensity, intensity, 1.0); // LINE 3
}
"#version 330 core\n"
"#define UNUSED_MACRO 0\n"
"uniform vec3 lightPosition;"
"in vec3 vertexPosition;"
"out vec4 fragColor;"
"void main() {"
"vec3 lightDirection = normalize(lightPosition - vertexPosition);"
"float intensity = dot(vec3(0.0, 1.0, 0.0), lightDirection);"
"fragColor = vec4(intensity, intensity, intensity, 1.0);"
"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment