| 
									
										
										
										
											2009-01-18 21:10:08 -05:00
										 |  |  | ! Copyright (C) 2005, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-10-08 21:23:20 -04:00
										 |  |  | ! 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. | 
					
						
							| 
									
										
										
										
											2009-07-14 13:00:37 -04:00
										 |  |  | USING: alien alien.c-types ascii calendar combinators.short-circuit | 
					
						
							|  |  |  | continuations kernel libc math macros namespaces math.vectors | 
					
						
							|  |  |  | math.parser opengl.gl combinators combinators.smart arrays | 
					
						
							|  |  |  | sequences splitting words byte-arrays assocs vocabs | 
					
						
							| 
									
										
										
										
											2009-02-15 07:09:04 -05:00
										 |  |  | colors colors.constants accessors generalizations locals fry | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | specialized-arrays ;
 | 
					
						
							| 
									
										
										
										
											2009-09-16 22:25:46 -04:00
										 |  |  | FROM: alien.c-types => float ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | SPECIALIZED-ARRAY: float
 | 
					
						
							|  |  |  | SPECIALIZED-ARRAY: uint | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: opengl | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-21 20:34:42 -05:00
										 |  |  | : gl-color ( color -- ) >rgba-components glColor4d ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-10 20:01:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-21 20:34:42 -05:00
										 |  |  | : gl-clear-color ( color -- ) >rgba-components glClearColor ;
 | 
					
						
							| 
									
										
										
										
											2007-10-08 21:23:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : gl-clear ( color -- )
 | 
					
						
							| 
									
										
										
										
											2007-10-08 21:23:20 -04:00
										 |  |  |     gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:08 -04:00
										 |  |  | : error>string ( n -- string )
 | 
					
						
							|  |  |  |     H{ | 
					
						
							|  |  |  |         { HEX: 0 "No error" } | 
					
						
							|  |  |  |         { HEX: 0501 "Invalid value" } | 
					
						
							|  |  |  |         { HEX: 0500 "Invalid enumerant" } | 
					
						
							|  |  |  |         { HEX: 0502 "Invalid operation" } | 
					
						
							|  |  |  |         { HEX: 0503 "Stack overflow" } | 
					
						
							|  |  |  |         { HEX: 0504 "Stack underflow" } | 
					
						
							|  |  |  |         { HEX: 0505 "Out of memory" } | 
					
						
							| 
									
										
										
										
											2009-06-16 19:14:22 -04:00
										 |  |  |         { HEX: 0506 "Invalid framebuffer operation" } | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:08 -04:00
										 |  |  |     } at "Unknown error" or ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:53:57 -04:00
										 |  |  | TUPLE: gl-error function code string ;
 | 
					
						
							| 
									
										
										
										
											2009-04-11 16:17:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:53:57 -04:00
										 |  |  | : <gl-error> ( function code -- gl-error )
 | 
					
						
							| 
									
										
										
										
											2009-07-14 13:00:37 -04:00
										 |  |  |     dup error>string \ gl-error boa ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-error-code ( -- code/f )
 | 
					
						
							|  |  |  |     glGetError dup 0 = [ drop f ] when ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:53:57 -04:00
										 |  |  | : (gl-error) ( function -- )
 | 
					
						
							|  |  |  |     gl-error-code [ <gl-error> throw ] [ drop ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : gl-error ( -- )
 | 
					
						
							| 
									
										
										
										
											2009-07-15 14:53:57 -04:00
										 |  |  |     f (gl-error) ; inline
 | 
					
						
							| 
									
										
										
										
											2009-07-14 13:00:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : do-enabled ( what quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-12 17:23:34 -05:00
										 |  |  |     over glEnable dip glDisable ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  | : do-enabled-client-state ( what quot -- )
 | 
					
						
							|  |  |  |     over glEnableClientState dip glDisableClientState ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  | : words>values ( word/value-seq -- value-seq )
 | 
					
						
							| 
									
										
										
										
											2009-03-26 22:27:45 -04:00
										 |  |  |     [ ?execute ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (all-enabled) ( seq quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-11-09 01:54:31 -05:00
										 |  |  |     [ dup [ glEnable ] each ] dip
 | 
					
						
							|  |  |  |     dip
 | 
					
						
							|  |  |  |     [ glDisable ] each ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  | : (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  | MACRO: all-enabled ( seq quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:08:07 -05:00
										 |  |  |     [ words>values ] dip '[ _ _ (all-enabled) ] ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  | MACRO: all-enabled-client-state ( seq quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-07 02:22:21 -05:00
										 |  |  |     [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-04 21:03:30 -04:00
										 |  |  | : do-matrix ( quot -- )
 | 
					
						
							|  |  |  |     glPushMatrix call glPopMatrix ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-08 21:23:20 -04:00
										 |  |  | : gl-material ( face pname params -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 05:37:28 -05:00
										 |  |  |     float-array{ } like glMaterialfv ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | : gl-vertex-pointer ( seq -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 05:37:28 -05:00
										 |  |  |     [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : gl-color-pointer ( seq -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 05:37:28 -05:00
										 |  |  |     [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : gl-texture-coord-pointer ( seq -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 05:37:28 -05:00
										 |  |  |     [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : line-vertices ( a b -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-18 23:18:35 -05:00
										 |  |  |     [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence | 
					
						
							|  |  |  |     gl-vertex-pointer ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : gl-line ( a b -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  |     line-vertices GL_LINES 0 2 glDrawArrays ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-27 19:31:25 -04:00
										 |  |  | :: (rect-vertices) ( loc 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. | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     loc first2 :> ( x y )
 | 
					
						
							|  |  |  |     dim first2 :> ( w h )
 | 
					
						
							| 
									
										
										
										
											2009-03-27 19:31:25 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         x 0.5 +     y 0.5 +
 | 
					
						
							|  |  |  |         x w + 0.3 - y 0.5 +
 | 
					
						
							|  |  |  |         x w + 0.3 - y h + 0.3 -
 | 
					
						
							|  |  |  |         x           y h + 0.3 -
 | 
					
						
							|  |  |  |         x 0.5 +     y 0.5 +
 | 
					
						
							|  |  |  |     ] float-array{ } output>sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : rect-vertices ( loc dim -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 03:31:56 -05:00
										 |  |  |     (rect-vertices) gl-vertex-pointer ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-27 19:31:25 -04:00
										 |  |  | : gl-rect ( loc dim -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 03:31:56 -05:00
										 |  |  |     rect-vertices (gl-rect) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-27 19:31:25 -04:00
										 |  |  | :: (fill-rect-vertices) ( loc dim -- vertices )
 | 
					
						
							| 
									
										
										
										
											2009-10-28 17:11:33 -04:00
										 |  |  |     loc first2 :> ( x y )
 | 
					
						
							|  |  |  |     dim first2 :> ( w h )
 | 
					
						
							| 
									
										
										
										
											2009-03-27 19:31:25 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         x      y | 
					
						
							|  |  |  |         x w +  y | 
					
						
							|  |  |  |         x w +  y h +
 | 
					
						
							|  |  |  |         x      y h +
 | 
					
						
							|  |  |  |     ] float-array{ } output>sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : fill-rect-vertices ( loc dim -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 03:31:56 -05:00
										 |  |  |     (fill-rect-vertices) gl-vertex-pointer ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | : (gl-fill-rect) ( -- )
 | 
					
						
							|  |  |  |     GL_QUADS 0 4 glDrawArrays ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-27 19:31:25 -04:00
										 |  |  | : gl-fill-rect ( loc dim -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 03:31:56 -05:00
										 |  |  |     fill-rect-vertices (gl-fill-rect) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-11 05:54:06 -05:00
										 |  |  | : do-attribs ( bits quot -- )
 | 
					
						
							|  |  |  |     swap glPushAttrib call glPopAttrib ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  | : (gen-gl-object) ( quot -- id )
 | 
					
						
							| 
									
										
										
										
											2008-11-29 01:20:29 -05:00
										 |  |  |     [ 1 0 <uint> ] dip keep *uint ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  | : (delete-gl-object) ( id quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-29 01:20:29 -05:00
										 |  |  |     [ 1 swap <uint> ] dip call ; inline
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-01 14:53:28 -04:00
										 |  |  | : gen-gl-buffer ( -- id )
 | 
					
						
							|  |  |  |     [ glGenBuffers ] (gen-gl-object) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  | : delete-gl-buffer ( id -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  |     [ 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
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : 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 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  |     ] with-gl-buffer ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-01 14:53:28 -04:00
										 |  |  | : gen-vertex-array ( -- id )
 | 
					
						
							|  |  |  |     [ glGenVertexArrays ] (gen-gl-object) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete-vertex-array ( id -- )
 | 
					
						
							|  |  |  |     [ glDeleteVertexArrays ] (delete-gl-object) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: with-vertex-array ( id quot -- )
 | 
					
						
							|  |  |  |     id glBindVertexArray | 
					
						
							| 
									
										
										
										
											2009-07-01 18:57:21 -04:00
										 |  |  |     quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
 | 
					
						
							| 
									
										
										
										
											2009-07-01 14:53:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  | : <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 ;
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : buffer-offset ( int -- alien )
 | 
					
						
							|  |  |  |     <alien> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  | : bind-texture-unit ( id target unit -- )
 | 
					
						
							|  |  |  |     glActiveTexture swap glBindTexture gl-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 01:37:28 -05:00
										 |  |  | : (set-draw-buffers) ( buffers -- )
 | 
					
						
							| 
									
										
										
										
											2009-02-06 05:37:28 -05:00
										 |  |  |     [ length ] [ >uint-array ] bi glDrawBuffers ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 01:37:28 -05:00
										 |  |  | MACRO: set-draw-buffers ( buffers -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-29 23:08:07 -05:00
										 |  |  |     words>values '[ _ (set-draw-buffers) ] ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | : gl-translate ( point -- ) first2 0.0 glTranslated ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete-dlist ( id -- ) 1 glDeleteLists ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-translation ( loc quot -- )
 | 
					
						
							| 
									
										
										
										
											2009-04-04 21:03:30 -04:00
										 |  |  |     [ [ gl-translate ] dip call ] do-matrix ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 01:28:37 -05: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 ( -- )
 | 
					
						
							| 
									
										
										
										
											2009-04-04 21:03:30 -04:00
										 |  |  |     #! Leaves with matrix mode GL_MODELVIEW | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     GL_PROJECTION glMatrixMode | 
					
						
							|  |  |  |     glLoadIdentity | 
					
						
							|  |  |  |     GL_MODELVIEW glMatrixMode | 
					
						
							| 
									
										
										
										
											2009-06-16 19:14:22 -04:00
										 |  |  |     glLoadIdentity ;
 |