]> gitweb.factorcode.org Git - factor.git/blob - extra/bunny/bunny.factor
b7400c4acb53e054c7497d95dd2d451b8cc41848
[factor.git] / extra / bunny / bunny.factor
1 USING: accessors arrays bunny.cel-shaded bunny.fixed-pipeline
2 bunny.model bunny.outlined destructors kernel math opengl.demo-support
3 opengl.gl sequences ui ui.gadgets ui.gadgets.worlds ui.gestures
4 ui.render words ui.pixel-formats ;
5 IN: bunny
6
7 TUPLE: bunny-world < demo-world model-triangles geom draw-seq draw-n ;
8
9 : get-draw ( gadget -- draw )
10     [ draw-n>> ] [ draw-seq>> ] bi nth ;
11
12 : next-draw ( gadget -- )
13     dup [ draw-seq>> ] [ draw-n>> ] bi
14     1 + swap length mod
15     >>draw-n relayout-1 ;
16
17 : make-draws ( gadget -- draw-seq )
18     [ <bunny-fixed-pipeline> ]
19     [ <bunny-cel-shaded> ]
20     [ <bunny-outlined> ] tri 3array
21     sift ;
22
23 M: bunny-world begin-world
24     GL_DEPTH_TEST glEnable
25     0.0 0.0 0.375 set-demo-orientation
26     maybe-download read-model
27     [ >>model-triangles ] [ <bunny-geom> >>geom ] bi
28     dup make-draws >>draw-seq
29     0 >>draw-n
30     drop ;
31
32 M: bunny-world end-world
33     dup find-gl-context
34     [ geom>> [ dispose ] when* ]
35     [ draw-seq>> [ [ dispose ] when* ] each ] bi ;
36
37 M: bunny-world draw-world*
38     dup draw-seq>> empty? [ drop ] [
39         0.15 0.15 0.15 1.0 glClearColor
40         GL_DEPTH_BUFFER_BIT GL_COLOR_BUFFER_BIT bitor glClear
41         dup demo-world-set-matrix
42         GL_MODELVIEW glMatrixMode
43         0.02 -0.105 0.0 glTranslatef
44         [ geom>> ] [ get-draw ] bi draw-bunny
45     ] if ;
46
47 M: bunny-world pref-dim* ( gadget -- dim )
48     drop { 640 480 } ;
49     
50 bunny-world H{
51     { T{ key-down f f "TAB" } [ next-draw ] }
52 } set-gestures
53
54 : bunny-window ( -- )
55     [
56         f T{ world-attributes
57             { world-class bunny-world }
58             { title "Bunny" }
59             { pixel-format-attributes {
60                 windowed
61                 double-buffered
62                 T{ depth-bits { value 16 } }
63             } }
64         } open-window
65     ] with-ui ;
66
67 MAIN: bunny-window