]> gitweb.factorcode.org Git - factor.git/blob - extra/bunny/model/model.factor
Fixing everything for mandatory stack effects
[factor.git] / extra / bunny / model / model.factor
1 USING: alien alien.c-types arrays sequences math math.vectors
2 math.matrices math.parser io io.files kernel opengl opengl.gl
3 opengl.glu io.encodings.ascii opengl.capabilities shuffle
4 http.client vectors splitting tools.time system combinators
5 float-arrays continuations destructors namespaces sequences.lib
6 accessors ;
7 IN: bunny.model
8
9 : numbers ( str -- seq )
10     " " split [ string>number ] map sift ;
11
12 : (parse-model) ( vs is -- vs is )
13     readln [
14         numbers {
15             { [ dup length 5 = ] [ 3 head pick push ] }
16             { [ dup first 3 = ] [ rest over push ] }
17             [ drop ]
18         } cond (parse-model)
19     ] when* ;
20
21 : parse-model ( -- vs is )
22     100000 <vector> 100000 <vector> (parse-model) ;
23
24 : n ( vs triple -- n )
25     swap [ nth ] curry map
26     dup third over first v- >r dup second swap first v- r> cross
27     vneg normalize ;
28
29 : normal ( ns vs triple -- )
30     [ n ] keep [ rot [ v+ ] change-nth ] each-with2 ;
31
32 : normals ( vs is -- ns )
33     over length { 0.0 0.0 0.0 } <array> -rot
34     [ >r 2dup r> normal ] each drop
35     [ normalize ] map ;
36
37 : read-model ( stream -- model )
38     ascii [ parse-model ] with-file-reader
39     [ normals ] 2keep 3array ;
40
41 : model-path ( -- path ) "bun_zipper.ply" temp-file ;
42
43 : model-url ( -- url ) "http://factorcode.org/bun_zipper.ply" ;
44
45 : maybe-download ( -- path )
46     model-path dup exists? [
47         "Downloading bunny from " write
48         model-url dup print flush
49         over download-to
50     ] unless ;
51
52 : (draw-triangle) ( ns vs triple -- )
53     [ dup roll nth gl-normal swap nth gl-vertex ] each-with2 ;
54
55 : draw-triangles ( ns vs is -- )
56     GL_TRIANGLES [ [ (draw-triangle) ] each-with2 ] do-state ;
57
58 TUPLE: bunny-dlist list ;
59 TUPLE: bunny-buffers array element-array nv ni ;
60
61 : <bunny-dlist> ( model -- geom )
62     GL_COMPILE [ first3 draw-triangles ] make-dlist
63     bunny-dlist boa ;
64
65 : <bunny-buffers> ( model -- geom )
66     {
67         [
68             [ first concat ] [ second concat ] bi
69             append >float-array
70             GL_ARRAY_BUFFER swap GL_STATIC_DRAW <gl-buffer>
71         ]
72         [
73             third concat >c-uint-array
74             GL_ELEMENT_ARRAY_BUFFER swap GL_STATIC_DRAW <gl-buffer>
75         ]
76         [ first length 3 * ]
77         [ third length 3 * ]
78     } cleave bunny-buffers boa ;
79
80 GENERIC: bunny-geom ( geom -- )
81 GENERIC: draw-bunny ( geom draw -- )
82
83 M: bunny-dlist bunny-geom
84     bunny-dlist-list glCallList ;
85
86 M: bunny-buffers bunny-geom
87     dup { array>> element-array>> } get-slots [
88         { GL_VERTEX_ARRAY GL_NORMAL_ARRAY } [
89             GL_DOUBLE 0 0 buffer-offset glNormalPointer
90             [
91                 nv>> "double" heap-size * buffer-offset
92                 3 GL_DOUBLE 0 roll glVertexPointer
93             ] [
94                 ni>>
95                 GL_TRIANGLES swap GL_UNSIGNED_INT 0 buffer-offset glDrawElements
96             ] bi
97         ] all-enabled-client-state
98     ] with-array-element-buffers ;
99
100 M: bunny-dlist dispose
101     list>> delete-dlist ;
102
103 M: bunny-buffers dispose
104     { array>> element-array>> } get-slots
105     delete-gl-buffer delete-gl-buffer ;
106
107 : <bunny-geom> ( model -- geom )
108     "1.5" { "GL_ARB_vertex_buffer_object" }
109     has-gl-version-or-extensions?
110     [ <bunny-buffers> ] [ <bunny-dlist> ] if ;