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