jellyfish.scm 4.36 KB
Newer Older
Dave Griffiths's avatar
Dave Griffiths committed
1
2
3
4
5
6
7
8
9
10
11
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; jellyfish livecoding stuff

(define (jelly-compiled code)
  (define addr 0)
  (for-each
   (lambda (v)
     (pdata-set! "x" addr v)
     (set! addr (+ addr 1)))
   code))

Dave Griffiths's avatar
Dave Griffiths committed
12
13
14
15
16
(define jellyfish
  (list
   (build-jellyfish 512)
   (build-jellyfish 512)
   (build-jellyfish 512)))
Dave Griffiths's avatar
Dave Griffiths committed
17

Dave Griffiths's avatar
Dave Griffiths committed
18
(define current 0)
Dave Griffiths's avatar
Dave Griffiths committed
19
20

(define (make-jelly speed prim-type code)
Dave Griffiths's avatar
Dave Griffiths committed
21
22
  (let ((p (list-ref jellyfish current)))
    (msg p)
Dave Griffiths's avatar
Dave Griffiths committed
23
24
25
26
27
    (with-primitive
     p
     (let ((c (compile-program speed prim-type 1 code)))
       ;; (disassemble c)
       (jelly-compiled c))
Dave Griffiths's avatar
Dave Griffiths committed
28
     (set! current (modulo (+ current 1) (length jellyfish)))
Dave Griffiths's avatar
Dave Griffiths committed
29
30
31
     p)))


Dave Griffiths's avatar
Dave Griffiths committed
32
33
34



Dave Griffiths's avatar
Dave Griffiths committed
35
36
(with-primitive
 (make-jelly
Dave Griffiths's avatar
Dave Griffiths committed
37
  10000 prim-triangles
Dave Griffiths's avatar
Dave Griffiths committed
38
  '(let ((vertex positions-start)
Dave Griffiths's avatar
Dave Griffiths committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
         (flingdamp (vector 50 -20 0))
         (world (vector 0 0 0))
         (t 0))

     (define recycle
       (lambda (dir)
         ;; shift along x and y coordinates:
         ;; set z to zero for each vertex
         (write! vertex
                 (+ (*v (read vertex)
                        (vector 1 1 0)) dir)
                 (+ (*v (read (+ vertex 1))
                        (vector 1 1 0)) dir)
                 (+ (*v (read (+ vertex 2))
                        (vector 1 1 0)) dir))

         ;; get the perlin noise values for each vertex
         (let ((a (noise (* (- (read vertex) world) 0.2)))
               (b (noise (* (- (read (+ vertex 1))
                               world) 0.2)))
               (c (noise (* (- (read (+ vertex 2))
                               world) 0.2))))

           ;; set the z coordinate for height
           (write-add!
            vertex
            (+ (*v a (vector 0 0 8))
               (vector 0 0 -4))
            (+ (*v b (vector 0 0 8))
               (vector 0 0 -4))
            (+ (*v c (vector 0 0 8))
               (vector 0 0 -4)))

           ;; recalculate normals
           (define n (normalise
                      (cross (- (read vertex)
                                (read (+ vertex 1)))
                             (- (read vertex)
                                (read (+ vertex 2))))))

           ;; write to normal data
           (write! (+ vertex 512) n n n)

           ;; write the z height as texture coordinates
           (write! (+ vertex 1536)
                   (*v (swizzle zzz a) (vector 0 4 0))
                   (*v (swizzle zzz b) (vector 0 4 0))
                   (*v (swizzle zzz c) (vector 0 4 0))))))

     ;; forever
Dave Griffiths's avatar
Dave Griffiths committed
89
90
     (forever

Dave Griffiths's avatar
Dave Griffiths committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
      (define vel (* flingdamp 0.002))
      ;; update the world coordinates
      (set! world (+ world vel))
      (set! t (+ t 0.1))

       ;; for each vertex
      (loop (< vertex (- positions-end 3))
            ;; update the vertex position
            (write-add! vertex vel vel vel)

            ;; check for out of area polygons to recycle
            (cond
             ((> (read vertex) 5.0)
              (recycle (vector -10 0 0)))
             ((< (read vertex) -5.0)
              (recycle (vector 10 0 0))))

            (cond
             ((> (swizzle yzz (read vertex)) 4.0)
              (recycle (vector 0 -8 0)))
             ((< (swizzle yzz (read vertex)) -4.0)
              (recycle (vector 0 8 0))))

            (set! vertex (+ vertex 3)))
      (set! vertex positions-start))))

 (pdata-map! (lambda (n) (vmul (vector (crndf) (crndf) 0) 0.001)) "n")
 (pdata-map! (lambda (c) (vector 1 1 1)) "c")
 (texture (load-texture "stripes.png"))
 (translate (vector -1 2 0))
 (rotate (vector -45 0 0))
                                        ;  (rotate (vector 0 0 100))
 (scale (vector 1.5 1.5 1.5))

 (let ((tsize 1)
       (twidth 8))
   (pdata-index-map!
    (lambda (i p)
      (let* ((tpos (modulo i 3))
             (tri (quotient i 3))
             (flip (modulo tri 2))
             (quad (quotient tri 2))
             (col (modulo quad twidth))
             (row (quotient quad twidth)))
        (vadd
         (vector (+ (* row tsize) 10) (* col tsize) 0)
         (vmul
          (if (zero? flip)
              (cond
               ((eqv? tpos 0) (vector 0 0 0))
               ((eqv? tpos 1) (vector tsize 0 0))
               ((eqv? tpos 2) (vector tsize tsize 0)))
              (cond
               ((eqv? tpos 0) (vector 0 0 0))
               ((eqv? tpos 1) (vector tsize tsize 0))
               ((eqv? tpos 2) (vector 0 tsize 0))))
          1))))
    "p"))

 (pdata-map! (lambda (t) (vector 0 0 0)) "t")
 )