| 
									
										
										
										
											2008-02-02 16:00:16 -05:00
										 |  |  | ! Copyright (C) 2005, 2008 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. | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2008-02-02 21:15:22 -05:00
										 |  |  | splitting words byte-arrays assocs combinators.lib ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: opengl | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  | : coordinates [ first2 ] bi@ ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  | : fix-coordinates [ first2 [ >fixnum ] bi@ ] bi@ ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : gl-color ( color -- ) first4 glColor4d ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-08 21:23:20 -04:00
										 |  |  | : gl-clear-color ( color -- )
 | 
					
						
							|  |  |  |     first4 glClearColor ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | : 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-15 17:07:53 -04:00
										 |  |  | : do-state ( mode quot -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     swap glBegin call glEnd ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : do-enabled ( what quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-12 17:23:34 -05:00
										 |  |  |     over glEnable dip glDisable ; inline
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							|  |  |  |     [ dup word? [ execute ] [ ] if ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (all-enabled) ( seq quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  |     over [ glEnable ] each dip [ glDisable ] each ; inline
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  | : (all-enabled-client-state) ( seq quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  |     over [ glEnableClientState ] each 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 -- )
 | 
					
						
							|  |  |  |     >r words>values r> [ (all-enabled) ] 2curry ;
 | 
					
						
							|  |  |  | MACRO: all-enabled-client-state ( seq quot -- )
 | 
					
						
							|  |  |  |     >r words>values r> [ (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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-08 21:23:20 -04:00
										 |  |  | : gl-vertex ( point -- )
 | 
					
						
							|  |  |  |     dup length { | 
					
						
							|  |  |  |         { 2 [ first2 glVertex2d ] } | 
					
						
							|  |  |  |         { 3 [ first3 glVertex3d ] } | 
					
						
							|  |  |  |         { 4 [ first4 glVertex4d ] } | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-normal ( normal -- ) first3 glNormal3d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-material ( face pname params -- )
 | 
					
						
							|  |  |  |     >c-float-array glMaterialfv ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : gl-line ( a b -- )
 | 
					
						
							|  |  |  |     GL_LINES [ gl-vertex gl-vertex ] do-state ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-fill-rect ( loc ext -- )
 | 
					
						
							|  |  |  |     coordinates glRectd ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-rect ( loc ext -- )
 | 
					
						
							|  |  |  |     GL_FRONT_AND_BACK GL_LINE glPolygonMode | 
					
						
							|  |  |  |     >r { 0.5 0.5 } v+ r> { 0.5 0.5 } v- gl-fill-rect | 
					
						
							|  |  |  |     GL_FRONT_AND_BACK GL_FILL glPolygonMode ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (gl-poly) [ [ gl-vertex ] each ] do-state ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-fill-poly ( points -- )
 | 
					
						
							|  |  |  |     dup length 2 > GL_POLYGON GL_LINES ? (gl-poly) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-poly ( points -- )
 | 
					
						
							|  |  |  |     GL_LINE_LOOP (gl-poly) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-31 01:04:54 -04:00
										 |  |  | : circle-steps dup length v/n 2 pi * v*n ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : unit-circle dup [ sin ] map swap [ cos ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  | : adjust-points [ [ 1 + 0.5 * ] map ] bi@ ;
 | 
					
						
							| 
									
										
										
										
											2007-10-31 01:04:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  | : scale-points 2array flip [ 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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-circle ( loc dim steps -- )
 | 
					
						
							|  |  |  |     circle-points gl-poly ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-fill-circle ( loc dim steps -- )
 | 
					
						
							|  |  |  |     circle-points gl-fill-poly ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : prepare-gradient ( direction dim -- v1 v2 )
 | 
					
						
							|  |  |  |     tuck v* [ v- ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gl-gradient ( direction colors dim -- )
 | 
					
						
							|  |  |  |     GL_QUAD_STRIP [ | 
					
						
							|  |  |  |         swap >r prepare-gradient r> | 
					
						
							|  |  |  |         [ length dup 1- v/n ] keep [ | 
					
						
							|  |  |  |             >r >r 2dup r> r> gl-color v*n | 
					
						
							|  |  |  |             dup gl-vertex v+ gl-vertex | 
					
						
							|  |  |  |         ] 2each 2drop
 | 
					
						
							|  |  |  |     ] do-state ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  | : (gen-gl-object) ( quot -- id )
 | 
					
						
							|  |  |  |     >r 1 0 <uint> r> keep *uint ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : gen-texture ( -- id )
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  |     [ glGenTextures ] (gen-gl-object) ;
 | 
					
						
							| 
									
										
										
										
											2008-01-31 23:24:08 -05:00
										 |  |  | : gen-gl-buffer ( -- id )
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  |     [ glGenBuffers ] (gen-gl-object) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (delete-gl-object) ( id quot -- )
 | 
					
						
							|  |  |  |     >r 1 swap <uint> r> call ; inline
 | 
					
						
							|  |  |  | : delete-texture ( id -- )
 | 
					
						
							|  |  |  |     [ glDeleteTextures ] (delete-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-01-31 23:24:08 -05:00
										 |  |  | : 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 [ [ | 
					
						
							|  |  |  |         >r dup byte-length swap r> glBufferData | 
					
						
							|  |  |  |     ] with-gl-buffer ] keep ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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 -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  |     dup length swap >c-uint-array glDrawBuffers ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 01:37:28 -05:00
										 |  |  | MACRO: set-draw-buffers ( buffers -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-03 18:59:47 -05:00
										 |  |  |     words>values [ (set-draw-buffers) ] curry ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : do-attribs ( bits quot -- )
 | 
					
						
							|  |  |  |     swap glPushAttrib call glPopAttrib ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-08 21:23:20 -04:00
										 |  |  | : gl-look-at ( eye focus up -- )
 | 
					
						
							|  |  |  |     >r >r first3 r> first3 r> first3 gluLookAt ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | TUPLE: sprite loc dim dim2 dlist texture ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <sprite> ( loc dim dim2 -- sprite )
 | 
					
						
							|  |  |  |     f f sprite construct-boa ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sprite-size2 sprite-dim2 first2 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : sprite-width sprite-dim first ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : gray-texture ( sprite pixmap -- id )
 | 
					
						
							|  |  |  |     gen-texture [ | 
					
						
							|  |  |  |         GL_TEXTURE_BIT [ | 
					
						
							|  |  |  |             GL_TEXTURE_2D swap glBindTexture | 
					
						
							|  |  |  |             >r >r GL_TEXTURE_2D 0 GL_RGBA r> | 
					
						
							|  |  |  |             sprite-size2 0 GL_LUMINANCE_ALPHA | 
					
						
							|  |  |  |             GL_UNSIGNED_BYTE r> glTexImage2D | 
					
						
							|  |  |  |         ] do-attribs | 
					
						
							|  |  |  |     ] keep ;
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											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 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : top-left drop 0 0 glTexCoord2i 0.0 0.0 glVertex2d ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : top-right 1 0 glTexCoord2i first 0.0 glVertex2d ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : bottom-left 0 1 glTexCoord2i second 0.0 swap glVertex2d ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : bottom-right 1 1 glTexCoord2i gl-vertex ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : four-sides ( dim -- )
 | 
					
						
							|  |  |  |     dup top-left dup top-right dup bottom-right bottom-left ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : draw-sprite ( sprite -- )
 | 
					
						
							|  |  |  |     dup sprite-loc gl-translate | 
					
						
							|  |  |  |     GL_TEXTURE_2D over sprite-texture glBindTexture | 
					
						
							|  |  |  |     init-texture | 
					
						
							|  |  |  |     GL_QUADS [ dup sprite-dim2 four-sides ] do-state | 
					
						
							|  |  |  |     dup sprite-dim { 1 0 } v* | 
					
						
							|  |  |  |     swap sprite-loc v- gl-translate | 
					
						
							|  |  |  |     GL_TEXTURE_2D 0 glBindTexture ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-27 01:37:28 -05:00
										 |  |  | : rect-vertices ( lower-left upper-right -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  |     GL_QUADS [ | 
					
						
							|  |  |  |         over first2 glVertex2d | 
					
						
							|  |  |  |         dup first pick second glVertex2d | 
					
						
							|  |  |  |         dup first2 glVertex2d | 
					
						
							|  |  |  |         swap first swap second glVertex2d | 
					
						
							|  |  |  |     ] do-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 -- )
 | 
					
						
							|  |  |  |     [ set-sprite-texture ] keep
 | 
					
						
							|  |  |  |     [ make-sprite-dlist ] keep set-sprite-dlist ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : delete-dlist ( id -- ) 1 glDeleteLists ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : free-sprite ( sprite -- )
 | 
					
						
							|  |  |  |     dup sprite-dlist delete-dlist | 
					
						
							| 
									
										
										
										
											2008-01-25 01:16:36 -05:00
										 |  |  |     sprite-texture delete-texture ;
 | 
					
						
							| 
									
										
										
										
											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 -- )
 | 
					
						
							|  |  |  |     GL_MODELVIEW [ >r gl-translate r> call ] do-matrix ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : 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 ;
 |