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
20 GLSL-SHADER: obj-vertex-shader vertex-shader
21 uniform mat4 mv_matrix;
22 uniform mat4 p_matrix;
24 attribute vec3 POSITION;
25 attribute vec3 TEXCOORD;
26 attribute vec3 NORMAL;
28 varying vec2 texcoord_fs;
29 varying vec3 normal_fs;
30 varying vec3 world_pos_fs;
34 vec4 position = mv_matrix * vec4(POSITION, 1.0);
35 gl_Position = p_matrix * position;
36 world_pos_fs = POSITION;
37 texcoord_fs = TEXCOORD;
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;
47 uniform vec3 view_pos;
49 varying vec2 texcoord_fs;
50 varying vec3 normal_fs;
51 varying vec3 world_pos_fs;
54 vec4 d = texture2D(map_Ka, texcoord_fs.xy);
55 vec3 b = texture2D(map_bump, texcoord_fs.xy).xyz;
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) ;
66 GLSL-PROGRAM: obj-program
67 obj-vertex-shader obj-fragment-shader ;
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 }
85 TUPLE: model-world < wasd-world model-path model-state ;
89 index-buffer index-count vertex-format texture bump ka ;
91 : white-image ( -- image )
92 { 1 1 } BGR ubyte-components f
93 B{ 255 255 255 } image boa ;
95 : up-image ( -- image )
96 { 1 1 } BGR ubyte-components f
97 B{ 0 0 0 } image boa ;
99 : make-texture ( pathname alt -- texture )
100 swap [ nip load-image ] [ ] if*
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 } }
111 0 swap [ allocate-texture-image ] 3keep 2drop
114 : <model-buffers> ( models -- buffers )
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 ]
121 [ material>> ambient-map>> white-image make-texture ]
122 [ material>> bump-map>> up-image make-texture ]
123 [ material>> ambient-reflectivity>> ]
127 : fill-model-state ( model-state -- )
128 dup models>> <model-buffers>
132 [ vertex-buffer>> obj-program <program-instance> ]
133 [ vertex-format>> ] bi <vertex-array*>
134 ] map >>vertex-arrays drop
138 [ index-buffer>> ] [ index-count>> ] bi
139 '[ _ 0 <buffer-ptr> _ uint-indexes <index-elements> ] call
140 ] map >>index-vectors drop
142 [ [ texture>> ] map >>textures drop ]
143 [ [ bump>> ] map >>bumps drop ]
144 [ [ ka>> ] map >>kas drop ]
147 : <model-state> ( model-world -- model-state )
148 model-path>> 1array model-state new swap
149 [ load-models ] [ append ] map-reduce >>models ;
151 :: <model-uniforms> ( world -- uniforms )
153 [ textures>> ] [ bumps>> ] [ kas>> ] tri
163 : clear-screen ( -- )
166 HEX: ffffffff glClearStencil
167 flags{ GL_COLOR_BUFFER_BIT
169 GL_STENCIL_BUFFER_BIT } glClear ;
171 : draw-model ( world -- )
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>> ]
181 { "primitive-mode" [ 3drop triangles-mode ] }
182 { "uniforms" [ nip nip ] }
183 { "vertex-array" [ drop drop ] }
184 { "indexes" [ drop nip ] }
185 } 3<render-set> render
188 TUPLE: model-attributes < game-attributes model-path ;
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
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
200 [ model-path>> >>model-path ]
204 :: open-model-viewer ( model-path -- )
208 { world-class model-world }
210 { title "Model Viewer" }
211 { pixel-format-attributes
212 { windowed double-buffered }
214 { pref-dim { 1024 768 } }
215 { tick-interval-micros 16666 }
216 { use-game-input? t }
217 { model-path model-path }