[Fluxus] kaleidoscope

Kassen signal.automatique at gmail.com
Wed Dec 12 15:24:17 PST 2012


Dear all,

I wanted to do a kaleidoscope; those are really the pinnacle of
cheap&cheerful visual entertainment. Mine turned out a bit too close
to one of those VLC visualisations, but the techniques used are quite
interesting so I wanted to share. I'm -of course- using a pair of
pixel renderers for feedback, then I'm also slightly rotating the
planes that cause the feedback to make the feedback run "inside" or
"outside". With that the ways in which the feedback turns it all into
visual "soup" varies. I'm also happy about the shaping of the
segmented planes and their texture coordinates.

Overall good fun with very few poly's, but -yeah- not a 100% like a
real kaleidoscope :-).

Code may contain traces of impulsive hackery, but if bits are of use
to somebody GPLv2 would be fine.

Yours,
Kas.

(clear)
(reset-camera)

(hint-unlit)

(define pix (list
        (build-pixels 1024 1024 #t) 
        (build-pixels 1024 1024 #t)))

(for-each 
    (lambda (p) 
        (with-primitive p (scale 0)))
    pix)

;(colour 0)
;(hint-wire)
(translate (vector 0 0 3.55))

(define tube null)
(define core (with-state (translate (vector 0 0 -3.7)) (build-locator)))

(define counter-planes
    (for/list ((x (in-range (length pix))))
        (with-pixels-renderer (list-ref pix x)
            (ortho) (reset-camera) 
            
            (light-diffuse 0 (vector 0 0 0))
            (light-specular 0 (vector 0 0 0))
            (light-ambient 0 (vector 0 0 0))
            (let ((mylight (make-light 'point 'free)))
                (light-position mylight (vector 5 2 3))
                (light-diffuse mylight (vmul (vector 1 1 1) 1))
                (light-ambient mylight (vmul (vector 1 1 1) 1))
                (light-specular mylight (vmul (vector 1 1 1) 10)))
            
            (let ((obj (build-plane)))
                (with-primitive obj
                    (pdata-map! (lambda (t) 
                            (vector
                                (vector-ref t 0)
                                (* -1 (vector-ref t 1))
                                (vector-ref t 2)))
                        "t"))
                obj))))


(define triangles
    (for/list ((x (in-range (length pix))))
        (with-pixels-renderer (list-ref pix x)
            (let ((p (build-polygons 6 'triangle-list)))
                (with-primitive p
                    (pdata-set! "p" 0 (vector -.5 -.5 0))
                    (pdata-set! "p" 1 (vector .5 -.5 0))
                    (pdata-set! "p" 2 (vector 0 .5 0))
                    (pdata-set! "p" 3 (vector -.5 -.5 0))
                    (pdata-set! "p" 5 (vector .5 -.5 0))
                    (pdata-set! "p" 4 (vector 0 .5 0))
                    (recalc-normals 0))
                p))))


(let* ( (sides 8)
        (corner (/ 360 sides)))
    (for ((x (in-range sides)))
        (with-state
            (parent core)
            ;(hint-wire)
            (rotate (vector 0 0 (* x corner)))
            (translate (vector 1 0 .15))
            (rotate (vector 0 90 0))
            (scale (vector 10 .83 1))
            (let ((o (build-seg-plane 20 1)))
                (with-primitive o
                    (rotate (vector 0 180 0))
                    (pdata-map! (lambda (t)
                            (vector 
                                (* (vector-ref t 0) (vector-ref t 0))
                                (* (if (odd? x) 1 -1) (vector-ref t 1))
                                (vector-ref t 2)))
                        "t")
                    (pdata-map! (lambda (p t)
                            (vector 
                                (vector-ref p 0)
                                (*  (vector-ref t 0) (vector-ref p 1))
                                (- 1 (* 1 (vector-ref t 0)))))
                        "p" "t"))
                (set! tube (cons o tube))))))

(define (pix-ani) 
    (let ((tex (pixels->texture (cadr pix))))        
        (with-pixels-renderer (car pix)
            (let* (  (sides 8)
                    (corner (/ 360 sides)))
                (with-state
                    (colour 1)
                    (light-position 1 (vector (* 10 (sin (time))) 5 5))    
                    (rotate (vector 0 (* 20 (sin (* .3 (time)))) 0))
                    (translate (vector (* .4 (sin (* .35 (time)))) 0 0))
                    ;(hint-wire)
                    (scale 7.49)
                    (for ((x (in-range 3)))
                        (with-state
                            
                            (rotate (vector (- (* 1 corner) (* corner x)) 0 0))
                            (translate (vector 0 0 -1))
                            (scale (vector 2.67 .83 1))
                            (texture tex) (texture-params 0 '(wrap-s clamp ))
                            ;(texture (load-texture "test.png"))
                            (if (even? x) (draw-plane) (draw-instance (car counter-planes)))))))
            (for ((x (in-range -4 5)))
                (with-state
                    (translate (vector (* 2 x)(* 7.5 (sin (* .3 (+ x 5) (time)))) 0))
                    (colour (hsv->rgb (vector (+ 1 (sin (* .1 x (time)))) (abs (sin (+ x (time)))) 1)))
                    (scale (vmul 
                            (vnormalise 
                                (vector 
                                    (abs (sin (* (time) .01 x))) 
                                    (* 5 (abs (sin (* (time) .011 x))))
                                    .6)) 
                            (* 7 (+ 1 (sin (+ x (time)))))))
                    ;(hint-unlit)
                    (rotate (vector (* 20 (+ (time) x)) (* 45 (+ (time) x)) (* 10 (+ (time) x))))
                    (draw-instance (car triangles))))
            (with-state
                (colour (hsv->rgb (vector (abs (sin (* .1 2 (time)))) (abs (sin (+ 3 (time)))) 1)))
                (translate (vector (* 9 (sin (time))) (* 7.5 (cos (time))) 0))                
                (rotate (vector (* 11 (time)) (* 180 (time)) (* 10 (time))))
                (translate (vector 6 0 0))
                (scale 2)
                (draw-instance (car triangles)))
            (with-state
                (hint-wire)
                (wire-colour (vector (abs (cos (* .1001 (time))))  (abs (cos (* .201 (time))))  (abs (cos (* .142 (time))))))
                (line-width 2)
                (hint-anti-alias)
                (draw-line 
                    (vector  (* 10 (cos (* .4 (time)))) 7.5 0) 
                    (vector (* 7 (cos (* .31 (time)))) -7.5 0))))))



(every-frame
    (begin
        (pix-ani)
        (with-primitive core (rotate (vector 0 0 (* 2 (delta)))))
        (for-each 
            (lambda (plane) (with-primitive plane
                    (texture (pixels->texture (car pix)))
                    (texture-params 0 '(wrap-s clamp))))
            tube)
        (set! pix (reverse pix))
        ;(set! counter-planes (reverse counter-planes)) ;loadsa effort to avoid magic numbers, should be '(2 2)
        (pix-ani)))





More information about the Fluxus mailing list