]> gitweb.factorcode.org Git - factor.git/blob - extra/jamshred/gl/gl.factor
Merge branch 'master' of git://factorcode.org/git/factor into maintenance
[factor.git] / extra / 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 jamshred.player jamshred.tunnel kernel math math.constants math.functions math.vectors opengl opengl.gl opengl.glu sequences float-arrays ;
4 IN: jamshred.gl
5
6 : min-vertices 6 ; inline
7 : max-vertices 32 ; inline
8
9 : n-vertices ( -- n ) 32 ; inline
10
11 ! render enough of the tunnel that it looks continuous
12 : n-segments-ahead ( -- n ) 60 ; inline
13 : n-segments-behind ( -- n ) 40 ; inline
14
15 : wall-drawing-offset ( -- n )
16     #! so that we can't see through the wall, we draw it a bit further away
17     0.15 ;
18
19 : wall-drawing-radius ( segment -- r )
20     radius>> wall-drawing-offset + ;
21
22 : wall-up ( segment -- v )
23     [ wall-drawing-radius ] [ up>> ] bi n*v ;
24
25 : wall-left ( segment -- v )
26     [ wall-drawing-radius ] [ left>> ] bi n*v ;
27
28 : segment-vertex ( theta segment -- vertex )
29     [
30         [ wall-up swap sin v*n ] [ wall-left swap cos v*n ] 2bi v+
31     ] [
32         location>> v+
33     ] bi ;
34
35 : segment-vertex-normal ( vertex segment -- normal )
36     location>> swap v- normalize ;
37
38 : segment-vertex-and-normal ( segment theta -- vertex normal )
39     swap [ segment-vertex ] keep dupd segment-vertex-normal ;
40
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
46     gl-normal gl-vertex ;
47
48 : draw-vertex-pair ( theta next-segment segment -- )
49     rot tuck draw-segment-vertex draw-segment-vertex ;
50
51 : draw-segment ( next-segment segment -- )
52     GL_QUAD_STRIP [
53         [ draw-vertex-pair ] 2curry
54         n-vertices equally-spaced-radians F{ 0.0 } append swap each
55     ] do-state ;
56
57 : draw-segments ( segments -- )
58     1 over length pick subseq swap [ draw-segment ] 2each ;
59
60 : segments-to-render ( player -- segments )
61     dup nearest-segment>> number>> dup n-segments-behind -
62     swap n-segments-ahead + rot tunnel>> sub-tunnel ;
63
64 : draw-tunnel ( player -- )
65     segments-to-render draw-segments ;
66
67 : init-graphics ( width height -- )
68     GL_DEPTH_TEST glEnable
69     GL_SCISSOR_TEST glDisable
70     1.0 glClearDepth
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
76     GL_LEQUAL glDepthFunc
77     GL_LIGHTING glEnable
78     GL_LIGHT0 glEnable
79     GL_FOG glEnable
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 ;
87
88 : player-view ( player -- )
89     [ location>> ]
90     [ [ location>> ] [ forward>> ] bi v+ ]
91     [ up>> ] tri gl-look-at ;
92
93 : draw-jamshred ( jamshred width height -- )
94     init-graphics jamshred-player [ player-view ] [ draw-tunnel ] bi ;
95