! 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 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 ;
+USING: alien alien.c-types alien.data assocs colors
+combinators.smart continuations io kernel math
+math.functions math.parser namespaces opengl.gl sequences
+sequences.generalizations specialized-arrays system words ;
FROM: alien.c-types => float ;
SPECIALIZED-ARRAY: float
SPECIALIZED-ARRAY: uint
IN: opengl
+SYMBOL: gl-scale-factor
+
: gl-color ( color -- ) >rgba-components glColor4d ; inline
: gl-clear-color ( color -- ) >rgba-components glClearColor ;
: 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" }
+ { 0x0000 "No error" }
+ { 0x0501 "Invalid value" }
+ { 0x0500 "Invalid enumerant" }
+ { 0x0502 "Invalid operation" }
+ { 0x0503 "Stack overflow" }
+ { 0x0504 "Stack underflow" }
+ { 0x0505 "Out of memory" }
+ { 0x0506 "Invalid framebuffer operation" }
} at "Unknown error" or ;
-TUPLE: gl-error function code string ;
+TUPLE: gl-error-tuple function code string ;
: <gl-error> ( function code -- gl-error )
- dup error>string \ gl-error boa ; inline
+ dup error>string \ gl-error-tuple boa ; inline
: gl-error-code ( -- code/f )
glGetError dup 0 = [ drop f ] when ; inline
+: throw-gl-error? ( -- ? )
+ os macosx? [
+ ! This is kind of terrible, but we are having
+ ! problems on Mac OS X 10.11 where the
+ ! default framebuffer seems to be initialized
+ ! asynchronously or something, so we should
+ ! just log these for now in (gl-error).
+ GL_FRAMEBUFFER glCheckFramebufferStatus
+ GL_FRAMEBUFFER_UNDEFINED = not
+ ] [ t ] if ;
+
: (gl-error) ( function -- )
- gl-error-code [ <gl-error> throw ] [ drop ] if* ;
+ gl-error-code [
+ throw-gl-error? [
+ <gl-error> throw
+ ] [
+ [
+ [ number>string ] [ error>string ] bi ": " glue
+ "OpenGL error: " prepend print flush drop
+ ] with-global
+ ] if
+ ] [ drop ] if* ;
: gl-error ( -- )
f (gl-error) ; inline
over glEnableClientState dip glDisableClientState ; inline
: words>values ( word/value-seq -- value-seq )
- [ ?execute ] map ;
+ [ dup word? [ execute( -- x ) ] when ] map ;
: (all-enabled) ( seq quot -- )
[ dup [ glEnable ] each ] dip
dip
[ glDisableClientState ] each ; inline
-MACRO: all-enabled ( seq quot -- )
+MACRO: all-enabled ( seq quot -- quot )
[ words>values ] dip '[ _ _ (all-enabled) ] ;
-MACRO: all-enabled-client-state ( seq quot -- )
+MACRO: all-enabled-client-state ( seq quot -- quot )
[ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
: do-matrix ( quot -- )
: gl-texture-coord-pointer ( seq -- )
[ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
+: (line-vertices) ( a b -- vertices )
+ [ first2 [ 0.3 + ] bi@ ] bi@ 4 float-array{ } nsequence ;
+
: line-vertices ( a b -- )
- [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
- gl-vertex-pointer ;
+ (line-vertices) gl-vertex-pointer ;
: gl-line ( a b -- )
line-vertices GL_LINES 0 2 glDrawArrays ;
:: (rect-vertices) ( loc dim -- vertices )
- #! 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 :> ( x y )
- dim first2 :> ( w h )
+ ! 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 [ 0.3 + ] bi@ :> ( x y )
+ dim first2 [ 0.6 - ] bi@ :> ( w h )
[
- 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 +
+ x y
+ x w + y
+ x w + y h +
+ x y h +
+ x y
] float-array{ } output>sequence ;
: rect-vertices ( loc dim -- )
swap glPushAttrib call glPopAttrib ; inline
: (gen-gl-object) ( quot -- id )
- [ 1 0 <uint> ] dip keep *uint ; inline
+ [ 1 { uint } ] dip with-out-parameters ; inline
: (delete-gl-object) ( id quot -- )
- [ 1 swap <uint> ] dip call ; inline
+ [ 1 swap uint <ref> ] dip call ; inline
: gen-gl-buffer ( -- id )
[ glGenBuffers ] (gen-gl-object) ;
:: with-gl-buffer ( binding id quot -- )
binding id glBindBuffer
- quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
+ quot [ binding 0 glBindBuffer ] finally ; inline
: with-array-element-buffers ( array-buffer element-buffer quot -- )
[ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
:: with-vertex-array ( id quot -- )
id glBindVertexArray
- quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
+ quot [ 0 glBindVertexArray ] finally ; inline
: <gl-buffer> ( target data hint -- id )
pick gen-gl-buffer [
glActiveTexture swap glBindTexture gl-error ;
: (set-draw-buffers) ( buffers -- )
- [ length ] [ >uint-array ] bi glDrawBuffers ;
+ [ length ] [ uint >c-array ] bi glDrawBuffers ;
-MACRO: set-draw-buffers ( buffers -- )
+MACRO: set-draw-buffers ( buffers -- quot )
words>values '[ _ (set-draw-buffers) ] ;
: gen-dlist ( -- id ) 1 glGenLists ;
: with-translation ( loc quot -- )
[ [ gl-translate ] dip call ] do-matrix ; inline
-: fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
- [ first2 [ >fixnum ] bi@ ] bi@ ;
+: gl-scale ( m -- n )
+ gl-scale-factor get-global [ * ] when* ; inline
+
+: gl-unscale ( m -- n )
+ gl-scale-factor get-global [ / ] when* ; inline
+
+: gl-floor ( m -- n )
+ gl-scale floor gl-unscale ; inline
+
+: gl-ceiling ( m -- n )
+ gl-scale ceiling gl-unscale ; inline
+
+: gl-round ( m -- n )
+ gl-scale round gl-unscale ; inline
+
+: fix-coordinates ( point1 point2 -- x1 y1 x2 y2 )
+ [ first2 [ gl-scale >fixnum ] bi@ ] bi@ ;
: gl-set-clip ( loc dim -- )
fix-coordinates glScissor ;
fix-coordinates glViewport ;
: init-matrices ( -- )
- #! Leaves with matrix mode GL_MODELVIEW
+ ! Leaves with matrix mode GL_MODELVIEW
GL_PROJECTION glMatrixMode
glLoadIdentity
GL_MODELVIEW glMatrixMode
glLoadIdentity ;
+
+STARTUP-HOOK: [ f gl-scale-factor set-global ]