]> gitweb.factorcode.org Git - factor.git/blob - extra/jamshred/gl/gl.factor
Specialized array overhaul
[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 ;
7 SPECIALIZED-ARRAY: 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     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 float-array{ 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 ( -- )
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_PROJECTION glMatrixMode glPushMatrix
77     GL_MODELVIEW glMatrixMode glPushMatrix
78     GL_LEQUAL glDepthFunc
79     GL_LIGHTING glEnable
80     GL_LIGHT0 glEnable
81     GL_FOG glEnable
82     GL_FOG_DENSITY 0.09 glFogf
83     GL_FRONT GL_AMBIENT_AND_DIFFUSE glColorMaterial
84     GL_COLOR_MATERIAL glEnable
85     GL_LIGHT0 GL_POSITION float-array{ 0.0 0.0 0.0 1.0 } underlying>> glLightfv
86     GL_LIGHT0 GL_AMBIENT float-array{ 0.2 0.2 0.2 1.0 } underlying>> glLightfv
87     GL_LIGHT0 GL_DIFFUSE float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv
88     GL_LIGHT0 GL_SPECULAR float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv ;
89
90 : cleanup-graphics ( -- )
91     GL_DEPTH_TEST glDisable
92     GL_SCISSOR_TEST glEnable
93     GL_MODELVIEW glMatrixMode glPopMatrix
94     GL_PROJECTION glMatrixMode glPopMatrix
95     GL_LIGHTING glDisable
96     GL_LIGHT0 glDisable
97     GL_FOG glDisable
98     GL_COLOR_MATERIAL glDisable ;
99
100 : pre-draw ( width height -- )
101     GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
102     GL_PROJECTION glMatrixMode glLoadIdentity
103     dup 0 = [ 2drop ] [ / >float 45.0 swap 0.1 100.0 gluPerspective ] if
104     GL_MODELVIEW glMatrixMode glLoadIdentity ;
105
106 : player-view ( player -- )
107     [ location>> ]
108     [ [ location>> ] [ forward>> ] bi v+ ]
109     [ up>> ] tri gl-look-at ;
110
111 : draw-jamshred ( jamshred width height -- )
112     pre-draw jamshred-player [ player-view ] [ draw-tunnel ] bi ;