factor/basis/opengl/opengl.factor

238 lines
6.6 KiB
Factor
Raw Normal View History

2009-01-18 21:10:08 -05:00
! Copyright (C) 2005, 2009 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-12-22 06:41:01 -05:00
generalizations locals fry specialized-arrays.float
2008-11-14 21:18:16 -05:00
specialized-arrays.uint ;
2007-09-20 18:09:08 -04:00
IN: opengl
: gl-color ( color -- ) >rgba-components glColor4d ; inline
: gl-clear-color ( color -- ) >rgba-components 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) ;
2008-12-22 06:41:01 -05:00
:: with-gl-buffer ( binding id quot -- )
binding id glBindBuffer
quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
: with-array-element-buffers ( array-buffer element-buffer quot -- )
2008-12-22 06:41:01 -05:00
[ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
GL_ARRAY_BUFFER swap _ with-gl-buffer
] with-gl-buffer ; inline
: <gl-buffer> ( target data hint -- id )
2008-12-22 06:41:01 -05:00
pick gen-gl-buffer [
[
[ [ byte-length ] keep ] 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 ;
2009-01-18 21:10:08 -05:00
: make-texture ( dim pixmap type -- id )
[ gen-texture ] 3dip swap '[
2007-09-20 18:09:08 -04:00
GL_TEXTURE_BIT [
GL_TEXTURE_2D swap glBindTexture
2009-01-18 21:10:08 -05:00
GL_TEXTURE_2D
0
GL_RGBA
_ first2
0
_
GL_UNSIGNED_BYTE
_
glTexImage2D
2007-09-20 18:09:08 -04:00
] do-attribs
] keep ;
2009-01-18 21:10:08 -05:00
2007-09-20 18:09:08 -04:00
: gen-dlist ( -- id ) 1 glGenLists ;
: make-dlist ( type quot -- id )
2009-01-18 21:10:08 -05:00
[ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
2007-09-20 18:09:08 -04:00
: 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
: delete-dlist ( id -- ) 1 glDeleteLists ;
: 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
2009-01-18 21:10:08 -05:00
glLoadIdentity ;