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 ascii calendar
6 combinators.short-circuit continuations kernel libc math macros
7 namespaces math.vectors math.parser opengl.gl combinators
8 combinators.smart arrays sequences splitting words byte-arrays
9 assocs vocabs colors colors.constants accessors generalizations
10 sequences.generalizations locals fry specialized-arrays ;
11 FROM: alien.c-types => float ;
12 SPECIALIZED-ARRAY: float
13 SPECIALIZED-ARRAY: uint
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 { HEX: 0501 "Invalid value" }
27 { HEX: 0500 "Invalid enumerant" }
28 { HEX: 0502 "Invalid operation" }
29 { HEX: 0503 "Stack overflow" }
30 { HEX: 0504 "Stack underflow" }
31 { HEX: 0505 "Out of memory" }
32 { HEX: 0506 "Invalid framebuffer operation" }
33 } at "Unknown error" or ;
35 TUPLE: gl-error function code string ;
37 : <gl-error> ( function code -- gl-error )
38 dup error>string \ gl-error boa ; inline
40 : gl-error-code ( -- code/f )
41 glGetError dup 0 = [ drop f ] when ; inline
43 : (gl-error) ( function -- )
44 gl-error-code [ <gl-error> throw ] [ drop ] if* ;
49 : do-enabled ( what quot -- )
50 over glEnable dip glDisable ; inline
52 : do-enabled-client-state ( what quot -- )
53 over glEnableClientState dip glDisableClientState ; inline
55 : words>values ( word/value-seq -- value-seq )
58 : (all-enabled) ( seq quot -- )
59 [ dup [ glEnable ] each ] dip
61 [ glDisable ] each ; inline
63 : (all-enabled-client-state) ( seq quot -- )
64 [ dup [ glEnableClientState ] each ] dip
66 [ glDisableClientState ] each ; inline
68 MACRO: all-enabled ( seq quot -- )
69 [ words>values ] dip '[ _ _ (all-enabled) ] ;
71 MACRO: all-enabled-client-state ( seq quot -- )
72 [ words>values ] dip '[ _ _ (all-enabled-client-state) ] ;
74 : do-matrix ( quot -- )
75 glPushMatrix call glPopMatrix ; inline
77 : gl-material ( face pname params -- )
78 float-array{ } like glMaterialfv ;
80 : gl-vertex-pointer ( seq -- )
81 [ 2 GL_FLOAT 0 ] dip glVertexPointer ; inline
83 : gl-color-pointer ( seq -- )
84 [ 4 GL_FLOAT 0 ] dip glColorPointer ; inline
86 : gl-texture-coord-pointer ( seq -- )
87 [ 2 GL_FLOAT 0 ] dip glTexCoordPointer ; inline
89 : line-vertices ( a b -- )
90 [ first2 [ 0.5 + ] bi@ ] bi@ 4 float-array{ } nsequence
94 line-vertices GL_LINES 0 2 glDrawArrays ;
96 :: (rect-vertices) ( loc dim -- vertices )
97 #! We use GL_LINE_STRIP with a duplicated first vertex
98 #! instead of GL_LINE_LOOP to work around a bug in Apple's
100 loc first2 :> ( x y )
101 dim first2 :> ( w h )
105 x w + 0.3 - y h + 0.3 -
108 ] float-array{ } output>sequence ;
110 : rect-vertices ( loc dim -- )
111 (rect-vertices) gl-vertex-pointer ;
114 GL_LINE_STRIP 0 5 glDrawArrays ;
116 : gl-rect ( loc dim -- )
117 rect-vertices (gl-rect) ;
119 :: (fill-rect-vertices) ( loc dim -- vertices )
120 loc first2 :> ( x y )
121 dim first2 :> ( w h )
127 ] float-array{ } output>sequence ;
129 : fill-rect-vertices ( loc dim -- )
130 (fill-rect-vertices) gl-vertex-pointer ;
132 : (gl-fill-rect) ( -- )
133 GL_QUADS 0 4 glDrawArrays ;
135 : gl-fill-rect ( loc dim -- )
136 fill-rect-vertices (gl-fill-rect) ;
138 : do-attribs ( bits quot -- )
139 swap glPushAttrib call glPopAttrib ; inline
141 : (gen-gl-object) ( quot -- id )
142 [ 1 { uint } ] dip [ ] with-out-parameters ; inline
144 : (delete-gl-object) ( id quot -- )
145 [ 1 swap <uint> ] dip call ; inline
147 : gen-gl-buffer ( -- id )
148 [ glGenBuffers ] (gen-gl-object) ;
150 : delete-gl-buffer ( id -- )
151 [ glDeleteBuffers ] (delete-gl-object) ;
153 :: with-gl-buffer ( binding id quot -- )
154 binding id glBindBuffer
155 quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
157 : with-array-element-buffers ( array-buffer element-buffer quot -- )
158 [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
159 GL_ARRAY_BUFFER swap _ with-gl-buffer
160 ] with-gl-buffer ; inline
162 : gen-vertex-array ( -- id )
163 [ glGenVertexArrays ] (gen-gl-object) ;
165 : delete-vertex-array ( id -- )
166 [ glDeleteVertexArrays ] (delete-gl-object) ;
168 :: with-vertex-array ( id quot -- )
170 quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
172 : <gl-buffer> ( target data hint -- id )
175 [ [ byte-length ] keep ] dip glBufferData
179 : buffer-offset ( int -- alien )
182 : bind-texture-unit ( id target unit -- )
183 glActiveTexture swap glBindTexture gl-error ;
185 : (set-draw-buffers) ( buffers -- )
186 [ length ] [ >uint-array ] bi glDrawBuffers ;
188 MACRO: set-draw-buffers ( buffers -- )
189 words>values '[ _ (set-draw-buffers) ] ;
191 : gen-dlist ( -- id ) 1 glGenLists ;
193 : make-dlist ( type quot -- id )
194 [ gen-dlist ] 2dip '[ _ glNewList @ glEndList ] keep ; inline
196 : gl-translate ( point -- ) first2 0.0 glTranslated ;
198 : delete-dlist ( id -- ) 1 glDeleteLists ;
200 : with-translation ( loc quot -- )
201 [ [ gl-translate ] dip call ] do-matrix ; inline
203 : fix-coordinates ( point1 point2 -- x1 y2 x2 y2 )
204 [ first2 [ >fixnum ] bi@ ] bi@ ;
206 : gl-set-clip ( loc dim -- )
207 fix-coordinates glScissor ;
209 : gl-viewport ( loc dim -- )
210 fix-coordinates glViewport ;
212 : init-matrices ( -- )
213 #! Leaves with matrix mode GL_MODELVIEW
214 GL_PROJECTION glMatrixMode
216 GL_MODELVIEW glMatrixMode