GithubHelp home page GithubHelp logo

longsight / shaderforth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daeken/shaderforth

0.0 2.0 0.0 193 KB

Compiler from a mangled Forth to GLSL

Python 55.92% Glyph 41.83% CoffeeScript 2.25%

shaderforth's Introduction

Shaderforth

Shaderforth is a horrendously ugly experiment in making GLSL shaders using a Forth- and APL-inspired language.

Structure

The structure of Shaderforth files is a top-level main word, one or more globals words, any number of normal words, and macro words.

Here's a simple GLSL fragment shader:

uniform vec3 iResolution;
uniform float iGlobalTime;

void main() {
	vec2 pos = (gl_FragCoord.xy / iResolution.xy - 0.5) * 2.0;
	float temp = abs(sin(length(pos) * cos(iGlobalTime)));
	gl_FragColor = vec4(temp, temp, temp, 1.0);
}

And here's one terse ShaderForth equivalent:

:globals
	@vec3 uniform =iResolution
	@float uniform =iGlobalTime
;

gl_FragCoord .xy iResolution .xy / 0.5 - 2.0 *
length iGlobalTime cos * sin abs
dup dup 1.0 vec4 =gl_FragColor

Which compiles to:

uniform vec3 iResolution;
uniform float iGlobalTime;
void main() {
	float var_0 = abs(sin((length(((((gl_FragCoord).xy) / ((iResolution).xy)) - (0.5)) * (2.0))) * (cos(iGlobalTime))));
	gl_FragColor = vec4(var_0, var_0, var_0, 1.0);
}

And another:

:globals
	@vec3 uniform =iResolution
	@float uniform =iGlobalTime
;

gl_FragCoord .xy iResolution .xy / 0.5 - 2.0 * length =distance
distance iGlobalTime cos * sin abs =>color
color color color 1.0 vec4 =gl_FragColor

Which compiles to:

uniform vec3 iResolution;
uniform float iGlobalTime;
void main() {
	float distance = length(((((gl_FragCoord).xy) / ((iResolution).xy)) - (0.5)) * (2.0));
	gl_FragColor = vec4(abs(sin((distance) * (cos(iGlobalTime)))), abs(sin((distance) * (cos(iGlobalTime)))), abs(sin((distance) * (cos(iGlobalTime)))), 1.0);
}

Words

  • GLSL functions are all there as words (This isn't complete, but it's pretty straightforward to see how they are added)
  • =name -- Assigns to a GLSL variable named name
  • =>name -- Creates a local macro variable, whose value will be embedded literally wherever it's used
  • : name ( argtype argtype argtype -> returntype ) atom atom atom ; -- Defines a word that will be created as a GLSL function
    • This can optionally take argument names, argname:argtype, which will become macro locals.
  • :m name atom atom atom ; -- Defines a macro word whose contents will be inlined upon use
    • This can optionally take argument names, which will become macro locals. :m name ( arg1 arg2 ) atom atom atom ;
  • ( ) -- Everything between parentheses (make sure you include spaces around them -- these are both words) will be ignored as a comment, with the exception of type specifiers on words
  • [ atom atom atom ] -- Defines an array
  • [ atom atom atom ]v -- Defines a vector. Equivalent to the same array followed by avec
  • /word -- For each element of the array at the top of the stack, map against word -- this can take a macro word
  • \word -- Performs a reduce operation with word against the array at the top of the stack -- this can take a macro word
  • flatten -- Given an array at the top of the stack, this will turn the elements into native stack elements
  • avec -- Takes an array from the top of the stack and generates a vector of the requisite size
  • dup -- Duplicates the element at the top of the stack
  • swap -- Swaps the top two elements of the stack
  • Binary math operators -- They're used as expected
  • Unary math operator negate -- Flips the sign on the topmost element
  • &word -- Pushes a word reference to the stack
  • call -- Executes a word reference
  • *name -- Equivalent to name call

shaderforth's People

Contributors

daeken avatar

Watchers

 avatar  avatar

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.