]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/jamshred/gl/gl.factor
b78e7de88e892008ad9a9e9c2765b090030a3e57
[factor.git] / unmaintained / jamshred / gl / gl.factor
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 ;
7 IN: jamshred.gl
8
9 : min-vertices 6 ; inline
10 : max-vertices 32 ; inline
11
12 : n-vertices ( -- n ) 32 ; inline
13
14 ! render enough of the tunnel that it looks continuous
15 : n-segments-ahead ( -- n ) 60 ; inline
16 : n-segments-behind ( -- n ) 40 ; inline
17
18 : wall-drawing-offset ( -- n )
19     #! so that we can't see through the wall, we draw it a bit further away
20     0.15 ;
21
22 : wall-drawing-radius ( segment -- r )
23     radius>> wall-drawing-offset + ;
24
25 : wall-up ( segment -- v )
26     [ wall-drawing-radius ] [ up>> ] bi n*v ;
27
28 : wall-left ( segment -- v )
29     [ wall-drawing-radius ] [ left>> ] bi n*v ;
30
31 : segment-vertex ( theta segment -- vertex )
32     [
33         [ wall-up swap sin v*n ] [ wall-left swap cos v*n ] 2bi v+
34     ] [
35         location>> v+
36     ] bi ;
37
38 : segment-vertex-normal ( vertex segment -- normal )
39     location>> swap v- normalize ;
40
41 : segment-vertex-and-normal ( segment theta -- vertex normal )
42     swap [ segment-vertex ] keep dupd segment-vertex-normal ;
43
44 : equally-spaced-radians ( n -- seq )
45     #! return a sequence of n numbers between 0 and 2pi
46     dup [ / pi 2 * * ] curry map ;
47
48 : draw-segment-vertex ( segment theta -- )
49     over color>> gl-color segment-vertex-and-normal
50     gl-normal gl-vertex ;
51
52 : draw-vertex-pair ( theta next-segment segment -- )
53     rot tuck draw-segment-vertex draw-segment-vertex ;
54
55 : draw-segment ( next-segment segment -- )
56     GL_QUAD_STRIP [
57         [ draw-vertex-pair ] 2curry
58         n-vertices equally-spaced-radians F{ 0.0 } append swap each
59     ] do-state ;
60
61 : draw-segments ( segments -- )
62     1 over length pick subseq swap [ draw-segment ] 2each ;
63
64 : segments-to-render ( player -- segments )
65     dup nearest-segment>> number>> dup n-segments-behind -
66     swap n-segments-ahead + rot tunnel>> sub-tunnel ;
67
68 : draw-tunnel ( player -- )
69     segments-to-render draw-segments ;
70
71 : init-graphics ( width height -- )
72     GL_DEPTH_TEST glEnable
73     GL_SCISSOR_TEST glDisable
74     1.0 glClearDepth
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
80     GL_LEQUAL glDepthFunc
81     GL_LIGHTING glEnable
82     GL_LIGHT0 glEnable
83     GL_FOG glEnable
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 ;
91
92 : player-view ( player -- )
93     [ location>> ]
94     [ [ location>> ] [ forward>> ] bi v+ ]
95     [ up>> ] tri gl-look-at ;
96
97 : draw-jamshred ( jamshred width height -- )
98     init-graphics jamshred-player [ player-view ] [ draw-tunnel ] bi ;
99