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 jamshred.player jamshred.tunnel kernel math math.constants math.functions math.vectors opengl opengl.gl opengl.glu sequences float-arrays ;
6 : min-vertices 6 ; inline
7 : max-vertices 32 ; inline
9 : n-vertices ( -- n ) 32 ; inline
11 ! render enough of the tunnel that it looks continuous
12 : n-segments-ahead ( -- n ) 60 ; inline
13 : n-segments-behind ( -- n ) 40 ; inline
15 : wall-drawing-offset ( -- n )
16 #! so that we can't see through the wall, we draw it a bit further away
19 : wall-drawing-radius ( segment -- r )
20 radius>> wall-drawing-offset + ;
22 : wall-up ( segment -- v )
23 [ wall-drawing-radius ] [ up>> ] bi n*v ;
25 : wall-left ( segment -- v )
26 [ wall-drawing-radius ] [ left>> ] bi n*v ;
28 : segment-vertex ( theta segment -- vertex )
30 [ wall-up swap sin v*n ] [ wall-left swap cos v*n ] 2bi v+
35 : segment-vertex-normal ( vertex segment -- normal )
36 location>> swap v- normalize ;
38 : segment-vertex-and-normal ( segment theta -- vertex normal )
39 swap [ segment-vertex ] keep dupd segment-vertex-normal ;
41 : equally-spaced-radians ( n -- seq )
42 #! return a sequence of n numbers between 0 and 2pi
43 dup [ / pi 2 * * ] curry map ;
44 : draw-segment-vertex ( segment theta -- )
45 over color>> gl-color segment-vertex-and-normal
48 : draw-vertex-pair ( theta next-segment segment -- )
49 rot tuck draw-segment-vertex draw-segment-vertex ;
51 : draw-segment ( next-segment segment -- )
53 [ draw-vertex-pair ] 2curry
54 n-vertices equally-spaced-radians F{ 0.0 } append swap each
57 : draw-segments ( segments -- )
58 1 over length pick subseq swap [ draw-segment ] 2each ;
60 : segments-to-render ( player -- segments )
61 dup nearest-segment>> number>> dup n-segments-behind -
62 swap n-segments-ahead + rot tunnel>> sub-tunnel ;
64 : draw-tunnel ( player -- )
65 segments-to-render draw-segments ;
67 : init-graphics ( width height -- )
68 GL_DEPTH_TEST glEnable
69 GL_SCISSOR_TEST glDisable
71 0.0 0.0 0.0 0.0 glClearColor
72 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
73 GL_PROJECTION glMatrixMode glLoadIdentity
74 dup 0 = [ 2drop ] [ / >float 45.0 swap 0.1 100.0 gluPerspective ] if
75 GL_MODELVIEW glMatrixMode glLoadIdentity
80 GL_FOG_DENSITY 0.09 glFogf
81 GL_FRONT GL_AMBIENT_AND_DIFFUSE glColorMaterial
82 GL_COLOR_MATERIAL glEnable
83 GL_LIGHT0 GL_POSITION F{ 0.0 0.0 0.0 1.0 } >c-float-array glLightfv
84 GL_LIGHT0 GL_AMBIENT F{ 0.2 0.2 0.2 1.0 } >c-float-array glLightfv
85 GL_LIGHT0 GL_DIFFUSE F{ 1.0 1.0 1.0 1.0 } >c-float-array glLightfv
86 GL_LIGHT0 GL_SPECULAR F{ 1.0 1.0 1.0 1.0 } >c-float-array glLightfv ;
88 : player-view ( player -- )
90 [ [ location>> ] [ forward>> ] bi v+ ]
91 [ up>> ] tri gl-look-at ;
93 : draw-jamshred ( jamshred width height -- )
94 init-graphics jamshred-player [ player-view ] [ draw-tunnel ] bi ;