]> gitweb.factorcode.org Git - factor.git/blob - extra/model-viewer/model-viewer.factor
Use flags{ instead of flags all over the place
[factor.git] / extra / model-viewer / model-viewer.factor
1 ! Copyright (C) 2010 Erik Charlebois
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types arrays classes.struct combinators
4 combinators.short-circuit game.loop game.worlds gpu gpu.buffers
5 gpu.util.wasd gpu.framebuffers gpu.render gpu.shaders gpu.state
6 gpu.textures gpu.util grouping http.client images images.loader
7 io io.encodings.ascii io.files io.files.temp kernel locals math
8 math.matrices math.vectors.simd math.parser math.vectors
9 method-chains namespaces sequences splitting threads ui ui.gadgets
10 ui.gadgets.worlds ui.pixel-formats specialized-arrays
11 specialized-vectors literals fry
12 sequences.deep destructors math.bitwise opengl.gl
13 game.models game.models.obj game.models.loader game.models.collada
14 prettyprint images.tga literals ;
15 FROM: alien.c-types => float ;
16 SPECIALIZED-ARRAY: float
17 SPECIALIZED-VECTOR: uint
18 IN: model-viewer
19
20 GLSL-SHADER: obj-vertex-shader vertex-shader
21 uniform mat4 mv_matrix;
22 uniform mat4 p_matrix;
23
24 attribute vec3 POSITION;
25 attribute vec3 TEXCOORD;
26 attribute vec3 NORMAL;
27
28 varying vec2 texcoord_fs;
29 varying vec3 normal_fs;
30 varying vec3 world_pos_fs;
31
32 void main()
33 {
34     vec4 position = mv_matrix * vec4(POSITION, 1.0);
35     gl_Position   = p_matrix * position;
36     world_pos_fs  = POSITION;
37     texcoord_fs   = TEXCOORD;
38     normal_fs     = NORMAL;
39 }
40 ;
41
42 GLSL-SHADER: obj-fragment-shader fragment-shader
43 uniform mat4 mv_matrix, p_matrix;
44 uniform sampler2D map_Ka;
45 uniform sampler2D map_bump;
46 uniform vec3 Ka;
47 uniform vec3 view_pos;
48 uniform vec3 light;
49 varying vec2 texcoord_fs;
50 varying vec3 normal_fs;
51 varying vec3 world_pos_fs;
52 void main()
53 {
54     vec4 d = texture2D(map_Ka, texcoord_fs.xy);
55     vec3 b = texture2D(map_bump, texcoord_fs.xy).xyz;
56     vec3 n = normal_fs;
57     vec3 v = normalize(view_pos - world_pos_fs);
58     vec3 l = normalize(light);
59     vec3 h = normalize(v + l);
60     float cosTh = saturate(dot(n, l));
61     gl_FragColor = d * cosTh
62                  + d * 0.5 * cosTh * pow(saturate(dot(n, h)), 10.0) ;
63 }
64 ;
65
66 GLSL-PROGRAM: obj-program
67     obj-vertex-shader obj-fragment-shader ;
68
69 UNIFORM-TUPLE: model-uniforms < mvp-uniforms
70     { "map_Ka"    texture-uniform   f }
71     { "map_bump"  texture-uniform   f }
72     { "Ka"        vec3-uniform      f }
73     { "light"     vec3-uniform      f }
74     { "view_pos"  vec3-uniform      f }
75     ;
76
77 TUPLE: model-state
78     models
79     vertex-arrays
80     index-vectors
81     textures
82     bumps
83     kas ;
84
85 TUPLE: model-world < wasd-world model-path model-state ;
86
87 TUPLE: vbo
88     vertex-buffer
89     index-buffer index-count vertex-format texture bump ka ;
90
91 : white-image ( -- image )
92     { 1 1 } BGR ubyte-components f
93     B{ 255 255 255 } image boa ;
94
95 : up-image ( -- image )
96     { 1 1 } BGR ubyte-components f
97     B{ 0 0 0 } image boa ;
98         
99 : make-texture ( pathname alt -- texture )
100     swap [ nip load-image ] [ ] if*
101     [
102         [ component-order>> ]
103         [ component-type>> ] bi
104         T{ texture-parameters
105            { wrap repeat-texcoord }
106            { min-filter filter-linear }
107            { min-mipmap-filter f } }
108         <texture-2d>
109     ]
110     [
111         0 swap [ allocate-texture-image ] 3keep 2drop
112     ] bi ;
113         
114 : <model-buffers> ( models -- buffers )
115     [
116         {
117             [ attribute-buffer>> underlying>> static-upload draw-usage vertex-buffer byte-array>buffer ]
118             [ index-buffer>> underlying>> static-upload draw-usage index-buffer byte-array>buffer ]
119             [ index-buffer>> length ]
120             [ vertex-format>> ]
121             [ material>> ambient-map>> white-image make-texture ]
122             [ material>> bump-map>> up-image make-texture ]
123             [ material>> ambient-reflectivity>> ]
124         } cleave vbo boa
125     ] map ;
126
127 : fill-model-state ( model-state -- )
128     dup models>> <model-buffers>
129     {
130         [
131             [
132                 [ vertex-buffer>> obj-program <program-instance> ]
133                 [ vertex-format>> ] bi <vertex-array*>
134             ] map >>vertex-arrays drop
135         ]
136         [
137             [
138                 [ index-buffer>> ] [ index-count>> ] bi
139                 '[ _ 0 <buffer-ptr> _ uint-indexes <index-elements> ] call
140             ] map >>index-vectors drop
141         ]
142         [ [ texture>> ] map >>textures drop ]
143         [ [ bump>> ] map >>bumps drop ]
144         [ [ ka>> ] map >>kas drop ]
145     } 2cleave ;
146
147 : <model-state> ( model-world -- model-state )
148     model-path>> 1array model-state new swap
149     [ load-models ] [ append ] map-reduce >>models ;
150
151 :: <model-uniforms> ( world -- uniforms )
152     world model-state>>
153     [ textures>> ] [ bumps>> ] [ kas>> ] tri
154     [| texture bump ka |
155         world wasd-mv-matrix
156         world wasd-p-matrix
157         texture bump ka
158         { 0.5 0.5 0.5 }
159         world location>>
160         model-uniforms boa
161     ] 3map ;
162
163 : clear-screen ( -- )
164     0 0 0 0 glClearColor 
165     1 glClearDepth
166     HEX: ffffffff glClearStencil
167     flags{ GL_COLOR_BUFFER_BIT
168       GL_DEPTH_BUFFER_BIT
169       GL_STENCIL_BUFFER_BIT } glClear ;
170     
171 : draw-model ( world -- )
172     clear-screen
173     face-ccw cull-back <triangle-cull-state> set-gpu-state
174     cmp-less <depth-state> set-gpu-state
175     [ model-state>> vertex-arrays>> ]
176     [ model-state>> index-vectors>> ]
177     [ <model-uniforms> ]
178     tri
179     [
180         {
181             { "primitive-mode"     [ 3drop triangles-mode ] }
182             { "uniforms"           [ nip nip ] }
183             { "vertex-array"       [ drop drop ] }
184             { "indexes"            [ drop nip ] }
185         } 3<render-set> render
186     ] 3each ;
187
188 TUPLE: model-attributes < game-attributes model-path ;
189
190 M: model-world draw-world* draw-model ;
191 M: model-world wasd-movement-speed drop 1/4. ;
192 M: model-world wasd-near-plane drop 1/32. ;
193 M: model-world wasd-far-plane drop 1024.0 ;
194 M: model-world begin-game-world
195     init-gpu
196     { 0.0 0.0 2.0 } 0 0 set-wasd-view
197     [ <model-state> [ fill-model-state ] keep ] [ (>>model-state) ] bi ;
198 M: model-world apply-world-attributes
199     {
200         [ model-path>> >>model-path ]
201         [ call-next-method ]
202     } cleave ;
203
204 :: open-model-viewer ( model-path -- )
205     [
206         f
207         T{ model-attributes
208            { world-class model-world }
209            { grab-input? t }
210            { title "Model Viewer" }
211            { pixel-format-attributes
212              { windowed double-buffered }
213            }
214            { pref-dim { 1024 768 } }
215            { tick-interval-micros 16666 }
216            { use-game-input? t }
217            { model-path model-path }
218         }
219         clone
220         open-window
221     ] with-ui ;