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.tunnel
4 kernel literals locals math math.constants math.functions
5 math.vectors opengl opengl.demo-support opengl.gl opengl.glu
6 sequences specialized-arrays ;
7 SPECIALIZED-ARRAY: alien.c-types:float
10 CONSTANT: min-vertices 6
11 CONSTANT: max-vertices 32
13 CONSTANT: n-vertices 32
15 ! render enough of the tunnel that it looks continuous
16 CONSTANT: n-segments-ahead 60
17 CONSTANT: n-segments-behind 40
19 ! so that we can't see through the wall, we draw it a bit further away
20 CONSTANT: wall-drawing-offset 0.15
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 [ <iota> ] keep [ / 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 segment theta draw-segment-vertex
54 next-segment theta draw-segment-vertex ;
56 : draw-segment ( next-segment segment -- )
58 [ draw-vertex-pair ] 2curry
59 n-vertices equally-spaced-radians float-array{ 0.0 } append swap each
62 : draw-segments ( segments -- )
63 1 over length pick subseq swap [ draw-segment ] 2each ;
65 : segments-to-render ( player -- segments )
66 dup nearest-segment>> number>> dup n-segments-behind -
67 swap n-segments-ahead + rot tunnel>> sub-tunnel ;
69 : draw-tunnel ( player -- )
70 segments-to-render draw-segments ;
72 : init-graphics ( -- )
73 GL_DEPTH_TEST glEnable
74 GL_SCISSOR_TEST glDisable
76 0.0 0.0 0.0 0.0 glClearColor
77 GL_PROJECTION glMatrixMode glPushMatrix
78 GL_MODELVIEW glMatrixMode glPushMatrix
83 GL_FOG_DENSITY 0.09 glFogf
84 GL_FRONT GL_AMBIENT_AND_DIFFUSE glColorMaterial
85 GL_COLOR_MATERIAL glEnable
86 GL_LIGHT0 GL_POSITION float-array{ 0.0 0.0 0.0 1.0 } underlying>> glLightfv
87 GL_LIGHT0 GL_AMBIENT float-array{ 0.2 0.2 0.2 1.0 } underlying>> glLightfv
88 GL_LIGHT0 GL_DIFFUSE float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv
89 GL_LIGHT0 GL_SPECULAR float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv ;
91 : cleanup-graphics ( -- )
92 GL_DEPTH_TEST glDisable
93 GL_SCISSOR_TEST glEnable
94 GL_MODELVIEW glMatrixMode glPopMatrix
95 GL_PROJECTION glMatrixMode glPopMatrix
99 GL_COLOR_MATERIAL glDisable ;
101 : pre-draw ( width height -- )
102 flags{ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT } glClear
103 GL_PROJECTION glMatrixMode glLoadIdentity
104 dup 0 = [ 2drop ] [ / >float 45.0 swap 0.1 100.0 gluPerspective ] if
105 GL_MODELVIEW glMatrixMode glLoadIdentity ;
107 : player-view ( player -- )
109 [ [ location>> ] [ forward>> ] bi v+ ]
110 [ up>> ] tri gl-look-at ;
112 : draw-jamshred ( jamshred width height -- )
113 pre-draw jamshred-player [ player-view ] [ draw-tunnel ] bi ;