2006-01-30 02:03:34 -05:00
|
|
|
! Copyright (C) 2005, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2005-10-13 00:23:17 -04:00
|
|
|
IN: opengl
|
2006-03-12 23:21:01 -05:00
|
|
|
USING: alien errors io kernel math namespaces opengl
|
2006-01-30 20:35:55 -05:00
|
|
|
sequences ;
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2005-10-29 23:25:38 -04:00
|
|
|
: gl-color ( { r g b a } -- ) first4 glColor4d ; inline
|
2005-10-28 15:37:28 -04:00
|
|
|
|
2005-10-18 20:19:10 -04:00
|
|
|
: gl-error ( -- )
|
2006-06-29 00:00:21 -04:00
|
|
|
glGetError dup zero? [
|
|
|
|
"GL error: " write dup gluErrorString print flush
|
|
|
|
] unless drop ;
|
2005-10-18 20:19:10 -04:00
|
|
|
|
2005-10-13 00:23:17 -04:00
|
|
|
: do-state ( what quot -- )
|
|
|
|
swap glBegin call glEnd ; inline
|
|
|
|
|
2006-03-18 01:57:57 -05:00
|
|
|
: do-enabled ( what quot -- )
|
|
|
|
over glEnable swap slip glDisable ; inline
|
|
|
|
|
2005-10-25 17:06:15 -04:00
|
|
|
: do-matrix ( mode quot -- )
|
2005-11-15 03:29:03 -05:00
|
|
|
swap [ glMatrixMode glPushMatrix call ] keep
|
|
|
|
glMatrixMode glPopMatrix ; inline
|
2005-10-25 17:06:15 -04:00
|
|
|
|
2006-06-23 00:06:53 -04:00
|
|
|
: top-left drop 0 0 glTexCoord2i 0.0 0.0 glVertex2d ; inline
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2006-06-23 00:06:53 -04:00
|
|
|
: top-right 1 0 glTexCoord2i first 0.0 glVertex2d ; inline
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2006-06-23 00:06:53 -04:00
|
|
|
: bottom-left 0 1 glTexCoord2i second 0.0 swap glVertex2d ; inline
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2006-06-29 01:54:11 -04:00
|
|
|
: gl-vertex first2 glVertex2i ; inline
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2006-06-23 00:06:53 -04:00
|
|
|
: bottom-right 1 1 glTexCoord2i gl-vertex ; inline
|
2005-10-13 00:23:17 -04:00
|
|
|
|
|
|
|
: four-sides ( dim -- )
|
|
|
|
dup top-left dup top-right dup bottom-right bottom-left ;
|
|
|
|
|
2006-03-23 16:57:14 -05:00
|
|
|
: gl-line ( a b -- )
|
|
|
|
GL_LINES [ gl-vertex gl-vertex ] do-state ;
|
|
|
|
|
2005-10-20 04:33:22 -04:00
|
|
|
: gl-fill-rect ( dim -- )
|
2005-10-13 00:23:17 -04:00
|
|
|
#! Draws a two-dimensional box.
|
2005-10-20 04:33:22 -04:00
|
|
|
GL_QUADS [ four-sides ] do-state ;
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2005-10-20 04:33:22 -04:00
|
|
|
: gl-rect ( dim -- )
|
2005-10-13 00:23:17 -04:00
|
|
|
#! Draws a two-dimensional box.
|
2005-10-25 01:31:54 -04:00
|
|
|
GL_MODELVIEW [
|
2006-06-23 00:06:53 -04:00
|
|
|
0.5 0.5 0.0 glTranslated { 1 1 } v-
|
2005-10-25 01:31:54 -04:00
|
|
|
GL_LINE_STRIP [ dup four-sides top-left ] do-state
|
|
|
|
] do-matrix ;
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2005-10-20 04:33:22 -04:00
|
|
|
: (gl-poly) [ [ gl-vertex ] each ] do-state ;
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2005-10-20 04:33:22 -04:00
|
|
|
: gl-fill-poly ( points -- )
|
2005-10-13 00:23:17 -04:00
|
|
|
#! Draw a filled polygon.
|
2005-10-25 23:57:06 -04:00
|
|
|
dup length 2 > GL_POLYGON GL_LINES ? (gl-poly) ;
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2005-11-15 03:29:03 -05:00
|
|
|
: gl-poly ( points -- )
|
2005-10-25 01:31:54 -04:00
|
|
|
#! Draw a polygon.
|
2005-10-13 00:23:17 -04:00
|
|
|
GL_LINE_LOOP (gl-poly) ;
|
|
|
|
|
|
|
|
: prepare-gradient ( direction dim -- v1 v2 )
|
|
|
|
tuck v* [ v- ] keep ;
|
|
|
|
|
|
|
|
: gl-gradient ( direction colors dim -- )
|
|
|
|
#! Draws a quad strip.
|
|
|
|
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 ;
|
|
|
|
|
|
|
|
: gen-texture ( -- id )
|
|
|
|
#! Generate texture ID.
|
|
|
|
1 0 <uint> [ glGenTextures ] keep *uint ;
|
|
|
|
|
2005-10-18 20:19:10 -04:00
|
|
|
: save-attribs ( bits quot -- )
|
|
|
|
swap glPushAttrib call glPopAttrib ; inline
|
|
|
|
|
2005-10-20 04:33:22 -04:00
|
|
|
! A sprite is a texture and a display list.
|
|
|
|
TUPLE: sprite dlist texture loc dim dim2 ;
|
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
C: sprite ( loc dim dim2 -- sprite )
|
2005-10-20 04:33:22 -04:00
|
|
|
[ set-sprite-dim2 ] keep
|
|
|
|
[ set-sprite-dim ] keep
|
|
|
|
[ set-sprite-loc ] keep ;
|
|
|
|
|
|
|
|
: sprite-size2 sprite-dim2 first2 ;
|
|
|
|
|
|
|
|
: sprite-width sprite-dim first ;
|
|
|
|
|
|
|
|
: gray-texture ( sprite buffer -- id )
|
2005-10-13 00:23:17 -04:00
|
|
|
#! Given a buffer holding a width x height (powers of two)
|
|
|
|
#! grayscale texture, bind it and return the ID.
|
|
|
|
gen-texture [
|
2005-10-18 20:19:10 -04:00
|
|
|
GL_TEXTURE_BIT [
|
|
|
|
GL_TEXTURE_2D swap glBindTexture
|
2005-10-20 04:33:22 -04:00
|
|
|
>r >r GL_TEXTURE_2D 0 GL_RGBA r>
|
|
|
|
sprite-size2 0 GL_LUMINANCE_ALPHA
|
2005-10-18 20:19:10 -04:00
|
|
|
GL_UNSIGNED_BYTE r> glTexImage2D
|
|
|
|
] save-attribs
|
2005-10-13 00:23:17 -04:00
|
|
|
] keep ;
|
|
|
|
|
|
|
|
: gen-dlist ( -- id )
|
|
|
|
#! Generate display list ID.
|
|
|
|
1 glGenLists ;
|
|
|
|
|
|
|
|
: make-dlist ( type quot -- id )
|
|
|
|
#! Make a display list.
|
2005-10-13 00:30:44 -04:00
|
|
|
gen-dlist [ rot glNewList call glEndList ] keep ; inline
|
2005-10-13 00:23:17 -04:00
|
|
|
|
2005-10-20 04:33:22 -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 ;
|
|
|
|
|
2006-08-15 04:57:12 -04:00
|
|
|
: gl-translate ( point -- ) first2 0.0 glTranslated ;
|
2005-10-28 15:37:28 -04:00
|
|
|
|
2006-07-25 16:30:50 -04:00
|
|
|
: draw-sprite ( sprite -- )
|
|
|
|
dup sprite-loc gl-translate
|
|
|
|
GL_TEXTURE_2D over sprite-texture glBindTexture
|
|
|
|
init-texture
|
|
|
|
dup sprite-dim2 gl-fill-rect
|
|
|
|
dup sprite-dim { 1 0 } v*
|
|
|
|
swap sprite-loc v- gl-translate
|
|
|
|
GL_TEXTURE_2D 0 glBindTexture ;
|
|
|
|
|
2005-10-20 04:33:22 -04:00
|
|
|
: make-sprite-dlist ( sprite -- id )
|
2005-10-18 20:19:10 -04:00
|
|
|
GL_MODELVIEW [
|
2006-07-25 16:30:50 -04:00
|
|
|
GL_COMPILE [ draw-sprite ] make-dlist
|
2005-10-18 20:19:10 -04:00
|
|
|
] do-matrix ;
|
2005-10-20 04:33:22 -04:00
|
|
|
|
|
|
|
: init-sprite ( texture sprite -- )
|
|
|
|
[ set-sprite-texture ] keep
|
|
|
|
[ make-sprite-dlist ] keep set-sprite-dlist ;
|
2006-03-18 01:57:57 -05:00
|
|
|
|
|
|
|
: free-sprite ( sprite -- )
|
|
|
|
dup sprite-dlist 1 glDeleteLists
|
|
|
|
sprite-texture <uint> 1 swap glDeleteTextures ;
|
|
|
|
|
|
|
|
: free-sprites ( sprites -- ) [ [ free-sprite ] when* ] each ;
|