2009-04-15 05:59:52 -04:00
|
|
|
! Copyright (C) 2007, 2008 Alex Chapman
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2020-02-11 16:27:42 -05:00
|
|
|
USING: accessors alien.c-types jamshred.game jamshred.tunnel
|
|
|
|
kernel literals locals math math.constants math.functions
|
|
|
|
math.vectors opengl opengl.demo-support opengl.gl opengl.glu
|
|
|
|
sequences specialized-arrays ;
|
|
|
|
SPECIALIZED-ARRAY: alien.c-types:float
|
2009-04-15 05:59:52 -04:00
|
|
|
IN: jamshred.gl
|
|
|
|
|
2009-04-19 22:20:03 -04:00
|
|
|
CONSTANT: min-vertices 6
|
|
|
|
CONSTANT: max-vertices 32
|
2009-04-15 05:59:52 -04:00
|
|
|
|
2009-04-19 22:20:03 -04:00
|
|
|
CONSTANT: n-vertices 32
|
2009-04-15 05:59:52 -04:00
|
|
|
|
|
|
|
! render enough of the tunnel that it looks continuous
|
2009-04-19 22:20:03 -04:00
|
|
|
CONSTANT: n-segments-ahead 60
|
|
|
|
CONSTANT: n-segments-behind 40
|
2009-04-15 05:59:52 -04:00
|
|
|
|
2009-04-19 22:20:03 -04:00
|
|
|
! so that we can't see through the wall, we draw it a bit further away
|
|
|
|
CONSTANT: wall-drawing-offset 0.15
|
2009-04-15 05:59:52 -04:00
|
|
|
|
|
|
|
: wall-drawing-radius ( segment -- r )
|
|
|
|
radius>> wall-drawing-offset + ;
|
|
|
|
|
|
|
|
: wall-up ( segment -- v )
|
|
|
|
[ wall-drawing-radius ] [ up>> ] bi n*v ;
|
|
|
|
|
|
|
|
: wall-left ( segment -- v )
|
|
|
|
[ wall-drawing-radius ] [ left>> ] bi n*v ;
|
|
|
|
|
|
|
|
: segment-vertex ( theta segment -- vertex )
|
|
|
|
[
|
|
|
|
[ wall-up swap sin v*n ] [ wall-left swap cos v*n ] 2bi v+
|
|
|
|
] [
|
|
|
|
location>> v+
|
|
|
|
] bi ;
|
|
|
|
|
|
|
|
: segment-vertex-normal ( vertex segment -- normal )
|
|
|
|
location>> swap v- normalize ;
|
|
|
|
|
|
|
|
: segment-vertex-and-normal ( segment theta -- vertex normal )
|
|
|
|
swap [ segment-vertex ] keep dupd segment-vertex-normal ;
|
|
|
|
|
|
|
|
: equally-spaced-radians ( n -- seq )
|
2015-09-08 19:15:10 -04:00
|
|
|
! return a sequence of n numbers between 0 and 2pi
|
2017-06-01 17:59:35 -04:00
|
|
|
[ <iota> ] keep [ / pi 2 * * ] curry map ;
|
2009-04-15 05:59:52 -04:00
|
|
|
|
|
|
|
: draw-segment-vertex ( segment theta -- )
|
|
|
|
over color>> gl-color segment-vertex-and-normal
|
|
|
|
gl-normal gl-vertex ;
|
|
|
|
|
2009-11-05 23:22:21 -05:00
|
|
|
:: draw-vertex-pair ( theta next-segment segment -- )
|
|
|
|
segment theta draw-segment-vertex
|
|
|
|
next-segment theta draw-segment-vertex ;
|
2009-04-15 05:59:52 -04:00
|
|
|
|
|
|
|
: draw-segment ( next-segment segment -- )
|
|
|
|
GL_QUAD_STRIP [
|
|
|
|
[ draw-vertex-pair ] 2curry
|
2009-04-15 21:06:28 -04:00
|
|
|
n-vertices equally-spaced-radians float-array{ 0.0 } append swap each
|
2009-04-15 05:59:52 -04:00
|
|
|
] do-state ;
|
|
|
|
|
|
|
|
: draw-segments ( segments -- )
|
|
|
|
1 over length pick subseq swap [ draw-segment ] 2each ;
|
|
|
|
|
|
|
|
: segments-to-render ( player -- segments )
|
|
|
|
dup nearest-segment>> number>> dup n-segments-behind -
|
|
|
|
swap n-segments-ahead + rot tunnel>> sub-tunnel ;
|
|
|
|
|
|
|
|
: draw-tunnel ( player -- )
|
|
|
|
segments-to-render draw-segments ;
|
|
|
|
|
2009-04-15 21:06:28 -04:00
|
|
|
: init-graphics ( -- )
|
2009-04-15 05:59:52 -04:00
|
|
|
GL_DEPTH_TEST glEnable
|
|
|
|
GL_SCISSOR_TEST glDisable
|
|
|
|
1.0 glClearDepth
|
|
|
|
0.0 0.0 0.0 0.0 glClearColor
|
2009-04-15 21:06:28 -04:00
|
|
|
GL_PROJECTION glMatrixMode glPushMatrix
|
|
|
|
GL_MODELVIEW glMatrixMode glPushMatrix
|
2009-04-15 05:59:52 -04:00
|
|
|
GL_LEQUAL glDepthFunc
|
|
|
|
GL_LIGHTING glEnable
|
|
|
|
GL_LIGHT0 glEnable
|
|
|
|
GL_FOG glEnable
|
|
|
|
GL_FOG_DENSITY 0.09 glFogf
|
|
|
|
GL_FRONT GL_AMBIENT_AND_DIFFUSE glColorMaterial
|
|
|
|
GL_COLOR_MATERIAL glEnable
|
|
|
|
GL_LIGHT0 GL_POSITION float-array{ 0.0 0.0 0.0 1.0 } underlying>> glLightfv
|
|
|
|
GL_LIGHT0 GL_AMBIENT float-array{ 0.2 0.2 0.2 1.0 } underlying>> glLightfv
|
|
|
|
GL_LIGHT0 GL_DIFFUSE float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv
|
|
|
|
GL_LIGHT0 GL_SPECULAR float-array{ 1.0 1.0 1.0 1.0 } underlying>> glLightfv ;
|
|
|
|
|
2009-04-15 21:06:28 -04:00
|
|
|
: cleanup-graphics ( -- )
|
|
|
|
GL_DEPTH_TEST glDisable
|
|
|
|
GL_SCISSOR_TEST glEnable
|
|
|
|
GL_MODELVIEW glMatrixMode glPopMatrix
|
|
|
|
GL_PROJECTION glMatrixMode glPopMatrix
|
|
|
|
GL_LIGHTING glDisable
|
|
|
|
GL_LIGHT0 glDisable
|
|
|
|
GL_FOG glDisable
|
|
|
|
GL_COLOR_MATERIAL glDisable ;
|
|
|
|
|
|
|
|
: pre-draw ( width height -- )
|
2020-02-11 16:27:42 -05:00
|
|
|
flags{ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT } glClear
|
2009-04-15 21:06:28 -04:00
|
|
|
GL_PROJECTION glMatrixMode glLoadIdentity
|
|
|
|
dup 0 = [ 2drop ] [ / >float 45.0 swap 0.1 100.0 gluPerspective ] if
|
|
|
|
GL_MODELVIEW glMatrixMode glLoadIdentity ;
|
|
|
|
|
2009-04-15 05:59:52 -04:00
|
|
|
: player-view ( player -- )
|
|
|
|
[ location>> ]
|
|
|
|
[ [ location>> ] [ forward>> ] bi v+ ]
|
|
|
|
[ up>> ] tri gl-look-at ;
|
|
|
|
|
|
|
|
: draw-jamshred ( jamshred width height -- )
|
2009-04-15 21:06:28 -04:00
|
|
|
pre-draw jamshred-player [ player-view ] [ draw-tunnel ] bi ;
|