1 ! Copyright (C) 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types jamshred.game jamshred.oint
4 jamshred.player jamshred.tunnel kernel math math.constants
5 math.functions math.vectors opengl opengl.gl opengl.glu
6 opengl.demo-support sequences specialized-arrays.float ;
9 : min-vertices 6 ; inline
10 : max-vertices 32 ; inline
12 : n-vertices ( -- n ) 32 ; inline
14 ! render enough of the tunnel that it looks continuous
15 : n-segments-ahead ( -- n ) 60 ; inline
16 : n-segments-behind ( -- n ) 40 ; inline
18 : wall-drawing-offset ( -- n )
19 #! so that we can't see through the wall, we draw it a bit further away
22 : wall-drawing-radius ( segment -- r )
23 radius>> wall-drawing-offset + ;
25 : wall-up ( segment -- v )
26 [ wall-drawing-radius ] [ up>> ] bi n*v ;
28 : wall-left ( segment -- v )
29 [ wall-drawing-radius ] [ left>> ] bi n*v ;
31 : segment-vertex ( theta segment -- vertex )
33 [ wall-up swap sin v*n ] [ wall-left swap cos v*n ] 2bi v+
38 : segment-vertex-normal ( vertex segment -- normal )
39 location>> swap v- normalize ;
41 : segment-vertex-and-normal ( segment theta -- vertex normal )
42 swap [ segment-vertex ] keep dupd segment-vertex-normal ;
44 : equally-spaced-radians ( n -- seq )
45 #! return a sequence of n numbers between 0 and 2pi
46 dup [ / pi 2 * * ] curry map ;
48 : draw-segment-vertex ( segment theta -- )
49 over color>> gl-color segment-vertex-and-normal
52 : draw-vertex-pair ( theta next-segment segment -- )
53 rot tuck draw-segment-vertex draw-segment-vertex ;
55 : draw-segment ( next-segment segment -- )
57 [ draw-vertex-pair ] 2curry
58 n-vertices equally-spaced-radians F{ 0.0 } append swap each
61 : draw-segments ( segments -- )
62 1 over length pick subseq swap [ draw-segment ] 2each ;
64 : segments-to-render ( player -- segments )
65 dup nearest-segment>> number>> dup n-segments-behind -
66 swap n-segments-ahead + rot tunnel>> sub-tunnel ;
68 : draw-tunnel ( player -- )
69 segments-to-render draw-segments ;
71 : init-graphics ( width height -- )
72 GL_DEPTH_TEST glEnable
73 GL_SCISSOR_TEST glDisable
75 0.0 0.0 0.0 0.0 glClearColor
76 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
77 GL_PROJECTION glMatrixMode glLoadIdentity
78 dup 0 = [ 2drop ] [ / >float 45.0 swap 0.1 100.0 gluPerspective ] if
79 GL_MODELVIEW glMatrixMode glLoadIdentity
84 GL_FOG_DENSITY 0.09 glFogf
85 GL_FRONT GL_AMBIENT_AND_DIFFUSE glColorMaterial
86 GL_COLOR_MATERIAL glEnable
87 GL_LIGHT0 GL_POSITION float-array{ 0.0 0.0 0.0 1.0 } underlying>> glLightfv
88 GL_LIGHT0 GL_AMBIENT float-array{ 0.2 0.2 0.2 1.0 } underlying>> glLightfv
89 GL_LIGHT0 GL_DIFFUSE float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv
90 GL_LIGHT0 GL_SPECULAR float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv ;
92 : player-view ( player -- )
94 [ [ location>> ] [ forward>> ] bi v+ ]
95 [ up>> ] tri gl-look-at ;
97 : draw-jamshred ( jamshred width height -- )
98 init-graphics jamshred-player [ player-view ] [ draw-tunnel ] bi ;