| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | ! Copyright (C) 2010 Erik Charlebois | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: accessors alien.c-types arrays classes.struct combinators | 
					
						
							|  |  |  | combinators.short-circuit game.loop game.worlds gpu gpu.buffers | 
					
						
							|  |  |  | gpu.util.wasd gpu.framebuffers gpu.render gpu.shaders gpu.state | 
					
						
							|  |  |  | gpu.textures gpu.util grouping http.client images images.loader | 
					
						
							|  |  |  | io io.encodings.ascii io.files io.files.temp kernel locals math | 
					
						
							|  |  |  | math.matrices math.vectors.simd math.parser math.vectors | 
					
						
							|  |  |  | method-chains namespaces sequences splitting threads ui ui.gadgets | 
					
						
							|  |  |  | ui.gadgets.worlds ui.pixel-formats specialized-arrays | 
					
						
							| 
									
										
										
										
											2010-04-21 01:27:52 -04:00
										 |  |  | specialized-vectors fry sequences.deep destructors math.bitwise opengl.gl | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | game.models game.models.obj game.models.loader game.models.collada | 
					
						
							| 
									
										
										
										
											2010-04-01 15:43:27 -04:00
										 |  |  | prettyprint images.tga literals ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | FROM: alien.c-types => float ;
 | 
					
						
							|  |  |  | SPECIALIZED-ARRAY: float
 | 
					
						
							|  |  |  | SPECIALIZED-VECTOR: uint | 
					
						
							| 
									
										
										
										
											2010-02-02 01:12:22 -05:00
										 |  |  | IN: model-viewer | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | GLSL-SHADER: obj-vertex-shader vertex-shader | 
					
						
							|  |  |  | uniform mat4 mv_matrix; | 
					
						
							|  |  |  | uniform mat4 p_matrix; | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | attribute vec3 POSITION; | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | attribute vec3 TEXCOORD; | 
					
						
							| 
									
										
										
										
											2010-01-30 22:48:10 -05:00
										 |  |  | attribute vec3 NORMAL; | 
					
						
							| 
									
										
										
										
											2010-01-31 19:56:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | varying vec2 texcoord_fs; | 
					
						
							|  |  |  | varying vec3 normal_fs; | 
					
						
							|  |  |  | varying vec3 world_pos_fs; | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | void main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     vec4 position = mv_matrix * vec4(POSITION, 1.0); | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |     gl_Position   = p_matrix * position; | 
					
						
							|  |  |  |     world_pos_fs  = POSITION; | 
					
						
							|  |  |  |     texcoord_fs   = TEXCOORD; | 
					
						
							|  |  |  |     normal_fs     = NORMAL; | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | } | 
					
						
							|  |  |  | ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | GLSL-SHADER: obj-fragment-shader fragment-shader | 
					
						
							| 
									
										
										
										
											2010-01-30 22:48:10 -05:00
										 |  |  | uniform mat4 mv_matrix, p_matrix; | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | uniform sampler2D map_Ka; | 
					
						
							|  |  |  | uniform sampler2D map_bump; | 
					
						
							|  |  |  | uniform vec3 Ka; | 
					
						
							|  |  |  | uniform vec3 view_pos; | 
					
						
							|  |  |  | uniform vec3 light; | 
					
						
							|  |  |  | varying vec2 texcoord_fs; | 
					
						
							|  |  |  | varying vec3 normal_fs; | 
					
						
							|  |  |  | varying vec3 world_pos_fs; | 
					
						
							| 
									
										
										
										
											2010-01-30 22:48:10 -05:00
										 |  |  | void main() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |     vec4 d = texture2D(map_Ka, texcoord_fs.xy); | 
					
						
							|  |  |  |     vec3 b = texture2D(map_bump, texcoord_fs.xy).xyz; | 
					
						
							|  |  |  |     vec3 n = normal_fs; | 
					
						
							|  |  |  |     vec3 v = normalize(view_pos - world_pos_fs); | 
					
						
							|  |  |  |     vec3 l = normalize(light); | 
					
						
							|  |  |  |     vec3 h = normalize(v + l); | 
					
						
							|  |  |  |     float cosTh = saturate(dot(n, l)); | 
					
						
							|  |  |  |     gl_FragColor = d * cosTh | 
					
						
							|  |  |  |                  + d * 0.5 * cosTh * pow(saturate(dot(n, h)), 10.0) ;
 | 
					
						
							| 
									
										
										
										
											2010-01-30 22:48:10 -05:00
										 |  |  | } | 
					
						
							|  |  |  | ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | GLSL-PROGRAM: obj-program | 
					
						
							|  |  |  |     obj-vertex-shader obj-fragment-shader ;
 | 
					
						
							| 
									
										
										
										
											2010-01-30 22:48:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-02 01:12:22 -05:00
										 |  |  | UNIFORM-TUPLE: model-uniforms < mvp-uniforms | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |     { "map_Ka"    texture-uniform   f } | 
					
						
							|  |  |  |     { "map_bump"  texture-uniform   f } | 
					
						
							|  |  |  |     { "Ka"        vec3-uniform      f } | 
					
						
							|  |  |  |     { "light"     vec3-uniform      f } | 
					
						
							|  |  |  |     { "view_pos"  vec3-uniform      f } | 
					
						
							|  |  |  |     ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-02 01:12:22 -05:00
										 |  |  | TUPLE: model-state | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  |     models | 
					
						
							|  |  |  |     vertex-arrays | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |     index-vectors | 
					
						
							|  |  |  |     textures | 
					
						
							|  |  |  |     bumps | 
					
						
							|  |  |  |     kas ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | TUPLE: model-world < wasd-world model-path model-state ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | TUPLE: vbo | 
					
						
							|  |  |  |     vertex-buffer | 
					
						
							|  |  |  |     index-buffer index-count vertex-format texture bump ka ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | : white-image ( -- image )
 | 
					
						
							| 
									
										
										
										
											2010-07-31 14:34:15 -04:00
										 |  |  |     <image> | 
					
						
							|  |  |  |         { 1 1 } >>dim | 
					
						
							|  |  |  |         BGR >>component-order | 
					
						
							|  |  |  |         ubyte-components >>component-type | 
					
						
							|  |  |  |         B{ 255 255 255 } >>bitmap ;
 | 
					
						
							| 
									
										
										
										
											2010-02-01 21:44:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | : up-image ( -- image )
 | 
					
						
							| 
									
										
										
										
											2010-07-31 14:34:15 -04:00
										 |  |  |     <image> | 
					
						
							|  |  |  |         { 1 1 } >>dim | 
					
						
							|  |  |  |         BGR >>component-order | 
					
						
							|  |  |  |         ubyte-components >>component-type | 
					
						
							|  |  |  |         B{ 0 0 0 } >>bitmap ;
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |          | 
					
						
							|  |  |  | : make-texture ( pathname alt -- texture )
 | 
					
						
							|  |  |  |     swap [ nip load-image ] [ ] if*
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ component-order>> ] | 
					
						
							|  |  |  |         [ component-type>> ] bi
 | 
					
						
							|  |  |  |         T{ texture-parameters | 
					
						
							|  |  |  |            { wrap repeat-texcoord } | 
					
						
							|  |  |  |            { min-filter filter-linear } | 
					
						
							|  |  |  |            { min-mipmap-filter f } } | 
					
						
							|  |  |  |         <texture-2d> | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         0 swap [ allocate-texture-image ] 3keep 2drop
 | 
					
						
							|  |  |  |     ] bi ;
 | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2010-02-02 01:12:22 -05:00
										 |  |  | : <model-buffers> ( models -- buffers )
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2010-02-01 21:44:09 -05:00
										 |  |  |         { | 
					
						
							|  |  |  |             [ attribute-buffer>> underlying>> static-upload draw-usage vertex-buffer byte-array>buffer ] | 
					
						
							|  |  |  |             [ index-buffer>> underlying>> static-upload draw-usage index-buffer byte-array>buffer ] | 
					
						
							|  |  |  |             [ index-buffer>> length ] | 
					
						
							|  |  |  |             [ vertex-format>> ] | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |             [ material>> ambient-map>> white-image make-texture ] | 
					
						
							|  |  |  |             [ material>> bump-map>> up-image make-texture ] | 
					
						
							|  |  |  |             [ material>> ambient-reflectivity>> ] | 
					
						
							| 
									
										
										
										
											2010-02-01 21:44:09 -05:00
										 |  |  |         } cleave vbo boa
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  |     ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-02 01:12:22 -05:00
										 |  |  | : fill-model-state ( model-state -- )
 | 
					
						
							|  |  |  |     dup models>> <model-buffers> | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 [ vertex-buffer>> obj-program <program-instance> ] | 
					
						
							| 
									
										
										
										
											2010-02-10 18:27:57 -05:00
										 |  |  |                 [ vertex-format>> ] bi <vertex-array*> | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |             ] map >>vertex-arrays drop
 | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 [ index-buffer>> ] [ index-count>> ] bi
 | 
					
						
							|  |  |  |                 '[ _ 0 <buffer-ptr> _ uint-indexes <index-elements> ] call
 | 
					
						
							|  |  |  |             ] map >>index-vectors drop
 | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         [ [ texture>> ] map >>textures drop ] | 
					
						
							|  |  |  |         [ [ bump>> ] map >>bumps drop ] | 
					
						
							|  |  |  |         [ [ ka>> ] map >>kas drop ] | 
					
						
							|  |  |  |     } 2cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <model-state> ( model-world -- model-state )
 | 
					
						
							|  |  |  |     model-path>> 1array model-state new swap
 | 
					
						
							|  |  |  |     [ load-models ] [ append ] map-reduce >>models ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: <model-uniforms> ( world -- uniforms )
 | 
					
						
							|  |  |  |     world model-state>> | 
					
						
							|  |  |  |     [ textures>> ] [ bumps>> ] [ kas>> ] tri
 | 
					
						
							|  |  |  |     [| texture bump ka | | 
					
						
							|  |  |  |         world wasd-mv-matrix | 
					
						
							|  |  |  |         world wasd-p-matrix | 
					
						
							|  |  |  |         texture bump ka | 
					
						
							|  |  |  |         { 0.5 0.5 0.5 } | 
					
						
							|  |  |  |         world location>> | 
					
						
							|  |  |  |         model-uniforms boa
 | 
					
						
							|  |  |  |     ] 3map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : clear-screen ( -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-31 19:56:26 -05:00
										 |  |  |     0 0 0 0 glClearColor  | 
					
						
							|  |  |  |     1 glClearDepth | 
					
						
							|  |  |  |     HEX: ffffffff glClearStencil | 
					
						
							| 
									
										
										
										
											2010-04-01 15:43:27 -04:00
										 |  |  |     flags{ GL_COLOR_BUFFER_BIT | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |       GL_DEPTH_BUFFER_BIT | 
					
						
							| 
									
										
										
										
											2010-04-01 15:43:27 -04:00
										 |  |  |       GL_STENCIL_BUFFER_BIT } glClear ;
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |      | 
					
						
							|  |  |  | : draw-model ( world -- )
 | 
					
						
							|  |  |  |     clear-screen | 
					
						
							|  |  |  |     face-ccw cull-back <triangle-cull-state> set-gpu-state | 
					
						
							|  |  |  |     cmp-less <depth-state> set-gpu-state | 
					
						
							|  |  |  |     [ model-state>> vertex-arrays>> ] | 
					
						
							|  |  |  |     [ model-state>> index-vectors>> ] | 
					
						
							|  |  |  |     [ <model-uniforms> ] | 
					
						
							|  |  |  |     tri
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |         { | 
					
						
							|  |  |  |             { "primitive-mode"     [ 3drop triangles-mode ] } | 
					
						
							|  |  |  |             { "uniforms"           [ nip nip ] } | 
					
						
							|  |  |  |             { "vertex-array"       [ drop drop ] } | 
					
						
							|  |  |  |             { "indexes"            [ drop nip ] } | 
					
						
							|  |  |  |         } 3<render-set> render | 
					
						
							|  |  |  |     ] 3each ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | TUPLE: model-attributes < game-attributes model-path ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | M: model-world draw-world* draw-model ;
 | 
					
						
							| 
									
										
										
										
											2010-02-02 01:12:22 -05:00
										 |  |  | M: model-world wasd-movement-speed drop 1/4. ;
 | 
					
						
							|  |  |  | M: model-world wasd-near-plane drop 1/32. ;
 | 
					
						
							|  |  |  | M: model-world wasd-far-plane drop 1024.0 ;
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | M: model-world begin-game-world | 
					
						
							|  |  |  |     init-gpu | 
					
						
							|  |  |  |     { 0.0 0.0 2.0 } 0 0 set-wasd-view | 
					
						
							| 
									
										
										
										
											2010-05-05 16:52:54 -04:00
										 |  |  |     [ <model-state> [ fill-model-state ] keep ] [ model-state<< ] bi ;
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | M: model-world apply-world-attributes | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ model-path>> >>model-path ] | 
					
						
							|  |  |  |         [ call-next-method ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							| 
									
										
										
										
											2010-01-29 18:48:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  | :: open-model-viewer ( model-path -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         f
 | 
					
						
							|  |  |  |         T{ model-attributes | 
					
						
							|  |  |  |            { world-class model-world } | 
					
						
							|  |  |  |            { grab-input? t } | 
					
						
							|  |  |  |            { title "Model Viewer" } | 
					
						
							|  |  |  |            { pixel-format-attributes | 
					
						
							|  |  |  |              { windowed double-buffered } | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  |            { pref-dim { 1024 768 } } | 
					
						
							| 
									
										
										
										
											2010-05-22 00:50:42 -04:00
										 |  |  |            { tick-interval-nanos $[ 60 fps ] } | 
					
						
							| 
									
										
										
										
											2010-02-05 18:51:53 -05:00
										 |  |  |            { use-game-input? t } | 
					
						
							|  |  |  |            { model-path model-path } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         clone
 | 
					
						
							|  |  |  |         open-window | 
					
						
							|  |  |  |     ] with-ui ;
 |