1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! Portions copyright (C) 2007 Eduardo Cavazos.
3 ! Portions copyright (C) 2008 Joe Groff.
4 ! See http://factorcode.org/license.txt for BSD license.
5 USING: alien alien.c-types alien.data assocs colors
6 combinators.smart continuations io kernel math
7 math.functions math.parser namespaces opengl.gl sequences
8 sequences.generalizations specialized-arrays system words ;
9 FROM: alien.c-types => float ;
10 SPECIALIZED-ARRAY: float
11 SPECIALIZED-ARRAY: uint
14 SYMBOL: gl-scale-factor
16 : gl-color ( color -- ) >rgba-components glColor4d ; inline
18 : gl-clear-color ( color -- ) >rgba-components glClearColor ;
20 : gl-clear ( color -- )
21 gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
23 : error>string ( n -- string )
26 { 0x0501 "Invalid value" }
27 { 0x0500 "Invalid enumerant" }
28 { 0x0502 "Invalid operation" }
29 { 0x0503 "Stack overflow" }
30 { 0x0504 "Stack underflow" }
31 { 0x0505 "Out of memory" }
32 { 0x0506 "Invalid framebuffer operation" }
33 } at "Unknown error" or ;
35 TUPLE: gl-error-tuple function code string ;
37 : <gl-error> ( function code -- gl-error )
38 dup error>string \ gl-error-tuple boa ; inline
40 : gl-error-code ( -- code/f )
41 glGetError dup 0 = [ drop f ] when ; inline
43 : throw-gl-error? ( -- ? )
45 ! This is kind of terrible, but we are having
46 ! problems on Mac OS X 10.11 where the
47 ! default framebuffer seems to be initialized
48 ! asynchronously or something, so we should
49 ! just log these for now in (gl-error).
50 GL_FRAMEBUFFER glCheckFramebufferStatus
51 GL_FRAMEBUFFER_UNDEFINED = not
54 : (gl-error) ( function -- )
60 [ number>string ] [ error>string ] bi ": " glue
61 "OpenGL error: " prepend print flush drop
69 : do-enabled ( what quot -- )
70 over glEnable dip glDisable ; inline
72 : do-enabled-client-state ( what quot -- )
73 over glEnableClientState dip glDisableClientState ; inline
75 : words>values ( word/value-seq -- value-seq )
76 [ dup word? [ execute( -- x ) ] when ] map ;
78 : (all-enabled) ( seq quot -- )
79 [ dup [ glEnable ] each ] dip
81 [ glDisable ] each ; inline
83 : (all-enabled-client-state) ( seq quot -- )
84 [ dup [ glEnableClientState ] each ] dip
86 [ glDisableClientState ] each ; inline
88 MACRO: all-enabled ( seq quot -- quot )
89 [ words>values ] dip '[ _ _ (all-enabled) ] ;
91 MACRO: all-enabled-client-state ( seq quot -- quot )
92 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
94 : do-matrix ( quot -- )
95 glPushMatrix call glPopMatrix ; inline
97 : gl-material ( face pname params -- )
98 float-array{ } like glMaterialfv ;
100 : gl-vertex-pointer ( seq -- )
101 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
103 : gl-color-pointer ( seq -- )
104 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
106 : gl-texture-coord-pointer ( seq -- )
107 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
109 : (line-vertices) ( a b -- vertices )
110 [ first2 [ 0.3 + ] bi@ ] bi@ 4 float-array{ } nsequence ;
112 : line-vertices ( a b -- )
113 (line-vertices) gl-vertex-pointer ;
116 line-vertices GL_LINES 0 2 glDrawArrays ;
118 :: (rect-vertices) ( loc dim -- vertices )
119 ! We use GL_LINE_STRIP with a duplicated first vertex
120 ! instead of GL_LINE_LOOP to work around a bug in Apple's
122 loc first2 [ 0.3 + ] bi@ :> ( x y )
123 dim first2 [ 0.6 - ] bi@ :> ( w h )
130 ] float-array{ } output>sequence ;
132 : rect-vertices ( loc dim -- )
133 (rect-vertices) gl-vertex-pointer ;
136 GL_LINE_STRIP 0 5 glDrawArrays ;
138 : gl-rect ( loc dim -- )
139 rect-vertices (gl-rect) ;
141 :: (fill-rect-vertices) ( loc dim -- vertices )
142 loc first2 :> ( x y )
143 dim first2 :> ( w h )
149 ] float-array{ } output>sequence ;
151 : fill-rect-vertices ( loc dim -- )
152 (fill-rect-vertices) gl-vertex-pointer ;
154 : (gl-fill-rect) ( -- )
155 GL_QUADS 0 4 glDrawArrays ;
157 : gl-fill-rect ( loc dim -- )
158 fill-rect-vertices (gl-fill-rect) ;
160 : do-attribs ( bits quot -- )
161 swap glPushAttrib call glPopAttrib ; inline
163 : (gen-gl-object) ( quot -- id )
164 [ 1 { uint } ] dip with-out-parameters ; inline
166 : (delete-gl-object) ( id quot -- )
167 [ 1 swap uint <ref> ] dip call ; inline
169 : gen-gl-buffer ( -- id )
170 [ glGenBuffers ] (gen-gl-object) ;
172 : delete-gl-buffer ( id -- )
173 [ glDeleteBuffers ] (delete-gl-object) ;
175 :: with-gl-buffer ( binding id quot -- )
176 binding id glBindBuffer
177 quot [ binding 0 glBindBuffer ] finally ; inline
179 : with-array-element-buffers ( array-buffer element-buffer quot -- )
180 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
181 GL_ARRAY_BUFFER swap _ with-gl-buffer
182 ] with-gl-buffer ; inline
184 : gen-vertex-array ( -- id )
185 [ glGenVertexArrays ] (gen-gl-object) ;
187 : delete-vertex-array ( id -- )
188 [ glDeleteVertexArrays ] (delete-gl-object) ;
190 :: with-vertex-array ( id quot -- )
192 quot [ 0 glBindVertexArray ] finally ; inline
194 : <gl-buffer> ( target data hint -- id )
197 [ [ byte-length ] keep ] dip glBufferData
201 : buffer-offset ( int -- alien )
204 : bind-texture-unit ( id target unit -- )
205 glActiveTexture swap glBindTexture gl-error ;
207 : (set-draw-buffers) ( buffers -- )
208 [ length ] [ uint >c-array ] bi glDrawBuffers ;
210 MACRO: set-draw-buffers ( buffers -- quot )
211 words>values '[ _ (set-draw-buffers) ] ;
213 : gen-dlist ( -- id ) 1 glGenLists ;
215 : make-dlist ( type quot -- id )
216 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
218 : gl-translate ( point -- ) first2 0.0 glTranslated ;
220 : delete-dlist ( id -- ) 1 glDeleteLists ;
222 : with-translation ( loc quot -- )
223 [ [ gl-translate ] dip call ] do-matrix ; inline
225 : gl-scale ( m -- n )
226 gl-scale-factor get-global [ * ] when* ; inline
228 : gl-unscale ( m -- n )
229 gl-scale-factor get-global [ / ] when* ; inline
231 : gl-floor ( m -- n )
232 gl-scale floor gl-unscale ; inline
234 : gl-ceiling ( m -- n )
235 gl-scale ceiling gl-unscale ; inline
237 : gl-round ( m -- n )
238 gl-scale round gl-unscale ; inline
240 : fix-coordinates ( point1 point2 -- x1 y1 x2 y2 )
241 [ first2 [ gl-scale >fixnum ] bi@ ] bi@ ;
243 : gl-set-clip ( loc dim -- )
244 fix-coordinates glScissor ;
246 : gl-viewport ( loc dim -- )
247 fix-coordinates glViewport ;
249 : init-matrices ( -- )
250 ! Leaves with matrix mode GL_MODELVIEW
251 GL_PROJECTION glMatrixMode
253 GL_MODELVIEW glMatrixMode
256 STARTUP-HOOK: [ f gl-scale-factor set-global ]