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