[Fluxus] shader array arguments
Dave Griffiths
dave at pawfal.org
Wed Jul 29 15:58:40 PDT 2009
Hi all,
Array arguments are now supported in the hardware shader interface, and
read lists of float, int or vector (size 3 or 4) elements.
As an example of their use, if you need loads of lights (more than the
OpenGL fixed pipeline 8 light limit) you can do something like this:
http://www.flickr.com/photos/dave-griffiths/3769764975/
(clear)
(clear-shader-cache)
(define nlights 50)
(define pos (build-list nlights (lambda (n) (vadd (vector 0 0 1) (vmul
(srndvec) 3)))))
(define col (build-list nlights (lambda (_) (vmul (rndvec) 2))))
(define falloff (build-list nlights (lambda (_) (* (rndf) 0.3))))
; preview the lights
(for-each
(lambda (pos col falloff)
(with-state
(translate pos)
(colour col)
(scale falloff)
(hint-unlit)
(build-sphere 5 5)))
pos col falloff)
(with-state
(shader-source
"
varying vec4 P;
varying vec3 N;
void main()
{
gl_Position = ftransform();
P = gl_Vertex;
N = normalize(gl_NormalMatrix*gl_Normal);
}
"
"
uniform vec4 LightsPos[50];
uniform vec4 LightsCol[50];
uniform float LightsFalloff[50];
varying vec4 P;
varying vec3 N;
void main()
{
vec4 final = vec4(0,0,0,1);
vec3 n = normalize(N);
for (int i=0; i<10; i++)
{
vec4 l=LightsPos[i]-P;
float d=1.0/length(l)*LightsFalloff[i];
final+=LightsCol[i]*max(0.0,dot(n,vec3(normalize(l))))*d;
}
gl_FragColor = vec4(final.xyz,1);
}
")
(shader-set! (list
"LightsPos" pos
"LightsCol" col
"LightsFalloff" falloff
))
(build-torus 1 2 20 20))
More information about the Fluxus
mailing list