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 CONSTANT: min-vertices 6
10 CONSTANT: max-vertices 32
12 CONSTANT: n-vertices 32
14 ! render enough of the tunnel that it looks continuous
15 CONSTANT: n-segments-ahead 60
16 CONSTANT: n-segments-behind 40
18 ! so that we can't see through the wall, we draw it a bit further away
19 CONSTANT: wall-drawing-offset 0.15
21 : wall-drawing-radius ( segment -- r )
22 radius>> wall-drawing-offset + ;
24 : wall-up ( segment -- v )
25 [ wall-drawing-radius ] [ up>> ] bi n*v ;
27 : wall-left ( segment -- v )
28 [ wall-drawing-radius ] [ left>> ] bi n*v ;
30 : segment-vertex ( theta segment -- vertex )
32 [ wall-up swap sin v*n ] [ wall-left swap cos v*n ] 2bi v+
37 : segment-vertex-normal ( vertex segment -- normal )
38 location>> swap v- normalize ;
40 : segment-vertex-and-normal ( segment theta -- vertex normal )
41 swap [ segment-vertex ] keep dupd segment-vertex-normal ;
43 : equally-spaced-radians ( n -- seq )
44 #! return a sequence of n numbers between 0 and 2pi
45 dup [ / pi 2 * * ] curry map ;
47 : draw-segment-vertex ( segment theta -- )
48 over color>> gl-color segment-vertex-and-normal
51 : draw-vertex-pair ( theta next-segment segment -- )
52 rot tuck draw-segment-vertex draw-segment-vertex ;
54 : draw-segment ( next-segment segment -- )
56 [ draw-vertex-pair ] 2curry
57 n-vertices equally-spaced-radians float-array{ 0.0 } append swap each
60 : draw-segments ( segments -- )
61 1 over length pick subseq swap [ draw-segment ] 2each ;
63 : segments-to-render ( player -- segments )
64 dup nearest-segment>> number>> dup n-segments-behind -
65 swap n-segments-ahead + rot tunnel>> sub-tunnel ;
67 : draw-tunnel ( player -- )
68 segments-to-render draw-segments ;
70 : init-graphics ( -- )
71 GL_DEPTH_TEST glEnable
72 GL_SCISSOR_TEST glDisable
74 0.0 0.0 0.0 0.0 glClearColor
75 GL_PROJECTION glMatrixMode glPushMatrix
76 GL_MODELVIEW glMatrixMode glPushMatrix
81 GL_FOG_DENSITY 0.09 glFogf
82 GL_FRONT GL_AMBIENT_AND_DIFFUSE glColorMaterial
83 GL_COLOR_MATERIAL glEnable
84 GL_LIGHT0 GL_POSITION float-array{ 0.0 0.0 0.0 1.0 } underlying>> glLightfv
85 GL_LIGHT0 GL_AMBIENT float-array{ 0.2 0.2 0.2 1.0 } underlying>> glLightfv
86 GL_LIGHT0 GL_DIFFUSE float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv
87 GL_LIGHT0 GL_SPECULAR float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv ;
89 : cleanup-graphics ( -- )
90 GL_DEPTH_TEST glDisable
91 GL_SCISSOR_TEST glEnable
92 GL_MODELVIEW glMatrixMode glPopMatrix
93 GL_PROJECTION glMatrixMode glPopMatrix
97 GL_COLOR_MATERIAL glDisable ;
99 : pre-draw ( width height -- )
100 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
101 GL_PROJECTION glMatrixMode glLoadIdentity
102 dup 0 = [ 2drop ] [ / >float 45.0 swap 0.1 100.0 gluPerspective ] if
103 GL_MODELVIEW glMatrixMode glLoadIdentity ;
105 : player-view ( player -- )
107 [ [ location>> ] [ forward>> ] bi v+ ]
108 [ up>> ] tri gl-look-at ;
110 : draw-jamshred ( jamshred width height -- )
111 pre-draw jamshred-player [ player-view ] [ draw-tunnel ] bi ;