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 ascii calendar combinators.short-circuit
6 continuations kernel libc math macros namespaces math.vectors
7 math.parser opengl.gl combinators combinators.smart arrays
8 sequences splitting words byte-arrays assocs vocabs
9 colors colors.constants accessors generalizations locals fry
10 specialized-arrays.float specialized-arrays.uint
11 tools.annotations tools.annotations.private compiler.units ;
14 : gl-color ( color -- ) >rgba-components glColor4d ; inline
16 : gl-clear-color ( color -- ) >rgba-components glClearColor ;
18 : gl-clear ( color -- )
19 gl-clear-color GL_COLOR_BUFFER_BIT glClear ;
21 : error>string ( n -- string )
24 { HEX: 0501 "Invalid value" }
25 { HEX: 0500 "Invalid enumerant" }
26 { HEX: 0502 "Invalid operation" }
27 { HEX: 0503 "Stack overflow" }
28 { HEX: 0504 "Stack underflow" }
29 { HEX: 0505 "Out of memory" }
30 { HEX: 0506 "Invalid framebuffer operation" }
31 } at "Unknown error" or ;
33 TUPLE: gl-error function code string ;
36 { function word initial: t }
38 { timestamp timestamp } ;
40 gl-error-log [ V{ } clone ] initialize
42 : <gl-error> ( function code -- gl-error )
43 dup error>string \ gl-error boa ; inline
45 : <gl-error-log> ( function code -- gl-error-log )
46 [ dup ] dip <gl-error> now gl-error-log boa ;
48 : gl-error-code ( -- code/f )
49 glGetError dup 0 = [ drop f ] when ; inline
51 : (gl-error) ( function -- )
52 gl-error-code [ <gl-error> throw ] [ drop ] if* ;
57 : log-gl-error ( function -- )
58 gl-error-code [ <gl-error-log> gl-error-log get push ] [ drop ] if* ;
60 : gl-function? ( word -- ? )
61 name>> { [ "glGetError" = not ] [ "gl" head? ] [ third LETTER? ] } 1&& ;
63 : gl-functions ( -- words )
64 "opengl.gl" vocab words [ gl-function? ] filter ;
66 : annotate-gl-functions ( quot -- )
68 [ gl-functions ] dip [ [ dup ] dip curry (annotate) ] curry each
69 ] with-compilation-unit ;
71 : reset-gl-functions ( -- )
72 [ gl-functions [ (reset) ] each ] with-compilation-unit ;
74 : clear-gl-error-log ( -- )
75 V{ } clone gl-error-log set ;
77 : throw-gl-errors ( -- )
78 [ '[ @ _ (gl-error) ] ] annotate-gl-functions ;
80 : log-gl-errors ( -- )
81 [ '[ @ _ log-gl-error ] ] annotate-gl-functions ;
83 : do-enabled ( what quot -- )
84 over glEnable dip glDisable ; inline
86 : do-enabled-client-state ( what quot -- )
87 over glEnableClientState dip glDisableClientState ; inline
89 : words>values ( word/value-seq -- value-seq )
92 : (all-enabled) ( seq quot -- )
93 over [ glEnable ] each dip [ glDisable ] each ; inline
95 : (all-enabled-client-state) ( seq quot -- )
96 [ dup [ glEnableClientState ] each ] dip
98 [ glDisableClientState ] each ; inline
100 MACRO: all-enabled ( seq quot -- )
101 [ words>values ] dip '[ _ _ (all-enabled) ] ;
103 MACRO: all-enabled-client-state ( seq quot -- )
104 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
106 : do-matrix ( quot -- )
107 glPushMatrix call glPopMatrix ; inline
109 : gl-material ( face pname params -- )
110 float-array{ } like glMaterialfv ;
112 : gl-vertex-pointer ( seq -- )
113 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
115 : gl-color-pointer ( seq -- )
116 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
118 : gl-texture-coord-pointer ( seq -- )
119 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
121 : line-vertices ( a b -- )
122 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
126 line-vertices GL_LINES 0 2 glDrawArrays ;
128 :: (rect-vertices) ( loc dim -- vertices )
129 #! We use GL_LINE_STRIP with a duplicated first vertex
130 #! instead of GL_LINE_LOOP to work around a bug in Apple's
137 x w + 0.3 - y h + 0.3 -
140 ] float-array{ } output>sequence ;
142 : rect-vertices ( loc dim -- )
143 (rect-vertices) gl-vertex-pointer ;
146 GL_LINE_STRIP 0 5 glDrawArrays ;
148 : gl-rect ( loc dim -- )
149 rect-vertices (gl-rect) ;
151 :: (fill-rect-vertices) ( loc dim -- vertices )
159 ] float-array{ } output>sequence ;
161 : fill-rect-vertices ( loc dim -- )
162 (fill-rect-vertices) gl-vertex-pointer ;
164 : (gl-fill-rect) ( -- )
165 GL_QUADS 0 4 glDrawArrays ;
167 : gl-fill-rect ( loc dim -- )
168 fill-rect-vertices (gl-fill-rect) ;
170 : do-attribs ( bits quot -- )
171 swap glPushAttrib call glPopAttrib ; inline
173 : (gen-gl-object) ( quot -- id )
174 [ 1 0 <uint> ] dip keep *uint ; inline
176 : (delete-gl-object) ( id quot -- )
177 [ 1 swap <uint> ] dip call ; inline
179 : gen-gl-buffer ( -- id )
180 [ glGenBuffers ] (gen-gl-object) ;
182 : delete-gl-buffer ( id -- )
183 [ glDeleteBuffers ] (delete-gl-object) ;
185 :: with-gl-buffer ( binding id quot -- )
186 binding id glBindBuffer
187 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
189 : with-array-element-buffers ( array-buffer element-buffer quot -- )
190 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
191 GL_ARRAY_BUFFER swap _ with-gl-buffer
192 ] with-gl-buffer ; inline
194 : gen-vertex-array ( -- id )
195 [ glGenVertexArrays ] (gen-gl-object) ;
197 : delete-vertex-array ( id -- )
198 [ glDeleteVertexArrays ] (delete-gl-object) ;
200 :: with-vertex-array ( id quot -- )
202 quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
204 : <gl-buffer> ( target data hint -- id )
207 [ [ byte-length ] keep ] dip glBufferData
211 : buffer-offset ( int -- alien )
214 : bind-texture-unit ( id target unit -- )
215 glActiveTexture swap glBindTexture gl-error ;
217 : (set-draw-buffers) ( buffers -- )
218 [ length ] [ >uint-array ] bi glDrawBuffers ;
220 MACRO: set-draw-buffers ( buffers -- )
221 words>values '[ _ (set-draw-buffers) ] ;
223 : gen-dlist ( -- id ) 1 glGenLists ;
225 : make-dlist ( type quot -- id )
226 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
228 : gl-translate ( point -- ) first2 0.0 glTranslated ;
230 : delete-dlist ( id -- ) 1 glDeleteLists ;
232 : with-translation ( loc quot -- )
233 [ [ gl-translate ] dip call ] do-matrix ; inline
235 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
236 [ first2 [ >fixnum ] bi@ ] bi@ ;
238 : gl-set-clip ( loc dim -- )
239 fix-coordinates glScissor ;
241 : gl-viewport ( loc dim -- )
242 fix-coordinates glViewport ;
244 : init-matrices ( -- )
245 #! Leaves with matrix mode GL_MODELVIEW
246 GL_PROJECTION glMatrixMode
248 GL_MODELVIEW glMatrixMode