FluxusGpu HomePage PageList

back to FluxusPage

Let the GPU take the strain

[img]
Deformation and vertex colouring using glsl shaders, controlled from fluxus

This is much faster than using fluxus scheme natively, as your graphics card has transistors dedicated to this kind of work. Controlling glsl uniform parameters via (shader-set!) and attributes (per vertex values) with pdata is easy, and can be hooked up with your own glsl shaders.

shadertest.scm

; an example of passing parameters to a glsl shader, where all the
; deformation and colouring is done on the GPU.

; set the testcol pdata with a random colour for every vertex
(define (set-cols n)
    (pdata-set "testcol" n (vector (flxrnd) (flxrnd) (flxrnd)))
    (if (zero? n)
        0
        (set-cols (- n 1))))

(clear)
(push)
; assign the shaders to the surface
(shader "simplevert.glsl" "simplefrag.glsl")
(define s (build-sphere 20 20))
(pop)

(grab s)
; add and set the pdata - this is then picked up in the vertex shader
; as an input attribute called "testcol"
(pdata-add "testcol" "v")
(set-cols (pdata-size))
(ungrab)

(define (animate)
    (grab s)
     ; animate the deformamount uniform input parameter
    (shader-set! (list "deformamount" (cos (time))))
    (ungrab))

(every-frame (animate))


simplevert.glsl

// a daft little vertex shader to test fluxus

attribute vec3 testcol;
uniform float deformamount;
varying vec3 fragcol;

void main()
{     
     // apply the normal gl modelviewprojection to the vertex
       gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
     
     // add our sinewave distortion based on the transformed world
     // coordinates of the vertex
     gl_Position += vec4(sin(gl_Position.y*6),0,0,1)*deformamount*0.5;
     
     // pass the colour through to the fragement shader
     fragcol=testcol;
}


simplefrag.glsl

// a daft little fragment shader to test fluxus
// abount as simple as it gets

varying vec3 fragcol;

void main()
{
     // apply the input colour (and add alpha manually)
     gl_FragColor = vec4(fragcol.x,fragcol.y,fragcol.z,1);
}


normal fixed funtion pervertex lighting:
[img]

vs fragment shader perpixel lighting:
[img]

This work is licensed under a Creative Commons License