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 code string ;
36 { function word initial: t }
38 { timestamp timestamp } ;
40 gl-error-log [ V{ } clone ] initialize
42 : <gl-error> ( code -- gl-error )
43 dup error>string \ gl-error boa ; inline
45 : <gl-error-log> ( function code -- gl-error-log )
46 <gl-error> now gl-error-log boa ;
48 : gl-error-code ( -- code/f )
49 glGetError dup 0 = [ drop f ] when ; inline
52 gl-error-code [ <gl-error> throw ] [ ] if* ;
54 : log-gl-error ( function -- )
55 gl-error-code [ <gl-error-log> gl-error-log get push ] [ drop ] if* ;
57 : gl-function? ( word -- ? )
58 name>> { [ "glGetError" = not ] [ "gl" head? ] [ third LETTER? ] } 1&& ;
60 : gl-functions ( -- words )
61 "opengl.gl" vocab words [ gl-function? ] filter ;
63 : annotate-gl-functions ( quot -- )
65 [ gl-functions ] dip [ [ dup ] dip curry (annotate) ] curry each
66 ] with-compilation-unit ;
68 : reset-gl-functions ( -- )
69 [ gl-functions [ (reset) ] each ] with-compilation-unit ;
71 : clear-gl-error-log ( -- )
72 V{ } clone gl-error-log set ;
74 : throw-gl-errors ( -- )
75 [ drop '[ @ gl-error ] ] annotate-gl-functions ;
77 : log-gl-errors ( -- )
78 [ '[ @ _ log-gl-error ] ] annotate-gl-functions ;
80 : do-enabled ( what quot -- )
81 over glEnable dip glDisable ; inline
83 : do-enabled-client-state ( what quot -- )
84 over glEnableClientState dip glDisableClientState ; inline
86 : words>values ( word/value-seq -- value-seq )
89 : (all-enabled) ( seq quot -- )
90 over [ glEnable ] each dip [ glDisable ] each ; inline
92 : (all-enabled-client-state) ( seq quot -- )
93 [ dup [ glEnableClientState ] each ] dip
95 [ glDisableClientState ] each ; inline
97 MACRO: all-enabled ( seq quot -- )
98 [ words>values ] dip '[ _ _ (all-enabled) ] ;
100 MACRO: all-enabled-client-state ( seq quot -- )
101 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
103 : do-matrix ( quot -- )
104 glPushMatrix call glPopMatrix ; inline
106 : gl-material ( face pname params -- )
107 float-array{ } like glMaterialfv ;
109 : gl-vertex-pointer ( seq -- )
110 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
112 : gl-color-pointer ( seq -- )
113 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
115 : gl-texture-coord-pointer ( seq -- )
116 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
118 : line-vertices ( a b -- )
119 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
123 line-vertices GL_LINES 0 2 glDrawArrays ;
125 :: (rect-vertices) ( loc dim -- vertices )
126 #! We use GL_LINE_STRIP with a duplicated first vertex
127 #! instead of GL_LINE_LOOP to work around a bug in Apple's
134 x w + 0.3 - y h + 0.3 -
137 ] float-array{ } output>sequence ;
139 : rect-vertices ( loc dim -- )
140 (rect-vertices) gl-vertex-pointer ;
143 GL_LINE_STRIP 0 5 glDrawArrays ;
145 : gl-rect ( loc dim -- )
146 rect-vertices (gl-rect) ;
148 :: (fill-rect-vertices) ( loc dim -- vertices )
156 ] float-array{ } output>sequence ;
158 : fill-rect-vertices ( loc dim -- )
159 (fill-rect-vertices) gl-vertex-pointer ;
161 : (gl-fill-rect) ( -- )
162 GL_QUADS 0 4 glDrawArrays ;
164 : gl-fill-rect ( loc dim -- )
165 fill-rect-vertices (gl-fill-rect) ;
167 : do-attribs ( bits quot -- )
168 swap glPushAttrib call glPopAttrib ; inline
170 : (gen-gl-object) ( quot -- id )
171 [ 1 0 <uint> ] dip keep *uint ; inline
173 : (delete-gl-object) ( id quot -- )
174 [ 1 swap <uint> ] dip call ; inline
176 : gen-gl-buffer ( -- id )
177 [ glGenBuffers ] (gen-gl-object) ;
179 : delete-gl-buffer ( id -- )
180 [ glDeleteBuffers ] (delete-gl-object) ;
182 :: with-gl-buffer ( binding id quot -- )
183 binding id glBindBuffer
184 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
186 : with-array-element-buffers ( array-buffer element-buffer quot -- )
187 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
188 GL_ARRAY_BUFFER swap _ with-gl-buffer
189 ] with-gl-buffer ; inline
191 : gen-vertex-array ( -- id )
192 [ glGenVertexArrays ] (gen-gl-object) ;
194 : delete-vertex-array ( id -- )
195 [ glDeleteVertexArrays ] (delete-gl-object) ;
197 :: with-vertex-array ( id quot -- )
199 quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
201 : <gl-buffer> ( target data hint -- id )
204 [ [ byte-length ] keep ] dip glBufferData
208 : buffer-offset ( int -- alien )
211 : bind-texture-unit ( id target unit -- )
212 glActiveTexture swap glBindTexture gl-error ;
214 : (set-draw-buffers) ( buffers -- )
215 [ length ] [ >uint-array ] bi glDrawBuffers ;
217 MACRO: set-draw-buffers ( buffers -- )
218 words>values '[ _ (set-draw-buffers) ] ;
220 : gen-dlist ( -- id ) 1 glGenLists ;
222 : make-dlist ( type quot -- id )
223 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
225 : gl-translate ( point -- ) first2 0.0 glTranslated ;
227 : delete-dlist ( id -- ) 1 glDeleteLists ;
229 : with-translation ( loc quot -- )
230 [ [ gl-translate ] dip call ] do-matrix ; inline
232 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
233 [ first2 [ >fixnum ] bi@ ] bi@ ;
235 : gl-set-clip ( loc dim -- )
236 fix-coordinates glScissor ;
238 : gl-viewport ( loc dim -- )
239 fix-coordinates glViewport ;
241 : init-matrices ( -- )
242 #! Leaves with matrix mode GL_MODELVIEW
243 GL_PROJECTION glMatrixMode
245 GL_MODELVIEW glMatrixMode