]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/opengl/opengl.factor
Updating code to use with-out-parameters
[factor.git] / basis / opengl / opengl.factor
index 0a21f67376cc524d564c5af27a07c89f53dd8d9f..893a8dfbd69f2cfd3ba580f9fa1464d0ecf33585 100644 (file)
@@ -2,11 +2,15 @@
 ! Portions copyright (C) 2007 Eduardo Cavazos.
 ! Portions copyright (C) 2008 Joe Groff.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: alien alien.c-types continuations kernel libc math macros
-namespaces math.vectors math.parser opengl.gl opengl.glu combinators
-combinators.smart arrays sequences splitting words byte-arrays assocs
-colors colors.constants accessors generalizations locals fry
-specialized-arrays.float specialized-arrays.uint ;
+USING: alien alien.c-types alien.data 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 colors colors.constants accessors generalizations
+sequences.generalizations locals fry specialized-arrays ;
+FROM: alien.c-types => float ;
+SPECIALIZED-ARRAY: float
+SPECIALIZED-ARRAY: uint
 IN: opengl
 
 : gl-color ( color -- ) >rgba-components glColor4d ; inline
@@ -16,10 +20,31 @@ IN: opengl
 : gl-clear ( color -- )
     gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
 
+: 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" }
+        { HEX: 0506 "Invalid framebuffer operation" }
+    } at "Unknown error" or ;
+
+TUPLE: gl-error function code string ;
+
+: <gl-error> ( function code -- gl-error )
+    dup error>string \ gl-error boa ; inline
+
+: gl-error-code ( -- code/f )
+    glGetError dup 0 = [ drop f ] when ; inline
+
+: (gl-error) ( function -- )
+    gl-error-code [ <gl-error> throw ] [ drop ] if* ;
+
 : gl-error ( -- )
-    glGetError dup zero? [
-        "GL error: " over gluErrorString append throw
-    ] unless drop ;
+    f (gl-error) ; inline
 
 : do-enabled ( what quot -- )
     over glEnable dip glDisable ; inline
@@ -31,7 +56,9 @@ IN: opengl
     [ ?execute ] map ;
 
 : (all-enabled) ( seq quot -- )
-    over [ glEnable ] each dip [ glDisable ] each ; inline
+    [ dup [ glEnable ] each ] dip
+    dip
+    [ glDisable ] each ; inline
 
 : (all-enabled-client-state) ( seq quot -- )
     [ dup [ glEnableClientState ] each ] dip
@@ -44,9 +71,8 @@ MACRO: all-enabled ( seq quot -- )
 MACRO: all-enabled-client-state ( seq quot -- )
     [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
 
-: do-matrix ( mode quot -- )
-    swap [ glMatrixMode glPushMatrix call ] keep
-    glMatrixMode glPopMatrix ; inline
+: do-matrix ( quot -- )
+    glPushMatrix call glPopMatrix ; inline
 
 : gl-material ( face pname params -- )
     float-array{ } like glMaterialfv ;
@@ -71,8 +97,8 @@ MACRO: all-enabled-client-state ( seq quot -- )
     #! 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.
-    loc first2 :> y :> x
-    dim first2 :> h :> w
+    loc first2 :> ( x y )
+    dim first2 :> ( w h )
     [
         x 0.5 +     y 0.5 +
         x w + 0.3 - y 0.5 +
@@ -91,8 +117,8 @@ MACRO: all-enabled-client-state ( seq quot -- )
     rect-vertices (gl-rect) ;
 
 :: (fill-rect-vertices) ( loc dim -- vertices )
-    loc first2 :> y :> x
-    dim first2 :> h :> w
+    loc first2 :> ( x y )
+    dim first2 :> ( w h )
     [
         x      y
         x w +  y
@@ -113,14 +139,14 @@ MACRO: all-enabled-client-state ( seq quot -- )
     swap glPushAttrib call glPopAttrib ; inline
 
 : (gen-gl-object) ( quot -- id )
-    [ 1 0 <uint> ] dip keep *uint ; inline
-
-: gen-gl-buffer ( -- id )
-    [ glGenBuffers ] (gen-gl-object) ;
+    [ 1 { uint } ] dip [ ] with-out-parameters ; inline
 
 : (delete-gl-object) ( id quot -- )
     [ 1 swap <uint> ] dip call ; inline
 
+: gen-gl-buffer ( -- id )
+    [ glGenBuffers ] (gen-gl-object) ;
+
 : delete-gl-buffer ( id -- )
     [ glDeleteBuffers ] (delete-gl-object) ;
 
@@ -133,6 +159,16 @@ MACRO: all-enabled-client-state ( seq quot -- )
         GL_ARRAY_BUFFER swap _ with-gl-buffer
     ] with-gl-buffer ; inline
 
+: gen-vertex-array ( -- id )
+    [ glGenVertexArrays ] (gen-gl-object) ;
+
+: delete-vertex-array ( id -- )
+    [ glDeleteVertexArrays ] (delete-gl-object) ;
+
+:: with-vertex-array ( id quot -- )
+    id glBindVertexArray
+    quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
+
 : <gl-buffer> ( target data hint -- id )
     pick gen-gl-buffer [
         [
@@ -152,9 +188,6 @@ MACRO: all-enabled-client-state ( seq quot -- )
 MACRO: set-draw-buffers ( buffers -- )
     words>values '[ _ (set-draw-buffers) ] ;
 
-: gl-look-at ( eye focus up -- )
-    [ first3 ] tri@ gluLookAt ;
-
 : gen-dlist ( -- id ) 1 glGenLists ;
 
 : make-dlist ( type quot -- id )
@@ -165,7 +198,7 @@ MACRO: set-draw-buffers ( buffers -- )
 : delete-dlist ( id -- ) 1 glDeleteLists ;
 
 : with-translation ( loc quot -- )
-    GL_MODELVIEW [ [ gl-translate ] dip call ] do-matrix ; inline
+    [ [ gl-translate ] dip call ] do-matrix ; inline
 
 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
     [ first2 [ >fixnum ] bi@ ] bi@ ;
@@ -177,7 +210,8 @@ MACRO: set-draw-buffers ( buffers -- )
     fix-coordinates glViewport ;
 
 : init-matrices ( -- )
+    #! Leaves with matrix mode GL_MODELVIEW
     GL_PROJECTION glMatrixMode
     glLoadIdentity
     GL_MODELVIEW glMatrixMode
-    glLoadIdentity ;
\ No newline at end of file
+    glLoadIdentity ;