]> gitweb.factorcode.org Git - factor.git/blob - extra/jamshred/gl/gl.factor
core/basis/extra: use flags{ } in places.
[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.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
8 IN: jamshred.gl
9
10 CONSTANT: min-vertices 6
11 CONSTANT: max-vertices 32
12
13 CONSTANT: n-vertices 32
14
15 ! render enough of the tunnel that it looks continuous
16 CONSTANT: n-segments-ahead 60
17 CONSTANT: n-segments-behind 40
18
19 ! so that we can't see through the wall, we draw it a bit further away
20 CONSTANT: wall-drawing-offset 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     [ <iota> ] keep [ / 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     segment theta draw-segment-vertex
54     next-segment theta draw-segment-vertex ;
55
56 : draw-segment ( next-segment segment -- )
57     GL_QUAD_STRIP [
58         [ draw-vertex-pair ] 2curry
59         n-vertices equally-spaced-radians float-array{ 0.0 } append swap each
60     ] do-state ;
61
62 : draw-segments ( segments -- )
63     1 over length pick subseq swap [ draw-segment ] 2each ;
64
65 : segments-to-render ( player -- segments )
66     dup nearest-segment>> number>> dup n-segments-behind -
67     swap n-segments-ahead + rot tunnel>> sub-tunnel ;
68
69 : draw-tunnel ( player -- )
70     segments-to-render draw-segments ;
71
72 : init-graphics ( -- )
73     GL_DEPTH_TEST glEnable
74     GL_SCISSOR_TEST glDisable
75     1.0 glClearDepth
76     0.0 0.0 0.0 0.0 glClearColor
77     GL_PROJECTION glMatrixMode glPushMatrix
78     GL_MODELVIEW glMatrixMode glPushMatrix
79     GL_LEQUAL glDepthFunc
80     GL_LIGHTING glEnable
81     GL_LIGHT0 glEnable
82     GL_FOG glEnable
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 ;
90
91 : cleanup-graphics ( -- )
92     GL_DEPTH_TEST glDisable
93     GL_SCISSOR_TEST glEnable
94     GL_MODELVIEW glMatrixMode glPopMatrix
95     GL_PROJECTION glMatrixMode glPopMatrix
96     GL_LIGHTING glDisable
97     GL_LIGHT0 glDisable
98     GL_FOG glDisable
99     GL_COLOR_MATERIAL glDisable ;
100
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 ;
106
107 : player-view ( player -- )
108     [ location>> ]
109     [ [ location>> ] [ forward>> ] bi v+ ]
110     [ up>> ] tri gl-look-at ;
111
112 : draw-jamshred ( jamshred width height -- )
113     pre-draw jamshred-player [ player-view ] [ draw-tunnel ] bi ;