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