factor/basis/opengl/opengl.factor

271 lines
7.6 KiB
Factor
Raw Normal View History

2008-02-02 16:00:16 -05:00
! Copyright (C) 2005, 2008 Slava Pestov.
! Portions copyright (C) 2007 Eduardo Cavazos.
2008-01-26 23:05:37 -05:00
! Portions copyright (C) 2008 Joe Groff.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-01-28 02:06:01 -05:00
USING: alien alien.c-types continuations kernel libc math macros
namespaces math.vectors math.constants math.functions
math.parser opengl.gl opengl.glu combinators arrays sequences
splitting words byte-arrays assocs colors accessors
2008-11-14 21:18:16 -05:00
generalizations locals specialized-arrays.float
specialized-arrays.uint ;
2007-09-20 18:09:08 -04:00
IN: opengl
: color>raw ( object -- r g b a )
>rgba { [ red>> ] [ green>> ] [ blue>> ] [ alpha>> ] } cleave ; inline
: gl-color ( color -- ) color>raw glColor4d ; inline
2007-09-20 18:09:08 -04:00
: gl-clear-color ( color -- ) color>raw glClearColor ;
2007-09-20 18:09:08 -04:00
: gl-clear ( color -- )
gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
2007-09-20 18:09:08 -04:00
: gl-error ( -- )
glGetError dup zero? [
2008-01-13 01:58:56 -05:00
"GL error: " over gluErrorString append throw
2007-09-20 18:09:08 -04:00
] unless drop ;
: do-enabled ( what quot -- )
2008-01-12 17:23:34 -05:00
over glEnable dip glDisable ; inline
: do-enabled-client-state ( what quot -- )
over glEnableClientState dip glDisableClientState ; inline
: words>values ( word/value-seq -- value-seq )
2008-11-17 06:16:34 -05:00
[ dup word? [ execute ] when ] map ;
: (all-enabled) ( seq quot -- )
over [ glEnable ] each dip [ glDisable ] each ; inline
: (all-enabled-client-state) ( seq quot -- )
2008-09-19 02:08:27 -04:00
[ dup [ glEnableClientState ] each ] dip
dip
[ glDisableClientState ] each ; inline
2007-09-20 18:09:08 -04:00
MACRO: all-enabled ( seq quot -- )
2008-11-29 01:20:29 -05:00
[ words>values ] dip [ (all-enabled) ] 2curry ;
MACRO: all-enabled-client-state ( seq quot -- )
2008-11-29 01:20:29 -05:00
[ words>values ] dip [ (all-enabled-client-state) ] 2curry ;
2007-09-20 18:09:08 -04:00
: do-matrix ( mode quot -- )
swap [ glMatrixMode glPushMatrix call ] keep
glMatrixMode glPopMatrix ; inline
: gl-material ( face pname params -- )
2008-11-18 23:18:35 -05:00
float-array{ } like underlying>> glMaterialfv ;
2007-09-20 18:09:08 -04:00
: gl-vertex-pointer ( seq -- )
2008-11-18 23:18:35 -05:00
[ 2 GL_FLOAT 0 ] dip underlying>> glVertexPointer ; inline
: gl-color-pointer ( seq -- )
2008-11-18 23:18:35 -05:00
[ 4 GL_FLOAT 0 ] dip underlying>> glColorPointer ; inline
: gl-texture-coord-pointer ( seq -- )
[ 2 GL_FLOAT 0 ] dip underlying>> glTexCoordPointer ; inline
: line-vertices ( a b -- )
2008-11-18 23:18:35 -05:00
[ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
gl-vertex-pointer ;
2007-09-20 18:09:08 -04:00
: gl-line ( a b -- )
line-vertices GL_LINES 0 2 glDrawArrays ;
2007-09-20 18:09:08 -04:00
2008-11-11 03:31:56 -05:00
: (rect-vertices) ( dim -- vertices )
2008-11-26 02:41:13 -05:00
#! We use GL_LINE_STRIP with a duplicated first vertex
#! instead of GL_LINE_LOOP to work around a bug in Apple's
#! X3100 driver.
{
2008-11-17 06:16:34 -05:00
[ drop 0.5 0.5 ]
[ first 0.3 - 0.5 ]
[ [ first 0.3 - ] [ second 0.3 - ] bi ]
[ second 0.3 - 0.5 swap ]
[ drop 0.5 0.5 ]
2008-11-29 06:14:49 -05:00
} cleave 10 float-array{ } nsequence ;
2007-09-20 18:09:08 -04:00
2008-11-11 03:31:56 -05:00
: rect-vertices ( dim -- )
(rect-vertices) gl-vertex-pointer ;
2007-09-20 18:09:08 -04:00
: (gl-rect) ( -- )
2008-11-26 02:41:13 -05:00
GL_LINE_STRIP 0 5 glDrawArrays ;
2007-09-20 18:09:08 -04:00
: gl-rect ( dim -- )
2008-11-11 03:31:56 -05:00
rect-vertices (gl-rect) ;
: (fill-rect-vertices) ( dim -- vertices )
{
[ drop 0 0 ]
[ first 0 ]
[ first2 ]
[ second 0 swap ]
2008-11-18 23:18:35 -05:00
} cleave 8 float-array{ } nsequence ;
2008-11-11 03:31:56 -05:00
: fill-rect-vertices ( dim -- )
(fill-rect-vertices) gl-vertex-pointer ;
2007-09-20 18:09:08 -04:00
: (gl-fill-rect) ( -- )
GL_QUADS 0 4 glDrawArrays ;
: gl-fill-rect ( dim -- )
2008-11-11 03:31:56 -05:00
fill-rect-vertices (gl-fill-rect) ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: circle-steps ( steps -- angles )
dup length v/n 2 pi * v*n ;
2007-10-31 01:04:54 -04:00
2008-06-08 16:32:55 -04:00
: unit-circle ( angles -- points1 points2 )
[ [ sin ] map ] [ [ cos ] map ] bi ;
2007-10-31 01:04:54 -04:00
2008-06-08 16:32:55 -04:00
: adjust-points ( points1 points2 -- points1' points2' )
[ [ 1 + 0.5 * ] map ] bi@ ;
2007-10-31 01:04:54 -04:00
2008-06-08 16:32:55 -04:00
: scale-points ( loc dim points1 points2 -- points )
zip [ v* ] with map [ v+ ] with map ;
2007-10-31 01:04:54 -04:00
: circle-points ( loc dim steps -- points )
circle-steps unit-circle adjust-points scale-points ;
2008-11-26 02:41:13 -05:00
: close-path ( points -- points' )
dup first suffix ;
: circle-vertices ( loc dim steps -- vertices )
2008-11-26 02:41:13 -05:00
#! We use GL_LINE_STRIP with a duplicated first vertex
#! instead of GL_LINE_LOOP to work around a bug in Apple's
#! X3100 driver.
2008-11-29 06:14:49 -05:00
circle-points close-path concat >float-array ;
2008-11-26 02:41:13 -05:00
: fill-circle-vertices ( loc dim steps -- vertices )
2008-11-18 23:18:35 -05:00
circle-points concat >float-array ;
2007-09-20 18:09:08 -04:00
: (gen-gl-object) ( quot -- id )
2008-11-29 01:20:29 -05:00
[ 1 0 <uint> ] dip keep *uint ; inline
2007-09-20 18:09:08 -04:00
: gen-texture ( -- id )
[ glGenTextures ] (gen-gl-object) ;
: gen-gl-buffer ( -- id )
[ glGenBuffers ] (gen-gl-object) ;
: (delete-gl-object) ( id quot -- )
2008-11-29 01:20:29 -05:00
[ 1 swap <uint> ] dip call ; inline
: delete-texture ( id -- )
[ glDeleteTextures ] (delete-gl-object) ;
: delete-gl-buffer ( id -- )
[ glDeleteBuffers ] (delete-gl-object) ;
: with-gl-buffer ( binding id quot -- )
-rot dupd glBindBuffer
[ slip ] [ 0 glBindBuffer ] [ ] cleanup ; inline
: with-array-element-buffers ( array-buffer element-buffer quot -- )
-rot GL_ELEMENT_ARRAY_BUFFER swap [
swap GL_ARRAY_BUFFER -rot with-gl-buffer
] with-gl-buffer ; inline
: <gl-buffer> ( target data hint -- id )
pick gen-gl-buffer [ [
2008-11-29 01:20:29 -05:00
[ dup byte-length swap ] dip glBufferData
] with-gl-buffer ] keep ;
: buffer-offset ( int -- alien )
<alien> ; inline
: bind-texture-unit ( id target unit -- )
glActiveTexture swap glBindTexture gl-error ;
: (set-draw-buffers) ( buffers -- )
2008-11-14 21:18:16 -05:00
[ length ] [ >uint-array underlying>> ] bi glDrawBuffers ;
2007-09-20 18:09:08 -04:00
MACRO: set-draw-buffers ( buffers -- )
words>values [ (set-draw-buffers) ] curry ;
2007-09-20 18:09:08 -04:00
: do-attribs ( bits quot -- )
swap glPushAttrib call glPopAttrib ; inline
: gl-look-at ( eye focus up -- )
[ first3 ] tri@ gluLookAt ;
2007-09-20 18:09:08 -04:00
TUPLE: sprite loc dim dim2 dlist texture ;
: <sprite> ( loc dim dim2 -- sprite )
f f sprite boa ;
2007-09-20 18:09:08 -04:00
2008-08-29 17:42:12 -04:00
: sprite-size2 ( sprite -- w h ) dim2>> first2 ;
2007-09-20 18:09:08 -04:00
2008-08-29 17:42:12 -04:00
: sprite-width ( sprite -- w ) dim>> first ;
2007-09-20 18:09:08 -04:00
: gray-texture ( sprite pixmap -- id )
gen-texture [
GL_TEXTURE_BIT [
GL_TEXTURE_2D swap glBindTexture
2008-11-29 01:20:29 -05:00
[
[ GL_TEXTURE_2D 0 GL_RGBA ] dip
sprite-size2 0 GL_LUMINANCE_ALPHA
GL_UNSIGNED_BYTE
] dip glTexImage2D
2007-09-20 18:09:08 -04:00
] do-attribs
] keep ;
2007-09-20 18:09:08 -04:00
: gen-dlist ( -- id ) 1 glGenLists ;
: make-dlist ( type quot -- id )
gen-dlist [ rot glNewList call glEndList ] keep ; inline
: init-texture ( -- )
GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP glTexParameterf
GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP glTexParameterf ;
: gl-translate ( point -- ) first2 0.0 glTranslated ;
: rect-texture-coords ( -- )
2008-11-14 21:18:16 -05:00
float-array{ 0 0 1 0 1 1 0 1 } gl-texture-coord-pointer ;
2007-09-20 18:09:08 -04:00
: draw-sprite ( sprite -- )
GL_TEXTURE_COORD_ARRAY [
dup loc>> gl-translate
GL_TEXTURE_2D over texture>> glBindTexture
init-texture rect-texture-coords
2008-11-11 03:31:56 -05:00
dim2>> fill-rect-vertices
(gl-fill-rect)
GL_TEXTURE_2D 0 glBindTexture
] do-enabled-client-state ;
2007-09-20 18:09:08 -04:00
: make-sprite-dlist ( sprite -- id )
GL_MODELVIEW [
GL_COMPILE [ draw-sprite ] make-dlist
] do-matrix ;
: init-sprite ( texture sprite -- )
2008-08-29 17:42:12 -04:00
swap >>texture
dup make-sprite-dlist >>dlist drop ;
2007-09-20 18:09:08 -04:00
: delete-dlist ( id -- ) 1 glDeleteLists ;
: free-sprite ( sprite -- )
2008-08-29 17:42:12 -04:00
[ dlist>> delete-dlist ]
[ texture>> delete-texture ] bi ;
2007-09-20 18:09:08 -04:00
2008-02-02 16:00:16 -05:00
: free-sprites ( sprites -- )
[ nip [ free-sprite ] when* ] assoc-each ;
2007-09-20 18:09:08 -04:00
: with-translation ( loc quot -- )
2008-11-29 01:20:29 -05:00
GL_MODELVIEW [ [ gl-translate ] dip call ] do-matrix ; inline
2007-09-20 18:09:08 -04:00
: fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
[ first2 [ >fixnum ] bi@ ] bi@ ;
2007-09-20 18:09:08 -04:00
: gl-set-clip ( loc dim -- )
fix-coordinates glScissor ;
: gl-viewport ( loc dim -- )
fix-coordinates glViewport ;
: init-matrices ( -- )
GL_PROJECTION glMatrixMode
glLoadIdentity
GL_MODELVIEW glMatrixMode
glLoadIdentity ;