To-GLSL
Introduction
To-GLSL is a library for converting Clojure s-expressions to GLSL.
Usage
[to-glsl "0.1.0-SNAPSHOT"]
Example
(def basic-vs
'(do
(def-attribute vec4 vertex-position)
(def-attribute vec2 tex-coord-attr)
(def-uniform mat4 model-view)
(def-uniform mat4 projection)
(def-varying (highp vec2) tex-coord)
(defn main (void [])
(set! tex-coord tex-coord-attr)
(set!
gl/position
(* projection model-view vertex-position)))))
(def basic-fs
'(do
(def-uniform sampler-2D sampler)
(def-varying (highp vec2) tex-coord)
(defn main (void [])
(set! gl/frag-color (texture-2D sampler tex-coord)))))
(require '[to-glsl.core :refer [->glsl]])
(def vs-source (->glsl basic-vs))
(def fs-source (->glsl basic-fs))
Supported Language
defn
(defn add-one (void [(int x)]) (+ x 1))
becomes:
void addOne(int x) {
x + 1;
}
Note: types must be specified
def
(def float pi 3.14159)
becomes
float pi = 3.14159;
def-varying
- like def but creates a varying
def-unifrom
- like def but creates a uniform
def-attribute
- like def but creates an attribute
set!
(set! x 5)
becomes
x = 5;
when
(when (and (= x 10) (= z 30))
(set! y 20))
becomes
if (x == 10 && z == 30) {
y = 20;
}
if
(set! y (if (= x 10) 20 10))
becomes
y = x == 10 ? 20 : 10;
and
, or
, =
, +
, -
, /
, *
, bit-and
, bit-or
, bit-xor
, bit-not
, bit-shift-right
, bit-shift-left
- all compile into appriate infix form.
inc
and dec
- compile into ++
and --
repectively.
field
(field vec x)
becomes
vec.x
swizzle
(swizzle vec x z)
becomes
vec.xz
All symbols are automatically converted from kebab case into camel case.
The GLSL gl_
symbols like gl_Position
and gl_FragColor
can be accessed under the gl/
namespace in kebab case. i.e. gl/frag-color
.
To-GLSL doesn't make any attempt to validate the provided code.
License
Copyright 2019 Jeremy Kross
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.